summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/process/system.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
commit8c1ab65c0f548d20b7f177bdb736daaf603340e1 (patch)
treedf55b7e75bf43f2bf500845b105afe3ac3a5157e /libc-top-half/musl/src/process/system.c
parentInitial commit. (diff)
downloadwasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.tar.xz
wasi-libc-8c1ab65c0f548d20b7f177bdb736daaf603340e1.zip
Adding upstream version 0.0~git20221206.8b7148f.upstream/0.0_git20221206.8b7148f
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libc-top-half/musl/src/process/system.c')
-rw-r--r--libc-top-half/musl/src/process/system.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/process/system.c b/libc-top-half/musl/src/process/system.c
new file mode 100644
index 0000000..5af59b8
--- /dev/null
+++ b/libc-top-half/musl/src/process/system.c
@@ -0,0 +1,46 @@
+#include <unistd.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <spawn.h>
+#include <errno.h>
+#include "pthread_impl.h"
+
+extern char **__environ;
+
+int system(const char *cmd)
+{
+ pid_t pid;
+ sigset_t old, reset;
+ struct sigaction sa = { .sa_handler = SIG_IGN }, oldint, oldquit;
+ int status = -1, ret;
+ posix_spawnattr_t attr;
+
+ pthread_testcancel();
+
+ if (!cmd) return 1;
+
+ sigaction(SIGINT, &sa, &oldint);
+ sigaction(SIGQUIT, &sa, &oldquit);
+ sigaddset(&sa.sa_mask, SIGCHLD);
+ sigprocmask(SIG_BLOCK, &sa.sa_mask, &old);
+
+ sigemptyset(&reset);
+ if (oldint.sa_handler != SIG_IGN) sigaddset(&reset, SIGINT);
+ if (oldquit.sa_handler != SIG_IGN) sigaddset(&reset, SIGQUIT);
+ posix_spawnattr_init(&attr);
+ posix_spawnattr_setsigmask(&attr, &old);
+ posix_spawnattr_setsigdefault(&attr, &reset);
+ posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF|POSIX_SPAWN_SETSIGMASK);
+ ret = posix_spawn(&pid, "/bin/sh", 0, &attr,
+ (char *[]){"sh", "-c", (char *)cmd, 0}, __environ);
+ posix_spawnattr_destroy(&attr);
+
+ if (!ret) while (waitpid(pid, &status, 0)<0 && errno == EINTR);
+ sigaction(SIGINT, &oldint, NULL);
+ sigaction(SIGQUIT, &oldquit, NULL);
+ sigprocmask(SIG_SETMASK, &old, NULL);
+
+ if (ret) errno = ret;
+ return status;
+}