BSignal.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @file BSignal.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifdef BADVPN_USE_WINAPI
  30. #include <windows.h>
  31. #else
  32. #include <signal.h>
  33. #include <system/BUnixSignal.h>
  34. #endif
  35. #include <misc/debug.h>
  36. #include <base/BLog.h>
  37. #include <system/BSignal.h>
  38. #include <generated/blog_channel_BSignal.h>
  39. static struct {
  40. int initialized;
  41. int finished;
  42. BReactor *reactor;
  43. BSignal_handler handler;
  44. void *user;
  45. #ifdef BADVPN_USE_WINAPI
  46. BReactorIOCPOverlapped olap;
  47. CRITICAL_SECTION iocp_handle_mutex;
  48. HANDLE iocp_handle;
  49. #else
  50. BUnixSignal signal;
  51. #endif
  52. } bsignal_global = {
  53. 0
  54. };
  55. #ifdef BADVPN_USE_WINAPI
  56. static void olap_handler (void *user, int event, DWORD bytes)
  57. {
  58. ASSERT(bsignal_global.initialized)
  59. ASSERT(!(event == BREACTOR_IOCP_EVENT_EXITING) || bsignal_global.finished)
  60. if (event == BREACTOR_IOCP_EVENT_EXITING) {
  61. BReactorIOCPOverlapped_Free(&bsignal_global.olap);
  62. return;
  63. }
  64. if (!bsignal_global.finished) {
  65. // call handler
  66. bsignal_global.handler(bsignal_global.user);
  67. return;
  68. }
  69. }
  70. static BOOL WINAPI ctrl_handler (DWORD type)
  71. {
  72. EnterCriticalSection(&bsignal_global.iocp_handle_mutex);
  73. if (bsignal_global.iocp_handle) {
  74. PostQueuedCompletionStatus(bsignal_global.iocp_handle, 0, 0, &bsignal_global.olap.olap);
  75. }
  76. LeaveCriticalSection(&bsignal_global.iocp_handle_mutex);
  77. return TRUE;
  78. }
  79. #else
  80. static void unix_signal_handler (void *user, int signo)
  81. {
  82. ASSERT(signo == SIGTERM || signo == SIGINT || signo == SIGHUP)
  83. ASSERT(bsignal_global.initialized)
  84. ASSERT(!bsignal_global.finished)
  85. BLog(BLOG_DEBUG, "Dispatching signal");
  86. // call handler
  87. bsignal_global.handler(bsignal_global.user);
  88. return;
  89. }
  90. #endif
  91. int BSignal_Init (BReactor *reactor, BSignal_handler handler, void *user)
  92. {
  93. ASSERT(!bsignal_global.initialized)
  94. // init arguments
  95. bsignal_global.reactor = reactor;
  96. bsignal_global.handler = handler;
  97. bsignal_global.user = user;
  98. BLog(BLOG_DEBUG, "BSignal initializing");
  99. #ifdef BADVPN_USE_WINAPI
  100. // init olap
  101. BReactorIOCPOverlapped_Init(&bsignal_global.olap, bsignal_global.reactor, NULL, olap_handler);
  102. // init handler mutex
  103. InitializeCriticalSection(&bsignal_global.iocp_handle_mutex);
  104. // remember IOCP handle
  105. bsignal_global.iocp_handle = BReactor_GetIOCPHandle(bsignal_global.reactor);
  106. // configure ctrl handler
  107. if (!SetConsoleCtrlHandler(ctrl_handler, TRUE)) {
  108. BLog(BLOG_ERROR, "SetConsoleCtrlHandler failed");
  109. goto fail1;
  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. ASSERT_FORCE(sigaddset(&sset, SIGHUP) == 0)
  117. // init BUnixSignal
  118. if (!BUnixSignal_Init(&bsignal_global.signal, bsignal_global.reactor, sset, unix_signal_handler, NULL)) {
  119. BLog(BLOG_ERROR, "BUnixSignal_Init failed");
  120. goto fail0;
  121. }
  122. #endif
  123. bsignal_global.initialized = 1;
  124. bsignal_global.finished = 0;
  125. return 1;
  126. #ifdef BADVPN_USE_WINAPI
  127. fail1:
  128. DeleteCriticalSection(&bsignal_global.iocp_handle_mutex);
  129. BReactorIOCPOverlapped_Free(&bsignal_global.olap);
  130. #endif
  131. fail0:
  132. return 0;
  133. }
  134. void BSignal_Finish (void)
  135. {
  136. ASSERT(bsignal_global.initialized)
  137. ASSERT(!bsignal_global.finished)
  138. #ifdef BADVPN_USE_WINAPI
  139. // forget IOCP handle
  140. EnterCriticalSection(&bsignal_global.iocp_handle_mutex);
  141. bsignal_global.iocp_handle = NULL;
  142. LeaveCriticalSection(&bsignal_global.iocp_handle_mutex);
  143. #else
  144. // free BUnixSignal
  145. BUnixSignal_Free(&bsignal_global.signal, 0);
  146. #endif
  147. bsignal_global.finished = 1;
  148. }