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 <unistd.h>
|
|
|
|
|
2020-11-15 07:26:13 +00:00
|
|
|
#define NILL INT_MIN
|
2021-06-21 12:26:36 +00:00
|
|
|
#define LOCKFILE "/var/local/dwmblocks/dwmblocks.pid"
|
2020-07-03 21:04:11 +00:00
|
|
|
|
2020-12-08 06:34:12 +00:00
|
|
|
int
|
2021-04-04 07:34:23 +00:00
|
|
|
parsesignal(char *arg)
|
2020-12-08 06:34:12 +00:00
|
|
|
{
|
2020-12-24 19:23:39 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (; *arg != '\0'; arg++)
|
|
|
|
if (*arg >= '0' && *arg <= '9')
|
|
|
|
i = 10 * i + *arg - '0';
|
2021-04-04 07:34:23 +00:00
|
|
|
else {
|
|
|
|
fputs("Usage: sigdwmblocks <signal> [<sigval>]\n", stderr);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ((i += SIGRTMIN) > SIGRTMAX) {
|
|
|
|
fputs("Error: <signal> out of range.\n", stderr);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
return i;
|
2020-12-08 06:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-04-04 07:34:23 +00:00
|
|
|
parsesigval(char *arg)
|
2020-12-08 06:34:12 +00:00
|
|
|
{
|
2020-12-24 19:23:39 +00:00
|
|
|
int s = 1, i = 0;
|
2020-12-08 06:34:12 +00:00
|
|
|
|
2020-12-24 19:23:39 +00:00
|
|
|
if (*arg == '-') {
|
2020-12-08 06:34:12 +00:00
|
|
|
s = -1;
|
2020-12-24 19:23:39 +00:00
|
|
|
arg++;
|
|
|
|
} else if (*arg == '+')
|
|
|
|
arg++;
|
|
|
|
for (; *arg != '\0'; arg++)
|
|
|
|
if (*arg >= '0' && *arg <= '9')
|
|
|
|
i = 10 * i + *arg - '0';
|
2021-04-04 07:34:23 +00:00
|
|
|
else {
|
|
|
|
fputs("Usage: sigdwmblocks <signal> [<sigval>]\n", stderr);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
return s * i;
|
2020-12-08 06:34:12 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2021-01-10 07:55:58 +00:00
|
|
|
if ((fd = open(LOCKFILE, O_RDONLY)) == -1) {
|
2020-07-03 21:04:11 +00:00
|
|
|
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_whence = SEEK_SET;
|
2021-01-09 23:56:10 +00:00
|
|
|
fl.l_start = 0;
|
2020-07-03 21:04:11 +00:00
|
|
|
fl.l_len = 0;
|
|
|
|
if (fcntl(fd, F_GETLK, &fl) == -1) {
|
|
|
|
perror("sendsignal - fcntl");
|
2021-01-09 23:56:10 +00:00
|
|
|
close(fd);
|
2020-07-03 21:04:11 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2021-01-09 23:56:10 +00:00
|
|
|
close(fd);
|
2021-08-03 11:28:21 +00:00
|
|
|
if (fl.l_type != F_WRLCK) {
|
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
|
|
|
|
2021-04-04 07:34:23 +00:00
|
|
|
if (argc < 2 || argc > 3) {
|
|
|
|
fputs("Usage: sigdwmblocks <signal> [<sigval>]\n", stderr);
|
|
|
|
return 2;
|
2020-07-03 21:04:11 +00:00
|
|
|
}
|
2021-04-04 07:34:23 +00:00
|
|
|
sig = parsesignal(argv[1]);
|
|
|
|
sv.sival_int = argc == 2 ? NILL : parsesigval(argv[2]);
|
|
|
|
sendsignal(sig, sv);
|
|
|
|
return 0;
|
2020-07-03 21:04:11 +00:00
|
|
|
}
|