BUnixSignal.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file BUnixSignal.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <inttypes.h>
  23. #include <stdlib.h>
  24. #include <limits.h>
  25. #include <errno.h>
  26. #include <sys/signalfd.h>
  27. #include <fcntl.h>
  28. #include <system/BLog.h>
  29. #include <system/BUnixSignal.h>
  30. #include <generated/blog_channel_BUnixSignal.h>
  31. static void signalfd_handler (BUnixSignal *o, int events)
  32. {
  33. DebugObject_Access(&o->d_obj);
  34. // read a signal
  35. struct signalfd_siginfo siginfo;
  36. int bytes = read(o->signalfd_fd, &siginfo, sizeof(siginfo));
  37. if (bytes < 0) {
  38. int error = errno;
  39. if (error == EAGAIN || error == EWOULDBLOCK) {
  40. return;
  41. }
  42. BLog(BLOG_ERROR, "read failed (%d)", error);
  43. return;
  44. }
  45. ASSERT_FORCE(bytes == sizeof(siginfo))
  46. // check signal
  47. if (siginfo.ssi_signo > INT_MAX) {
  48. BLog(BLOG_ERROR, "read returned out of int range signo (%"PRIu32")", siginfo.ssi_signo);
  49. return;
  50. }
  51. int signo = siginfo.ssi_signo;
  52. if (sigismember(&o->signals, signo) <= 0) {
  53. BLog(BLOG_ERROR, "read returned wrong signo (%d)", signo);
  54. return;
  55. }
  56. BLog(BLOG_DEBUG, "dispatching signal %d", signo);
  57. // call handler
  58. o->handler(o->user, signo);
  59. return;
  60. }
  61. int BUnixSignal_Init (BUnixSignal *o, BReactor *reactor, sigset_t signals, BUnixSignal_handler handler, void *user)
  62. {
  63. // init arguments
  64. o->reactor = reactor;
  65. o->signals = signals;
  66. o->handler = handler;
  67. o->user = user;
  68. // init signalfd fd
  69. if ((o->signalfd_fd = signalfd(-1, &o->signals, 0)) < 0) {
  70. BLog(BLOG_ERROR, "signalfd failed");
  71. goto fail0;
  72. }
  73. // set non-blocking
  74. if (fcntl(o->signalfd_fd, F_SETFL, O_NONBLOCK) < 0) {
  75. DEBUG("cannot set non-blocking");
  76. goto fail1;
  77. }
  78. // init signalfd BFileDescriptor
  79. BFileDescriptor_Init(&o->signalfd_bfd, o->signalfd_fd, (BFileDescriptor_handler)signalfd_handler, o);
  80. if (!BReactor_AddFileDescriptor(o->reactor, &o->signalfd_bfd)) {
  81. BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  82. goto fail1;
  83. }
  84. BReactor_SetFileDescriptorEvents(o->reactor, &o->signalfd_bfd, BREACTOR_READ);
  85. // block signals
  86. if (sigprocmask(SIG_BLOCK, &o->signals, 0) < 0) {
  87. BLog(BLOG_ERROR, "sigprocmask block failed");
  88. goto fail2;
  89. }
  90. DebugObject_Init(&o->d_obj);
  91. return 1;
  92. fail2:
  93. BReactor_RemoveFileDescriptor(o->reactor, &o->signalfd_bfd);
  94. fail1:
  95. ASSERT_FORCE(close(o->signalfd_fd) == 0)
  96. fail0:
  97. return 0;
  98. }
  99. void BUnixSignal_Free (BUnixSignal *o, int unblock)
  100. {
  101. ASSERT(unblock == 0 || unblock == 1)
  102. DebugObject_Free(&o->d_obj);
  103. if (unblock) {
  104. // unblock signals
  105. ASSERT_FORCE(sigprocmask(SIG_UNBLOCK, &o->signals, 0) == 0)
  106. }
  107. // free signalfd BFileDescriptor
  108. BReactor_RemoveFileDescriptor(o->reactor, &o->signalfd_bfd);
  109. // free signalfd fd
  110. ASSERT_FORCE(close(o->signalfd_fd) == 0)
  111. }