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