BUnixSignal.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @file BUnixSignal.h
  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. * @section DESCRIPTION
  23. *
  24. * Object for catching unix signals.
  25. */
  26. #ifndef BADVPN_SYSTEM_BUNIXSIGNAL_H
  27. #define BADVPN_SYSTEM_BUNIXSIGNAL_H
  28. #if (defined(BADVPN_USE_SIGNALFD) + defined(BADVPN_USE_KEVENT) + defined(BADVPN_USE_SELFPIPE)) != 1
  29. #error Unknown signal backend or too many signal backends
  30. #endif
  31. #include <unistd.h>
  32. #include <signal.h>
  33. #include <misc/debug.h>
  34. #include <system/BReactor.h>
  35. #include <system/DebugObject.h>
  36. struct BUnixSignal_s;
  37. /**
  38. * Handler function called when a signal is received.
  39. *
  40. * @param user as in {@link BUnixSignal_Init}
  41. * @param signo signal number. Will be one of the signals provided to {@link signals}.
  42. */
  43. typedef void (*BUnixSignal_handler) (void *user, int signo);
  44. #ifdef BADVPN_USE_KEVENT
  45. struct BUnixSignal_kevent_entry {
  46. struct BUnixSignal_s *parent;
  47. int signo;
  48. BReactorKEvent kevent;
  49. };
  50. #endif
  51. #ifdef BADVPN_USE_SELFPIPE
  52. struct BUnixSignal_selfpipe_entry {
  53. struct BUnixSignal_s *parent;
  54. int signo;
  55. int pipefds[2];
  56. BFileDescriptor pipe_read_bfd;
  57. };
  58. #endif
  59. /**
  60. * Object for catching unix signals.
  61. */
  62. typedef struct BUnixSignal_s {
  63. BReactor *reactor;
  64. sigset_t signals;
  65. BUnixSignal_handler handler;
  66. void *user;
  67. #ifdef BADVPN_USE_SIGNALFD
  68. int signalfd_fd;
  69. BFileDescriptor signalfd_bfd;
  70. #endif
  71. #ifdef BADVPN_USE_KEVENT
  72. struct BUnixSignal_kevent_entry *entries;
  73. int num_entries;
  74. #endif
  75. #ifdef BADVPN_USE_SELFPIPE
  76. struct BUnixSignal_selfpipe_entry *entries;
  77. int num_entries;
  78. #endif
  79. DebugObject d_obj;
  80. } BUnixSignal;
  81. /**
  82. * Initializes the object.
  83. * {@link BLog_Init} must have been done.
  84. *
  85. * WARNING: for every signal number there should be at most one {@link BUnixSignal}
  86. * object handling it (or anything else that could interfere).
  87. *
  88. * This blocks the signal using sigprocmask() and sets up signalfd() for receiving
  89. * signals.
  90. *
  91. * @param o the object
  92. * @param reactor reactor we live in
  93. * @param signals signals to handle. See man 3 sigsetops.
  94. * @param handler handler function to call when a signal is received
  95. * @param user value passed to callback function
  96. * @return 1 on success, 0 on failure
  97. */
  98. int BUnixSignal_Init (BUnixSignal *o, BReactor *reactor, sigset_t signals, BUnixSignal_handler handler, void *user) WARN_UNUSED;
  99. /**
  100. * Frees the object.
  101. *
  102. * @param o the object
  103. * @param unblock whether to unblock the signals using sigprocmask(). Not unblocking it
  104. * can be used while the program is exiting gracefully to prevent the
  105. * signals from being handled handled according to its default disposition
  106. * after this function is called. Must be 0 or 1.
  107. */
  108. void BUnixSignal_Free (BUnixSignal *o, int unblock);
  109. #endif