aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2019-10-08 22:47:32 +0200
committerGravatar Christian Hesse <mail@eworm.de>2019-10-08 22:47:32 +0200
commit2962b4fb7fb06aa683005b5d2bdc7d69cc87e324 (patch)
tree456ab4587e83ac82bf18bb15ea4974981cbb2f25
parent352b268050e99795dd465f2c41f64eb7a164d451 (diff)
downloadudev-block-notify-2962b4fb7fb06aa683005b5d2bdc7d69cc87e324.tar.gz
udev-block-notify-2962b4fb7fb06aa683005b5d2bdc7d69cc87e324.tar.zst
add signal handler
-rw-r--r--udev-block-notify.c16
-rw-r--r--udev-block-notify.h3
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);