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