dotfiles

personal configuration files and scripts
git clone https://tongong.net/git/dotfiles.git
Log | Files | Refs | README

sigdwmblocks.c (2345B)


      1 #include <errno.h>
      2 #include <fcntl.h>
      3 #include <limits.h>
      4 #include <signal.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 #include <unistd.h>
      9 
     10 #define LOCKFILE                        "/tmp/dwmblocks.pid"
     11 
     12 void
     13 sendsignal(int signum, union sigval sv)
     14 {
     15         int fd;
     16         struct flock fl;
     17 
     18         fd = open(LOCKFILE, O_RDONLY);
     19         if (fd == -1) {
     20                 if (errno == ENOENT) {
     21                         fputs("Error: no running instance of dwmblocks.\n", stderr);
     22                         exit(2);
     23                 }
     24                 perror("sendsignal - open");
     25                 exit(1);
     26         }
     27         fl.l_type = F_WRLCK;
     28         fl.l_start = 0;
     29         fl.l_whence = SEEK_SET;
     30         fl.l_len = 0;
     31         if (fcntl(fd, F_GETLK, &fl) == -1) {
     32                 perror("sendsignal - fcntl");
     33                 exit(1);
     34         }
     35         if (fl.l_type == F_UNLCK) {
     36                 fputs("Error: no running instance of dwmblocks.\n", stderr);
     37                 exit(2);
     38         }
     39         if (sigqueue(fl.l_pid, signum, sv) == -1) {
     40                 if (errno == EINVAL) {
     41                         fputs("Error: invalid signal provided in argument.\n", stderr);
     42                         exit(3);
     43                 } else if (errno == ESRCH) {
     44                         fputs("Error: no running instance of dwmblocks.\n", stderr);
     45                         exit(2);
     46                 } else {
     47                         perror("sendsignal - sigqueue");
     48                         exit(1);
     49                 }
     50         }
     51 }
     52 
     53 int
     54 main(int argc, char *argv[])
     55 {
     56         if (argc > 1) {
     57                 int signal;
     58                 union sigval sv;
     59 
     60                 if (sscanf(argv[1], "%d", &signal) == 1 &&
     61                     signal > 0 && (signal += SIGRTMIN) <= SIGRTMAX) {
     62                         if (argc == 2) {
     63                                 sv.sival_int = INT_MIN;
     64                                 sendsignal(signal, sv);
     65                                 return 0;
     66                         } else if (argc == 3 &&
     67                                    sscanf(argv[2], "%d", &(sv.sival_int)) == 1) {
     68                                 sendsignal(signal, sv);
     69                                 return 0;
     70                         }
     71                 }
     72         }
     73         fprintf(stderr, "Usage: %s <signal> [<sigval>]\n", argv[0]);
     74         return 3;
     75 }