signal.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001, 2002, 2006 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/signal.c
  18. * @brief code for installing and uninstalling signal handlers
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #define LOG(kind, ...) GNUNET_log_from (kind, "util-signal", __VA_ARGS__)
  24. struct GNUNET_SIGNAL_Context
  25. {
  26. struct GNUNET_SIGNAL_Context *next;
  27. struct GNUNET_SIGNAL_Context *prev;
  28. int sig;
  29. GNUNET_SIGNAL_Handler method;
  30. struct sigaction oldsig;
  31. };
  32. static struct GNUNET_SIGNAL_Context *sc_head;
  33. static struct GNUNET_SIGNAL_Context *sc_tail;
  34. struct GNUNET_SIGNAL_Context *
  35. GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler)
  36. {
  37. struct GNUNET_SIGNAL_Context *ret;
  38. struct sigaction sig;
  39. ret = GNUNET_new (struct GNUNET_SIGNAL_Context);
  40. ret->sig = signum;
  41. ret->method = handler;
  42. memset (&sig, 0, sizeof(sig));
  43. sig.sa_handler = (void *) handler;
  44. sigemptyset (&sig.sa_mask);
  45. #ifdef SA_INTERRUPT
  46. sig.sa_flags = SA_INTERRUPT; /* SunOS */
  47. #else
  48. sig.sa_flags = SA_RESTART;
  49. #endif
  50. sigaction (signum, &sig, &ret->oldsig);
  51. GNUNET_CONTAINER_DLL_insert_tail (sc_head, sc_tail, ret);
  52. return ret;
  53. }
  54. void
  55. GNUNET_SIGNAL_handler_uninstall (struct GNUNET_SIGNAL_Context *ctx)
  56. {
  57. struct sigaction sig;
  58. sigemptyset (&sig.sa_mask);
  59. sigaction (ctx->sig, &ctx->oldsig, &sig);
  60. GNUNET_CONTAINER_DLL_remove (sc_head, sc_tail, ctx);
  61. GNUNET_free (ctx);
  62. }
  63. /**
  64. * Raise the given signal by calling the installed signal handlers. This will
  65. * not use the @em raise() system call but only calls the handlers registered
  66. * through GNUNET_SIGNAL_handler_install().
  67. *
  68. * @param sig the signal to raise
  69. */
  70. void
  71. GNUNET_SIGNAL_raise (const int sig)
  72. {
  73. struct GNUNET_SIGNAL_Context *ctx;
  74. for (ctx = sc_head; NULL != ctx; ctx = ctx->next)
  75. {
  76. if (sig != ctx->sig)
  77. continue;
  78. if (NULL == ctx->method)
  79. continue;
  80. ctx->method ();
  81. }
  82. }