diff --git a/src/util.c b/src/util.c index ce3d42d..0786fe9 100644 --- a/src/util.c +++ b/src/util.c @@ -108,16 +108,12 @@ spawn(const char *format, ...) if(!(sh = getenv("SHELL")) || sh[0] != '/') sh = "/bin/sh"; - if((pid = fork()) == 0) + if(!(pid = fork())) { - if((pid = fork()) == 0) - { - setsid(); - if (execl(sh, sh, "-c", cmd, (char*)NULL) == -1) - warnl("execl(sh -c %s)", cmd); - exit(EXIT_FAILURE); - } - exit(EXIT_SUCCESS); + setsid(); + if (execl(sh, sh, "-c", cmd, (char*)NULL) == -1) + warnl("execl(sh -c %s)", cmd); + exit(EXIT_FAILURE); } else if (pid == -1) warnl("fork"); diff --git a/src/wmfs.c b/src/wmfs.c index c985c88..b359df3 100644 --- a/src/wmfs.c +++ b/src/wmfs.c @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include @@ -538,6 +540,21 @@ exec_uicb_function(Display *dpy, Window root, char *func, char *cmd) XSync(dpy, False); } +static void +signal_handle(int sig) +{ + switch (sig) + { + case SIGQUIT: + case SIGTERM: + W->flags &= ~WMFS_RUNNING; + break; + case SIGCHLD: + while(waitpid(-1, NULL, WNOHANG) > 0); + break; + } +} + int main(int argc, char **argv) { @@ -545,6 +562,7 @@ main(int argc, char **argv) bool r; Display *dpy; char path[MAX_PATH_LEN] = { 0 }; + struct sigaction sa; (void)argc; sprintf(path, "%s/"CONFIG_DEFAULT_PATH, getenv("HOME")); @@ -600,6 +618,14 @@ main(int argc, char **argv) exit(EXIT_FAILURE); } + /* Set signal handler */ + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = signal_handle; + sigemptyset(&sa.sa_mask); + sigaction(SIGQUIT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + sigaction(SIGCHLD, &sa, NULL); + /* Core */ wmfs_init(); wmfs_scan();