delete a redundant if in dynamic linker ctor execution loop
[musl.git] / src / process / system.c
blob5af59b809f18d309e240d057177af6788a3b54be
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <sys/wait.h>
5 #include <spawn.h>
6 #include <errno.h>
7 #include "pthread_impl.h"
9 extern char **__environ;
11 int system(const char *cmd)
13 pid_t pid;
14 sigset_t old, reset;
15 struct sigaction sa = { .sa_handler = SIG_IGN }, oldint, oldquit;
16 int status = -1, ret;
17 posix_spawnattr_t attr;
19 pthread_testcancel();
21 if (!cmd) return 1;
23 sigaction(SIGINT, &sa, &oldint);
24 sigaction(SIGQUIT, &sa, &oldquit);
25 sigaddset(&sa.sa_mask, SIGCHLD);
26 sigprocmask(SIG_BLOCK, &sa.sa_mask, &old);
28 sigemptyset(&reset);
29 if (oldint.sa_handler != SIG_IGN) sigaddset(&reset, SIGINT);
30 if (oldquit.sa_handler != SIG_IGN) sigaddset(&reset, SIGQUIT);
31 posix_spawnattr_init(&attr);
32 posix_spawnattr_setsigmask(&attr, &old);
33 posix_spawnattr_setsigdefault(&attr, &reset);
34 posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF|POSIX_SPAWN_SETSIGMASK);
35 ret = posix_spawn(&pid, "/bin/sh", 0, &attr,
36 (char *[]){"sh", "-c", (char *)cmd, 0}, __environ);
37 posix_spawnattr_destroy(&attr);
39 if (!ret) while (waitpid(pid, &status, 0)<0 && errno == EINTR);
40 sigaction(SIGINT, &oldint, NULL);
41 sigaction(SIGQUIT, &oldquit, NULL);
42 sigprocmask(SIG_SETMASK, &old, NULL);
44 if (ret) errno = ret;
45 return status;