59f4a8bba2479b8749ccb69e0345f8bceecccc4b
[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) {
55                 struct fdop *op;
56                 int ret, fd;
57                 for (op = fa->__actions; op; op = op->next) {
58                         switch(op->cmd) {
59                         case FDOP_CLOSE:
60                                 ret = __syscall(SYS_close, op->fd);
61                                 break;
62                         case FDOP_DUP2:
63                                 ret = __syscall(SYS_dup2, op->fd, op->newfd)<0;
64                                 break;
65                         case FDOP_OPEN:
66                                 fd = __syscall(SYS_open, op->path,
67                                         op->oflag | O_LARGEFILE, op->mode);
68                                 if (fd == op->fd) {
69                                         ret = 0;
70                                 } else {
71                                         ret = __syscall(SYS_dup2, fd, op->fd)<0;
72                                         __syscall(SYS_close, fd);
73                                 }
74                                 break;
75                         }
76                         if (ret) _exit(127);
77                 }
78         }
79
80         sigprocmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
81                 ? &attr->__mask : &oldmask, 0);
82
83         if (envp) environ = envp;
84         exec(path, argv);
85         _exit(127);
86
87         return 0;
88 }
89
90 int posix_spawn(pid_t *res, const char *path,
91         const posix_spawn_file_actions_t *fa,
92         const posix_spawnattr_t *attr, char **argv, char **envp)
93 {
94         return __posix_spawnx(res, path, execv, fa, attr, argv, envp);
95 }