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