From 2962b4fb7fb06aa683005b5d2bdc7d69cc87e324 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 8 Oct 2019 22:47:32 +0200 Subject: add signal handler --- udev-block-notify.c | 16 ++++++++++++++-- udev-block-notify.h | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/udev-block-notify.c b/udev-block-notify.c index 3a67599..0a87af1 100644 --- a/udev-block-notify.c +++ b/udev-block-notify.c @@ -21,6 +21,7 @@ const static struct option options_long[] = { char *program; uint8_t verbose = 0; +uint8_t doexit = 0; /*** get_notification ***/ NotifyNotification * get_notification(struct notifications *notifications, dev_t devnum) { @@ -68,6 +69,14 @@ char * appendstr(const char *text, char *notifystr, const char *property, const return notifystr; } +/*** received_signal ***/ +void received_signal(int signal) { + if (verbose > 0) + printf("%s: Received signal: %s\n", program, strsignal(signal)); + + doexit++; +} + /*** main ***/ int main (int argc, char ** argv) { const char *action = NULL; @@ -142,11 +151,14 @@ int main (int argc, char ** argv) { notifications->notification = NULL; notifications->next = NULL; + signal(SIGINT, received_signal); + signal(SIGTERM, received_signal); + #ifdef HAVE_SYSTEMD sd_notify(0, "READY=1\nSTATUS=Waiting for udev block events..."); #endif - while (1) { + while (doexit == 0) { FD_ZERO(&readfds); if (mon != NULL) FD_SET(udev_monitor_get_fd(mon), &readfds); @@ -283,7 +295,7 @@ int main (int argc, char ** argv) { notify_notification_set_timeout(notification, notification_timeout); while(notify_notification_show(notification, &error) == FALSE) { - if (errcount > 1) { + if (errcount > 1 || doexit) { fprintf(stderr, "%s: Looks like we can not reconnect to notification daemon... Exiting.\n", program); exit(EXIT_FAILURE); } else { diff --git a/udev-block-notify.h b/udev-block-notify.h index 7e7be99..30e77e5 100644 --- a/udev-block-notify.h +++ b/udev-block-notify.h @@ -45,6 +45,9 @@ char * newstr(const char *text, const char *device, unsigned short int major, char * appendstr(const char *text, char *notifystr, const char *property, const char *value); +/*** received_signal ***/ +void received_signal(int signal); + /*** main ***/ int main (int argc, char ** argv); -- cgit v1.2.3-54-g00ecf