Term handling improvements and refactoring

This commit is contained in:
Ashish Kumar Yadav 2020-11-25 01:41:43 +05:30
parent 01f1481f54
commit a52626ae33

View file

@ -25,6 +25,7 @@ typedef struct {
#include "blocks.h" #include "blocks.h"
static void buttonhandler(int signal, siginfo_t *si, void *ucontext); static void buttonhandler(int signal, siginfo_t *si, void *ucontext);
static void cleanup();
static void getcmd(Block *block, int sigval); static void getcmd(Block *block, int sigval);
static void setroot(); static void setroot();
static void setupsignals(); static void setupsignals();
@ -34,7 +35,6 @@ static void termhandler(int signum);
static int updatestatus(); static int updatestatus();
static void writepid(); static void writepid();
static int statuscontinue = 1;
static char statustext[STTLENGTH]; static char statustext[STTLENGTH];
static size_t delimlength; static size_t delimlength;
static Display *dpy; static Display *dpy;
@ -64,6 +64,14 @@ buttonhandler(int signal, siginfo_t *si, void *ucontext)
} }
} }
void
cleanup()
{
unlink(LOCKFILE);
XStoreName(dpy, DefaultRootWindow(dpy), "");
XCloseDisplay(dpy);
}
void void
getcmd(Block *block, int sigval) getcmd(Block *block, int sigval)
{ {
@ -124,6 +132,16 @@ setupsignals()
{ {
struct sigaction sa; struct sigaction sa;
/* populate blocksigmask */
sigemptyset(&blocksigmask);
sigaddset(&blocksigmask, SIGHUP);
sigaddset(&blocksigmask, SIGINT);
sigaddset(&blocksigmask, SIGTERM);
for (Block *block = blocks; block->pathu; block++)
if (block->signal > 0)
sigaddset(&blocksigmask, SIGRTMIN + block->signal);
/* setup signal handlers */
/* to handle HUP, INT and TERM */ /* to handle HUP, INT and TERM */
sa.sa_flags = SA_RESTART; sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
@ -185,7 +203,7 @@ statusloop()
sleep(SLEEPINTERVAL); sleep(SLEEPINTERVAL);
i = SLEEPINTERVAL; i = SLEEPINTERVAL;
/* main loop */ /* main loop */
while (statuscontinue) { for (;; i += SLEEPINTERVAL) {
sigprocmask(SIG_BLOCK, &blocksigmask, NULL); sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->pathu; block++) for (Block *block = blocks; block->pathu; block++)
if (block->interval > 0 && i % block->interval == 0) if (block->interval > 0 && i % block->interval == 0)
@ -193,14 +211,14 @@ statusloop()
setroot(); setroot();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL); sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
sleep(SLEEPINTERVAL); sleep(SLEEPINTERVAL);
i += SLEEPINTERVAL;
} }
} }
void void
termhandler(int signum) termhandler(int signum)
{ {
statuscontinue = 0; cleanup();
exit(0);
} }
/* returns whether block outputs have changed and updates statustext if they have */ /* returns whether block outputs have changed and updates statustext if they have */
@ -338,17 +356,8 @@ main(int argc, char *argv[])
fputs("Error: could not open display.\n", stderr); fputs("Error: could not open display.\n", stderr);
return 1; return 1;
} }
sigemptyset(&blocksigmask);
sigaddset(&blocksigmask, SIGHUP);
sigaddset(&blocksigmask, SIGINT);
sigaddset(&blocksigmask, SIGTERM);
for (Block *block = blocks; block->pathu; block++)
if (block->signal > 0)
sigaddset(&blocksigmask, SIGRTMIN + block->signal);
setupsignals(); setupsignals();
statusloop(); statusloop();
unlink(LOCKFILE); cleanup();
XStoreName(dpy, DefaultRootWindow(dpy), "");
XCloseDisplay(dpy);
return 0; return 0;
} }