BUnixSignal.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. struct BUnixSignal_siginfo {
  34. int signo;
  35. pid_t pid;
  36. };
  37. typedef void (*BUnixSignal_handler) (void *user, struct BUnixSignal_siginfo siginfo);
  38. /**
  39. * Object for catching unix signals.
  40. */
  41. typedef struct {
  42. BReactor *reactor;
  43. sigset_t signals;
  44. BUnixSignal_handler handler;
  45. void *user;
  46. int signalfd_fd;
  47. BFileDescriptor signalfd_bfd;
  48. DebugObject d_obj;
  49. } BUnixSignal;
  50. /**
  51. * Initializes the object.
  52. * {@link BLog_Init} must have been done.
  53. *
  54. * This blocks the signal using sigprocmask() and sets up signalfd() for receiving
  55. * signals.
  56. *
  57. * @param o the object
  58. * @param reactor reactor we live in
  59. * @param signals signals to handle. See man 3 sigsetops.
  60. * @param handler handler function to call when a signal is received
  61. * @param user value passed to callback function
  62. * @return 1 on success, 0 on failure
  63. */
  64. int BUnixSignal_Init (BUnixSignal *o, BReactor *reactor, sigset_t signals, BUnixSignal_handler handler, void *user) WARN_UNUSED;
  65. /**
  66. * Frees the object.
  67. *
  68. * @param o the object
  69. * @param unblock whether to unblock the signals using sigprocmask(). Not unblocking it
  70. * can be used while the program is exiting gracefully to prevent the
  71. * signals from being handled handled according to its default disposition
  72. * after this function is called. Must be 0 or 1.
  73. */
  74. void BUnixSignal_Free (BUnixSignal *o, int unblock);
  75. #endif