Improved handling of sigval

This commit is contained in:
Ashish Kumar Yadav 2020-07-10 01:40:34 +05:30
parent a6a877927d
commit f0ab123d74
3 changed files with 18 additions and 15 deletions

View file

@ -33,9 +33,9 @@ as instructed on the page.
# Signaling changes # Signaling changes
To signal a specific block to update, run `sigdwmblocks signal [sigval]`. To signal a specific block to update, run `sigdwmblocks signal [sigval]`.
`sigval` is optional and must be an integer. When not provided it defaults to 0. `sigval` is optional and must be an integer other than `INT_MIN` defined in
`sigval` is passed as the first argument to the program specified for updating `limits.h` (`-2147483648` on my system). If provided, it is passed as the first
the block. argument to the program specified for updating the block.
# xgetrootname # xgetrootname

View file

@ -1,5 +1,6 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -10,6 +11,7 @@
#define CMDLENGTH 25 #define CMDLENGTH 25
#define STTLENGTH 256 #define STTLENGTH 256
#define LOCKFILE "/tmp/dwmblocks.pid" #define LOCKFILE "/tmp/dwmblocks.pid"
#define NILL INT_MIN
#define EMPTYCMDOUT(block) (block->cmdoutcur[0] == '\n' || block->cmdoutcur[0] == '\0') #define EMPTYCMDOUT(block) (block->cmdoutcur[0] == '\n' || block->cmdoutcur[0] == '\0')
#define NOTATCMDOUTEND(block, i) (i < CMDLENGTH && block->cmdoutcur[i] != '\n' && block->cmdoutcur[i] != '\0') #define NOTATCMDOUTEND(block, i) (i < CMDLENGTH && block->cmdoutcur[i] != '\n' && block->cmdoutcur[i] != '\0')
@ -26,7 +28,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 getcmd(Block *block, int *sigval); static void getcmd(Block *block, int sigval);
static void setroot(); static void setroot();
static void setupsignals(); static void setupsignals();
static void sighandler(int signal, siginfo_t *si, void *ucontext); static void sighandler(int signal, siginfo_t *si, void *ucontext);
@ -66,7 +68,7 @@ buttonhandler(int signal, siginfo_t *si, void *ucontext)
} }
void void
getcmd(Block *block, int *sigval) getcmd(Block *block, int sigval)
{ {
int fd[2]; int fd[2];
@ -86,15 +88,15 @@ getcmd(Block *block, int *sigval)
exit(1); exit(1);
} }
close(fd[1]); close(fd[1]);
if (sigval) { if (sigval == NILL) {
char *arg[] = { block->pathu, NULL };
execv(arg[0], arg);
} else {
char buf[12]; char buf[12];
char *arg[] = { block->pathu, buf, NULL }; char *arg[] = { block->pathu, buf, NULL };
snprintf(buf, sizeof buf, "%d", *sigval); snprintf(buf, sizeof buf, "%d", sigval);
execv(arg[0], arg);
} else {
char *arg[] = { block->pathu, NULL };
execv(arg[0], arg); execv(arg[0], arg);
} }
perror("getcmd - child - execv"); perror("getcmd - child - execv");
@ -155,7 +157,7 @@ sighandler(int signal, siginfo_t *si, void *ucontext)
signal -= SIGRTMIN; signal -= SIGRTMIN;
for (Block *current = blocks; current->pathu; current++) { for (Block *current = blocks; current->pathu; current++) {
if (current->signal == signal) if (current->signal == signal)
getcmd(current, &(si->si_value.sival_int)); getcmd(current, si->si_value.sival_int);
} }
setroot(); setroot();
} }
@ -168,14 +170,14 @@ statusloop()
setupsignals(); setupsignals();
for (Block *current = blocks; current->pathu; current++) for (Block *current = blocks; current->pathu; current++)
if (current->interval >= 0) if (current->interval >= 0)
getcmd(current, NULL); getcmd(current, NILL);
setroot(); setroot();
sleep(SLEEPINTERVAL); sleep(SLEEPINTERVAL);
i = SLEEPINTERVAL; i = SLEEPINTERVAL;
while (statusContinue) { while (statusContinue) {
for (Block *current = blocks; current->pathu; current++) for (Block *current = blocks; current->pathu; current++)
if (current->interval > 0 && i % current->interval == 0) if (current->interval > 0 && i % current->interval == 0)
getcmd(current, NULL); getcmd(current, NILL);
setroot(); setroot();
sleep(SLEEPINTERVAL); sleep(SLEEPINTERVAL);
i += SLEEPINTERVAL; i += SLEEPINTERVAL;

View file

@ -1,5 +1,6 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -59,7 +60,7 @@ main(int argc, char *argv[])
if (sscanf(argv[1], "%d", &signal) == 1 && if (sscanf(argv[1], "%d", &signal) == 1 &&
signal > 0 && (signal += SIGRTMIN) <= SIGRTMAX) { signal > 0 && (signal += SIGRTMIN) <= SIGRTMAX) {
if (argc == 2) { if (argc == 2) {
sv.sival_int = 0; sv.sival_int = INT_MIN;
sendsignal(signal, sv); sendsignal(signal, sv);
return 0; return 0;
} else if (argc == 3 && } else if (argc == 3 &&