dwmblocks/sigdwmblocks.c

77 lines
2.3 KiB
C
Raw Normal View History

2020-07-06 21:23:31 +00:00
#include <errno.h>
#include <fcntl.h>
2020-07-09 20:10:34 +00:00
#include <limits.h>
2020-07-06 21:23:31 +00:00
#include <signal.h>
2020-07-03 21:04:11 +00:00
#include <stdio.h>
2020-07-06 21:23:31 +00:00
#include <stdlib.h>
2020-07-03 21:04:11 +00:00
#include <string.h>
#include <unistd.h>
#define LOCKFILE "/tmp/dwmblocks.pid"
void
sendsignal(int signum, union sigval sv)
{
int fd;
struct flock fl;
fd = open(LOCKFILE, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
2020-07-11 21:12:35 +00:00
fputs("Error: no running instance of dwmblocks.\n", stderr);
2020-07-03 21:04:11 +00:00
exit(2);
}
perror("sendsignal - fd");
exit(1);
}
fl.l_type = F_WRLCK;
fl.l_start = 0;
fl.l_whence = SEEK_SET;
fl.l_len = 0;
if (fcntl(fd, F_GETLK, &fl) == -1) {
perror("sendsignal - fcntl");
exit(1);
}
if (fl.l_type == F_UNLCK) {
2020-07-11 21:12:35 +00:00
fputs("Error: no running instance of dwmblocks.\n", stderr);
2020-07-03 21:04:11 +00:00
exit(2);
}
if (sigqueue(fl.l_pid, signum, sv) == -1) {
if (errno == EINVAL) {
2020-07-11 21:12:35 +00:00
fputs("Error: invalid signal provided in argument.\n", stderr);
2020-07-03 21:04:11 +00:00
exit(3);
} else if (errno == ESRCH) {
2020-07-11 21:12:35 +00:00
fputs("Error: no running instance of dwmblocks.\n", stderr);
2020-07-03 21:04:11 +00:00
exit(2);
} else {
perror("sendsignal - sigqueue");
exit(1);
}
}
}
int
main(int argc, char *argv[])
{
if (argc > 1) {
int signal;
union sigval sv;
if (sscanf(argv[1], "%d", &signal) == 1 &&
signal > 0 && (signal += SIGRTMIN) <= SIGRTMAX) {
if (argc == 2) {
2020-07-09 20:10:34 +00:00
sv.sival_int = INT_MIN;
2020-07-03 21:04:11 +00:00
sendsignal(signal, sv);
return 0;
} else if (argc == 3 &&
sscanf(argv[2], "%d", &(sv.sival_int)) == 1) {
sendsignal(signal, sv);
return 0;
}
}
}
2020-07-13 20:00:57 +00:00
fprintf(stderr, "Usage: %s <signal> [<sigval>]\n", argv[0]);
2020-07-03 21:04:11 +00:00
return 3;
}