1 /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 /*
19  *	ISO C99 Standard: 7.14 Signal handling <signal.h>
20  */
21 
22 #ifndef	_SIGNAL_H
23 #define _SIGNAL_H
24 
25 #include <features.h>
26 
27 __BEGIN_DECLS
28 
29 #include <bits/types.h>
30 #include <bits/signum-generic.h>
31 
32 #include <bits/types/sig_atomic_t.h>
33 
34 #if defined __USE_POSIX
35 #include <bits/types/sigset_t.h>
36 #endif
37 
38 #if defined __USE_XOPEN || defined __USE_XOPEN2K
39 # ifndef __pid_t_defined
40 typedef __pid_t pid_t;
41 #  define __pid_t_defined
42 #endif
43 #ifdef __USE_XOPEN
44 # endif
45 # ifndef __uid_t_defined
46 typedef __uid_t uid_t;
47 #  define __uid_t_defined
48 # endif
49 #endif	/* Unix98 */
50 
51 #ifdef __USE_POSIX199309
52 /* We need `struct timespec' later on.  */
53 # include <bits/types/struct_timespec.h>
54 #endif
55 
56 #if defined __USE_POSIX199309 || defined __USE_XOPEN_EXTENDED
57 # include <bits/types/siginfo_t.h>
58 # include <bits/siginfo-consts.h>
59 #endif
60 
61 #ifdef __USE_MISC
62 # include <bits/types/sigval_t.h>
63 #endif
64 
65 #ifdef __USE_POSIX199309
66 # include <bits/types/sigevent_t.h>
67 # include <bits/sigevent-consts.h>
68 #endif
69 
70 
71 /* Type of a signal handler.  */
72 typedef void (*__sighandler_t) (int);
73 
74 /* The X/Open definition of `signal' specifies the SVID semantic.  Use
75    the additional function `sysv_signal' when X/Open compatibility is
76    requested.  */
77 extern __sighandler_t __sysv_signal (int __sig, __sighandler_t __handler)
78      __THROW;
79 #ifdef __USE_GNU
80 extern __sighandler_t sysv_signal (int __sig, __sighandler_t __handler)
81      __THROW;
82 #endif
83 
84 /* Set the handler for the signal SIG to HANDLER, returning the old
85    handler, or SIG_ERR on error.
86    By default `signal' has the BSD semantic.  */
87 #ifdef __USE_MISC
88 extern __sighandler_t signal (int __sig, __sighandler_t __handler)
89      __THROW;
90 #else
91 /* Make sure the used `signal' implementation is the SVID version. */
92 # ifdef __REDIRECT_NTH
93 extern __sighandler_t __REDIRECT_NTH (signal,
94 				      (int __sig, __sighandler_t __handler),
95 				      __sysv_signal);
96 # else
97 #  define signal __sysv_signal
98 # endif
99 #endif
100 
101 #if defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K8
102 /* The X/Open definition of `signal' conflicts with the BSD version.
103    So they defined another function `bsd_signal'.  */
104 extern __sighandler_t bsd_signal (int __sig, __sighandler_t __handler)
105      __THROW;
106 #endif
107 
108 /* Send signal SIG to process number PID.  If PID is zero,
109    send SIG to all processes in the current process's process group.
110    If PID is < -1, send SIG to all processes in process group - PID.  */
111 #ifdef __USE_POSIX
112 extern int kill (__pid_t __pid, int __sig) __THROW;
113 #endif /* Use POSIX.  */
114 
115 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
116 /* Send SIG to all processes in process group PGRP.
117    If PGRP is zero, send SIG to all processes in
118    the current process's process group.  */
119 extern int killpg (__pid_t __pgrp, int __sig) __THROW;
120 #endif /* Use misc || X/Open Unix.  */
121 
122 /* Raise signal SIG, i.e., send SIG to yourself.  */
123 extern int raise (int __sig) __THROW;
124 
125 #ifdef __USE_MISC
126 /* SVID names for the same things.  */
127 extern __sighandler_t ssignal (int __sig, __sighandler_t __handler)
128      __THROW;
129 extern int gsignal (int __sig) __THROW;
130 #endif /* Use misc.  */
131 
132 #ifdef __USE_XOPEN2K8
133 /* Print a message describing the meaning of the given signal number.  */
134 extern void psignal (int __sig, const char *__s);
135 
136 /* Print a message describing the meaning of the given signal information.  */
137 extern void psiginfo (const siginfo_t *__pinfo, const char *__s);
138 #endif /* POSIX 2008.  */
139 
140 
141 
142 /* The `sigpause' function in X/Open defines the argument as the
143    signal number.  This requires redirecting to another function
144    because the default version in glibc uses an old BSD interface.
145 
146    This function is a cancellation point and therefore not marked with
147    __THROW.  */
148 
149 #ifdef __USE_XOPEN_EXTENDED
150 # ifdef __GNUC__
151 extern int sigpause (int __sig) __asm__ ("__xpg_sigpause")
152   __attribute_deprecated_msg__ ("Use the sigsuspend function instead");
153 # else
154 extern int __sigpause (int __sig_or_mask, int __is_sig);
155 /* Remove a signal from the signal mask and suspend the process.  */
156 #  define sigpause(sig) __sigpause ((sig), 1)
157 # endif
158 #endif
159 
160 
161 #ifdef __USE_MISC
162 /* None of the following functions should be used anymore.  They are here
163    only for compatibility.  A single word (`int') is not guaranteed to be
164    enough to hold a complete signal mask and therefore these functions
165    simply do not work in many situations.  Use `sigprocmask' instead.  */
166 
167 /* Compute mask for signal SIG.  */
168 # define sigmask(sig) \
169   __glibc_macro_warning ("sigmask is deprecated") \
170   ((int)(1u << ((sig) - 1)))
171 
172 /* Block signals in MASK, returning the old mask.  */
173 extern int sigblock (int __mask) __THROW __attribute_deprecated__;
174 
175 /* Set the mask of blocked signals to MASK, returning the old mask.  */
176 extern int sigsetmask (int __mask) __THROW __attribute_deprecated__;
177 
178 /* Return currently selected signal mask.  */
179 extern int siggetmask (void) __THROW __attribute_deprecated__;
180 #endif /* Use misc.  */
181 
182 
183 #ifdef __USE_MISC
184 # define NSIG	_NSIG
185 #endif
186 
187 #ifdef __USE_GNU
188 typedef __sighandler_t sighandler_t;
189 #endif
190 
191 /* 4.4 BSD uses the name `sig_t' for this.  */
192 #ifdef __USE_MISC
193 typedef __sighandler_t sig_t;
194 #endif
195 
196 #ifdef __USE_POSIX
197 
198 /* Clear all signals from SET.  */
199 extern int sigemptyset (sigset_t *__set) __THROW __nonnull ((1));
200 
201 /* Set all signals in SET.  */
202 extern int sigfillset (sigset_t *__set) __THROW __nonnull ((1));
203 
204 /* Add SIGNO to SET.  */
205 extern int sigaddset (sigset_t *__set, int __signo) __THROW __nonnull ((1));
206 
207 /* Remove SIGNO from SET.  */
208 extern int sigdelset (sigset_t *__set, int __signo) __THROW __nonnull ((1));
209 
210 /* Return 1 if SIGNO is in SET, 0 if not.  */
211 extern int sigismember (const sigset_t *__set, int __signo)
212      __THROW __nonnull ((1));
213 
214 # ifdef __USE_GNU
215 /* Return non-empty value is SET is not empty.  */
216 extern int sigisemptyset (const sigset_t *__set) __THROW __nonnull ((1));
217 
218 /* Build new signal set by combining the two inputs set using logical AND.  */
219 extern int sigandset (sigset_t *__set, const sigset_t *__left,
220 		      const sigset_t *__right) __THROW __nonnull ((1, 2, 3));
221 
222 /* Build new signal set by combining the two inputs set using logical OR.  */
223 extern int sigorset (sigset_t *__set, const sigset_t *__left,
224 		     const sigset_t *__right) __THROW __nonnull ((1, 2, 3));
225 # endif /* GNU */
226 
227 /* Get the system-specific definitions of `struct sigaction'
228    and the `SA_*' and `SIG_*'. constants.  */
229 # include <bits/sigaction.h>
230 
231 /* Get and/or change the set of blocked signals.  */
232 extern int sigprocmask (int __how, const sigset_t *__restrict __set,
233 			sigset_t *__restrict __oset) __THROW;
234 
235 /* Change the set of blocked signals to SET,
236    wait until a signal arrives, and restore the set of blocked signals.
237 
238    This function is a cancellation point and therefore not marked with
239    __THROW.  */
240 extern int sigsuspend (const sigset_t *__set) __nonnull ((1));
241 
242 /* Get and/or set the action for signal SIG.  */
243 extern int sigaction (int __sig, const struct sigaction *__restrict __act,
244 		      struct sigaction *__restrict __oact) __THROW;
245 
246 /* Put in SET all signals that are blocked and waiting to be delivered.  */
247 extern int sigpending (sigset_t *__set) __THROW __nonnull ((1));
248 
249 
250 # ifdef __USE_POSIX199506
251 /* Select any of pending signals from SET or wait for any to arrive.
252 
253    This function is a cancellation point and therefore not marked with
254    __THROW.  */
255 extern int sigwait (const sigset_t *__restrict __set, int *__restrict __sig)
256      __nonnull ((1, 2));
257 # endif /* Use POSIX 1995.  */
258 
259 # ifdef __USE_POSIX199309
260 /* Select any of pending signals from SET and place information in INFO.
261 
262    This function is a cancellation point and therefore not marked with
263    __THROW.  */
264 extern int sigwaitinfo (const sigset_t *__restrict __set,
265 			siginfo_t *__restrict __info) __nonnull ((1));
266 
267 /* Select any of pending signals from SET and place information in INFO.
268    Wait the time specified by TIMEOUT if no signal is pending.
269 
270    This function is a cancellation point and therefore not marked with
271    __THROW.  */
272 #  ifndef __USE_TIME_BITS64
273 extern int sigtimedwait (const sigset_t *__restrict __set,
274 			 siginfo_t *__restrict __info,
275 			 const struct timespec *__restrict __timeout)
276      __nonnull ((1));
277 #  else
278 #   ifdef __REDIRECT
279 extern int __REDIRECT (sigtimedwait,
280                        (const sigset_t *__restrict __set,
281                         siginfo_t *__restrict __info,
282                         const struct timespec *__restrict __timeout),
283                        __sigtimedwait64)
284      __nonnull ((1));
285 #   else
286 #    define sigtimedwait __sigtimedwait64
287 #   endif
288 #  endif
289 
290 /* Send signal SIG to the process PID.  Associate data in VAL with the
291    signal.  */
292 extern int sigqueue (__pid_t __pid, int __sig, const union sigval __val)
293      __THROW;
294 # endif	/* Use POSIX 199306.  */
295 
296 #endif /* Use POSIX.  */
297 
298 #ifdef __USE_MISC
299 
300 /* Get machine-dependent `struct sigcontext' and signal subcodes.  */
301 # include <bits/sigcontext.h>
302 
303 /* Restore the state saved in SCP.  */
304 extern int sigreturn (struct sigcontext *__scp) __THROW;
305 
306 #endif /* Use misc.  */
307 
308 
309 #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
310 # define __need_size_t
311 # include <stddef.h>
312 
313 # include <bits/types/stack_t.h>
314 # if defined __USE_XOPEN || defined __USE_XOPEN2K8
315 /* This will define `ucontext_t' and `mcontext_t'.  */
316 #  include <sys/ucontext.h>
317 # endif
318 #endif /* Use POSIX.1-2008 or X/Open Unix.  */
319 
320 #if defined __USE_XOPEN_EXTENDED || defined __USE_MISC
321 /* If INTERRUPT is nonzero, make signal SIG interrupt system calls
322    (causing them to fail with EINTR); if INTERRUPT is zero, make system
323    calls be restarted after signal SIG.  */
324 extern int siginterrupt (int __sig, int __interrupt) __THROW
325   __attribute_deprecated_msg__ ("Use sigaction with SA_RESTART instead");
326 
327 # include <bits/sigstack.h>
328 # include <bits/sigstksz.h>
329 # include <bits/ss_flags.h>
330 
331 /* Alternate signal handler stack interface.
332    This interface should always be preferred over `sigstack'.  */
333 extern int sigaltstack (const stack_t *__restrict __ss,
334 			stack_t *__restrict __oss) __THROW;
335 #endif /* __USE_XOPEN_EXTENDED || __USE_MISC */
336 
337 #if ((defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K8)	\
338      || defined __USE_MISC)
339 # include <bits/types/struct_sigstack.h>
340 #endif
341 
342 #if ((defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K)	\
343      || defined __USE_MISC)
344 /* Run signals handlers on the stack specified by SS (if not NULL).
345    If OSS is not NULL, it is filled in with the old signal stack status.
346    This interface is obsolete and on many platform not implemented.  */
347 extern int sigstack (struct sigstack *__ss, struct sigstack *__oss)
348      __THROW __attribute_deprecated__;
349 #endif
350 
351 #ifdef __USE_XOPEN_EXTENDED
352 /* Simplified interface for signal management.  */
353 
354 /* Add SIG to the calling process' signal mask.  */
355 extern int sighold (int __sig) __THROW
356   __attribute_deprecated_msg__ ("Use the sigprocmask function instead");
357 
358 /* Remove SIG from the calling process' signal mask.  */
359 extern int sigrelse (int __sig) __THROW
360   __attribute_deprecated_msg__ ("Use the sigprocmask function instead");
361 
362 /* Set the disposition of SIG to SIG_IGN.  */
363 extern int sigignore (int __sig) __THROW
364   __attribute_deprecated_msg__ ("Use the signal function instead");
365 
366 /* Set the disposition of SIG.  */
367 extern __sighandler_t sigset (int __sig, __sighandler_t __disp) __THROW
368   __attribute_deprecated_msg__
369     ("Use the signal and sigprocmask functions instead");
370 #endif
371 
372 #if defined __USE_POSIX199506 || defined __USE_UNIX98
373 /* Some of the functions for handling signals in threaded programs must
374    be defined here.  */
375 # include <bits/pthreadtypes.h>
376 # include <bits/sigthread.h>
377 #endif /* use Unix98 */
378 
379 /* The following functions are used internally in the C library and in
380    other code which need deep insights.  */
381 
382 /* Return number of available real-time signal with highest priority.  */
383 extern int __libc_current_sigrtmin (void) __THROW;
384 /* Return number of available real-time signal with lowest priority.  */
385 extern int __libc_current_sigrtmax (void) __THROW;
386 
387 #define SIGRTMIN        (__libc_current_sigrtmin ())
388 #define SIGRTMAX        (__libc_current_sigrtmax ())
389 
390 /* System-specific extensions.  */
391 #include <bits/signal_ext.h>
392 
393 __END_DECLS
394 
395 #endif /* not signal.h */
396