From 14f03b8efbc40b840eda8e203a6002d6c3f24250 Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Thu, 9 Apr 2015 16:06:46 +0300 Subject: [PATCH] avoid leaving dangling children by killing spawned processes recursively --- src/spawn.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/spawn.c b/src/spawn.c index 1b7ac264..6651f0b6 100644 --- a/src/spawn.c +++ b/src/spawn.c @@ -299,7 +299,8 @@ spawn_kill(pid_t pid, int sig) if(s->pid == pid) break; if (s) { - r = kill(pid, sig); + /* kill the whole process group */ + r = kill(-pid, sig); if (r < 0) r = -errno; } @@ -502,8 +503,14 @@ spawn_and_give_stdout(const char *prog, char *argv[], char *envp[], close(fd[1]); *rd = fd[0]; - if (pid) + if (pid) { *pid = p; + + // make the spawned process a session leader so killing the + // process group recursively kills any child process that + // might have been spawned + setpgid(p, p); + } return 0; }