2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "namespace.h"
33 #include <sys/param.h>
34 #include <sys/queue.h>
45 #include "un-namespace.h"
46 #include "libc_private.h"
48 extern char **environ;
50 struct __posix_spawnattr {
53 struct sched_param sa_schedparam;
55 sigset_t sa_sigdefault;
59 struct __posix_spawn_file_actions {
60 STAILQ_HEAD(, __posix_spawn_file_actions_entry) fa_list;
63 typedef struct __posix_spawn_file_actions_entry {
64 STAILQ_ENTRY(__posix_spawn_file_actions_entry) fae_list;
65 enum { FAE_OPEN, FAE_DUP2, FAE_CLOSE } fae_action;
71 #define fae_path fae_data.open.path
73 #define fae_oflag fae_data.open.oflag
75 #define fae_mode fae_data.open.mode
79 #define fae_newfildes fae_data.dup2.newfildes
82 } posix_spawn_file_actions_entry_t;
89 process_spawnattr(const posix_spawnattr_t sa)
91 struct sigaction sigact = { .sa_flags = 0, .sa_handler = SIG_DFL };
95 * POSIX doesn't really describe in which order everything
96 * should be set. We'll just set them in the order in which they
100 /* Set process group */
101 if (sa->sa_flags & POSIX_SPAWN_SETPGROUP) {
102 if (setpgid(0, sa->sa_pgroup) != 0)
106 /* Set scheduler policy */
107 if (sa->sa_flags & POSIX_SPAWN_SETSCHEDULER) {
108 if (sched_setscheduler(0, sa->sa_schedpolicy,
109 &sa->sa_schedparam) != 0)
111 } else if (sa->sa_flags & POSIX_SPAWN_SETSCHEDPARAM) {
112 if (sched_setparam(0, &sa->sa_schedparam) != 0)
116 /* Reset user ID's */
117 if (sa->sa_flags & POSIX_SPAWN_RESETIDS) {
118 if (setegid(getgid()) != 0)
120 if (seteuid(getuid()) != 0)
125 * Set signal masks/defaults.
126 * Use unwrapped syscall, libthr is in undefined state after vfork().
128 if (sa->sa_flags & POSIX_SPAWN_SETSIGMASK) {
129 __sys_sigprocmask(SIG_SETMASK, &sa->sa_sigmask, NULL);
132 if (sa->sa_flags & POSIX_SPAWN_SETSIGDEF) {
133 for (i = 1; i <= _SIG_MAXSIG; i++) {
134 if (sigismember(&sa->sa_sigdefault, i))
135 if (__sys_sigaction(i, &sigact, NULL) != 0)
144 process_file_actions_entry(posix_spawn_file_actions_entry_t *fae)
148 switch (fae->fae_action) {
150 /* Perform an open(), make it use the right fd */
151 fd = _open(fae->fae_path, fae->fae_oflag, fae->fae_mode);
154 if (fd != fae->fae_fildes) {
155 if (_dup2(fd, fae->fae_fildes) == -1) {
158 return (saved_errno);
160 if (_close(fd) != 0) {
165 if (_fcntl(fae->fae_fildes, F_SETFD, 0) == -1)
169 /* Perform a dup2() */
170 if (_dup2(fae->fae_fildes, fae->fae_newfildes) == -1)
172 if (_fcntl(fae->fae_newfildes, F_SETFD, 0) == -1)
176 /* Perform a close(), do not fail if already closed */
177 (void)_close(fae->fae_fildes);
184 process_file_actions(const posix_spawn_file_actions_t fa)
186 posix_spawn_file_actions_entry_t *fae;
189 /* Replay all file descriptor modifications */
190 STAILQ_FOREACH(fae, &fa->fa_list, fae_list) {
191 error = process_file_actions_entry(fae);
198 struct posix_spawn_args {
200 const posix_spawn_file_actions_t *fa;
201 const posix_spawnattr_t *sa;
208 #define PSPAWN_STACK_ALIGNMENT 16
209 #define PSPAWN_STACK_ALIGNBYTES (PSPAWN_STACK_ALIGNMENT - 1)
210 #define PSPAWN_STACK_ALIGN(sz) \
211 (((sz) + PSPAWN_STACK_ALIGNBYTES) & ~PSPAWN_STACK_ALIGNBYTES)
213 #if defined(__i386__) || defined(__amd64__)
215 * Below we'll assume that _RFORK_THREAD_STACK_SIZE is appropriately aligned for
216 * the posix_spawn() case where we do not end up calling _execvpe and won't ever
217 * try to allocate space on the stack for argv[].
219 #define _RFORK_THREAD_STACK_SIZE 4096
220 _Static_assert((_RFORK_THREAD_STACK_SIZE % PSPAWN_STACK_ALIGNMENT) == 0,
221 "Inappropriate stack size alignment");
225 _posix_spawn_thr(void *data)
227 struct posix_spawn_args *psa;
231 if (psa->sa != NULL) {
232 psa->error = process_spawnattr(*psa->sa);
236 if (psa->fa != NULL) {
237 psa->error = process_file_actions(*psa->fa);
241 envp = psa->envp != NULL ? psa->envp : environ;
242 if (psa->use_env_path)
243 _execvpe(psa->path, psa->argv, envp);
245 _execve(psa->path, psa->argv, envp);
248 /* This is called in such a way that it must not exit. */
253 do_posix_spawn(pid_t *pid, const char *path,
254 const posix_spawn_file_actions_t *fa,
255 const posix_spawnattr_t *sa,
256 char * const argv[], char * const envp[], int use_env_path)
258 struct posix_spawn_args psa;
260 #ifdef _RFORK_THREAD_STACK_SIZE
264 stacksz = _RFORK_THREAD_STACK_SIZE;
267 * We need to make sure we have enough room on the stack for the
268 * potential alloca() in execvPe if it gets kicked back an
269 * ENOEXEC from execve(2), plus the original buffer we gave
270 * ourselves; this protects us in the event that the caller
271 * intentionally or inadvertently supplies enough arguments to
272 * make us blow past the stack we've allocated from it.
274 for (cnt = 0; argv[cnt] != NULL; ++cnt)
276 stacksz += MAX(3, cnt + 2) * sizeof(char *);
277 stacksz = PSPAWN_STACK_ALIGN(stacksz);
281 * aligned_alloc is not safe to use here, because we can't guarantee
282 * that aligned_alloc and free will be provided by the same
283 * implementation. We've actively hit at least one application that
284 * will provide its own malloc/free but not aligned_alloc leading to
285 * a free by the wrong allocator.
287 stack = malloc(stacksz);
290 stacksz = (((uintptr_t)stack + stacksz) & ~PSPAWN_STACK_ALIGNBYTES) -
298 psa.use_env_path = use_env_path;
302 * Passing RFSPAWN to rfork(2) gives us effectively a vfork that drops
303 * non-ignored signal handlers. We'll fall back to the slightly less
304 * ideal vfork(2) if we get an EINVAL from rfork -- this should only
305 * happen with newer libc on older kernel that doesn't accept
308 #ifdef _RFORK_THREAD_STACK_SIZE
310 * x86 stores the return address on the stack, so rfork(2) cannot work
311 * as-is because the child would clobber the return address om the
312 * parent. Because of this, we must use rfork_thread instead while
313 * almost every other arch stores the return address in a register.
315 p = rfork_thread(RFSPAWN, stack + stacksz, _posix_spawn_thr, &psa);
320 /* _posix_spawn_thr does not return */
321 _posix_spawn_thr(&psa);
324 * The above block should leave us in a state where we've either
325 * succeeded and we're ready to process the results, or we need to
326 * fallback to vfork() if the kernel didn't like RFSPAWN.
329 if (p == -1 && errno == EINVAL) {
332 /* _posix_spawn_thr does not return */
333 _posix_spawn_thr(&psa);
338 /* Failed; ready to reap */
339 _waitpid(p, NULL, WNOHANG);
340 else if (pid != NULL)
347 posix_spawn(pid_t *pid, const char *path,
348 const posix_spawn_file_actions_t *fa,
349 const posix_spawnattr_t *sa,
350 char * const argv[], char * const envp[])
352 return do_posix_spawn(pid, path, fa, sa, argv, envp, 0);
356 posix_spawnp(pid_t *pid, const char *path,
357 const posix_spawn_file_actions_t *fa,
358 const posix_spawnattr_t *sa,
359 char * const argv[], char * const envp[])
361 return do_posix_spawn(pid, path, fa, sa, argv, envp, 1);
365 * File descriptor actions
369 posix_spawn_file_actions_init(posix_spawn_file_actions_t *ret)
371 posix_spawn_file_actions_t fa;
373 fa = malloc(sizeof(struct __posix_spawn_file_actions));
377 STAILQ_INIT(&fa->fa_list);
383 posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *fa)
385 posix_spawn_file_actions_entry_t *fae;
387 while ((fae = STAILQ_FIRST(&(*fa)->fa_list)) != NULL) {
388 /* Remove file action entry from the queue */
389 STAILQ_REMOVE_HEAD(&(*fa)->fa_list, fae_list);
391 /* Deallocate file action entry */
392 if (fae->fae_action == FAE_OPEN)
402 posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * __restrict fa,
403 int fildes, const char * __restrict path, int oflag, mode_t mode)
405 posix_spawn_file_actions_entry_t *fae;
411 /* Allocate object */
412 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
416 /* Set values and store in queue */
417 fae->fae_action = FAE_OPEN;
418 fae->fae_path = strdup(path);
419 if (fae->fae_path == NULL) {
424 fae->fae_fildes = fildes;
425 fae->fae_oflag = oflag;
426 fae->fae_mode = mode;
428 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
433 posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fa,
434 int fildes, int newfildes)
436 posix_spawn_file_actions_entry_t *fae;
438 if (fildes < 0 || newfildes < 0)
441 /* Allocate object */
442 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
446 /* Set values and store in queue */
447 fae->fae_action = FAE_DUP2;
448 fae->fae_fildes = fildes;
449 fae->fae_newfildes = newfildes;
451 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
456 posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *fa,
459 posix_spawn_file_actions_entry_t *fae;
464 /* Allocate object */
465 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
469 /* Set values and store in queue */
470 fae->fae_action = FAE_CLOSE;
471 fae->fae_fildes = fildes;
473 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
482 posix_spawnattr_init(posix_spawnattr_t *ret)
484 posix_spawnattr_t sa;
486 sa = calloc(1, sizeof(struct __posix_spawnattr));
490 /* Set defaults as specified by POSIX, cleared above */
496 posix_spawnattr_destroy(posix_spawnattr_t *sa)
503 posix_spawnattr_getflags(const posix_spawnattr_t * __restrict sa,
504 short * __restrict flags)
506 *flags = (*sa)->sa_flags;
511 posix_spawnattr_getpgroup(const posix_spawnattr_t * __restrict sa,
512 pid_t * __restrict pgroup)
514 *pgroup = (*sa)->sa_pgroup;
519 posix_spawnattr_getschedparam(const posix_spawnattr_t * __restrict sa,
520 struct sched_param * __restrict schedparam)
522 *schedparam = (*sa)->sa_schedparam;
527 posix_spawnattr_getschedpolicy(const posix_spawnattr_t * __restrict sa,
528 int * __restrict schedpolicy)
530 *schedpolicy = (*sa)->sa_schedpolicy;
535 posix_spawnattr_getsigdefault(const posix_spawnattr_t * __restrict sa,
536 sigset_t * __restrict sigdefault)
538 *sigdefault = (*sa)->sa_sigdefault;
543 posix_spawnattr_getsigmask(const posix_spawnattr_t * __restrict sa,
544 sigset_t * __restrict sigmask)
546 *sigmask = (*sa)->sa_sigmask;
551 posix_spawnattr_setflags(posix_spawnattr_t *sa, short flags)
553 (*sa)->sa_flags = flags;
558 posix_spawnattr_setpgroup(posix_spawnattr_t *sa, pid_t pgroup)
560 (*sa)->sa_pgroup = pgroup;
565 posix_spawnattr_setschedparam(posix_spawnattr_t * __restrict sa,
566 const struct sched_param * __restrict schedparam)
568 (*sa)->sa_schedparam = *schedparam;
573 posix_spawnattr_setschedpolicy(posix_spawnattr_t *sa, int schedpolicy)
575 (*sa)->sa_schedpolicy = schedpolicy;
580 posix_spawnattr_setsigdefault(posix_spawnattr_t * __restrict sa,
581 const sigset_t * __restrict sigdefault)
583 (*sa)->sa_sigdefault = *sigdefault;
588 posix_spawnattr_setsigmask(posix_spawnattr_t * __restrict sa,
589 const sigset_t * __restrict sigmask)
591 (*sa)->sa_sigmask = *sigmask;