1 /*-
2 * Copyright (c) 2011 Jilles Tjoelker
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 /*
28 * Test program for posix_spawn() and posix_spawnp() as specified by
29 * IEEE Std. 1003.1-2008.
30 */
31
32 #include <sys/wait.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <spawn.h>
38
39 #include <atf-c.h>
40
41 char *myenv[2] = { "answer=42", NULL };
42
43 ATF_TC_WITHOUT_HEAD(posix_spawn_simple_test);
ATF_TC_BODY(posix_spawn_simple_test,tc)44 ATF_TC_BODY(posix_spawn_simple_test, tc)
45 {
46 char *myargs[4];
47 int error, status;
48 pid_t pid, waitres;
49
50 /* Make sure we have no child processes. */
51 while (waitpid(-1, NULL, 0) != -1)
52 ;
53 ATF_REQUIRE_MSG(errno == ECHILD, "errno was not ECHILD: %d", errno);
54
55 /* Simple test. */
56 myargs[0] = "sh";
57 myargs[1] = "-c";
58 myargs[2] = "exit $answer";
59 myargs[3] = NULL;
60 error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv);
61 ATF_REQUIRE(error == 0);
62 waitres = waitpid(pid, &status, 0);
63 ATF_REQUIRE(waitres == pid);
64 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 42);
65 }
66
67 ATF_TC_WITHOUT_HEAD(posix_spawn_no_such_command_negative_test);
ATF_TC_BODY(posix_spawn_no_such_command_negative_test,tc)68 ATF_TC_BODY(posix_spawn_no_such_command_negative_test, tc)
69 {
70 char *myargs[4];
71 int error, status;
72 pid_t pid, waitres;
73
74 /*
75 * If the executable does not exist, the function shall either fail
76 * and not create a child process or succeed and create a child
77 * process that exits with status 127.
78 */
79 myargs[0] = "/var/empty/nonexistent";
80 myargs[1] = NULL;
81 error = posix_spawn(&pid, myargs[0], NULL, NULL, myargs, myenv);
82 if (error == 0) {
83 waitres = waitpid(pid, &status, 0);
84 ATF_REQUIRE(waitres == pid);
85 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127);
86 } else {
87 ATF_REQUIRE(error == ENOENT);
88 waitres = waitpid(-1, NULL, 0);
89 ATF_REQUIRE(waitres == -1 && errno == ECHILD);
90 }
91 }
92
93 ATF_TC_WITHOUT_HEAD(posix_spawnp_enoexec_fallback);
ATF_TC_BODY(posix_spawnp_enoexec_fallback,tc)94 ATF_TC_BODY(posix_spawnp_enoexec_fallback, tc)
95 {
96 char buf[FILENAME_MAX];
97 char *myargs[2];
98 int error, status;
99 pid_t pid, waitres;
100
101 snprintf(buf, sizeof(buf), "%s/spawnp_enoexec.sh",
102 atf_tc_get_config_var(tc, "srcdir"));
103 myargs[0] = buf;
104 myargs[1] = NULL;
105 error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv);
106 ATF_REQUIRE(error == 0);
107 waitres = waitpid(pid, &status, 0);
108 ATF_REQUIRE(waitres == pid);
109 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 42);
110 }
111
112 ATF_TC_WITHOUT_HEAD(posix_spawnp_enoexec_fallback_null_argv0);
ATF_TC_BODY(posix_spawnp_enoexec_fallback_null_argv0,tc)113 ATF_TC_BODY(posix_spawnp_enoexec_fallback_null_argv0, tc)
114 {
115 char buf[FILENAME_MAX];
116 char *myargs[1];
117 int error;
118 pid_t pid;
119
120 snprintf(buf, sizeof(buf), "%s/spawnp_enoexec.sh",
121 atf_tc_get_config_var(tc, "srcdir"));
122 myargs[0] = NULL;
123 error = posix_spawnp(&pid, buf, NULL, NULL, myargs, myenv);
124 ATF_REQUIRE(error == EINVAL);
125 }
126
ATF_TP_ADD_TCS(tp)127 ATF_TP_ADD_TCS(tp)
128 {
129
130 ATF_TP_ADD_TC(tp, posix_spawn_simple_test);
131 ATF_TP_ADD_TC(tp, posix_spawn_no_such_command_negative_test);
132 ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback);
133 ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback_null_argv0);
134
135 return (atf_no_error());
136 }
137