overhaul system() and popen() to use vfork; fix various related bugs
[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
17 int system(const char *cmd)
18 {
19         pid_t pid;
20         sigset_t old;
21         struct sigaction sa = { .sa_handler = SIG_IGN }, oldint, oldquit;
22         int status = -1, i;
23
24         if (!cmd) return 1;
25
26         sigaction(SIGINT, &sa, &oldint);
27         sigaction(SIGQUIT, &sa, &oldquit);
28         sigprocmask(SIG_BLOCK, SIGALL_SET, &old);
29
30         __acquire_ptc();
31         pid = __vfork();
32         __release_ptc();
33
34         if (pid > 0) {
35                 sigset_t new = old;
36                 sigaddset(&new, SIGCHLD);
37                 sigprocmask(SIG_BLOCK, &new, 0);
38                 while (waitpid(pid, &status, 0) && errno == EINTR);
39         }
40
41         if (pid) {
42                 sigaction(SIGINT, &oldint, NULL);
43                 sigaction(SIGQUIT, &oldquit, NULL);
44                 sigprocmask(SIG_SETMASK, &old, NULL);
45                 return status;
46         }
47
48         /* Before we can unblock signals in the child, all signal
49          * handlers must be eliminated -- even implementation-internal
50          * ones. Otherwise, a signal handler could run in the child
51          * and clobber the parent's memory (due to vfork). */
52         for (i=1; i<=8*__SYSCALL_SSLEN; i++) {
53                 struct sigaction sa;
54                 __libc_sigaction(i, 0, &sa);
55                 if (sa.sa_handler!=SIG_IGN && sa.sa_handler!=SIG_DFL) {
56                         sa.sa_handler = SIG_DFL;
57                         __libc_sigaction(i, &sa, 0);
58                 }
59         }
60
61         sigprocmask(SIG_SETMASK, &old, NULL);
62         execl("/bin/sh", "sh", "-c", cmd, (char *)0);
63         _exit(127);
64 }