BUnixSignal.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <unistd.h>
  29. #include <signal.h>
  30. #include <misc/debug.h>
  31. #include <system/BReactor.h>
  32. #include <system/DebugObject.h>
  33. /**
  34. * Handler function called when a signal is received.
  35. *
  36. * @param user as in {@link BUnixSignal_Init}
  37. * @param signo signal number. Will be one of the signals provided to {@link signals}.
  38. */
  39. typedef void (*BUnixSignal_handler) (void *user, int signo);
  40. /**
  41. * Object for catching unix signals.
  42. */
  43. typedef struct {
  44. BReactor *reactor;
  45. sigset_t signals;
  46. BUnixSignal_handler handler;
  47. void *user;
  48. int signalfd_fd;
  49. BFileDescriptor signalfd_bfd;
  50. DebugObject d_obj;
  51. } BUnixSignal;
  52. /**
  53. * Initializes the object.
  54. * {@link BLog_Init} must have been done.
  55. *
  56. * WARNING: for every signal number there should be at most one {@link BUnixSignal}
  57. * object handling it (or anything else that could interfere).
  58. *
  59. * This blocks the signal using sigprocmask() and sets up signalfd() for receiving
  60. * signals.
  61. *
  62. * @param o the object
  63. * @param reactor reactor we live in
  64. * @param signals signals to handle. See man 3 sigsetops.
  65. * @param handler handler function to call when a signal is received
  66. * @param user value passed to callback function
  67. * @return 1 on success, 0 on failure
  68. */
  69. int BUnixSignal_Init (BUnixSignal *o, BReactor *reactor, sigset_t signals, BUnixSignal_handler handler, void *user) WARN_UNUSED;
  70. /**
  71. * Frees the object.
  72. *
  73. * @param o the object
  74. * @param unblock whether to unblock the signals using sigprocmask(). Not unblocking it
  75. * can be used while the program is exiting gracefully to prevent the
  76. * signals from being handled handled according to its default disposition
  77. * after this function is called. Must be 0 or 1.
  78. */
  79. void BUnixSignal_Free (BUnixSignal *o, int unblock);
  80. #endif