1570af302Sopenharmony_ci#include <unistd.h> 2570af302Sopenharmony_ci#include <stdlib.h> 3570af302Sopenharmony_ci#include <signal.h> 4570af302Sopenharmony_ci#include <sys/wait.h> 5570af302Sopenharmony_ci#include <spawn.h> 6570af302Sopenharmony_ci#include <errno.h> 7570af302Sopenharmony_ci#include "pthread_impl.h" 8570af302Sopenharmony_ci#include <unsupported_api.h> 9570af302Sopenharmony_ci 10570af302Sopenharmony_ciextern char **__environ; 11570af302Sopenharmony_ci 12570af302Sopenharmony_ciint system(const char *cmd) 13570af302Sopenharmony_ci{ 14570af302Sopenharmony_ci pid_t pid; 15570af302Sopenharmony_ci sigset_t old, reset; 16570af302Sopenharmony_ci struct sigaction sa = { .sa_handler = SIG_IGN }, oldint, oldquit; 17570af302Sopenharmony_ci int status = -1, ret; 18570af302Sopenharmony_ci posix_spawnattr_t attr; 19570af302Sopenharmony_ci 20570af302Sopenharmony_ci UNSUPPORTED_API_VOID(LITEOS_A); 21570af302Sopenharmony_ci pthread_testcancel(); 22570af302Sopenharmony_ci 23570af302Sopenharmony_ci if (!cmd) return 1; 24570af302Sopenharmony_ci 25570af302Sopenharmony_ci sigaction(SIGINT, &sa, &oldint); 26570af302Sopenharmony_ci sigaction(SIGQUIT, &sa, &oldquit); 27570af302Sopenharmony_ci sigaddset(&sa.sa_mask, SIGCHLD); 28570af302Sopenharmony_ci sigprocmask(SIG_BLOCK, &sa.sa_mask, &old); 29570af302Sopenharmony_ci 30570af302Sopenharmony_ci sigemptyset(&reset); 31570af302Sopenharmony_ci if (oldint.sa_handler != SIG_IGN) sigaddset(&reset, SIGINT); 32570af302Sopenharmony_ci if (oldquit.sa_handler != SIG_IGN) sigaddset(&reset, SIGQUIT); 33570af302Sopenharmony_ci posix_spawnattr_init(&attr); 34570af302Sopenharmony_ci posix_spawnattr_setsigmask(&attr, &old); 35570af302Sopenharmony_ci posix_spawnattr_setsigdefault(&attr, &reset); 36570af302Sopenharmony_ci posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF|POSIX_SPAWN_SETSIGMASK); 37570af302Sopenharmony_ci ret = posix_spawn(&pid, "/bin/sh", 0, &attr, 38570af302Sopenharmony_ci (char *[]){"sh", "-c", (char *)cmd, 0}, __environ); 39570af302Sopenharmony_ci posix_spawnattr_destroy(&attr); 40570af302Sopenharmony_ci 41570af302Sopenharmony_ci if (!ret) while (waitpid(pid, &status, 0)<0 && errno == EINTR); 42570af302Sopenharmony_ci sigaction(SIGINT, &oldint, NULL); 43570af302Sopenharmony_ci sigaction(SIGQUIT, &oldquit, NULL); 44570af302Sopenharmony_ci sigprocmask(SIG_SETMASK, &old, NULL); 45570af302Sopenharmony_ci 46570af302Sopenharmony_ci if (ret) errno = ret; 47570af302Sopenharmony_ci return status; 48570af302Sopenharmony_ci} 49