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