Skip to content

Commit ce7203b

Browse files
metan-ucwacerv
authored andcommitted
Make KILL properly terminate all forked processes
If the process we executed with ltx_handle_exec() forks children these did escape the attempt to kill these with ltx_handle_kill() because we only killed the parent, the children were reparented to init and may continue to run happily ever after. Since the parent process was killed we wait() on it and we got stuck in the read() that gets rest of the log from the pipe the stderr and stdout of the executed process is redirected to. That is because the pipe stil has writers active as long as any child process is active. If the main shell on the system was from busybox this happend for any executed command since busybox forks and executes in each case. To fix it, we move the top process in the slot into a seprate process group and the kill command kills a process group rather than a single process. In order to make sure that the process is moved into right process group right after the fork we move it both in the parent and in the child. We need it to move there in the parent in a case that subsequent command is kill and terminate the process before it has a chance to run. We need to make the move in the child because in case it runs before the parent (ltx process) we need the process group right before we may possibly fork any children. Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
1 parent 580e1f3 commit ce7203b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

ltx.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,11 +735,14 @@ static void ltx_handle_exec(struct ltx_session *session)
735735

736736
/* setup parent */
737737
if (pid) {
738+
setpgid(pid, pid);
738739
close(pipefd[1]);
739740
exec_slot->pid = pid;
740741
return;
741742
}
742743

744+
setpgid(0, 0);
745+
743746
/* redirect stdout to pipe */
744747
if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
745748
LTX_HANDLE_ERROR(session, "dup2() stdout", 1);
@@ -915,7 +918,7 @@ static void ltx_handle_kill(struct ltx_session *session)
915918
if (exec_slot->pid == -1)
916919
return;
917920

918-
int ret = kill(exec_slot->pid, SIGKILL);
921+
int ret = kill(-exec_slot->pid, SIGKILL);
919922
if (ret == -1 && errno != ESRCH) {
920923
LTX_HANDLE_ERROR(session, "kill() error", 1);
921924
return;

0 commit comments

Comments
 (0)