signal.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #define _GNU_SOURCE
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <signal.h>
  30. #include <sys/epoll.h>
  31. #include <sys/signalfd.h>
  32. #include "util.h"
  33. #include "io.h"
  34. #include "queue.h"
  35. #include "signal.h"
  36. #include "private.h"
  37. /**
  38. * SECTION:signal
  39. * @short_description: Unix signal support
  40. *
  41. * Unix signal support
  42. */
  43. /**
  44. * l_signal:
  45. *
  46. * Opague object representing the signal.
  47. */
  48. struct l_signal {
  49. struct signal_desc *desc;
  50. l_signal_notify_cb_t callback;
  51. void *user_data;
  52. l_signal_destroy_cb_t destroy;
  53. };
  54. struct signal_desc {
  55. uint32_t signo;
  56. struct l_queue *callbacks;
  57. };
  58. static struct l_io *signalfd_io = NULL;
  59. static struct l_queue *signal_list = NULL;
  60. static sigset_t signal_mask;
  61. static void handle_callback(struct signal_desc *desc)
  62. {
  63. const struct l_queue_entry *entry;
  64. for (entry = l_queue_get_entries(desc->callbacks); entry;
  65. entry = entry->next) {
  66. struct l_signal *signal = entry->data;
  67. if (signal->callback)
  68. signal->callback(signal->user_data);
  69. }
  70. }
  71. static bool desc_match_signo(const void *a, const void *b)
  72. {
  73. const struct signal_desc *desc = a;
  74. uint32_t signo = L_PTR_TO_UINT(b);
  75. return (desc->signo == signo);
  76. }
  77. static bool signalfd_read_cb(struct l_io *io, void *user_data)
  78. {
  79. int fd = l_io_get_fd(io);
  80. struct signal_desc *desc;
  81. struct signalfd_siginfo si;
  82. ssize_t result;
  83. result = read(fd, &si, sizeof(si));
  84. if (result != sizeof(si))
  85. return true;
  86. desc = l_queue_find(signal_list, desc_match_signo,
  87. L_UINT_TO_PTR(si.ssi_signo));
  88. if (desc)
  89. handle_callback(desc);
  90. return true;
  91. }
  92. static bool signalfd_add(int signo)
  93. {
  94. int fd;
  95. if (!signalfd_io) {
  96. fd = -1;
  97. sigemptyset(&signal_mask);
  98. } else
  99. fd = l_io_get_fd(signalfd_io);
  100. sigaddset(&signal_mask, signo);
  101. fd = signalfd(fd, &signal_mask, SFD_CLOEXEC);
  102. if (fd < 0)
  103. return false;
  104. if (signalfd_io)
  105. return true;
  106. signalfd_io = l_io_new(fd);
  107. if (!signalfd_io) {
  108. close(fd);
  109. return false;
  110. }
  111. l_io_set_close_on_destroy(signalfd_io, true);
  112. if (!l_io_set_read_handler(signalfd_io, signalfd_read_cb, NULL, NULL)) {
  113. l_io_destroy(signalfd_io);
  114. return false;
  115. }
  116. signal_list = l_queue_new();
  117. return true;
  118. }
  119. static void signalfd_remove(int signo)
  120. {
  121. if (!signalfd_io)
  122. return;
  123. sigdelset(&signal_mask, signo);
  124. if (!sigisemptyset(&signal_mask)) {
  125. signalfd(l_io_get_fd(signalfd_io), &signal_mask, SFD_CLOEXEC);
  126. return;
  127. }
  128. l_io_destroy(signalfd_io);
  129. signalfd_io = NULL;
  130. l_queue_destroy(signal_list, NULL);
  131. signal_list = NULL;
  132. }
  133. /**
  134. * l_signal_create:
  135. * @callback: signal callback function
  136. * @user_data: user data provided to signal callback function
  137. * @destroy: destroy function for user data
  138. *
  139. * Create new signal callback handling for a given set of signals.
  140. *
  141. * Returns: a newly allocated #l_signal object
  142. **/
  143. LIB_EXPORT struct l_signal *l_signal_create(uint32_t signo,
  144. l_signal_notify_cb_t callback,
  145. void *user_data, l_signal_destroy_cb_t destroy)
  146. {
  147. struct l_signal *signal;
  148. struct signal_desc *desc;
  149. sigset_t mask, oldmask;
  150. if (signo <= 1 || signo >= _NSIG)
  151. return NULL;
  152. signal = l_new(struct l_signal, 1);
  153. signal->callback = callback;
  154. signal->destroy = destroy;
  155. signal->user_data = user_data;
  156. desc = l_queue_find(signal_list, desc_match_signo,
  157. L_UINT_TO_PTR(signo));
  158. if (desc)
  159. goto done;
  160. sigemptyset(&mask);
  161. sigaddset(&mask, signo);
  162. if (sigprocmask(SIG_BLOCK, &mask, &oldmask) < 0) {
  163. l_free(signal);
  164. return NULL;
  165. }
  166. if (!signalfd_add(signo)) {
  167. sigprocmask(SIG_SETMASK, &oldmask, NULL);
  168. l_free(signal);
  169. return NULL;
  170. }
  171. desc = l_new(struct signal_desc, 1);
  172. desc->signo = signo;
  173. desc->callbacks = l_queue_new();
  174. l_queue_push_tail(signal_list, desc);
  175. done:
  176. l_queue_push_tail(desc->callbacks, signal);
  177. signal->desc = desc;
  178. return signal;
  179. }
  180. /**
  181. * l_signal_remove:
  182. * @signal: signal object
  183. *
  184. * Remove signal handling.
  185. **/
  186. LIB_EXPORT void l_signal_remove(struct l_signal *signal)
  187. {
  188. struct signal_desc *desc;
  189. sigset_t mask;
  190. if (!signal)
  191. return;
  192. desc = signal->desc;
  193. l_queue_remove(desc->callbacks, signal);
  194. /*
  195. * As long as the signal descriptor has callbacks registered, it is
  196. * still needed to be active.
  197. */
  198. if (!l_queue_isempty(desc->callbacks))
  199. goto done;
  200. if (!l_queue_remove(signal_list, desc))
  201. goto done;
  202. sigemptyset(&mask);
  203. sigaddset(&mask, desc->signo);
  204. /*
  205. * When the number of signals goes to zero, then this will close
  206. * the signalfd file descriptor, otherwise it will only adjust the
  207. * signal mask to account for the removed signal.
  208. *
  209. */
  210. signalfd_remove(desc->signo);
  211. sigprocmask(SIG_UNBLOCK, &mask, NULL);
  212. l_queue_destroy(desc->callbacks, NULL);
  213. l_free(desc);
  214. done:
  215. if (signal->destroy)
  216. signal->destroy(signal->user_data);
  217. l_free(signal);
  218. }