Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.3 KiB

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