t_execve.c revision 1.4 1 /* $NetBSD: t_execve.c,v 1.4 2025/03/13 12:48:22 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_execve.c,v 1.4 2025/03/13 12:48:22 riastradh Exp $");
31
32 #include <sys/wait.h>
33
34 #include <atf-c.h>
35
36 #include <errno.h>
37 #include <limits.h>
38 #include <signal.h>
39 #include <stddef.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <time.h>
43
44 #include "h_macros.h"
45
46 ATF_TC(t_execve_null);
47 ATF_TC_HEAD(t_execve_null, tc)
48 {
49 atf_tc_set_md_var(tc, "descr",
50 "Tests an empty execve(2) executing");
51 }
52 ATF_TC_BODY(t_execve_null, tc)
53 {
54 int err;
55
56 err = execve(NULL, NULL, NULL);
57 ATF_REQUIRE(err == -1);
58 ATF_REQUIRE_MSG(errno == EFAULT,
59 "wrong error returned %d instead of %d", errno, EFAULT);
60 }
61
62 ATF_TC(t_execve_sig);
63 ATF_TC_HEAD(t_execve_sig, tc)
64 {
65 atf_tc_set_md_var(tc, "descr",
66 "Checks that execve does not drop pending signals");
67 }
68 ATF_TC_BODY(t_execve_sig, tc)
69 {
70 const char *srcdir = atf_tc_get_config_var(tc, "srcdir");
71 char h_execsig[PATH_MAX];
72 time_t start;
73
74 snprintf(h_execsig, sizeof(h_execsig), "%s/../h_execsig", srcdir);
75 REQUIRE_LIBC(signal(SIGPIPE, SIG_IGN), SIG_ERR);
76
77 for (start = time(NULL); time(NULL) - start <= 10;) {
78 int fd[2];
79 char *const argv[] = {h_execsig, NULL};
80 pid_t pid;
81 int status;
82
83 RL(pipe(fd));
84 RL(pid = vfork());
85 if (pid == 0) {
86 if (dup2(fd[0], STDIN_FILENO) == -1)
87 _exit(1);
88 (void)execve(argv[0], argv, NULL);
89 _exit(2);
90 }
91 RL(close(fd[0]));
92 RL(kill(pid, SIGTERM));
93 if (write(fd[1], (char[]){0}, 1) == -1 && errno != EPIPE)
94 atf_tc_fail("write failed: %s", strerror(errno));
95 RL(waitpid(pid, &status, 0));
96 ATF_REQUIRE_MSG(WIFSIGNALED(status),
97 "child exited with status 0x%x", status);
98 ATF_REQUIRE_EQ_MSG(WTERMSIG(status), SIGTERM,
99 "child exited on signal %d (%s)",
100 WTERMSIG(status), strsignal(WTERMSIG(status)));
101 RL(close(fd[1]));
102 }
103 }
104
105 ATF_TP_ADD_TCS(tp)
106 {
107
108 ATF_TP_ADD_TC(tp, t_execve_null);
109 ATF_TP_ADD_TC(tp, t_execve_sig);
110
111 return atf_no_error();
112 }
113