Run startup_cmd in new session and kill the entire group

When a user's startup_cmd is a little more complex, e.g. a shell script,
and forks off several processes, then killing only the main child pid
might leave unwanted processes behind on exit. Not all children will
notice when their parent or the compositor has quit.

To fix this, put startup_cmd into its own session and process group, and
kill the entire group on exit.
This commit is contained in:
Peter Hofmann 2024-06-18 19:09:32 +02:00 committed by Leonardo Hernández Hernández
parent c2e7350f2e
commit 7a46fccdba
No known key found for this signature in database
GPG Key ID: E538897EE11B9624

3
dwl.c
View File

@ -674,7 +674,7 @@ cleanup(void)
#endif #endif
wl_display_destroy_clients(dpy); wl_display_destroy_clients(dpy);
if (child_pid > 0) { if (child_pid > 0) {
kill(child_pid, SIGTERM); kill(-child_pid, SIGTERM);
waitpid(child_pid, NULL, 0); waitpid(child_pid, NULL, 0);
} }
wlr_xcursor_manager_destroy(cursor_mgr); wlr_xcursor_manager_destroy(cursor_mgr);
@ -2141,6 +2141,7 @@ run(char *startup_cmd)
if ((child_pid = fork()) < 0) if ((child_pid = fork()) < 0)
die("startup: fork:"); die("startup: fork:");
if (child_pid == 0) { if (child_pid == 0) {
setsid();
dup2(piperw[0], STDIN_FILENO); dup2(piperw[0], STDIN_FILENO);
close(piperw[0]); close(piperw[0]);
close(piperw[1]); close(piperw[1]);