signal.in.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* A GNU-like <signal.h>.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #if __GNUC__ >= 3
  14. @PRAGMA_SYSTEM_HEADER@
  15. #endif
  16. @PRAGMA_COLUMNS@
  17. #if defined __need_sig_atomic_t || defined __need_sigset_t || defined _GL_ALREADY_INCLUDING_SIGNAL_H || (defined _SIGNAL_H && !defined __SIZEOF_PTHREAD_MUTEX_T)
  18. /* Special invocation convention:
  19. - Inside glibc header files.
  20. - On glibc systems we have a sequence of nested includes
  21. <signal.h> -> <ucontext.h> -> <signal.h>.
  22. In this situation, the functions are not yet declared, therefore we cannot
  23. provide the C++ aliases.
  24. - On glibc systems with GCC 4.3 we have a sequence of nested includes
  25. <csignal> -> </usr/include/signal.h> -> <sys/ucontext.h> -> <signal.h>.
  26. In this situation, some of the functions are not yet declared, therefore
  27. we cannot provide the C++ aliases. */
  28. # @INCLUDE_NEXT@ @NEXT_SIGNAL_H@
  29. #else
  30. /* Normal invocation convention. */
  31. #ifndef _@GUARD_PREFIX@_SIGNAL_H
  32. #define _GL_ALREADY_INCLUDING_SIGNAL_H
  33. /* Define pid_t, uid_t.
  34. Also, mingw defines sigset_t not in <signal.h>, but in <sys/types.h>.
  35. On Solaris 10, <signal.h> includes <sys/types.h>, which eventually includes
  36. us; so include <sys/types.h> now, before the second inclusion guard. */
  37. #include <sys/types.h>
  38. /* The include_next requires a split double-inclusion guard. */
  39. #@INCLUDE_NEXT@ @NEXT_SIGNAL_H@
  40. #undef _GL_ALREADY_INCLUDING_SIGNAL_H
  41. #ifndef _@GUARD_PREFIX@_SIGNAL_H
  42. #define _@GUARD_PREFIX@_SIGNAL_H
  43. /* Mac OS X 10.3, FreeBSD 6.4, OpenBSD 3.8, OSF/1 4.0, Solaris 2.6, Android,
  44. OS/2 kLIBC declare pthread_sigmask in <pthread.h>, not in <signal.h>.
  45. But avoid namespace pollution on glibc systems.*/
  46. #if (@GNULIB_PTHREAD_SIGMASK@ || defined GNULIB_POSIXCHECK) \
  47. && ((defined __APPLE__ && defined __MACH__) \
  48. || defined __FreeBSD__ || defined __OpenBSD__ || defined __osf__ \
  49. || defined __sun || defined __ANDROID__ || defined __KLIBC__) \
  50. && ! defined __GLIBC__
  51. # include <pthread.h>
  52. #endif
  53. /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
  54. /* The definition of _GL_ARG_NONNULL is copied here. */
  55. /* The definition of _GL_WARN_ON_USE is copied here. */
  56. /* On AIX, sig_atomic_t already includes volatile. C99 requires that
  57. 'volatile sig_atomic_t' ignore the extra modifier, but C89 did not.
  58. Hence, redefine this to a non-volatile type as needed. */
  59. #if ! @HAVE_TYPE_VOLATILE_SIG_ATOMIC_T@
  60. # if !GNULIB_defined_sig_atomic_t
  61. typedef int rpl_sig_atomic_t;
  62. # undef sig_atomic_t
  63. # define sig_atomic_t rpl_sig_atomic_t
  64. # define GNULIB_defined_sig_atomic_t 1
  65. # endif
  66. #endif
  67. /* A set or mask of signals. */
  68. #if !@HAVE_SIGSET_T@
  69. # if !GNULIB_defined_sigset_t
  70. typedef unsigned int sigset_t;
  71. # define GNULIB_defined_sigset_t 1
  72. # endif
  73. #endif
  74. /* Define sighandler_t, the type of signal handlers. A GNU extension. */
  75. #if !@HAVE_SIGHANDLER_T@
  76. # ifdef __cplusplus
  77. extern "C" {
  78. # endif
  79. # if !GNULIB_defined_sighandler_t
  80. typedef void (*sighandler_t) (int);
  81. # define GNULIB_defined_sighandler_t 1
  82. # endif
  83. # ifdef __cplusplus
  84. }
  85. # endif
  86. #endif
  87. #if @GNULIB_SIGNAL_H_SIGPIPE@
  88. # ifndef SIGPIPE
  89. /* Define SIGPIPE to a value that does not overlap with other signals. */
  90. # define SIGPIPE 13
  91. # define GNULIB_defined_SIGPIPE 1
  92. /* To actually use SIGPIPE, you also need the gnulib modules 'sigprocmask',
  93. 'write', 'stdio'. */
  94. # endif
  95. #endif
  96. /* Maximum signal number + 1. */
  97. #ifndef NSIG
  98. # if defined __TANDEM
  99. # define NSIG 32
  100. # endif
  101. #endif
  102. #if @GNULIB_PTHREAD_SIGMASK@
  103. # if @REPLACE_PTHREAD_SIGMASK@
  104. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  105. # undef pthread_sigmask
  106. # define pthread_sigmask rpl_pthread_sigmask
  107. # endif
  108. _GL_FUNCDECL_RPL (pthread_sigmask, int,
  109. (int how,
  110. const sigset_t *restrict new_mask,
  111. sigset_t *restrict old_mask));
  112. _GL_CXXALIAS_RPL (pthread_sigmask, int,
  113. (int how,
  114. const sigset_t *restrict new_mask,
  115. sigset_t *restrict old_mask));
  116. # else
  117. # if !(@HAVE_PTHREAD_SIGMASK@ || defined pthread_sigmask)
  118. _GL_FUNCDECL_SYS (pthread_sigmask, int,
  119. (int how,
  120. const sigset_t *restrict new_mask,
  121. sigset_t *restrict old_mask));
  122. # endif
  123. _GL_CXXALIAS_SYS (pthread_sigmask, int,
  124. (int how,
  125. const sigset_t *restrict new_mask,
  126. sigset_t *restrict old_mask));
  127. # endif
  128. # if __GLIBC__ >= 2
  129. _GL_CXXALIASWARN (pthread_sigmask);
  130. # endif
  131. #elif defined GNULIB_POSIXCHECK
  132. # undef pthread_sigmask
  133. # if HAVE_RAW_DECL_PTHREAD_SIGMASK
  134. _GL_WARN_ON_USE (pthread_sigmask, "pthread_sigmask is not portable - "
  135. "use gnulib module pthread_sigmask for portability");
  136. # endif
  137. #endif
  138. #if @GNULIB_RAISE@
  139. # if @REPLACE_RAISE@
  140. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  141. # undef raise
  142. # define raise rpl_raise
  143. # endif
  144. _GL_FUNCDECL_RPL (raise, int, (int sig));
  145. _GL_CXXALIAS_RPL (raise, int, (int sig));
  146. # else
  147. # if !@HAVE_RAISE@
  148. _GL_FUNCDECL_SYS (raise, int, (int sig));
  149. # endif
  150. _GL_CXXALIAS_SYS (raise, int, (int sig));
  151. # endif
  152. # if __GLIBC__ >= 2
  153. _GL_CXXALIASWARN (raise);
  154. # endif
  155. #elif defined GNULIB_POSIXCHECK
  156. # undef raise
  157. /* Assume raise is always declared. */
  158. _GL_WARN_ON_USE (raise, "raise can crash on native Windows - "
  159. "use gnulib module raise for portability");
  160. #endif
  161. #if @GNULIB_SIGPROCMASK@
  162. # if !@HAVE_POSIX_SIGNALBLOCKING@
  163. # ifndef GNULIB_defined_signal_blocking
  164. # define GNULIB_defined_signal_blocking 1
  165. # endif
  166. /* Maximum signal number + 1. */
  167. # ifndef NSIG
  168. # define NSIG 32
  169. # endif
  170. /* This code supports only 32 signals. */
  171. # if !GNULIB_defined_verify_NSIG_constraint
  172. typedef int verify_NSIG_constraint[NSIG <= 32 ? 1 : -1];
  173. # define GNULIB_defined_verify_NSIG_constraint 1
  174. # endif
  175. # endif
  176. /* When also using extern inline, suppress the use of static inline in
  177. standard headers of problematic Apple configurations, as Libc at
  178. least through Libc-825.26 (2013-04-09) mishandles it; see, e.g.,
  179. <https://lists.gnu.org/r/bug-gnulib/2012-12/msg00023.html>.
  180. Perhaps Apple will fix this some day. */
  181. #if (defined _GL_EXTERN_INLINE_IN_USE && defined __APPLE__ \
  182. && (defined __i386__ || defined __x86_64__))
  183. # undef sigaddset
  184. # undef sigdelset
  185. # undef sigemptyset
  186. # undef sigfillset
  187. # undef sigismember
  188. #endif
  189. /* Test whether a given signal is contained in a signal set. */
  190. # if @HAVE_POSIX_SIGNALBLOCKING@
  191. /* This function is defined as a macro on Mac OS X. */
  192. # if defined __cplusplus && defined GNULIB_NAMESPACE
  193. # undef sigismember
  194. # endif
  195. # else
  196. _GL_FUNCDECL_SYS (sigismember, int, (const sigset_t *set, int sig)
  197. _GL_ARG_NONNULL ((1)));
  198. # endif
  199. _GL_CXXALIAS_SYS (sigismember, int, (const sigset_t *set, int sig));
  200. _GL_CXXALIASWARN (sigismember);
  201. /* Initialize a signal set to the empty set. */
  202. # if @HAVE_POSIX_SIGNALBLOCKING@
  203. /* This function is defined as a macro on Mac OS X. */
  204. # if defined __cplusplus && defined GNULIB_NAMESPACE
  205. # undef sigemptyset
  206. # endif
  207. # else
  208. _GL_FUNCDECL_SYS (sigemptyset, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
  209. # endif
  210. _GL_CXXALIAS_SYS (sigemptyset, int, (sigset_t *set));
  211. _GL_CXXALIASWARN (sigemptyset);
  212. /* Add a signal to a signal set. */
  213. # if @HAVE_POSIX_SIGNALBLOCKING@
  214. /* This function is defined as a macro on Mac OS X. */
  215. # if defined __cplusplus && defined GNULIB_NAMESPACE
  216. # undef sigaddset
  217. # endif
  218. # else
  219. _GL_FUNCDECL_SYS (sigaddset, int, (sigset_t *set, int sig)
  220. _GL_ARG_NONNULL ((1)));
  221. # endif
  222. _GL_CXXALIAS_SYS (sigaddset, int, (sigset_t *set, int sig));
  223. _GL_CXXALIASWARN (sigaddset);
  224. /* Remove a signal from a signal set. */
  225. # if @HAVE_POSIX_SIGNALBLOCKING@
  226. /* This function is defined as a macro on Mac OS X. */
  227. # if defined __cplusplus && defined GNULIB_NAMESPACE
  228. # undef sigdelset
  229. # endif
  230. # else
  231. _GL_FUNCDECL_SYS (sigdelset, int, (sigset_t *set, int sig)
  232. _GL_ARG_NONNULL ((1)));
  233. # endif
  234. _GL_CXXALIAS_SYS (sigdelset, int, (sigset_t *set, int sig));
  235. _GL_CXXALIASWARN (sigdelset);
  236. /* Fill a signal set with all possible signals. */
  237. # if @HAVE_POSIX_SIGNALBLOCKING@
  238. /* This function is defined as a macro on Mac OS X. */
  239. # if defined __cplusplus && defined GNULIB_NAMESPACE
  240. # undef sigfillset
  241. # endif
  242. # else
  243. _GL_FUNCDECL_SYS (sigfillset, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
  244. # endif
  245. _GL_CXXALIAS_SYS (sigfillset, int, (sigset_t *set));
  246. _GL_CXXALIASWARN (sigfillset);
  247. /* Return the set of those blocked signals that are pending. */
  248. # if !@HAVE_POSIX_SIGNALBLOCKING@
  249. _GL_FUNCDECL_SYS (sigpending, int, (sigset_t *set) _GL_ARG_NONNULL ((1)));
  250. # endif
  251. _GL_CXXALIAS_SYS (sigpending, int, (sigset_t *set));
  252. _GL_CXXALIASWARN (sigpending);
  253. /* If OLD_SET is not NULL, put the current set of blocked signals in *OLD_SET.
  254. Then, if SET is not NULL, affect the current set of blocked signals by
  255. combining it with *SET as indicated in OPERATION.
  256. In this implementation, you are not allowed to change a signal handler
  257. while the signal is blocked. */
  258. # if !@HAVE_POSIX_SIGNALBLOCKING@
  259. # define SIG_BLOCK 0 /* blocked_set = blocked_set | *set; */
  260. # define SIG_SETMASK 1 /* blocked_set = *set; */
  261. # define SIG_UNBLOCK 2 /* blocked_set = blocked_set & ~*set; */
  262. _GL_FUNCDECL_SYS (sigprocmask, int,
  263. (int operation,
  264. const sigset_t *restrict set,
  265. sigset_t *restrict old_set));
  266. # endif
  267. _GL_CXXALIAS_SYS (sigprocmask, int,
  268. (int operation,
  269. const sigset_t *restrict set,
  270. sigset_t *restrict old_set));
  271. _GL_CXXALIASWARN (sigprocmask);
  272. /* Install the handler FUNC for signal SIG, and return the previous
  273. handler. */
  274. # ifdef __cplusplus
  275. extern "C" {
  276. # endif
  277. # if !GNULIB_defined_function_taking_int_returning_void_t
  278. typedef void (*_gl_function_taking_int_returning_void_t) (int);
  279. # define GNULIB_defined_function_taking_int_returning_void_t 1
  280. # endif
  281. # ifdef __cplusplus
  282. }
  283. # endif
  284. # if !@HAVE_POSIX_SIGNALBLOCKING@
  285. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  286. # define signal rpl_signal
  287. # endif
  288. _GL_FUNCDECL_RPL (signal, _gl_function_taking_int_returning_void_t,
  289. (int sig, _gl_function_taking_int_returning_void_t func));
  290. _GL_CXXALIAS_RPL (signal, _gl_function_taking_int_returning_void_t,
  291. (int sig, _gl_function_taking_int_returning_void_t func));
  292. # else
  293. /* On OpenBSD, the declaration of 'signal' may not be present at this point,
  294. because it occurs in <sys/signal.h>, not <signal.h> directly. */
  295. # if defined __OpenBSD__
  296. _GL_FUNCDECL_SYS (signal, _gl_function_taking_int_returning_void_t,
  297. (int sig, _gl_function_taking_int_returning_void_t func));
  298. # endif
  299. _GL_CXXALIAS_SYS (signal, _gl_function_taking_int_returning_void_t,
  300. (int sig, _gl_function_taking_int_returning_void_t func));
  301. # endif
  302. # if __GLIBC__ >= 2
  303. _GL_CXXALIASWARN (signal);
  304. # endif
  305. # if !@HAVE_POSIX_SIGNALBLOCKING@ && GNULIB_defined_SIGPIPE
  306. /* Raise signal SIGPIPE. */
  307. _GL_EXTERN_C int _gl_raise_SIGPIPE (void);
  308. # endif
  309. #elif defined GNULIB_POSIXCHECK
  310. # undef sigaddset
  311. # if HAVE_RAW_DECL_SIGADDSET
  312. _GL_WARN_ON_USE (sigaddset, "sigaddset is unportable - "
  313. "use the gnulib module sigprocmask for portability");
  314. # endif
  315. # undef sigdelset
  316. # if HAVE_RAW_DECL_SIGDELSET
  317. _GL_WARN_ON_USE (sigdelset, "sigdelset is unportable - "
  318. "use the gnulib module sigprocmask for portability");
  319. # endif
  320. # undef sigemptyset
  321. # if HAVE_RAW_DECL_SIGEMPTYSET
  322. _GL_WARN_ON_USE (sigemptyset, "sigemptyset is unportable - "
  323. "use the gnulib module sigprocmask for portability");
  324. # endif
  325. # undef sigfillset
  326. # if HAVE_RAW_DECL_SIGFILLSET
  327. _GL_WARN_ON_USE (sigfillset, "sigfillset is unportable - "
  328. "use the gnulib module sigprocmask for portability");
  329. # endif
  330. # undef sigismember
  331. # if HAVE_RAW_DECL_SIGISMEMBER
  332. _GL_WARN_ON_USE (sigismember, "sigismember is unportable - "
  333. "use the gnulib module sigprocmask for portability");
  334. # endif
  335. # undef sigpending
  336. # if HAVE_RAW_DECL_SIGPENDING
  337. _GL_WARN_ON_USE (sigpending, "sigpending is unportable - "
  338. "use the gnulib module sigprocmask for portability");
  339. # endif
  340. # undef sigprocmask
  341. # if HAVE_RAW_DECL_SIGPROCMASK
  342. _GL_WARN_ON_USE (sigprocmask, "sigprocmask is unportable - "
  343. "use the gnulib module sigprocmask for portability");
  344. # endif
  345. #endif /* @GNULIB_SIGPROCMASK@ */
  346. #if @GNULIB_SIGACTION@
  347. # if !@HAVE_SIGACTION@
  348. # if !@HAVE_SIGINFO_T@
  349. # if !GNULIB_defined_siginfo_types
  350. /* Present to allow compilation, but unsupported by gnulib. */
  351. union sigval
  352. {
  353. int sival_int;
  354. void *sival_ptr;
  355. };
  356. /* Present to allow compilation, but unsupported by gnulib. */
  357. struct siginfo_t
  358. {
  359. int si_signo;
  360. int si_code;
  361. int si_errno;
  362. pid_t si_pid;
  363. uid_t si_uid;
  364. void *si_addr;
  365. int si_status;
  366. long si_band;
  367. union sigval si_value;
  368. };
  369. typedef struct siginfo_t siginfo_t;
  370. # define GNULIB_defined_siginfo_types 1
  371. # endif
  372. # endif /* !@HAVE_SIGINFO_T@ */
  373. /* We assume that platforms which lack the sigaction() function also lack
  374. the 'struct sigaction' type, and vice versa. */
  375. # if !GNULIB_defined_struct_sigaction
  376. struct sigaction
  377. {
  378. union
  379. {
  380. void (*_sa_handler) (int);
  381. /* Present to allow compilation, but unsupported by gnulib. POSIX
  382. says that implementations may, but not must, make sa_sigaction
  383. overlap with sa_handler, but we know of no implementation where
  384. they do not overlap. */
  385. void (*_sa_sigaction) (int, siginfo_t *, void *);
  386. } _sa_func;
  387. sigset_t sa_mask;
  388. /* Not all POSIX flags are supported. */
  389. int sa_flags;
  390. };
  391. # define sa_handler _sa_func._sa_handler
  392. # define sa_sigaction _sa_func._sa_sigaction
  393. /* Unsupported flags are not present. */
  394. # define SA_RESETHAND 1
  395. # define SA_NODEFER 2
  396. # define SA_RESTART 4
  397. # define GNULIB_defined_struct_sigaction 1
  398. # endif
  399. _GL_FUNCDECL_SYS (sigaction, int, (int, const struct sigaction *restrict,
  400. struct sigaction *restrict));
  401. # elif !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@
  402. # define sa_sigaction sa_handler
  403. # endif /* !@HAVE_SIGACTION@, !@HAVE_STRUCT_SIGACTION_SA_SIGACTION@ */
  404. _GL_CXXALIAS_SYS (sigaction, int, (int, const struct sigaction *restrict,
  405. struct sigaction *restrict));
  406. _GL_CXXALIASWARN (sigaction);
  407. #elif defined GNULIB_POSIXCHECK
  408. # undef sigaction
  409. # if HAVE_RAW_DECL_SIGACTION
  410. _GL_WARN_ON_USE (sigaction, "sigaction is unportable - "
  411. "use the gnulib module sigaction for portability");
  412. # endif
  413. #endif
  414. /* Some systems don't have SA_NODEFER. */
  415. #ifndef SA_NODEFER
  416. # define SA_NODEFER 0
  417. #endif
  418. #endif /* _@GUARD_PREFIX@_SIGNAL_H */
  419. #endif /* _@GUARD_PREFIX@_SIGNAL_H */
  420. #endif