sigprocmask.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* POSIX compatible signal blocking.
  2. Copyright (C) 2006-2008 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2006.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <signal.h>
  17. #include <errno.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. /* We assume that a platform without POSIX signal blocking functions
  21. also does not have the POSIX sigaction() function, only the
  22. signal() function. We also assume signal() has SysV semantics,
  23. where any handler is uninstalled prior to being invoked. This is
  24. true for Woe32 platforms. */
  25. /* We use raw signal(), but also provide a wrapper rpl_signal() so
  26. that applications can query or change a blocked signal. */
  27. #undef signal
  28. /* Provide invalid signal numbers as fallbacks if the uncatchable
  29. signals are not defined. */
  30. #ifndef SIGKILL
  31. # define SIGKILL (-1)
  32. #endif
  33. #ifndef SIGSTOP
  34. # define SIGSTOP (-1)
  35. #endif
  36. /* On native Windows, as of 2008, the signal SIGABRT_COMPAT is an alias
  37. for the signal SIGABRT. Only one signal handler is stored for both
  38. SIGABRT and SIGABRT_COMPAT. SIGABRT_COMPAT is not a signal of its own. */
  39. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  40. # undef SIGABRT_COMPAT
  41. # define SIGABRT_COMPAT 6
  42. #endif
  43. #ifdef SIGABRT_COMPAT
  44. # define SIGABRT_COMPAT_MASK (1U << SIGABRT_COMPAT)
  45. #else
  46. # define SIGABRT_COMPAT_MASK 0
  47. #endif
  48. typedef void (*handler_t) (int);
  49. /* Handling of gnulib defined signals. */
  50. #if GNULIB_defined_SIGPIPE
  51. static handler_t SIGPIPE_handler = SIG_DFL;
  52. #endif
  53. #if GNULIB_defined_SIGPIPE
  54. static handler_t
  55. ext_signal (int sig, handler_t handler)
  56. {
  57. switch (sig)
  58. {
  59. case SIGPIPE:
  60. {
  61. handler_t old_handler = SIGPIPE_handler;
  62. SIGPIPE_handler = handler;
  63. return old_handler;
  64. }
  65. default: /* System defined signal */
  66. return signal (sig, handler);
  67. }
  68. }
  69. # define signal ext_signal
  70. #endif
  71. int
  72. sigismember (const sigset_t *set, int sig)
  73. {
  74. if (sig >= 0 && sig < NSIG)
  75. {
  76. #ifdef SIGABRT_COMPAT
  77. if (sig == SIGABRT_COMPAT)
  78. sig = SIGABRT;
  79. #endif
  80. return (*set >> sig) & 1;
  81. }
  82. else
  83. return 0;
  84. }
  85. int
  86. sigemptyset (sigset_t *set)
  87. {
  88. *set = 0;
  89. return 0;
  90. }
  91. int
  92. sigaddset (sigset_t *set, int sig)
  93. {
  94. if (sig >= 0 && sig < NSIG)
  95. {
  96. #ifdef SIGABRT_COMPAT
  97. if (sig == SIGABRT_COMPAT)
  98. sig = SIGABRT;
  99. #endif
  100. *set |= 1U << sig;
  101. return 0;
  102. }
  103. else
  104. {
  105. errno = EINVAL;
  106. return -1;
  107. }
  108. }
  109. int
  110. sigdelset (sigset_t *set, int sig)
  111. {
  112. if (sig >= 0 && sig < NSIG)
  113. {
  114. #ifdef SIGABRT_COMPAT
  115. if (sig == SIGABRT_COMPAT)
  116. sig = SIGABRT;
  117. #endif
  118. *set &= ~(1U << sig);
  119. return 0;
  120. }
  121. else
  122. {
  123. errno = EINVAL;
  124. return -1;
  125. }
  126. }
  127. int
  128. sigfillset (sigset_t *set)
  129. {
  130. *set = ((2U << (NSIG - 1)) - 1) & ~ SIGABRT_COMPAT_MASK;
  131. return 0;
  132. }
  133. /* Set of currently blocked signals. */
  134. static volatile sigset_t blocked_set /* = 0 */;
  135. /* Set of currently blocked and pending signals. */
  136. static volatile sig_atomic_t pending_array[NSIG] /* = { 0 } */;
  137. /* Signal handler that is installed for blocked signals. */
  138. static void
  139. blocked_handler (int sig)
  140. {
  141. /* Reinstall the handler, in case the signal occurs multiple times
  142. while blocked. There is an inherent race where an asynchronous
  143. signal in between when the kernel uninstalled the handler and
  144. when we reinstall it will trigger the default handler; oh
  145. well. */
  146. signal (sig, blocked_handler);
  147. if (sig >= 0 && sig < NSIG)
  148. pending_array[sig] = 1;
  149. }
  150. int
  151. sigpending (sigset_t *set)
  152. {
  153. sigset_t pending = 0;
  154. int sig;
  155. for (sig = 0; sig < NSIG; sig++)
  156. if (pending_array[sig])
  157. pending |= 1U << sig;
  158. *set = pending;
  159. return 0;
  160. }
  161. /* The previous signal handlers.
  162. Only the array elements corresponding to blocked signals are relevant. */
  163. static volatile handler_t old_handlers[NSIG];
  164. int
  165. sigprocmask (int operation, const sigset_t *set, sigset_t *old_set)
  166. {
  167. if (old_set != NULL)
  168. *old_set = blocked_set;
  169. if (set != NULL)
  170. {
  171. sigset_t new_blocked_set;
  172. sigset_t to_unblock;
  173. sigset_t to_block;
  174. switch (operation)
  175. {
  176. case SIG_BLOCK:
  177. new_blocked_set = blocked_set | *set;
  178. break;
  179. case SIG_SETMASK:
  180. new_blocked_set = *set;
  181. break;
  182. case SIG_UNBLOCK:
  183. new_blocked_set = blocked_set & ~*set;
  184. break;
  185. default:
  186. errno = EINVAL;
  187. return -1;
  188. }
  189. to_unblock = blocked_set & ~new_blocked_set;
  190. to_block = new_blocked_set & ~blocked_set;
  191. if (to_block != 0)
  192. {
  193. int sig;
  194. for (sig = 0; sig < NSIG; sig++)
  195. if ((to_block >> sig) & 1)
  196. {
  197. pending_array[sig] = 0;
  198. if ((old_handlers[sig] = signal (sig, blocked_handler)) != SIG_ERR)
  199. blocked_set |= 1U << sig;
  200. }
  201. }
  202. if (to_unblock != 0)
  203. {
  204. sig_atomic_t received[NSIG];
  205. int sig;
  206. for (sig = 0; sig < NSIG; sig++)
  207. if ((to_unblock >> sig) & 1)
  208. {
  209. if (signal (sig, old_handlers[sig]) != blocked_handler)
  210. /* The application changed a signal handler while the signal
  211. was blocked, bypassing our rpl_signal replacement.
  212. We don't support this. */
  213. abort ();
  214. received[sig] = pending_array[sig];
  215. blocked_set &= ~(1U << sig);
  216. pending_array[sig] = 0;
  217. }
  218. else
  219. received[sig] = 0;
  220. for (sig = 0; sig < NSIG; sig++)
  221. if (received[sig])
  222. raise (sig);
  223. }
  224. }
  225. return 0;
  226. }
  227. /* Install the handler FUNC for signal SIG, and return the previous
  228. handler. */
  229. handler_t
  230. rpl_signal (int sig, handler_t handler)
  231. {
  232. /* We must provide a wrapper, so that a user can query what handler
  233. they installed even if that signal is currently blocked. */
  234. if (sig >= 0 && sig < NSIG && sig != SIGKILL && sig != SIGSTOP
  235. && handler != SIG_ERR)
  236. {
  237. #ifdef SIGABRT_COMPAT
  238. if (sig == SIGABRT_COMPAT)
  239. sig = SIGABRT;
  240. #endif
  241. if (blocked_set & (1U << sig))
  242. {
  243. /* POSIX states that sigprocmask and signal are both
  244. async-signal-safe. This is not true of our
  245. implementation - there is a slight data race where an
  246. asynchronous interrupt on signal A can occur after we
  247. install blocked_handler but before we have updated
  248. old_handlers for signal B, such that handler A can see
  249. stale information if it calls signal(B). Oh well -
  250. signal handlers really shouldn't try to manipulate the
  251. installed handlers of unrelated signals. */
  252. handler_t result = old_handlers[sig];
  253. old_handlers[sig] = handler;
  254. return result;
  255. }
  256. else
  257. return signal (sig, handler);
  258. }
  259. else
  260. {
  261. errno = EINVAL;
  262. return SIG_ERR;
  263. }
  264. }
  265. #if GNULIB_defined_SIGPIPE
  266. /* Raise the signal SIG. */
  267. int
  268. rpl_raise (int sig)
  269. # undef raise
  270. {
  271. switch (sig)
  272. {
  273. case SIGPIPE:
  274. if (blocked_set & (1U << sig))
  275. pending_array[sig] = 1;
  276. else
  277. {
  278. handler_t handler = SIGPIPE_handler;
  279. if (handler == SIG_DFL)
  280. exit (128 + SIGPIPE);
  281. else if (handler != SIG_IGN)
  282. (*handler) (sig);
  283. }
  284. return 0;
  285. default: /* System defined signal */
  286. return raise (sig);
  287. }
  288. }
  289. #endif