0cc8b8102d54519d52078c76276d1490bd116663
[musl] / src / process / system.c
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <signal.h>
4 #include <sys/wait.h>
5 #include <errno.h>
6 #include "pthread_impl.h"
7 #include "libc.h"
8
9 static void dummy_0()
10 {
11 }
12 weak_alias(dummy_0, __acquire_ptc);
13 weak_alias(dummy_0, __release_ptc);
14
15 pid_t __vfork(void);
16 void __testcancel(void);
17
18 int system(const char *cmd)
19 {
20         pid_t pid;
21         sigset_t old;
22         struct sigaction sa = { .sa_handler = SIG_IGN }, oldint, oldquit;
23         int status = -1, i;
24
25         __testcancel();
26
27         if (!cmd) return 1;
28
29         sigaction(SIGINT, &sa, &oldint);
30         sigaction(SIGQUIT, &sa, &oldquit);
31         sigprocmask(SIG_BLOCK, SIGALL_SET, &old);
32
33         __acquire_ptc();
34         pid = __vfork();
35
36         if (pid) __release_ptc();
37
38         if (pid > 0) {
39                 sigset_t new = old;
40                 sigaddset(&new, SIGCHLD);
41                 sigprocmask(SIG_BLOCK, &new, 0);
42                 while (waitpid(pid, &status, 0) && errno == EINTR);
43         }
44
45         if (pid) {
46                 sigaction(SIGINT, &oldint, NULL);
47                 sigaction(SIGQUIT, &oldquit, NULL);
48                 sigprocmask(SIG_SETMASK, &old, NULL);
49                 return status;
50         }
51
52         /* Before we can unblock signals in the child, all signal
53          * handlers must be eliminated -- even implementation-internal
54          * ones. Otherwise, a signal handler could run in the child
55          * and clobber the parent's memory (due to vfork). */
56         for (i=1; i<=8*__SYSCALL_SSLEN; i++) {
57                 struct sigaction sa;
58                 __libc_sigaction(i, 0, &sa);
59                 if (sa.sa_handler!=SIG_IGN && sa.sa_handler!=SIG_DFL) {
60                         sa.sa_handler = SIG_DFL;
61                         __libc_sigaction(i, &sa, 0);
62                 }
63         }
64
65         sigprocmask(SIG_SETMASK, &old, NULL);
66         execl("/bin/sh", "sh", "-c", cmd, (char *)0);
67         _exit(127);
68 }