| OLD | NEW |
| 1 #include <unistd.h> | 1 #include <unistd.h> |
| 2 #include <stdlib.h> | 2 #include <stdlib.h> |
| 3 #include <signal.h> | 3 #include <signal.h> |
| 4 #include <sys/wait.h> | 4 #include <sys/wait.h> |
| 5 #include <spawn.h> | 5 #include <spawn.h> |
| 6 #include <errno.h> | 6 #include <errno.h> |
| 7 #include "pthread_impl.h" | 7 #include "pthread_impl.h" |
| 8 #include "libc.h" | 8 #include "libc.h" |
| 9 | 9 |
| 10 extern char **__environ; | 10 extern char** __environ; |
| 11 | 11 |
| 12 int system(const char *cmd) | 12 int system(const char* cmd) { |
| 13 { | 13 pid_t pid; |
| 14 » pid_t pid; | 14 sigset_t old, reset; |
| 15 » sigset_t old, reset; | 15 struct sigaction sa = {.sa_handler = SIG_IGN}, oldint, oldquit; |
| 16 » struct sigaction sa = { .sa_handler = SIG_IGN }, oldint, oldquit; | 16 int status = 0x7f00, ret; |
| 17 » int status = 0x7f00, ret; | 17 posix_spawnattr_t attr; |
| 18 » posix_spawnattr_t attr; | |
| 19 | 18 |
| 20 » pthread_testcancel(); | 19 pthread_testcancel(); |
| 21 | 20 |
| 22 » if (!cmd) return 1; | 21 if (!cmd) |
| 22 return 1; |
| 23 | 23 |
| 24 » sigaction(SIGINT, &sa, &oldint); | 24 sigaction(SIGINT, &sa, &oldint); |
| 25 » sigaction(SIGQUIT, &sa, &oldquit); | 25 sigaction(SIGQUIT, &sa, &oldquit); |
| 26 » sigaddset(&sa.sa_mask, SIGCHLD); | 26 sigaddset(&sa.sa_mask, SIGCHLD); |
| 27 » sigprocmask(SIG_BLOCK, &sa.sa_mask, &old); | 27 sigprocmask(SIG_BLOCK, &sa.sa_mask, &old); |
| 28 | 28 |
| 29 » sigemptyset(&reset); | 29 sigemptyset(&reset); |
| 30 » if (oldint.sa_handler != SIG_IGN) sigaddset(&reset, SIGINT); | 30 if (oldint.sa_handler != SIG_IGN) |
| 31 » if (oldquit.sa_handler != SIG_IGN) sigaddset(&reset, SIGQUIT); | 31 sigaddset(&reset, SIGINT); |
| 32 » posix_spawnattr_init(&attr); | 32 if (oldquit.sa_handler != SIG_IGN) |
| 33 » posix_spawnattr_setsigmask(&attr, &old); | 33 sigaddset(&reset, SIGQUIT); |
| 34 » posix_spawnattr_setsigdefault(&attr, &reset); | 34 posix_spawnattr_init(&attr); |
| 35 » posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF|POSIX_SPAWN_SETSIG
MASK); | 35 posix_spawnattr_setsigmask(&attr, &old); |
| 36 » ret = posix_spawn(&pid, "/bin/sh", 0, &attr, | 36 posix_spawnattr_setsigdefault(&attr, &reset); |
| 37 » » (char *[]){"sh", "-c", (char *)cmd, 0}, __environ); | 37 posix_spawnattr_setflags(&attr, |
| 38 » posix_spawnattr_destroy(&attr); | 38 POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK); |
| 39 ret = posix_spawn(&pid, "/bin/sh", 0, &attr, |
| 40 (char* []){"sh", "-c", (char*)cmd, 0}, __environ); |
| 41 posix_spawnattr_destroy(&attr); |
| 39 | 42 |
| 40 » if (!ret) while (waitpid(pid, &status, 0)<0 && errno == EINTR); | 43 if (!ret) |
| 41 » sigaction(SIGINT, &oldint, NULL); | 44 while (waitpid(pid, &status, 0) < 0 && errno == EINTR) |
| 42 » sigaction(SIGQUIT, &oldquit, NULL); | 45 ; |
| 43 » sigprocmask(SIG_SETMASK, &old, NULL); | 46 sigaction(SIGINT, &oldint, NULL); |
| 47 sigaction(SIGQUIT, &oldquit, NULL); |
| 48 sigprocmask(SIG_SETMASK, &old, NULL); |
| 44 | 49 |
| 45 » if (ret) errno = ret; | 50 if (ret) |
| 46 » return status; | 51 errno = ret; |
| 52 return status; |
| 47 } | 53 } |
| OLD | NEW |