From 67d3f9c5405b633df7bae93c330bbe313e0c9e94 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 19:09:01 +0200 Subject: signal handling to exit gracefully + small coding style fixes --- slstatus.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/slstatus.c b/slstatus.c index 6e86092..cb5f1f2 100644 --- a/slstatus.c +++ b/slstatus.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -62,9 +63,11 @@ static char *username(void); static char *vol_perc(const char *); static char *wifi_perc(const char *); static char *wifi_essid(const char *); +static void sighandler(const int); static unsigned short int delay; static Display *dpy; +static int done = 0; #include "config.h" @@ -470,7 +473,7 @@ uid(void) } -static char * +static char * vol_perc(const char *snd_card) { /* FIX THIS SHIT! */ long int vol, max, min; @@ -516,7 +519,7 @@ wifi_perc(const char *wificard) fp = fopen(concat, "r"); - if(fp == NULL) { + if (fp == NULL) { warn("Error opening wifi operstate file"); return smprintf(UNKNOWN_STR); } @@ -558,7 +561,7 @@ wifi_essid(const char *wificard) memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); - if(sockfd == -1) { + if (sockfd == -1) { warn("Cannot open socket for interface: %s", wificard); return smprintf(UNKNOWN_STR); } @@ -576,6 +579,14 @@ wifi_essid(const char *wificard) return smprintf("%s", (char *)wreq.u.essid.pointer); } +static void +sighandler(int signo) +{ + if (signo == SIGTERM || signo == SIGINT) { + done = 1; + } +} + int main(void) { @@ -583,10 +594,16 @@ main(void) char status_string[4096]; char *res, *element; struct arg argument; + struct sigaction act; + + memset(&act, 0, sizeof(act)); + act.sa_handler = sighandler; + sigaction(SIGINT, &act, 0); + sigaction(SIGTERM, &act, 0); dpy = XOpenDisplay(NULL); - for (;;) { + while (!done) { status_string[0] = '\0'; for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { argument = args[i]; -- cgit v1.2.3