From 33202881c4123084ba3eae98e33dc4378bd0fd5c Mon Sep 17 00:00:00 2001 From: Axel Wagner Date: Thu, 11 Nov 2010 02:47:30 +0100 Subject: [PATCH] Handle SIGTERM/-INT/-HUP --- i3bar/src/main.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/i3bar/src/main.c b/i3bar/src/main.c index 22c85cc0..6624f2ff 100644 --- a/i3bar/src/main.c +++ b/i3bar/src/main.c @@ -90,6 +90,26 @@ void print_usage(char *elf_name) { printf("-h\t\tDisplay this help-message and exit\n"); } +/* + * We watch various signals, that are there to make our application stop. + * If we get one of those, we ev_unloop() and invoke the cleanup-routines + * in main() with that + * + */ +void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) { + switch (watcher->signum) { + case SIGTERM: + DLOG("Got a SIGTERM, stopping\n"); + break; + case SIGINT: + DLOG("Got a SIGINT, stopping\n"); + break; + case SIGHUP: + DLOG("Got a SIGHUP, stopping\n"); + } + ev_unloop(main_loop, EVUNLOOP_ALL); +} + int main(int argc, char **argv) { int opt; int option_index = 0; @@ -210,6 +230,19 @@ int main(int argc, char **argv) { * this function initiates the watchers to listen on stdin and react accordingly */ start_child(command); + /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop. + * We only need those watchers on the stack, so putting them on the stack saves us + * some calls to free() */ + ev_signal sig_term, sig_quit, sig_int, sig_hup; + + ev_signal_init(&sig_term, &sig_cb, SIGTERM); + ev_signal_init(&sig_int, &sig_cb, SIGINT); + ev_signal_init(&sig_hup, &sig_cb, SIGHUP); + + ev_signal_start(main_loop, &sig_term); + ev_signal_start(main_loop, &sig_int); + ev_signal_start(main_loop, &sig_hup); + /* From here on everything should run smooth for itself, just start listening for * events. We stop simply stop the event-loop, when we are finished */ ev_loop(main_loop, 0);