sigprocmask.c 8.4 KB

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