BUnixSignal.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file BUnixSignal.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Object for catching unix signals.
  32. */
  33. #ifndef BADVPN_SYSTEM_BUNIXSIGNAL_H
  34. #define BADVPN_SYSTEM_BUNIXSIGNAL_H
  35. #if (defined(BADVPN_USE_SIGNALFD) + defined(BADVPN_USE_KEVENT) + defined(BADVPN_USE_SELFPIPE)) != 1
  36. #error Unknown signal backend or too many signal backends
  37. #endif
  38. #include <unistd.h>
  39. #include <signal.h>
  40. #include <misc/debug.h>
  41. #include <system/BReactor.h>
  42. #include <base/DebugObject.h>
  43. struct BUnixSignal_s;
  44. /**
  45. * Handler function called when a signal is received.
  46. *
  47. * @param user as in {@link BUnixSignal_Init}
  48. * @param signo signal number. Will be one of the signals provided to {@link signals}.
  49. */
  50. typedef void (*BUnixSignal_handler) (void *user, int signo);
  51. #ifdef BADVPN_USE_KEVENT
  52. struct BUnixSignal_kevent_entry {
  53. struct BUnixSignal_s *parent;
  54. int signo;
  55. BReactorKEvent kevent;
  56. };
  57. #endif
  58. #ifdef BADVPN_USE_SELFPIPE
  59. struct BUnixSignal_selfpipe_entry {
  60. struct BUnixSignal_s *parent;
  61. int signo;
  62. int pipefds[2];
  63. BFileDescriptor pipe_read_bfd;
  64. };
  65. #endif
  66. /**
  67. * Object for catching unix signals.
  68. */
  69. typedef struct BUnixSignal_s {
  70. BReactor *reactor;
  71. sigset_t signals;
  72. BUnixSignal_handler handler;
  73. void *user;
  74. #ifdef BADVPN_USE_SIGNALFD
  75. int signalfd_fd;
  76. BFileDescriptor signalfd_bfd;
  77. #endif
  78. #ifdef BADVPN_USE_KEVENT
  79. struct BUnixSignal_kevent_entry *entries;
  80. int num_entries;
  81. #endif
  82. #ifdef BADVPN_USE_SELFPIPE
  83. struct BUnixSignal_selfpipe_entry *entries;
  84. int num_entries;
  85. #endif
  86. DebugObject d_obj;
  87. } BUnixSignal;
  88. /**
  89. * Initializes the object.
  90. * {@link BLog_Init} must have been done.
  91. *
  92. * WARNING: for every signal number there should be at most one {@link BUnixSignal}
  93. * object handling it (or anything else that could interfere).
  94. *
  95. * This blocks the signal using pthread_sigmask() and sets up signalfd() for receiving
  96. * signals.
  97. *
  98. * @param o the object
  99. * @param reactor reactor we live in
  100. * @param signals signals to handle. See man 3 sigsetops.
  101. * @param handler handler function to call when a signal is received
  102. * @param user value passed to callback function
  103. * @return 1 on success, 0 on failure
  104. */
  105. int BUnixSignal_Init (BUnixSignal *o, BReactor *reactor, sigset_t signals, BUnixSignal_handler handler, void *user) WARN_UNUSED;
  106. /**
  107. * Frees the object.
  108. *
  109. * @param o the object
  110. * @param unblock whether to unblock the signals using pthread_sigmask(). Not unblocking it
  111. * can be used while the program is exiting gracefully to prevent the
  112. * signals from being handled handled according to its default disposition
  113. * after this function is called. Must be 0 or 1.
  114. */
  115. void BUnixSignal_Free (BUnixSignal *o, int unblock);
  116. #endif