da_signal.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "da_signal.h"
  17. #include "da_log.h"
  18. #include "da_mem_pool.h"
  19. #include "da_core.h"
  20. /* Principle : we keep an in-order list of the first occurrence of all received
  21. * signals. All occurrences of a same signal are grouped though. The signal
  22. * queue does not need to be deeper than the number of signals we can handle.
  23. * The handlers will be called asynchronously with the signal number. They can
  24. * check themselves the number of calls by checking the descriptor this signal.
  25. */
  26. volatile int signal_queue_len; /* length of signal queue, <= MAX_SIGNAL (1 entry per signal max) */
  27. int signal_queue[MAX_SIGNAL]; /* in-order queue of received signals */
  28. struct signal_descriptor signal_state[MAX_SIGNAL];
  29. struct pool_head *pool2_sig_handlers = NULL;
  30. sigset_t blocked_sig;
  31. int signal_pending = 0; /* non-zero if t least one signal remains unprocessed */
  32. /* Common signal handler, used by all signals. Received signals are queued.
  33. * Signal number zero has a specific status, as it cannot be delivered by the
  34. * system, any function may call it to perform asynchronous signal delivery.
  35. */
  36. void signal_handler(int sig) {
  37. if (sig < 0 || sig >= MAX_SIGNAL) {
  38. /* unhandled signal */
  39. signal(sig, SIG_IGN);
  40. log_error("Received unhandled signal %d. Signal has been disabled.\n",
  41. sig);
  42. return;
  43. }
  44. if (!signal_state[sig].count) {
  45. /* signal was not queued yet */
  46. if (signal_queue_len < MAX_SIGNAL)
  47. signal_queue[signal_queue_len++] = sig;
  48. else
  49. log_error("Signal %d : signal queue is unexpectedly full.\n", sig);
  50. }
  51. signal_state[sig].count++;
  52. if (sig)
  53. signal(sig, signal_handler); /* re-arm signal */
  54. log_debug("size of signal_queue_len is %d",signal_queue_len);
  55. }
  56. /* Call handlers of all pending signals and clear counts and queue length. The
  57. * handlers may unregister themselves by calling signal_register() while they
  58. * are called, just like it is done with normal signal handlers.
  59. * Note that it is more efficient to call the inline version which checks the
  60. * queue length before getting here.
  61. */
  62. void __signal_process_queue() {
  63. int sig, cur_pos = 0;
  64. struct signal_descriptor *desc;
  65. sigset_t old_sig;
  66. /* block signal delivery during processing */
  67. sigprocmask(SIG_SETMASK, &blocked_sig, &old_sig);
  68. /* It is important that we scan the queue forwards so that we can
  69. * catch any signal that would have been queued by another signal
  70. * handler. That allows real signal handlers to redistribute signals
  71. * to tasks subscribed to signal zero.
  72. */
  73. for (cur_pos = 0; cur_pos < signal_queue_len; cur_pos++) {
  74. sig = signal_queue[cur_pos];
  75. desc = &signal_state[sig];
  76. if (desc->count) {
  77. struct sig_handler *sh, *shb;
  78. TAILQ_FOREACH_SAFE(sh,&desc->sig_tqh,sig_tqe,shb)
  79. {
  80. if (sh->handler) {
  81. ((void (*)(struct sig_handler *)) sh->handler)(sh);
  82. }
  83. }
  84. desc->count = 0;
  85. }
  86. }
  87. signal_queue_len = 0;
  88. /* restore signal delivery */
  89. sigprocmask(SIG_SETMASK, &old_sig, NULL);
  90. }
  91. /* perform minimal intializations*/
  92. int signal_init() {
  93. int sig;
  94. signal_queue_len = 0;
  95. memset(signal_queue, 0, sizeof(signal_queue));
  96. memset(signal_state, 0, sizeof(signal_state));
  97. sigfillset(&blocked_sig);
  98. sigdelset(&blocked_sig, SIGPROF);
  99. for (sig = 0; sig < MAX_SIGNAL; sig++)
  100. TAILQ_INIT(&signal_state[sig].sig_tqh);
  101. pool2_sig_handlers = create_pool("sig_handlers", sizeof(struct sig_handler),
  102. MEM_F_SHARED);
  103. if(pool2_sig_handlers == NULL)
  104. {
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. /* releases all registered signal handlers */
  110. void deinit_signals() {
  111. int sig;
  112. struct sig_handler *sh, *shb;
  113. for (sig = 0; sig < MAX_SIGNAL; sig++) {
  114. if (sig != SIGPROF)
  115. signal(sig, SIG_DFL);
  116. TAILQ_FOREACH_SAFE(sh,&signal_state[sig].sig_tqh,sig_tqe,shb)
  117. {
  118. TAILQ_REMOVE(&signal_state[sig].sig_tqh, sh, sig_tqe);
  119. pool_free(pool2_sig_handlers, sh);
  120. }
  121. }
  122. }
  123. /* Register a function and an integer argument on a signal. A pointer to the
  124. * newly allocated sig_handler is returned, or NULL in case of any error. The
  125. * caller is responsible for unregistering the function when not used anymore.
  126. * Note that passing a NULL as the function pointer enables interception of the
  127. * signal without processing, which is identical to SIG_IGN. If the signal is
  128. * zero (which the system cannot deliver), only internal functions will be able
  129. * to notify the registered functions.
  130. */
  131. struct sig_handler *signal_register_fct(int sig,
  132. void (*fct)(struct sig_handler *), int arg) {
  133. log_debug("enter signal_register_fct");
  134. struct sig_handler *sh;
  135. if (sig < 0 || sig >= MAX_SIGNAL)
  136. return NULL;
  137. if (sig)
  138. signal(sig, fct ? signal_handler : SIG_IGN);
  139. if (!fct)
  140. return NULL;
  141. sh = pool_alloc(pool2_sig_handlers);
  142. if (!sh)
  143. return NULL;
  144. sh->handler = fct;
  145. sh->arg = arg;
  146. TAILQ_INSERT_TAIL(&signal_state[sig].sig_tqh, sh, sig_tqe);
  147. return sh;
  148. }
  149. /* Immediately unregister a handler so that no further signals may be delivered
  150. * to it. The struct is released so the caller may not reference it anymore.
  151. */
  152. void signal_unregister_handler(int sig, struct sig_handler *handler) {
  153. if (handler == NULL)
  154. return;
  155. if (sig < 0 || sig >= MAX_SIGNAL)
  156. return;
  157. TAILQ_REMOVE(&signal_state[sig].sig_tqh, handler, sig_tqe);
  158. pool_free(pool2_sig_handlers, handler);
  159. }
  160. /* Immediately unregister a handler so that no further signals may be delivered
  161. * to it. The handler struct does not need to be known, only the function or
  162. * task pointer. This method is expensive because it scans all the list, so it
  163. * should only be used for rare cases (eg: exit). The struct is released so the
  164. * caller may not reference it anymore.
  165. */
  166. void signal_unregister_target(int sig, void *target) {
  167. struct sig_handler *sh, *shb;
  168. if (sig < 0 || sig >= MAX_SIGNAL)
  169. return;
  170. if (!target)
  171. return;
  172. TAILQ_FOREACH_SAFE(sh,&signal_state[sig].sig_tqh,sig_tqe,shb)
  173. {
  174. if (sh->handler == target) {
  175. TAILQ_REMOVE(&signal_state[sig].sig_tqh, sh, sig_tqe);
  176. pool_free(pool2_sig_handlers, sh);
  177. break;
  178. }
  179. }
  180. }