BUnixSignal.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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)) != 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. /**
  52. * Object for catching unix signals.
  53. */
  54. typedef struct BUnixSignal_s {
  55. BReactor *reactor;
  56. sigset_t signals;
  57. BUnixSignal_handler handler;
  58. void *user;
  59. #ifdef BADVPN_USE_SIGNALFD
  60. int signalfd_fd;
  61. BFileDescriptor signalfd_bfd;
  62. #endif
  63. #ifdef BADVPN_USE_KEVENT
  64. struct BUnixSignal_kevent_entry *entries;
  65. int num_entries;
  66. #endif
  67. DebugObject d_obj;
  68. } BUnixSignal;
  69. /**
  70. * Initializes the object.
  71. * {@link BLog_Init} must have been done.
  72. *
  73. * WARNING: for every signal number there should be at most one {@link BUnixSignal}
  74. * object handling it (or anything else that could interfere).
  75. *
  76. * This blocks the signal using sigprocmask() and sets up signalfd() for receiving
  77. * signals.
  78. *
  79. * @param o the object
  80. * @param reactor reactor we live in
  81. * @param signals signals to handle. See man 3 sigsetops.
  82. * @param handler handler function to call when a signal is received
  83. * @param user value passed to callback function
  84. * @return 1 on success, 0 on failure
  85. */
  86. int BUnixSignal_Init (BUnixSignal *o, BReactor *reactor, sigset_t signals, BUnixSignal_handler handler, void *user) WARN_UNUSED;
  87. /**
  88. * Frees the object.
  89. *
  90. * @param o the object
  91. * @param unblock whether to unblock the signals using sigprocmask(). Not unblocking it
  92. * can be used while the program is exiting gracefully to prevent the
  93. * signals from being handled handled according to its default disposition
  94. * after this function is called. Must be 0 or 1.
  95. */
  96. void BUnixSignal_Free (BUnixSignal *o, int unblock);
  97. #endif