xevrep.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2011 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "xevrep.h"
  15. #include "log.h"
  16. #include "util.h"
  17. #include "conf.h"
  18. #include "main.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <signal.h>
  24. #include <unistd.h>
  25. #include <sys/wait.h>
  26. #include <sys/resource.h>
  27. #define XEVREP_HELPER "pwrtray-xevrep"
  28. #define XEVREP_HELPER_PATH stringify(PREFIX) "/bin/" XEVREP_HELPER
  29. int xevrep_enable(struct xevrep *xr)
  30. {
  31. pid_t pid;
  32. int err, grace_period, nice_level;
  33. char command[128];
  34. #ifndef FEATURE_XEVREP
  35. # warning "X11 input event reporting feature disabled"
  36. logdebug("X11 input event reporting feature disabled");
  37. return 0;
  38. #endif
  39. if (!xr)
  40. return 0;
  41. if (xr->helper_pid)
  42. return 0;
  43. grace_period = config_get_int(backend.config,
  44. "XEVREP", "grace_period",
  45. 1000);
  46. snprintf(command, sizeof(command), "%s %d %d %d",
  47. XEVREP_HELPER_PATH,
  48. getpid(), SIGUSR1, grace_period);
  49. pid = subprocess_exec(command);
  50. if (pid < 0) {
  51. logerr("xevrep_enable: Failed to execute '%s'.\n",
  52. XEVREP_HELPER_PATH);
  53. return -ENOENT;
  54. }
  55. xr->helper_pid = pid;
  56. logdebug("Forked X11 input event helper %s (pid=%d)\n",
  57. XEVREP_HELPER_PATH, (int)pid);
  58. nice_level = config_get_int(backend.config,
  59. "XEVREP", "nice", 5);
  60. nice_level = clamp(nice_level, -20, 19);
  61. err = setpriority(PRIO_PROCESS, pid, nice_level);
  62. if (err) {
  63. logerr("xevrep_enable: Failed to set %s niceness to %d: %s\n",
  64. XEVREP_HELPER, nice_level, strerror(errno));
  65. }
  66. return 0;
  67. }
  68. void xevrep_disable(struct xevrep *xr)
  69. {
  70. int err;
  71. pid_t pid;
  72. #ifndef FEATURE_XEVREP
  73. return;
  74. #endif
  75. if (!xr)
  76. return;
  77. pid = xr->helper_pid;
  78. if (!pid)
  79. return;
  80. block_signals();
  81. xr->killed = 1;
  82. err = kill(pid, SIGTERM);
  83. if (err) {
  84. logerr("xevrep_disable: Failed to kill helper process PID %d\n",
  85. (int)pid);
  86. }
  87. unblock_signals();
  88. }
  89. void xevrep_sigchld(struct xevrep *xr, int wait)
  90. {
  91. int status;
  92. pid_t pid, res;
  93. #ifndef FEATURE_XEVREP
  94. return;
  95. #endif
  96. if (!xr)
  97. return;
  98. pid = xr->helper_pid;
  99. if (!pid)
  100. return;
  101. res = waitpid(pid, &status, wait ? 0 : WNOHANG);
  102. if (res < 0) {
  103. if (errno == ECHILD)
  104. return;
  105. logerr("xevrep_sigchld: waitpid failed for PID %d: %s\n",
  106. (int)pid, strerror(errno));
  107. return;
  108. }
  109. if (!xr->killed) {
  110. logerr("xevrep_sigchld: X11 input event helper was "
  111. "murdered by a stranger.\n");
  112. }
  113. xr->helper_pid = 0;
  114. xr->killed = 0;
  115. if (status) {
  116. logerr("xevrep_sigchld: The helper process "
  117. "returned an error code: %d\n", status);
  118. } else {
  119. logdebug("X11 input event helper (pid=%d) terminated\n",
  120. (int)pid);
  121. }
  122. }