xref: /minix3/minix/lib/libc/sys/posix_spawn.c (revision 1facb0487c831b9ead49737c23d6f936b553009b)
1*1facb048SJean-Baptiste Boric /*
2*1facb048SJean-Baptiste Boric  * Taken from newlib/libc/posix/posix_spawn.c
3*1facb048SJean-Baptiste Boric  */
4*1facb048SJean-Baptiste Boric 
5*1facb048SJean-Baptiste Boric /*-
6*1facb048SJean-Baptiste Boric  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
7*1facb048SJean-Baptiste Boric  * All rights reserved.
8*1facb048SJean-Baptiste Boric  *
9*1facb048SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
10*1facb048SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
11*1facb048SJean-Baptiste Boric  * are met:
12*1facb048SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
13*1facb048SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
14*1facb048SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
15*1facb048SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
16*1facb048SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
17*1facb048SJean-Baptiste Boric  *
18*1facb048SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*1facb048SJean-Baptiste Boric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*1facb048SJean-Baptiste Boric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*1facb048SJean-Baptiste Boric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*1facb048SJean-Baptiste Boric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*1facb048SJean-Baptiste Boric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*1facb048SJean-Baptiste Boric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*1facb048SJean-Baptiste Boric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*1facb048SJean-Baptiste Boric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*1facb048SJean-Baptiste Boric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*1facb048SJean-Baptiste Boric  * SUCH DAMAGE.
29*1facb048SJean-Baptiste Boric  */
30*1facb048SJean-Baptiste Boric 
31*1facb048SJean-Baptiste Boric #include <sys/cdefs.h>
32*1facb048SJean-Baptiste Boric 
33*1facb048SJean-Baptiste Boric #include <sys/queue.h>
34*1facb048SJean-Baptiste Boric #include <sys/wait.h>
35*1facb048SJean-Baptiste Boric 
36*1facb048SJean-Baptiste Boric #include <errno.h>
37*1facb048SJean-Baptiste Boric #include <fcntl.h>
38*1facb048SJean-Baptiste Boric #include <sched.h>
39*1facb048SJean-Baptiste Boric #include <spawn.h>
40*1facb048SJean-Baptiste Boric #include <signal.h>
41*1facb048SJean-Baptiste Boric #include <stdlib.h>
42*1facb048SJean-Baptiste Boric #include <string.h>
43*1facb048SJean-Baptiste Boric #include <unistd.h>
44*1facb048SJean-Baptiste Boric 
45*1facb048SJean-Baptiste Boric extern char **environ;
46*1facb048SJean-Baptiste Boric 
47*1facb048SJean-Baptiste Boric /* Only deal with a pointer to environ, to work around subtle bugs with shared
48*1facb048SJean-Baptiste Boric    libraries and/or small data systems where the user declares his own
49*1facb048SJean-Baptiste Boric    'environ'.  */
50*1facb048SJean-Baptiste Boric static char ***p_environ = &environ;
51*1facb048SJean-Baptiste Boric 
52*1facb048SJean-Baptiste Boric /*
53*1facb048SJean-Baptiste Boric  * Spawn routines
54*1facb048SJean-Baptiste Boric  */
55*1facb048SJean-Baptiste Boric 
56*1facb048SJean-Baptiste Boric static int
57*1facb048SJean-Baptiste Boric process_spawnattr(const posix_spawnattr_t * sa)
58*1facb048SJean-Baptiste Boric {
59*1facb048SJean-Baptiste Boric 	struct sigaction sigact = { .sa_flags = 0, .sa_handler = SIG_DFL };
60*1facb048SJean-Baptiste Boric 	int i;
61*1facb048SJean-Baptiste Boric 
62*1facb048SJean-Baptiste Boric 	/*
63*1facb048SJean-Baptiste Boric 	 * POSIX doesn't really describe in which order everything
64*1facb048SJean-Baptiste Boric 	 * should be set. We'll just set them in the order in which they
65*1facb048SJean-Baptiste Boric 	 * are mentioned.
66*1facb048SJean-Baptiste Boric 	 */
67*1facb048SJean-Baptiste Boric 
68*1facb048SJean-Baptiste Boric 	/* Set process group */
69*1facb048SJean-Baptiste Boric 	if (sa->sa_flags & POSIX_SPAWN_SETPGROUP) {
70*1facb048SJean-Baptiste Boric 		if (setpgid(0, sa->sa_pgroup) != 0)
71*1facb048SJean-Baptiste Boric 			return errno;
72*1facb048SJean-Baptiste Boric 	}
73*1facb048SJean-Baptiste Boric 
74*1facb048SJean-Baptiste Boric 	/* Set scheduler policy */
75*1facb048SJean-Baptiste Boric 	/* XXX: We don't have scheduler policy for now */
76*1facb048SJean-Baptiste Boric #if 0
77*1facb048SJean-Baptiste Boric 	if (sa->sa_flags & POSIX_SPAWN_SETSCHEDULER) {
78*1facb048SJean-Baptiste Boric 		if (sched_setscheduler(0, sa->sa_schedpolicy,
79*1facb048SJean-Baptiste Boric 		    &sa->sa_schedparam) != 0)
80*1facb048SJean-Baptiste Boric 			return errno;
81*1facb048SJean-Baptiste Boric 	} else if (sa->sa_flags & POSIX_SPAWN_SETSCHEDPARAM) {
82*1facb048SJean-Baptiste Boric 		if (sched_setparam(0, &sa->sa_schedparam) != 0)
83*1facb048SJean-Baptiste Boric 			return errno;
84*1facb048SJean-Baptiste Boric 	}
85*1facb048SJean-Baptiste Boric #endif
86*1facb048SJean-Baptiste Boric 
87*1facb048SJean-Baptiste Boric 	/* Reset user ID's */
88*1facb048SJean-Baptiste Boric 	if (sa->sa_flags & POSIX_SPAWN_RESETIDS) {
89*1facb048SJean-Baptiste Boric 		if (setegid(getgid()) != 0)
90*1facb048SJean-Baptiste Boric 			return errno;
91*1facb048SJean-Baptiste Boric 		if (seteuid(getuid()) != 0)
92*1facb048SJean-Baptiste Boric 			return errno;
93*1facb048SJean-Baptiste Boric 	}
94*1facb048SJean-Baptiste Boric 
95*1facb048SJean-Baptiste Boric 	/* Set signal masks/defaults */
96*1facb048SJean-Baptiste Boric 	if (sa->sa_flags & POSIX_SPAWN_SETSIGMASK) {
97*1facb048SJean-Baptiste Boric 		sigprocmask(SIG_SETMASK, &sa->sa_sigmask, NULL);
98*1facb048SJean-Baptiste Boric 	}
99*1facb048SJean-Baptiste Boric 
100*1facb048SJean-Baptiste Boric 	if (sa->sa_flags & POSIX_SPAWN_SETSIGDEF) {
101*1facb048SJean-Baptiste Boric 		for (i = 1; i < NSIG; i++) {
102*1facb048SJean-Baptiste Boric 			if (sigismember(&sa->sa_sigdefault, i))
103*1facb048SJean-Baptiste Boric 				if (sigaction(i, &sigact, NULL) != 0)
104*1facb048SJean-Baptiste Boric 					return errno;
105*1facb048SJean-Baptiste Boric 		}
106*1facb048SJean-Baptiste Boric 	}
107*1facb048SJean-Baptiste Boric 
108*1facb048SJean-Baptiste Boric 	return 0;
109*1facb048SJean-Baptiste Boric }
110*1facb048SJean-Baptiste Boric 
111*1facb048SJean-Baptiste Boric static int
112*1facb048SJean-Baptiste Boric move_fd_up(int * statusfd)
113*1facb048SJean-Baptiste Boric {
114*1facb048SJean-Baptiste Boric 	/*
115*1facb048SJean-Baptiste Boric 	 * Move given file descriptor on a higher fd number.
116*1facb048SJean-Baptiste Boric 	 *
117*1facb048SJean-Baptiste Boric 	 * This is used to hide the status file descriptor from the application
118*1facb048SJean-Baptiste Boric 	 * by pushing it out of the way if it tries to use its number.
119*1facb048SJean-Baptiste Boric 	 */
120*1facb048SJean-Baptiste Boric 	int newstatusfd;
121*1facb048SJean-Baptiste Boric 
122*1facb048SJean-Baptiste Boric 	newstatusfd = fcntl(*statusfd, F_DUPFD, *statusfd+1);
123*1facb048SJean-Baptiste Boric 	if (newstatusfd == -1)
124*1facb048SJean-Baptiste Boric 		return -1;
125*1facb048SJean-Baptiste Boric 
126*1facb048SJean-Baptiste Boric 	close(*statusfd);
127*1facb048SJean-Baptiste Boric 	*statusfd = newstatusfd;
128*1facb048SJean-Baptiste Boric 	return 0;
129*1facb048SJean-Baptiste Boric }
130*1facb048SJean-Baptiste Boric 
131*1facb048SJean-Baptiste Boric static int
132*1facb048SJean-Baptiste Boric process_file_actions_entry(posix_spawn_file_actions_entry_t * fae,
133*1facb048SJean-Baptiste Boric 	int * statusfd)
134*1facb048SJean-Baptiste Boric {
135*1facb048SJean-Baptiste Boric 	int fd;
136*1facb048SJean-Baptiste Boric 
137*1facb048SJean-Baptiste Boric 	switch (fae->fae_action) {
138*1facb048SJean-Baptiste Boric 	case FAE_OPEN:
139*1facb048SJean-Baptiste Boric 		/* Perform an open(), make it use the right fd */
140*1facb048SJean-Baptiste Boric 		fd = open(fae->fae_path, fae->fae_oflag, fae->fae_mode);
141*1facb048SJean-Baptiste Boric 		if (fd < 0)
142*1facb048SJean-Baptiste Boric 			return errno;
143*1facb048SJean-Baptiste Boric 		if (fd != fae->fae_fildes) {
144*1facb048SJean-Baptiste Boric 			if (fae->fae_fildes == *statusfd) {
145*1facb048SJean-Baptiste Boric 				/* Move the status fd out of the way */
146*1facb048SJean-Baptiste Boric 				if (move_fd_up(statusfd) == -1)
147*1facb048SJean-Baptiste Boric 					return errno;
148*1facb048SJean-Baptiste Boric 			}
149*1facb048SJean-Baptiste Boric 			if (dup2(fd, fae->fae_fildes) == -1)
150*1facb048SJean-Baptiste Boric 				return errno;
151*1facb048SJean-Baptiste Boric 			if (close(fd) != 0) {
152*1facb048SJean-Baptiste Boric 				if (errno == EBADF)
153*1facb048SJean-Baptiste Boric 					return EBADF;
154*1facb048SJean-Baptiste Boric 			}
155*1facb048SJean-Baptiste Boric 		}
156*1facb048SJean-Baptiste Boric 		if (fcntl(fae->fae_fildes, F_SETFD, 0) == -1)
157*1facb048SJean-Baptiste Boric 			return errno;
158*1facb048SJean-Baptiste Boric 		break;
159*1facb048SJean-Baptiste Boric 
160*1facb048SJean-Baptiste Boric 	case FAE_DUP2:
161*1facb048SJean-Baptiste Boric 		if (fae->fae_fildes == *statusfd) {
162*1facb048SJean-Baptiste Boric 			/* Nice try */
163*1facb048SJean-Baptiste Boric 			return EBADF;
164*1facb048SJean-Baptiste Boric 		}
165*1facb048SJean-Baptiste Boric 		if (fae->fae_newfildes == *statusfd) {
166*1facb048SJean-Baptiste Boric 			/* Move the status file descriptor out of the way */
167*1facb048SJean-Baptiste Boric 			if (move_fd_up(statusfd) == -1)
168*1facb048SJean-Baptiste Boric 				return errno;
169*1facb048SJean-Baptiste Boric 		}
170*1facb048SJean-Baptiste Boric 		/* Perform a dup2() */
171*1facb048SJean-Baptiste Boric 		if (dup2(fae->fae_fildes, fae->fae_newfildes) == -1)
172*1facb048SJean-Baptiste Boric 			return errno;
173*1facb048SJean-Baptiste Boric 		if (fcntl(fae->fae_newfildes, F_SETFD, 0) == -1)
174*1facb048SJean-Baptiste Boric 			return errno;
175*1facb048SJean-Baptiste Boric 		break;
176*1facb048SJean-Baptiste Boric 
177*1facb048SJean-Baptiste Boric 	case FAE_CLOSE:
178*1facb048SJean-Baptiste Boric 		/* Perform a close(), do not fail if already closed */
179*1facb048SJean-Baptiste Boric 		if (fae->fae_fildes != *statusfd)
180*1facb048SJean-Baptiste Boric 			(void)close(fae->fae_fildes);
181*1facb048SJean-Baptiste Boric 		break;
182*1facb048SJean-Baptiste Boric 	}
183*1facb048SJean-Baptiste Boric 	return 0;
184*1facb048SJean-Baptiste Boric }
185*1facb048SJean-Baptiste Boric 
186*1facb048SJean-Baptiste Boric static int
187*1facb048SJean-Baptiste Boric process_file_actions(const posix_spawn_file_actions_t * fa, int * statusfd)
188*1facb048SJean-Baptiste Boric {
189*1facb048SJean-Baptiste Boric 	posix_spawn_file_actions_entry_t *fae;
190*1facb048SJean-Baptiste Boric 	int error;
191*1facb048SJean-Baptiste Boric 
192*1facb048SJean-Baptiste Boric 	/* Replay all file descriptor modifications */
193*1facb048SJean-Baptiste Boric 	for (unsigned i = 0; i < fa->len; i++) {
194*1facb048SJean-Baptiste Boric 		fae = &fa->fae[i];
195*1facb048SJean-Baptiste Boric 		error = process_file_actions_entry(fae, statusfd);
196*1facb048SJean-Baptiste Boric 		if (error)
197*1facb048SJean-Baptiste Boric 			return error;
198*1facb048SJean-Baptiste Boric 	}
199*1facb048SJean-Baptiste Boric 	return 0;
200*1facb048SJean-Baptiste Boric }
201*1facb048SJean-Baptiste Boric 
202*1facb048SJean-Baptiste Boric int
203*1facb048SJean-Baptiste Boric posix_spawn(pid_t * __restrict pid, const char * __restrict path,
204*1facb048SJean-Baptiste Boric 	const posix_spawn_file_actions_t * fa,
205*1facb048SJean-Baptiste Boric 	const posix_spawnattr_t * __restrict sa,
206*1facb048SJean-Baptiste Boric 	char * const * __restrict argv, char * const * __restrict envp)
207*1facb048SJean-Baptiste Boric {
208*1facb048SJean-Baptiste Boric 	pid_t p;
209*1facb048SJean-Baptiste Boric 	int r, error, pfd[2];
210*1facb048SJean-Baptiste Boric 
211*1facb048SJean-Baptiste Boric 	/*
212*1facb048SJean-Baptiste Boric 	 * Due to the lack of vfork() in Minix, an alternative solution with
213*1facb048SJean-Baptiste Boric 	 * pipes is used. The writing end is set to close on exec() and the
214*1facb048SJean-Baptiste Boric 	 * parent performs a read() on it.
215*1facb048SJean-Baptiste Boric 	 *
216*1facb048SJean-Baptiste Boric 	 * On success, a successful 0-length read happens.
217*1facb048SJean-Baptiste Boric 	 * On failure, the child writes the errno to the pipe before exiting,
218*1facb048SJean-Baptiste Boric 	 * the error is thus transmitted to the parent.
219*1facb048SJean-Baptiste Boric 	 *
220*1facb048SJean-Baptiste Boric 	 * This solution was taken from stackoverflow.com question 3703013.
221*1facb048SJean-Baptiste Boric 	 */
222*1facb048SJean-Baptiste Boric 	if (pipe(pfd) == -1)
223*1facb048SJean-Baptiste Boric 		return errno;
224*1facb048SJean-Baptiste Boric 
225*1facb048SJean-Baptiste Boric 	p = fork();
226*1facb048SJean-Baptiste Boric 	switch (p) {
227*1facb048SJean-Baptiste Boric 	case -1:
228*1facb048SJean-Baptiste Boric 		close(pfd[0]);
229*1facb048SJean-Baptiste Boric 		close(pfd[1]);
230*1facb048SJean-Baptiste Boric 
231*1facb048SJean-Baptiste Boric 		return errno;
232*1facb048SJean-Baptiste Boric 
233*1facb048SJean-Baptiste Boric 	case 0:
234*1facb048SJean-Baptiste Boric 		close(pfd[0]);
235*1facb048SJean-Baptiste Boric 
236*1facb048SJean-Baptiste Boric 		if (fcntl(pfd[1], F_SETFD, FD_CLOEXEC) != 0) {
237*1facb048SJean-Baptiste Boric 			error = errno;
238*1facb048SJean-Baptiste Boric 			break;
239*1facb048SJean-Baptiste Boric 		}
240*1facb048SJean-Baptiste Boric 
241*1facb048SJean-Baptiste Boric 		if (sa != NULL) {
242*1facb048SJean-Baptiste Boric 			error = process_spawnattr(sa);
243*1facb048SJean-Baptiste Boric 			if (error)
244*1facb048SJean-Baptiste Boric 				break;
245*1facb048SJean-Baptiste Boric 		}
246*1facb048SJean-Baptiste Boric 		if (fa != NULL) {
247*1facb048SJean-Baptiste Boric 			error = process_file_actions(fa, &pfd[1]);
248*1facb048SJean-Baptiste Boric 			if (error)
249*1facb048SJean-Baptiste Boric 				break;
250*1facb048SJean-Baptiste Boric 		}
251*1facb048SJean-Baptiste Boric 
252*1facb048SJean-Baptiste Boric 		(void)execve(path, argv, envp != NULL ? envp : *p_environ);
253*1facb048SJean-Baptiste Boric 
254*1facb048SJean-Baptiste Boric 		error = errno;
255*1facb048SJean-Baptiste Boric 		break;
256*1facb048SJean-Baptiste Boric 
257*1facb048SJean-Baptiste Boric 	default:
258*1facb048SJean-Baptiste Boric 		close(pfd[1]);
259*1facb048SJean-Baptiste Boric 
260*1facb048SJean-Baptiste Boric 		/* Retrieve child process status through pipe. */
261*1facb048SJean-Baptiste Boric 		r = read(pfd[0], &error, sizeof(error));
262*1facb048SJean-Baptiste Boric 		if (r == 0)
263*1facb048SJean-Baptiste Boric 			error = 0;
264*1facb048SJean-Baptiste Boric 		else if (r == -1)
265*1facb048SJean-Baptiste Boric 			error = errno;
266*1facb048SJean-Baptiste Boric 		close(pfd[0]);
267*1facb048SJean-Baptiste Boric 
268*1facb048SJean-Baptiste Boric 		if (pid != NULL)
269*1facb048SJean-Baptiste Boric 			*pid = p;
270*1facb048SJean-Baptiste Boric 		return error;
271*1facb048SJean-Baptiste Boric 	}
272*1facb048SJean-Baptiste Boric 
273*1facb048SJean-Baptiste Boric 	/* Child failed somewhere, propagate error through pipe and exit. */
274*1facb048SJean-Baptiste Boric 	write(pfd[1], &error, sizeof(error));
275*1facb048SJean-Baptiste Boric 	close(pfd[1]);
276*1facb048SJean-Baptiste Boric 	_exit(127);
277*1facb048SJean-Baptiste Boric }
278