xref: /freebsd/sys/sys/signal.h (revision 53b70c86)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)signal.h	8.4 (Berkeley) 5/4/95
37  * $FreeBSD$
38  */
39 
40 #ifndef _SYS_SIGNAL_H_
41 #define	_SYS_SIGNAL_H_
42 
43 #include <sys/cdefs.h>
44 #include <sys/_types.h>
45 #include <sys/_sigset.h>
46 
47 #include <machine/_limits.h>	/* __MINSIGSTKSZ */
48 #include <machine/signal.h>	/* sig_atomic_t; trap codes; sigcontext */
49 
50 #if __POSIX_VISIBLE >= 200809
51 
52 #include <sys/_pthreadtypes.h>
53 #include <sys/_timespec.h>
54 
55 #ifndef _SIZE_T_DECLARED
56 typedef	__size_t	size_t;
57 #define	_SIZE_T_DECLARED
58 #endif
59 
60 #ifndef _UID_T_DECLARED
61 typedef	__uid_t		uid_t;
62 #define	_UID_T_DECLARED
63 #endif
64 
65 #endif /* __POSIX_VISIBLE >= 200809 */
66 
67 /*
68  * System defined signals.
69  */
70 #if __POSIX_VISIBLE || __XSI_VISIBLE
71 #define	SIGHUP		1	/* hangup */
72 #endif
73 #define	SIGINT		2	/* interrupt */
74 #if __POSIX_VISIBLE || __XSI_VISIBLE
75 #define	SIGQUIT		3	/* quit */
76 #endif
77 #define	SIGILL		4	/* illegal instr. (not reset when caught) */
78 #if __XSI_VISIBLE
79 #define	SIGTRAP		5	/* trace trap (not reset when caught) */
80 #endif
81 #define	SIGABRT		6	/* abort() */
82 #if __BSD_VISIBLE
83 #define	SIGIOT		SIGABRT	/* compatibility */
84 #define	SIGEMT		7	/* EMT instruction */
85 #endif
86 #define	SIGFPE		8	/* floating point exception */
87 #if __POSIX_VISIBLE || __XSI_VISIBLE
88 #define	SIGKILL		9	/* kill (cannot be caught or ignored) */
89 #endif
90 #if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
91 #define	SIGBUS		10	/* bus error */
92 #endif
93 #define	SIGSEGV		11	/* segmentation violation */
94 #if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
95 #define	SIGSYS		12	/* non-existent system call invoked */
96 #endif
97 #if __POSIX_VISIBLE || __XSI_VISIBLE
98 #define	SIGPIPE		13	/* write on a pipe with no one to read it */
99 #define	SIGALRM		14	/* alarm clock */
100 #endif
101 #define	SIGTERM		15	/* software termination signal from kill */
102 #if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
103 #define	SIGURG		16	/* urgent condition on IO channel */
104 #endif
105 #if __POSIX_VISIBLE || __XSI_VISIBLE
106 #define	SIGSTOP		17	/* sendable stop signal not from tty */
107 #define	SIGTSTP		18	/* stop signal from tty */
108 #define	SIGCONT		19	/* continue a stopped process */
109 #define	SIGCHLD		20	/* to parent on child stop or exit */
110 #define	SIGTTIN		21	/* to readers pgrp upon background tty read */
111 #define	SIGTTOU		22	/* like TTIN if (tp->t_local&LTOSTOP) */
112 #endif
113 #if __BSD_VISIBLE
114 #define	SIGIO		23	/* input/output possible signal */
115 #endif
116 #if __XSI_VISIBLE
117 #define	SIGXCPU		24	/* exceeded CPU time limit */
118 #define	SIGXFSZ		25	/* exceeded file size limit */
119 #define	SIGVTALRM	26	/* virtual time alarm */
120 #define	SIGPROF		27	/* profiling time alarm */
121 #endif
122 #if __BSD_VISIBLE
123 #define	SIGWINCH	28	/* window size changes */
124 #define	SIGINFO		29	/* information request */
125 #endif
126 #if __POSIX_VISIBLE || __XSI_VISIBLE
127 #define	SIGUSR1		30	/* user defined signal 1 */
128 #define	SIGUSR2		31	/* user defined signal 2 */
129 #endif
130 #if __BSD_VISIBLE
131 #define	SIGTHR		32	/* reserved by thread library. */
132 #define	SIGLWP		SIGTHR
133 #define	SIGLIBRT	33	/* reserved by real-time library. */
134 #endif
135 
136 #define	SIGRTMIN	65
137 #define	SIGRTMAX	126
138 
139 #define	SIG_DFL		((__sighandler_t *)0)
140 #define	SIG_IGN		((__sighandler_t *)1)
141 #define	SIG_ERR		((__sighandler_t *)-1)
142 /* #define	SIG_CATCH	((__sighandler_t *)2) See signalvar.h */
143 #define SIG_HOLD        ((__sighandler_t *)3)
144 
145 /*
146  * Type of a signal handling function.
147  *
148  * Language spec sez signal handlers take exactly one arg, even though we
149  * actually supply three.  Ugh!
150  *
151  * We don't try to hide the difference by leaving out the args because
152  * that would cause warnings about conformant programs.  Nonconformant
153  * programs can avoid the warnings by casting to (__sighandler_t *) or
154  * sig_t before calling signal() or assigning to sa_handler or sv_handler.
155  *
156  * The kernel should reverse the cast before calling the function.  It
157  * has no way to do this, but on most machines 1-arg and 3-arg functions
158  * have the same calling protocol so there is no problem in practice.
159  * A bit in sa_flags could be used to specify the number of args.
160  */
161 typedef	void __sighandler_t(int);
162 
163 #if __POSIX_VISIBLE || __XSI_VISIBLE
164 #ifndef _SIGSET_T_DECLARED
165 #define	_SIGSET_T_DECLARED
166 typedef	__sigset_t	sigset_t;
167 #endif
168 #endif
169 
170 #if __POSIX_VISIBLE >= 199309 || __XSI_VISIBLE >= 500
171 union sigval {
172 	/* Members as suggested by Annex C of POSIX 1003.1b. */
173 	int	sival_int;
174 	void	*sival_ptr;
175 	/* 6.0 compatibility */
176 	int     sigval_int;
177 	void    *sigval_ptr;
178 };
179 
180 #if defined(_WANT_LWPINFO32) || (defined(_KERNEL) && defined(__LP64__))
181 union sigval32 {
182 	int	sival_int;
183 	uint32_t sival_ptr;
184 	/* 6.0 compatibility */
185 	int	sigval_int;
186 	uint32_t sigval_ptr;
187 };
188 #endif
189 #endif
190 
191 #if __POSIX_VISIBLE >= 199309
192 
193 struct pthread_attr;
194 
195 struct sigevent {
196 	int	sigev_notify;		/* Notification type */
197 	int	sigev_signo;		/* Signal number */
198 	union sigval sigev_value;	/* Signal value */
199 	union {
200 		__lwpid_t	_threadid;
201 		struct {
202 			void (*_function)(union sigval);
203 			struct pthread_attr **_attribute;
204 		} _sigev_thread;
205 		unsigned short _kevent_flags;
206 		long __spare__[8];
207 	} _sigev_un;
208 };
209 
210 #if __BSD_VISIBLE
211 #define	sigev_notify_kqueue		sigev_signo
212 #define	sigev_notify_kevent_flags	_sigev_un._kevent_flags
213 #define	sigev_notify_thread_id		_sigev_un._threadid
214 #endif
215 #define	sigev_notify_function		_sigev_un._sigev_thread._function
216 #define	sigev_notify_attributes		_sigev_un._sigev_thread._attribute
217 
218 #define	SIGEV_NONE	0		/* No async notification. */
219 #define	SIGEV_SIGNAL	1		/* Generate a queued signal. */
220 #define	SIGEV_THREAD	2		/* Call back from another pthread. */
221 #if __BSD_VISIBLE
222 #define	SIGEV_KEVENT	3		/* Generate a kevent. */
223 #define	SIGEV_THREAD_ID	4		/* Send signal to a kernel thread. */
224 #endif
225 
226 #endif /* __POSIX_VISIBLE >= 199309 */
227 
228 #if __POSIX_VISIBLE >= 199309 || __XSI_VISIBLE
229 typedef	struct __siginfo {
230 	int	si_signo;		/* signal number */
231 	int	si_errno;		/* errno association */
232 	/*
233 	 * Cause of signal, one of the SI_ macros or signal-specific
234 	 * values, i.e. one of the FPE_... values for SIGFPE.  This
235 	 * value is equivalent to the second argument to an old-style
236 	 * FreeBSD signal handler.
237 	 */
238 	int	si_code;		/* signal code */
239 	__pid_t	si_pid;			/* sending process */
240 	__uid_t	si_uid;			/* sender's ruid */
241 	int	si_status;		/* exit value */
242 	void	*si_addr;		/* faulting instruction */
243 	union sigval si_value;		/* signal value */
244 	union	{
245 		struct {
246 			int	_trapno;/* machine specific trap code */
247 		} _fault;
248 		struct {
249 			int	_timerid;
250 			int	_overrun;
251 		} _timer;
252 		struct {
253 			int	_mqd;
254 		} _mesgq;
255 		struct {
256 			long	_band;		/* band event for SIGPOLL */
257 		} _poll;			/* was this ever used ? */
258 		struct {
259 			int	_syscall;	/* Syscall number for signals
260 						 * delivered as a result of
261 						 * system calls denied by
262 						 * Capsicum. */
263 		} _capsicum;
264 		struct {
265 			long	__spare1__;
266 			int	__spare2__[7];
267 		} __spare__;
268 	} _reason;
269 } siginfo_t;
270 
271 #define si_trapno	_reason._fault._trapno
272 #define si_timerid	_reason._timer._timerid
273 #define si_overrun	_reason._timer._overrun
274 #define si_mqd		_reason._mesgq._mqd
275 #define si_band		_reason._poll._band
276 #define si_syscall	_reason._capsicum._syscall
277 
278 #if defined(_WANT_LWPINFO32) || (defined(_KERNEL) && defined(__LP64__))
279 struct siginfo32 {
280 	int	si_signo;		/* signal number */
281 	int	si_errno;		/* errno association */
282 	int	si_code;		/* signal code */
283 	__pid_t	si_pid;			/* sending process */
284 	__uid_t	si_uid;			/* sender's ruid */
285 	int	si_status;		/* exit value */
286 	uint32_t si_addr;		/* faulting instruction */
287 	union sigval32 si_value;	/* signal value */
288 	union	{
289 		struct {
290 			int	_trapno;/* machine specific trap code */
291 		} _fault;
292 		struct {
293 			int	_timerid;
294 			int	_overrun;
295 		} _timer;
296 		struct {
297 			int	_mqd;
298 		} _mesgq;
299 		struct {
300 			int32_t	_band;		/* band event for SIGPOLL */
301 		} _poll;			/* was this ever used ? */
302 		struct {
303 			int32_t	__spare1__;
304 			int	__spare2__[7];
305 		} __spare__;
306 	} _reason;
307 };
308 #endif
309 
310 /** si_code **/
311 /* codes for SIGILL */
312 #define ILL_ILLOPC 	1	/* Illegal opcode.			*/
313 #define ILL_ILLOPN 	2	/* Illegal operand.			*/
314 #define ILL_ILLADR 	3	/* Illegal addressing mode.		*/
315 #define ILL_ILLTRP 	4	/* Illegal trap.			*/
316 #define ILL_PRVOPC 	5	/* Privileged opcode.			*/
317 #define ILL_PRVREG 	6	/* Privileged register.			*/
318 #define ILL_COPROC 	7	/* Coprocessor error.			*/
319 #define ILL_BADSTK 	8	/* Internal stack error.		*/
320 
321 /* codes for SIGBUS */
322 #define BUS_ADRALN	1	/* Invalid address alignment.		*/
323 #define BUS_ADRERR	2	/* Nonexistent physical address.	*/
324 #define BUS_OBJERR	3	/* Object-specific hardware error.	*/
325 #define BUS_OOMERR	100	/* Non-standard: No memory.		*/
326 
327 /* codes for SIGSEGV */
328 #define SEGV_MAPERR	1	/* Address not mapped to object.	*/
329 #define SEGV_ACCERR	2	/* Invalid permissions for mapped	*/
330 				/* object.				*/
331 #define	SEGV_PKUERR	100	/* x86: PKU violation			*/
332 
333 /* codes for SIGFPE */
334 #define FPE_INTOVF	1	/* Integer overflow.			*/
335 #define FPE_INTDIV	2	/* Integer divide by zero.		*/
336 #define FPE_FLTDIV	3	/* Floating point divide by zero.	*/
337 #define FPE_FLTOVF	4	/* Floating point overflow.		*/
338 #define FPE_FLTUND	5	/* Floating point underflow.		*/
339 #define FPE_FLTRES	6	/* Floating point inexact result.	*/
340 #define FPE_FLTINV	7	/* Invalid floating point operation.	*/
341 #define FPE_FLTSUB	8	/* Subscript out of range.		*/
342 
343 /* codes for SIGTRAP */
344 #define TRAP_BRKPT	1	/* Process breakpoint.			*/
345 #define TRAP_TRACE	2	/* Process trace trap.			*/
346 #define	TRAP_DTRACE	3	/* DTrace induced trap.			*/
347 #define	TRAP_CAP	4	/* Capabilities protective trap.	*/
348 
349 /* codes for SIGCHLD */
350 #define CLD_EXITED	1	/* Child has exited			*/
351 #define CLD_KILLED	2	/* Child has terminated abnormally but	*/
352 				/* did not create a core file		*/
353 #define CLD_DUMPED	3	/* Child has terminated abnormally and	*/
354 				/* created a core file			*/
355 #define CLD_TRAPPED	4	/* Traced child has trapped		*/
356 #define CLD_STOPPED	5	/* Child has stopped			*/
357 #define CLD_CONTINUED	6	/* Stopped child has continued		*/
358 
359 /* codes for SIGPOLL */
360 #define POLL_IN		1	/* Data input available			*/
361 #define POLL_OUT	2	/* Output buffers available		*/
362 #define POLL_MSG	3	/* Input message available		*/
363 #define POLL_ERR	4	/* I/O Error				*/
364 #define POLL_PRI	5	/* High priority input available	*/
365 #define POLL_HUP	6	/* Device disconnected			*/
366 
367 #endif
368 
369 #if __POSIX_VISIBLE || __XSI_VISIBLE
370 struct __siginfo;
371 
372 /*
373  * Signal vector "template" used in sigaction call.
374  */
375 struct sigaction {
376 	union {
377 		void    (*__sa_handler)(int);
378 		void    (*__sa_sigaction)(int, struct __siginfo *, void *);
379 	} __sigaction_u;		/* signal handler */
380 	int	sa_flags;		/* see signal options below */
381 	sigset_t sa_mask;		/* signal mask to apply */
382 };
383 
384 #define	sa_handler	__sigaction_u.__sa_handler
385 #endif
386 
387 #if __XSI_VISIBLE
388 /* If SA_SIGINFO is set, sa_sigaction must be used instead of sa_handler. */
389 #define	sa_sigaction	__sigaction_u.__sa_sigaction
390 #endif
391 
392 #if __POSIX_VISIBLE || __XSI_VISIBLE
393 #define	SA_NOCLDSTOP	0x0008	/* do not generate SIGCHLD on child stop */
394 #endif /* __POSIX_VISIBLE || __XSI_VISIBLE */
395 
396 #if __XSI_VISIBLE
397 #define	SA_ONSTACK	0x0001	/* take signal on signal stack */
398 #define	SA_RESTART	0x0002	/* restart system call on signal return */
399 #define	SA_RESETHAND	0x0004	/* reset to SIG_DFL when taking signal */
400 #define	SA_NODEFER	0x0010	/* don't mask the signal we're delivering */
401 #define	SA_NOCLDWAIT	0x0020	/* don't keep zombies around */
402 #define	SA_SIGINFO	0x0040	/* signal handler with SA_SIGINFO args */
403 #endif
404 
405 #if __BSD_VISIBLE
406 #define	NSIG		32	/* number of old signals (counting 0) */
407 #endif
408 
409 #if __POSIX_VISIBLE || __XSI_VISIBLE
410 #define	SI_NOINFO	0		/* No signal info besides si_signo. */
411 #define	SI_USER		0x10001		/* Signal sent by kill(). */
412 #define	SI_QUEUE	0x10002		/* Signal sent by the sigqueue(). */
413 #define	SI_TIMER	0x10003		/* Signal generated by expiration of */
414 					/* a timer set by timer_settime(). */
415 #define	SI_ASYNCIO	0x10004		/* Signal generated by completion of */
416 					/* an asynchronous I/O request.*/
417 #define	SI_MESGQ	0x10005		/* Signal generated by arrival of a */
418 					/* message on an empty message queue. */
419 #define	SI_KERNEL	0x10006
420 #define	SI_LWP		0x10007		/* Signal sent by thr_kill */
421 #endif
422 #if __BSD_VISIBLE
423 #define	SI_UNDEFINED	0
424 #endif
425 
426 #if __BSD_VISIBLE
427 typedef	__sighandler_t	*sig_t;	/* type of pointer to a signal function */
428 typedef	void __siginfohandler_t(int, struct __siginfo *, void *);
429 #endif
430 
431 #if __XSI_VISIBLE
432 #if __BSD_VISIBLE
433 #define	__stack_t sigaltstack
434 #endif
435 typedef	struct __stack_t stack_t;
436 
437 #define	SS_ONSTACK	0x0001	/* take signal on alternate stack */
438 #define	SS_DISABLE	0x0004	/* disable taking signals on alternate stack */
439 #define	MINSIGSTKSZ	__MINSIGSTKSZ		/* minimum stack size */
440 #define	SIGSTKSZ	(MINSIGSTKSZ + 32768)	/* recommended stack size */
441 #endif
442 
443 /*
444  * Structure used in sigaltstack call.  Its definition is always
445  * needed for __ucontext.  If __BSD_VISIBLE is defined, the structure
446  * tag is actually sigaltstack.
447  */
448 struct __stack_t {
449 	void	*ss_sp;			/* signal stack base */
450 	__size_t ss_size;		/* signal stack length */
451 	int	ss_flags;		/* SS_DISABLE and/or SS_ONSTACK */
452 };
453 
454 #if __BSD_VISIBLE
455 /*
456  * 4.3 compatibility:
457  * Signal vector "template" used in sigvec call.
458  */
459 struct sigvec {
460 	__sighandler_t *sv_handler;	/* signal handler */
461 	int	sv_mask;		/* signal mask to apply */
462 	int	sv_flags;		/* see signal options below */
463 };
464 
465 #define	SV_ONSTACK	SA_ONSTACK
466 #define	SV_INTERRUPT	SA_RESTART	/* same bit, opposite sense */
467 #define	SV_RESETHAND	SA_RESETHAND
468 #define	SV_NODEFER	SA_NODEFER
469 #define	SV_NOCLDSTOP	SA_NOCLDSTOP
470 #define	SV_SIGINFO	SA_SIGINFO
471 #define	sv_onstack	sv_flags	/* isn't compatibility wonderful! */
472 #endif
473 
474 /* Keep this in one place only */
475 #if defined(_KERNEL) && defined(COMPAT_43) && \
476     !defined(__i386__)
477 struct osigcontext {
478 	int _not_used;
479 };
480 #endif
481 
482 #if __XSI_VISIBLE
483 /*
484  * Structure used in sigstack call.
485  */
486 struct sigstack {
487 	void	*ss_sp;			/* signal stack pointer */
488 	int	ss_onstack;		/* current status */
489 };
490 #endif
491 
492 #if __BSD_VISIBLE || __POSIX_VISIBLE > 0 && __POSIX_VISIBLE <= 200112
493 /*
494  * Macro for converting signal number to a mask suitable for
495  * sigblock().
496  */
497 #define	sigmask(m)	(1 << ((m)-1))
498 #endif
499 
500 #if __BSD_VISIBLE
501 #define	BADSIG		SIG_ERR
502 #endif
503 
504 #if __POSIX_VISIBLE || __XSI_VISIBLE
505 /*
506  * Flags for sigprocmask:
507  */
508 #define	SIG_BLOCK	1	/* block specified signal set */
509 #define	SIG_UNBLOCK	2	/* unblock specified signal set */
510 #define	SIG_SETMASK	3	/* set specified signal set */
511 #endif
512 
513 /*
514  * For historical reasons; programs expect signal's return value to be
515  * defined by <sys/signal.h>.
516  */
517 __BEGIN_DECLS
518 __sighandler_t *signal(int, __sighandler_t *);
519 __END_DECLS
520 
521 #endif /* !_SYS_SIGNAL_H_ */
522