summaryrefslogtreecommitdiffstats
path: root/src/shell.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-05-23 02:59:46 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-05-23 02:59:46 +0200
commitd899fca42570d53c714d7656f628551939a2ac0c (patch)
tree2b9d6ba4dba2ede61488623c762ec4a541dd858e /src/shell.c
parent47a34ece26a093102675e0a2dfc42c8182b98257 (diff)
downloadfastd-d899fca42570d53c714d7656f628551939a2ac0c.tar
fastd-d899fca42570d53c714d7656f628551939a2ac0c.zip
Implement a different fix for the waitpid race condition not needing a reaper thread for each child
Diffstat (limited to 'src/shell.c')
-rw-r--r--src/shell.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/src/shell.c b/src/shell.c
index 9d03fbc..2be7f82 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -103,18 +103,15 @@ static void shell_command_setenv(pid_t pid, const fastd_shell_env_t *env) {
}
}
-static bool shell_command_do_exec(const fastd_shell_command_t *command, const fastd_shell_env_t *env, pid_t *pid_ret) {
+static bool shell_command_do_exec(const fastd_shell_command_t *command, const fastd_shell_env_t *env, pid_t *pid) {
pid_t parent = getpid();
- pid_t pid = fork();
- if (pid < 0) {
+ *pid = fork();
+ if (*pid < 0) {
pr_error_errno("shell_command_do_exec: fork");
return false;
}
- else if (pid > 0) {
- if (pid_ret)
- *pid_ret = pid;
-
+ else if (*pid > 0) {
return true;
}
@@ -174,6 +171,19 @@ bool fastd_shell_command_exec_sync(const fastd_shell_command_t *command, const f
return true;
}
+static void shell_command_exec_async(const fastd_shell_command_t *command, const fastd_shell_env_t *env) {
+ /* block SIGCHLD */
+ sigset_t set, oldset;
+ sigemptyset(&set);
+ sigaddset(&set, SIGCHLD);
+ pthread_sigmask(SIG_BLOCK, &set, &oldset);
+
+ pid_t pid;
+ if (shell_command_do_exec(command, env, &pid))
+ VECTOR_ADD(ctx.async_pids, pid);
+
+ pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+}
void fastd_shell_command_exec(const fastd_shell_command_t *command, const fastd_shell_env_t *env) {
if (!fastd_shell_command_isset(command))
@@ -182,5 +192,5 @@ void fastd_shell_command_exec(const fastd_shell_command_t *command, const fastd_
if (command->sync)
fastd_shell_command_exec_sync(command, env, NULL);
else
- shell_command_do_exec(command, env, NULL);
+ shell_command_exec_async(command, env);
}