signal.in.h 15 KB

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