2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "namespace.h"
31 #include <sys/queue.h>
42 #include "un-namespace.h"
43 #include "libc_private.h"
45 extern char **environ;
47 struct __posix_spawnattr {
50 struct sched_param sa_schedparam;
52 sigset_t sa_sigdefault;
56 struct __posix_spawn_file_actions {
57 STAILQ_HEAD(, __posix_spawn_file_actions_entry) fa_list;
60 typedef struct __posix_spawn_file_actions_entry {
61 STAILQ_ENTRY(__posix_spawn_file_actions_entry) fae_list;
62 enum { FAE_OPEN, FAE_DUP2, FAE_CLOSE } fae_action;
68 #define fae_path fae_data.open.path
70 #define fae_oflag fae_data.open.oflag
72 #define fae_mode fae_data.open.mode
76 #define fae_newfildes fae_data.dup2.newfildes
79 } posix_spawn_file_actions_entry_t;
86 process_spawnattr(const posix_spawnattr_t sa)
88 struct sigaction sigact = { .sa_flags = 0, .sa_handler = SIG_DFL };
92 * POSIX doesn't really describe in which order everything
93 * should be set. We'll just set them in the order in which they
97 /* Set process group */
98 if (sa->sa_flags & POSIX_SPAWN_SETPGROUP) {
99 if (setpgid(0, sa->sa_pgroup) != 0)
103 /* Set scheduler policy */
104 if (sa->sa_flags & POSIX_SPAWN_SETSCHEDULER) {
105 if (sched_setscheduler(0, sa->sa_schedpolicy,
106 &sa->sa_schedparam) != 0)
108 } else if (sa->sa_flags & POSIX_SPAWN_SETSCHEDPARAM) {
109 if (sched_setparam(0, &sa->sa_schedparam) != 0)
113 /* Reset user ID's */
114 if (sa->sa_flags & POSIX_SPAWN_RESETIDS) {
115 if (setegid(getgid()) != 0)
117 if (seteuid(getuid()) != 0)
121 /* Set signal masks/defaults */
122 if (sa->sa_flags & POSIX_SPAWN_SETSIGMASK) {
123 _sigprocmask(SIG_SETMASK, &sa->sa_sigmask, NULL);
126 if (sa->sa_flags & POSIX_SPAWN_SETSIGDEF) {
127 for (i = 1; i <= _SIG_MAXSIG; i++) {
128 if (sigismember(&sa->sa_sigdefault, i))
129 if (_sigaction(i, &sigact, NULL) != 0)
138 process_file_actions_entry(posix_spawn_file_actions_entry_t *fae)
142 switch (fae->fae_action) {
144 /* Perform an open(), make it use the right fd */
145 fd = _open(fae->fae_path, fae->fae_oflag, fae->fae_mode);
148 if (fd != fae->fae_fildes) {
149 if (_dup2(fd, fae->fae_fildes) == -1)
151 if (_close(fd) != 0) {
156 if (_fcntl(fae->fae_fildes, F_SETFD, 0) == -1)
160 /* Perform a dup2() */
161 if (_dup2(fae->fae_fildes, fae->fae_newfildes) == -1)
163 if (_fcntl(fae->fae_newfildes, F_SETFD, 0) == -1)
167 /* Perform a close(), do not fail if already closed */
168 (void)_close(fae->fae_fildes);
175 process_file_actions(const posix_spawn_file_actions_t fa)
177 posix_spawn_file_actions_entry_t *fae;
180 /* Replay all file descriptor modifications */
181 STAILQ_FOREACH(fae, &fa->fa_list, fae_list) {
182 error = process_file_actions_entry(fae);
190 do_posix_spawn(pid_t *pid, const char *path,
191 const posix_spawn_file_actions_t *fa,
192 const posix_spawnattr_t *sa,
193 char * const argv[], char * const envp[], int use_env_path)
196 volatile int error = 0;
204 error = process_spawnattr(*sa);
209 error = process_file_actions(*fa);
214 _execvpe(path, argv, envp != NULL ? envp : environ);
216 _execve(path, argv, envp != NULL ? envp : environ);
221 _waitpid(p, NULL, WNOHANG);
222 else if (pid != NULL)
229 posix_spawn(pid_t *pid, const char *path,
230 const posix_spawn_file_actions_t *fa,
231 const posix_spawnattr_t *sa,
232 char * const argv[], char * const envp[])
234 return do_posix_spawn(pid, path, fa, sa, argv, envp, 0);
238 posix_spawnp(pid_t *pid, const char *path,
239 const posix_spawn_file_actions_t *fa,
240 const posix_spawnattr_t *sa,
241 char * const argv[], char * const envp[])
243 return do_posix_spawn(pid, path, fa, sa, argv, envp, 1);
247 * File descriptor actions
251 posix_spawn_file_actions_init(posix_spawn_file_actions_t *ret)
253 posix_spawn_file_actions_t fa;
255 fa = malloc(sizeof(struct __posix_spawn_file_actions));
259 STAILQ_INIT(&fa->fa_list);
265 posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *fa)
267 posix_spawn_file_actions_entry_t *fae;
269 while ((fae = STAILQ_FIRST(&(*fa)->fa_list)) != NULL) {
270 /* Remove file action entry from the queue */
271 STAILQ_REMOVE_HEAD(&(*fa)->fa_list, fae_list);
273 /* Deallocate file action entry */
274 if (fae->fae_action == FAE_OPEN)
284 posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * __restrict fa,
285 int fildes, const char * __restrict path, int oflag, mode_t mode)
287 posix_spawn_file_actions_entry_t *fae;
293 /* Allocate object */
294 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
298 /* Set values and store in queue */
299 fae->fae_action = FAE_OPEN;
300 fae->fae_path = strdup(path);
301 if (fae->fae_path == NULL) {
306 fae->fae_fildes = fildes;
307 fae->fae_oflag = oflag;
308 fae->fae_mode = mode;
310 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
315 posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fa,
316 int fildes, int newfildes)
318 posix_spawn_file_actions_entry_t *fae;
320 if (fildes < 0 || newfildes < 0)
323 /* Allocate object */
324 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
328 /* Set values and store in queue */
329 fae->fae_action = FAE_DUP2;
330 fae->fae_fildes = fildes;
331 fae->fae_newfildes = newfildes;
333 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
338 posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *fa,
341 posix_spawn_file_actions_entry_t *fae;
346 /* Allocate object */
347 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
351 /* Set values and store in queue */
352 fae->fae_action = FAE_CLOSE;
353 fae->fae_fildes = fildes;
355 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
364 posix_spawnattr_init(posix_spawnattr_t *ret)
366 posix_spawnattr_t sa;
368 sa = calloc(1, sizeof(struct __posix_spawnattr));
372 /* Set defaults as specified by POSIX, cleared above */
378 posix_spawnattr_destroy(posix_spawnattr_t *sa)
385 posix_spawnattr_getflags(const posix_spawnattr_t * __restrict sa,
386 short * __restrict flags)
388 *flags = (*sa)->sa_flags;
393 posix_spawnattr_getpgroup(const posix_spawnattr_t * __restrict sa,
394 pid_t * __restrict pgroup)
396 *pgroup = (*sa)->sa_pgroup;
401 posix_spawnattr_getschedparam(const posix_spawnattr_t * __restrict sa,
402 struct sched_param * __restrict schedparam)
404 *schedparam = (*sa)->sa_schedparam;
409 posix_spawnattr_getschedpolicy(const posix_spawnattr_t * __restrict sa,
410 int * __restrict schedpolicy)
412 *schedpolicy = (*sa)->sa_schedpolicy;
417 posix_spawnattr_getsigdefault(const posix_spawnattr_t * __restrict sa,
418 sigset_t * __restrict sigdefault)
420 *sigdefault = (*sa)->sa_sigdefault;
425 posix_spawnattr_getsigmask(const posix_spawnattr_t * __restrict sa,
426 sigset_t * __restrict sigmask)
428 *sigmask = (*sa)->sa_sigmask;
433 posix_spawnattr_setflags(posix_spawnattr_t *sa, short flags)
435 (*sa)->sa_flags = flags;
440 posix_spawnattr_setpgroup(posix_spawnattr_t *sa, pid_t pgroup)
442 (*sa)->sa_pgroup = pgroup;
447 posix_spawnattr_setschedparam(posix_spawnattr_t * __restrict sa,
448 const struct sched_param * __restrict schedparam)
450 (*sa)->sa_schedparam = *schedparam;
455 posix_spawnattr_setschedpolicy(posix_spawnattr_t *sa, int schedpolicy)
457 (*sa)->sa_schedpolicy = schedpolicy;
462 posix_spawnattr_setsigdefault(posix_spawnattr_t * __restrict sa,
463 const sigset_t * __restrict sigdefault)
465 (*sa)->sa_sigdefault = *sigdefault;
470 posix_spawnattr_setsigmask(posix_spawnattr_t * __restrict sa,
471 const sigset_t * __restrict sigmask)
473 (*sa)->sa_sigmask = *sigmask;