fix usage of locks with vfork
[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
33         if (pid) __release_ptc();
34
35         if (pid > 0) {
36                 sigset_t new = old;
37                 sigaddset(&new, SIGCHLD);
38                 sigprocmask(SIG_BLOCK, &new, 0);
39                 while (waitpid(pid, &status, 0) && errno == EINTR);
40         }
41
42         if (pid) {
43                 sigaction(SIGINT, &oldint, NULL);
44                 sigaction(SIGQUIT, &oldquit, NULL);
45                 sigprocmask(SIG_SETMASK, &old, NULL);
46                 return status;
47         }
48
49         /* Before we can unblock signals in the child, all signal
50          * handlers must be eliminated -- even implementation-internal
51          * ones. Otherwise, a signal handler could run in the child
52          * and clobber the parent's memory (due to vfork). */
53         for (i=1; i<=8*__SYSCALL_SSLEN; i++) {
54                 struct sigaction sa;
55                 __libc_sigaction(i, 0, &sa);
56                 if (sa.sa_handler!=SIG_IGN && sa.sa_handler!=SIG_DFL) {
57                         sa.sa_handler = SIG_DFL;
58                         __libc_sigaction(i, &sa, 0);
59                 }
60         }
61
62         sigprocmask(SIG_SETMASK, &old, NULL);
63         execl("/bin/sh", "sh", "-c", cmd, (char *)0);
64         _exit(127);
65 }