604756e9f99239ac1ccf1b294d9270cd1263bd1f
[musl] / src / process / posix_spawn.c
1 #include <spawn.h>
2 #include <unistd.h>
3 #include <signal.h>
4 #include <stdint.h>
5 #include <fcntl.h>
6 #include "syscall.h"
7 #include "fdop.h"
8
9 extern char **environ;
10
11 pid_t __vfork(void);
12
13 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
14         int (*exec)(const char *, char *const *),
15         const posix_spawn_file_actions_t *fa,
16         const posix_spawnattr_t *restrict attr,
17         char *const argv[restrict], char *const envp[restrict])
18 {
19         pid_t pid;
20         sigset_t oldmask;
21         int i;
22         posix_spawnattr_t dummy_attr = { 0 };
23
24         if (!attr) attr = &dummy_attr;
25
26         sigprocmask(SIG_BLOCK, (void *)(uint64_t []){-1}, &oldmask);
27         pid = __vfork();
28
29         if (pid) {
30                 sigprocmask(SIG_SETMASK, &oldmask, 0);
31                 if (pid < 0) return -pid;
32                 *res = pid;
33                 return 0;
34         }
35
36         for (i=1; i<=64; i++) {
37                 struct sigaction sa;
38                 sigaction(i, 0, &sa);
39                 if (sa.sa_handler!=SIG_IGN ||
40                     ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
41                      && sigismember(&attr->__def, i) )) {
42                         sa.sa_handler = SIG_DFL;
43                         sigaction(i, &sa, 0);
44                 }
45         }
46
47         if ((attr->__flags&POSIX_SPAWN_SETPGROUP) && setpgid(0, attr->__pgrp))
48                 _exit(127);
49
50         /* Use syscalls directly because pthread state is not consistent
51          * for making calls to the library wrappers... */
52         if ((attr->__flags&POSIX_SPAWN_RESETIDS) && (
53                 __syscall(SYS_setgid, __syscall(SYS_getgid)) ||
54                 __syscall(SYS_setuid, __syscall(SYS_getuid)) ))
55                 _exit(127);
56
57         if (fa && fa->__actions) {
58                 struct fdop *op;
59                 int ret, fd;
60                 for (op = fa->__actions; op->next; op = op->next);
61                 for (; op; op = op->prev) {
62                         switch(op->cmd) {
63                         case FDOP_CLOSE:
64                                 ret = __syscall(SYS_close, op->fd);
65                                 break;
66                         case FDOP_DUP2:
67                                 ret = __syscall(SYS_dup2, op->fd, op->newfd)<0;
68                                 break;
69                         case FDOP_OPEN:
70                                 fd = __syscall(SYS_open, op->path,
71                                         op->oflag | O_LARGEFILE, op->mode);
72                                 if (fd == op->fd) {
73                                         ret = 0;
74                                 } else {
75                                         ret = __syscall(SYS_dup2, fd, op->fd)<0;
76                                         __syscall(SYS_close, fd);
77                                 }
78                                 break;
79                         }
80                         if (ret) _exit(127);
81                 }
82         }
83
84         sigprocmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
85                 ? &attr->__mask : &oldmask, 0);
86
87         if (envp) environ = (char **)envp;
88         exec(path, argv);
89         _exit(127);
90
91         return 0;
92 }
93
94 int posix_spawn(pid_t *restrict res, const char *restrict path,
95         const posix_spawn_file_actions_t *fa,
96         const posix_spawnattr_t *restrict attr,
97         char *const argv[restrict], char *const envp[restrict])
98 {
99         return __posix_spawnx(res, path, execv, fa, attr, argv, envp);
100 }