From 11d440972d61ca2dc9a5770b72e32c779f3cdea1 Mon Sep 17 00:00:00 2001 From: Genki Sky Date: Fri, 10 Aug 2018 10:27:16 -0400 Subject: [PATCH] rootston: Double fork for keyboard bindings This avoids leaving around zombies, without having to setup SIGCHLD handler (which interferes with other fork/waitpid calls). --- rootston/keyboard.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 40d4a7c7..b5a8093b 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -84,6 +85,39 @@ static void pressed_keysyms_update(xkb_keysym_t *pressed_keysyms, } } +static void double_fork_shell_cmd(const char *shell_cmd) { + pid_t pid = fork(); + if (pid < 0) { + wlr_log(WLR_ERROR, "cannot execute binding command: fork() failed"); + return; + } + + if (pid == 0) { + pid = fork(); + if (pid == 0) { + execl("/bin/sh", "/bin/sh", "-c", shell_cmd, NULL); + _exit(EXIT_FAILURE); + } else { + _exit(pid == -1); + } + } + + int status; + while (waitpid(pid, &status, 0) < 0) { + if (errno == EINTR) { + continue; + } + wlr_log_errno(WLR_ERROR, "waitpid() on first child failed"); + return; + } + + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { + return; + } + + wlr_log(WLR_ERROR, "first child failed to fork command"); +} + static const char *exec_prefix = "exec "; static bool outputs_enabled = true; @@ -113,13 +147,7 @@ static void keyboard_binding_execute(struct roots_keyboard *keyboard, } } else if (strncmp(exec_prefix, command, strlen(exec_prefix)) == 0) { const char *shell_cmd = command + strlen(exec_prefix); - pid_t pid = fork(); - if (pid < 0) { - wlr_log(WLR_ERROR, "cannot execute binding command: fork() failed"); - return; - } else if (pid == 0) { - execl("/bin/sh", "/bin/sh", "-c", shell_cmd, (void *)NULL); - } + double_fork_shell_cmd(shell_cmd); } else if (strcmp(command, "maximize") == 0) { struct roots_view *focus = roots_seat_get_focus(seat); if (focus != NULL) {