BSignal.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * @file BSignal.c
  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. #ifdef BADVPN_USE_WINAPI
  23. #include <windows.h>
  24. #else
  25. #include <signal.h>
  26. #include <system/BUnixSignal.h>
  27. #endif
  28. #include <misc/debug.h>
  29. #include <system/BLog.h>
  30. #include <system/BSignal.h>
  31. #include <generated/blog_channel_BSignal.h>
  32. struct {
  33. int initialized;
  34. int finished;
  35. BReactor *reactor;
  36. BSignal_handler handler;
  37. void *user;
  38. #ifdef BADVPN_USE_WINAPI
  39. CRITICAL_SECTION handler_mutex; // mutex to make sure only one handler is working at a time
  40. HANDLE signal_sem1;
  41. HANDLE signal_sem2;
  42. BHandle bhandle;
  43. #else
  44. BUnixSignal signal;
  45. #endif
  46. } bsignal_global = {
  47. .initialized = 0,
  48. };
  49. #ifdef BADVPN_USE_WINAPI
  50. static void signal_handle_handler (void *user)
  51. {
  52. ASSERT(bsignal_global.initialized)
  53. ASSERT(!bsignal_global.finished)
  54. ASSERT_FORCE(ReleaseSemaphore(bsignal_global.signal_sem2, 1, NULL))
  55. BLog(BLOG_DEBUG, "Dispatching signal");
  56. // call handler
  57. bsignal_global.handler(bsignal_global.user);
  58. return;
  59. }
  60. static BOOL WINAPI ctrl_handler (DWORD type)
  61. {
  62. // don't check bsignal_global.initialized to avoid a race
  63. EnterCriticalSection(&bsignal_global.handler_mutex);
  64. ASSERT_FORCE(ReleaseSemaphore(bsignal_global.signal_sem1, 1, NULL))
  65. ASSERT_FORCE(WaitForSingleObject(bsignal_global.signal_sem2, INFINITE) == WAIT_OBJECT_0)
  66. LeaveCriticalSection(&bsignal_global.handler_mutex);
  67. return TRUE;
  68. }
  69. #else
  70. static void unix_signal_handler (void *user, int signo)
  71. {
  72. ASSERT(signo == SIGTERM || signo == SIGINT)
  73. ASSERT(bsignal_global.initialized)
  74. ASSERT(!bsignal_global.finished)
  75. BLog(BLOG_DEBUG, "Dispatching signal");
  76. // call handler
  77. bsignal_global.handler(bsignal_global.user);
  78. return;
  79. }
  80. #endif
  81. int BSignal_Init (BReactor *reactor, BSignal_handler handler, void *user)
  82. {
  83. ASSERT(!bsignal_global.initialized)
  84. // init arguments
  85. bsignal_global.reactor = reactor;
  86. bsignal_global.handler = handler;
  87. bsignal_global.user = user;
  88. BLog(BLOG_DEBUG, "BSignal initializing");
  89. #ifdef BADVPN_USE_WINAPI
  90. InitializeCriticalSection(&bsignal_global.handler_mutex);
  91. if (!(bsignal_global.signal_sem1 = CreateSemaphore(NULL, 0, 1, NULL))) {
  92. BLog(BLOG_ERROR, "CreateSemaphore failed");
  93. goto fail1;
  94. }
  95. if (!(bsignal_global.signal_sem2 = CreateSemaphore(NULL, 0, 1, NULL))) {
  96. BLog(BLOG_ERROR, "CreateSemaphore failed");
  97. goto fail2;
  98. }
  99. // init BHandle
  100. BHandle_Init(&bsignal_global.bhandle, bsignal_global.signal_sem1, signal_handle_handler, NULL);
  101. if (!BReactor_AddHandle(bsignal_global.reactor, &bsignal_global.bhandle)) {
  102. BLog(BLOG_ERROR, "BReactor_AddHandle failed");
  103. goto fail3;
  104. }
  105. BReactor_EnableHandle(bsignal_global.reactor, &bsignal_global.bhandle);
  106. // configure ctrl handler
  107. if (!SetConsoleCtrlHandler(ctrl_handler, TRUE)) {
  108. BLog(BLOG_ERROR, "SetConsoleCtrlHandler failed");
  109. goto fail4;
  110. }
  111. #else
  112. sigset_t sset;
  113. ASSERT_FORCE(sigemptyset(&sset) == 0)
  114. ASSERT_FORCE(sigaddset(&sset, SIGTERM) == 0)
  115. ASSERT_FORCE(sigaddset(&sset, SIGINT) == 0)
  116. // init BUnixSignal
  117. if (!BUnixSignal_Init(&bsignal_global.signal, bsignal_global.reactor, sset, unix_signal_handler, NULL)) {
  118. BLog(BLOG_ERROR, "BUnixSignal_Init failed");
  119. goto fail0;
  120. }
  121. #endif
  122. bsignal_global.initialized = 1;
  123. bsignal_global.finished = 0;
  124. return 1;
  125. #ifdef BADVPN_USE_WINAPI
  126. fail4:
  127. BReactor_RemoveHandle(bsignal_global.reactor, &bsignal_global.bhandle);
  128. fail3:
  129. ASSERT_FORCE(CloseHandle(bsignal_global.signal_sem2))
  130. fail2:
  131. ASSERT_FORCE(CloseHandle(bsignal_global.signal_sem1))
  132. fail1:
  133. DeleteCriticalSection(&bsignal_global.handler_mutex);
  134. #endif
  135. fail0:
  136. return 0;
  137. }
  138. void BSignal_Finish (void)
  139. {
  140. ASSERT(bsignal_global.initialized)
  141. ASSERT(!bsignal_global.finished)
  142. #ifdef BADVPN_USE_WINAPI
  143. // free BHandle
  144. BReactor_RemoveHandle(bsignal_global.reactor, &bsignal_global.bhandle);
  145. #else
  146. // free BUnixSignal
  147. BUnixSignal_Free(&bsignal_global.signal, 0);
  148. #endif
  149. bsignal_global.finished = 1;
  150. }