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>
|
|
|
|
|
2020-11-15 07:26:13 +00:00
|
|
|
#define NILL INT_MIN
|
2020-08-22 17:43:56 +00:00
|
|
|
#define LOCKFILE "/tmp/dwmblocks.pid"
|
2020-07-03 21:04:11 +00:00
|
|
|
|
2020-12-08 06:34:12 +00:00
|
|
|
int
|
|
|
|
parsesignal(char *c, int *s)
|
|
|
|
{
|
|
|
|
for (*s = 0; *c != '\0'; c++)
|
|
|
|
if (*c >= '0' && *c <= '9') {
|
|
|
|
*s *= 10;
|
|
|
|
*s += *c - '0';
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
if ((*s += SIGRTMIN) > SIGRTMAX)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
parsesigval(char *c, int *v)
|
|
|
|
{
|
|
|
|
int s = 1;
|
|
|
|
|
|
|
|
if (*c == '-') {
|
|
|
|
s = -1;
|
|
|
|
c++;
|
|
|
|
} else if (*c == '+')
|
|
|
|
c++;
|
|
|
|
for (*v = 0; *c != '\0'; c++)
|
|
|
|
if (*c >= '0' && *c <= '9') {
|
|
|
|
*v *= 10;
|
|
|
|
*v += *c - '0';
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
*v *= s;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-03 21:04:11 +00:00
|
|
|
void
|
2020-12-08 06:34:12 +00:00
|
|
|
sendsignal(int sig, union sigval sv)
|
2020-07-03 21:04:11 +00:00
|
|
|
{
|
|
|
|
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-12-04 21:59:07 +00:00
|
|
|
exit(3);
|
2020-07-03 21:04:11 +00:00
|
|
|
}
|
2020-10-12 16:50:09 +00:00
|
|
|
perror("sendsignal - open");
|
2020-07-03 21:04:11 +00:00
|
|
|
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-12-04 21:59:07 +00:00
|
|
|
exit(3);
|
2020-07-03 21:04:11 +00:00
|
|
|
}
|
2020-12-08 06:34:12 +00:00
|
|
|
if (sigqueue(fl.l_pid, sig, sv) == -1) {
|
|
|
|
if (errno == ESRCH) {
|
2020-07-11 21:12:35 +00:00
|
|
|
fputs("Error: no running instance of dwmblocks.\n", stderr);
|
2020-12-04 21:59:07 +00:00
|
|
|
exit(3);
|
2020-07-03 21:04:11 +00:00
|
|
|
} else {
|
|
|
|
perror("sendsignal - sigqueue");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2020-12-08 06:34:12 +00:00
|
|
|
int sig;
|
|
|
|
union sigval sv;
|
2020-07-03 21:04:11 +00:00
|
|
|
|
2020-12-08 06:34:12 +00:00
|
|
|
if (argc > 1 && parsesignal(argv[1], &sig)) {
|
|
|
|
if (argc == 2) {
|
|
|
|
sv.sival_int = NILL;
|
|
|
|
sendsignal(sig, sv);
|
|
|
|
return 0;
|
|
|
|
} else if (argc == 3 && parsesigval(argv[2], &(sv.sival_int))) {
|
|
|
|
sendsignal(sig, sv);
|
|
|
|
return 0;
|
2020-07-03 21:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 20:00:57 +00:00
|
|
|
fprintf(stderr, "Usage: %s <signal> [<sigval>]\n", argv[0]);
|
2020-12-04 21:59:07 +00:00
|
|
|
return 2;
|
2020-07-03 21:04:11 +00:00
|
|
|
}
|