diff --git a/Makefile b/Makefile index a9558d2..54bd79f 100644 --- a/Makefile +++ b/Makefile @@ -17,10 +17,10 @@ LDFLAGS?=-Wl,--gc-sections LIBS=-lpam -_DEPS = version.h log.h util.h ui.h ui_state.h config.h pam.h desktop.h desktop_exec.h auth.h ofield.h efield.h keys.h users.h sessions.h chvt.h macros.h launch_state.h +_DEPS = version.h log.h util.h ui.h ui_state.h config.h pam.h desktop.h desktop_exec.h auth.h ofield.h efield.h keys.h users.h sessions.h chvt.h macros.h launch_state.h signal_handler.h DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) -_OBJ = main.o log.o util.o ui.o ui_state.o config.o pam.o desktop.o desktop_exec.o auth.o ofield.o efield.o users.o sessions.o chvt.o launch_state.o +_OBJ = main.o log.o util.o ui.o ui_state.o config.o pam.o desktop.o desktop_exec.o auth.o ofield.o efield.o users.o sessions.o chvt.o launch_state.o signal_handler.o OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) INFO_GIT_REV?=$$(git describe --long --tags --always || echo '?') diff --git a/include/signal_handler.h b/include/signal_handler.h new file mode 100644 index 0000000..94ece02 --- /dev/null +++ b/include/signal_handler.h @@ -0,0 +1,3 @@ +// handle SIGTERM by sending SIGTERM to all children, resulting +// in a graceful graphical shutdown +void setup_sigterm(); diff --git a/src/main.c b/src/main.c index 4a8aa29..484ac2e 100644 --- a/src/main.c +++ b/src/main.c @@ -12,12 +12,14 @@ #include "log.h" #include "macros.h" #include "sessions.h" +#include "signal_handler.h" #include "ui.h" #include "users.h" #include "util.h" #include "version.h" #define DATESTR_MAXBUFSIZE 0x20 + int main(int argc, char* argv[]) { // Logger char* log_output = getenv("LIDM_LOG"); @@ -85,6 +87,8 @@ int main(int argc, char* argv[]) { struct Vector users = get_human_users(); struct Vector sessions = get_avaliable_sessions(); + setup_sigterm(); + int ret = load(&users, &sessions); if (ret == 0) execl(argv[0], argv[0], NULL); } diff --git a/src/signal_handler.c b/src/signal_handler.c new file mode 100644 index 0000000..281a5fa --- /dev/null +++ b/src/signal_handler.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +static void handle_sigterm(int) { + signal(SIGTERM, SIG_IGN); + + kill(-getpgrp(), SIGTERM); + + int status; + while (waitpid(-1, &status, 0) > 0 || errno == EINTR) { + } + + _exit(0); +} + +void setup_sigterm() { + setpgid(0, 0); + + struct sigaction sa; + sa.sa_handler = handle_sigterm; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sigaction(SIGTERM, &sa, NULL); +}