x11lock.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2010 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 "x11lock.h"
  15. #include "log.h"
  16. #include "util.h"
  17. #include "main.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #include <sys/wait.h>
  25. #define X11LOCK_HELPER "pwrtray-xlock"
  26. #define X11LOCK_HELPER_PATH stringify(PREFIX) "/bin/" X11LOCK_HELPER
  27. int block_x11_input(struct x11lock *xl)
  28. {
  29. pid_t pid;
  30. #ifndef FEATURE_XLOCK
  31. # warning "X11 input blocking feature disabled"
  32. logdebug("X11 input blocking feature disabled\n");
  33. return 0;
  34. #endif
  35. if (!xl)
  36. return 0;
  37. if (xl->helper_pid)
  38. return 0;
  39. pid = subprocess_exec(X11LOCK_HELPER_PATH);
  40. if (pid < 0) {
  41. logerr("block_x11_input: Failed to execute '%s'.\n",
  42. X11LOCK_HELPER_PATH);
  43. return -ENOENT;
  44. }
  45. xl->helper_pid = pid;
  46. logdebug("Forked X11 input blocker helper %s (pid=%d)\n",
  47. X11LOCK_HELPER_PATH, (int)pid);
  48. return 0;
  49. }
  50. void unblock_x11_input(struct x11lock *xl)
  51. {
  52. int err;
  53. pid_t pid;
  54. #ifndef FEATURE_XLOCK
  55. return;
  56. #endif
  57. if (!xl)
  58. return;
  59. pid = xl->helper_pid;
  60. if (!pid)
  61. return;
  62. block_signals();
  63. xl->killed = 1;
  64. err = kill(pid, SIGTERM);
  65. if (err) {
  66. logerr("unlock_x11_input: Failed to kill helper process PID %d\n",
  67. (int)pid);
  68. return;
  69. }
  70. unblock_signals();
  71. }
  72. void x11lock_sigchld(struct x11lock *xl, int wait)
  73. {
  74. int status;
  75. pid_t pid, res;
  76. #ifndef FEATURE_XLOCK
  77. return;
  78. #endif
  79. if (!xl)
  80. return;
  81. pid = xl->helper_pid;
  82. if (!pid)
  83. return;
  84. res = waitpid(pid, &status, wait ? 0 : WNOHANG);
  85. if (res < 0) {
  86. if (errno == ECHILD)
  87. return;
  88. logerr("x11lock_sigchld: waitpid failed for PID %d: %s\n",
  89. (int)pid, strerror(errno));
  90. return;
  91. }
  92. if (!xl->killed) {
  93. logerr("x11lock_sigchld: X11 lock helper was "
  94. "murdered by a stranger.\n");
  95. }
  96. xl->helper_pid = 0;
  97. xl->killed = 0;
  98. if (status) {
  99. logerr("x11lock_sigchld: The helper process "
  100. "returned an error code: %d\n", status);
  101. } else {
  102. logdebug("X11 lock helper (pid=%d) terminated\n",
  103. (int)pid);
  104. }
  105. }