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/queue.h>
44 #include "un-namespace.h"
45 #include "libc_private.h"
47 extern char **environ;
49 struct __posix_spawnattr {
52 struct sched_param sa_schedparam;
54 sigset_t sa_sigdefault;
58 struct __posix_spawn_file_actions {
59 STAILQ_HEAD(, __posix_spawn_file_actions_entry) fa_list;
62 typedef struct __posix_spawn_file_actions_entry {
63 STAILQ_ENTRY(__posix_spawn_file_actions_entry) fae_list;
64 enum { FAE_OPEN, FAE_DUP2, FAE_CLOSE } fae_action;
70 #define fae_path fae_data.open.path
72 #define fae_oflag fae_data.open.oflag
74 #define fae_mode fae_data.open.mode
78 #define fae_newfildes fae_data.dup2.newfildes
81 } posix_spawn_file_actions_entry_t;
88 process_spawnattr(const posix_spawnattr_t sa)
90 struct sigaction sigact = { .sa_flags = 0, .sa_handler = SIG_DFL };
94 * POSIX doesn't really describe in which order everything
95 * should be set. We'll just set them in the order in which they
99 /* Set process group */
100 if (sa->sa_flags & POSIX_SPAWN_SETPGROUP) {
101 if (setpgid(0, sa->sa_pgroup) != 0)
105 /* Set scheduler policy */
106 if (sa->sa_flags & POSIX_SPAWN_SETSCHEDULER) {
107 if (sched_setscheduler(0, sa->sa_schedpolicy,
108 &sa->sa_schedparam) != 0)
110 } else if (sa->sa_flags & POSIX_SPAWN_SETSCHEDPARAM) {
111 if (sched_setparam(0, &sa->sa_schedparam) != 0)
115 /* Reset user ID's */
116 if (sa->sa_flags & POSIX_SPAWN_RESETIDS) {
117 if (setegid(getgid()) != 0)
119 if (seteuid(getuid()) != 0)
124 * Set signal masks/defaults.
125 * Use unwrapped syscall, libthr is in undefined state after vfork().
127 if (sa->sa_flags & POSIX_SPAWN_SETSIGMASK) {
128 __sys_sigprocmask(SIG_SETMASK, &sa->sa_sigmask, NULL);
131 if (sa->sa_flags & POSIX_SPAWN_SETSIGDEF) {
132 for (i = 1; i <= _SIG_MAXSIG; i++) {
133 if (sigismember(&sa->sa_sigdefault, i))
134 if (__sys_sigaction(i, &sigact, NULL) != 0)
143 process_file_actions_entry(posix_spawn_file_actions_entry_t *fae)
147 switch (fae->fae_action) {
149 /* Perform an open(), make it use the right fd */
150 fd = _open(fae->fae_path, fae->fae_oflag, fae->fae_mode);
153 if (fd != fae->fae_fildes) {
154 if (_dup2(fd, fae->fae_fildes) == -1) {
157 return (saved_errno);
159 if (_close(fd) != 0) {
164 if (_fcntl(fae->fae_fildes, F_SETFD, 0) == -1)
168 /* Perform a dup2() */
169 if (_dup2(fae->fae_fildes, fae->fae_newfildes) == -1)
171 if (_fcntl(fae->fae_newfildes, F_SETFD, 0) == -1)
175 /* Perform a close(), do not fail if already closed */
176 (void)_close(fae->fae_fildes);
183 process_file_actions(const posix_spawn_file_actions_t fa)
185 posix_spawn_file_actions_entry_t *fae;
188 /* Replay all file descriptor modifications */
189 STAILQ_FOREACH(fae, &fa->fa_list, fae_list) {
190 error = process_file_actions_entry(fae);
198 do_posix_spawn(pid_t *pid, const char *path,
199 const posix_spawn_file_actions_t *fa,
200 const posix_spawnattr_t *sa,
201 char * const argv[], char * const envp[], int use_env_path)
204 volatile int error = 0;
212 error = process_spawnattr(*sa);
217 error = process_file_actions(*fa);
222 _execvpe(path, argv, envp != NULL ? envp : environ);
224 _execve(path, argv, envp != NULL ? envp : environ);
229 _waitpid(p, NULL, WNOHANG);
230 else if (pid != NULL)
237 posix_spawn(pid_t *pid, const char *path,
238 const posix_spawn_file_actions_t *fa,
239 const posix_spawnattr_t *sa,
240 char * const argv[], char * const envp[])
242 return do_posix_spawn(pid, path, fa, sa, argv, envp, 0);
246 posix_spawnp(pid_t *pid, const char *path,
247 const posix_spawn_file_actions_t *fa,
248 const posix_spawnattr_t *sa,
249 char * const argv[], char * const envp[])
251 return do_posix_spawn(pid, path, fa, sa, argv, envp, 1);
255 * File descriptor actions
259 posix_spawn_file_actions_init(posix_spawn_file_actions_t *ret)
261 posix_spawn_file_actions_t fa;
263 fa = malloc(sizeof(struct __posix_spawn_file_actions));
267 STAILQ_INIT(&fa->fa_list);
273 posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *fa)
275 posix_spawn_file_actions_entry_t *fae;
277 while ((fae = STAILQ_FIRST(&(*fa)->fa_list)) != NULL) {
278 /* Remove file action entry from the queue */
279 STAILQ_REMOVE_HEAD(&(*fa)->fa_list, fae_list);
281 /* Deallocate file action entry */
282 if (fae->fae_action == FAE_OPEN)
292 posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * __restrict fa,
293 int fildes, const char * __restrict path, int oflag, mode_t mode)
295 posix_spawn_file_actions_entry_t *fae;
301 /* Allocate object */
302 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
306 /* Set values and store in queue */
307 fae->fae_action = FAE_OPEN;
308 fae->fae_path = strdup(path);
309 if (fae->fae_path == NULL) {
314 fae->fae_fildes = fildes;
315 fae->fae_oflag = oflag;
316 fae->fae_mode = mode;
318 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
323 posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fa,
324 int fildes, int newfildes)
326 posix_spawn_file_actions_entry_t *fae;
328 if (fildes < 0 || newfildes < 0)
331 /* Allocate object */
332 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
336 /* Set values and store in queue */
337 fae->fae_action = FAE_DUP2;
338 fae->fae_fildes = fildes;
339 fae->fae_newfildes = newfildes;
341 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
346 posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *fa,
349 posix_spawn_file_actions_entry_t *fae;
354 /* Allocate object */
355 fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
359 /* Set values and store in queue */
360 fae->fae_action = FAE_CLOSE;
361 fae->fae_fildes = fildes;
363 STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
372 posix_spawnattr_init(posix_spawnattr_t *ret)
374 posix_spawnattr_t sa;
376 sa = calloc(1, sizeof(struct __posix_spawnattr));
380 /* Set defaults as specified by POSIX, cleared above */
386 posix_spawnattr_destroy(posix_spawnattr_t *sa)
393 posix_spawnattr_getflags(const posix_spawnattr_t * __restrict sa,
394 short * __restrict flags)
396 *flags = (*sa)->sa_flags;
401 posix_spawnattr_getpgroup(const posix_spawnattr_t * __restrict sa,
402 pid_t * __restrict pgroup)
404 *pgroup = (*sa)->sa_pgroup;
409 posix_spawnattr_getschedparam(const posix_spawnattr_t * __restrict sa,
410 struct sched_param * __restrict schedparam)
412 *schedparam = (*sa)->sa_schedparam;
417 posix_spawnattr_getschedpolicy(const posix_spawnattr_t * __restrict sa,
418 int * __restrict schedpolicy)
420 *schedpolicy = (*sa)->sa_schedpolicy;
425 posix_spawnattr_getsigdefault(const posix_spawnattr_t * __restrict sa,
426 sigset_t * __restrict sigdefault)
428 *sigdefault = (*sa)->sa_sigdefault;
433 posix_spawnattr_getsigmask(const posix_spawnattr_t * __restrict sa,
434 sigset_t * __restrict sigmask)
436 *sigmask = (*sa)->sa_sigmask;
441 posix_spawnattr_setflags(posix_spawnattr_t *sa, short flags)
443 (*sa)->sa_flags = flags;
448 posix_spawnattr_setpgroup(posix_spawnattr_t *sa, pid_t pgroup)
450 (*sa)->sa_pgroup = pgroup;
455 posix_spawnattr_setschedparam(posix_spawnattr_t * __restrict sa,
456 const struct sched_param * __restrict schedparam)
458 (*sa)->sa_schedparam = *schedparam;
463 posix_spawnattr_setschedpolicy(posix_spawnattr_t *sa, int schedpolicy)
465 (*sa)->sa_schedpolicy = schedpolicy;
470 posix_spawnattr_setsigdefault(posix_spawnattr_t * __restrict sa,
471 const sigset_t * __restrict sigdefault)
473 (*sa)->sa_sigdefault = *sigdefault;
478 posix_spawnattr_setsigmask(posix_spawnattr_t * __restrict sa,
479 const sigset_t * __restrict sigmask)
481 (*sa)->sa_sigmask = *sigmask;