1SIGACTION(2) Linux Programmer's Manual SIGACTION(2)
2
3
4
6 sigaction, rt_sigaction - examine and change a signal action
7
9 #include <signal.h>
10
11 int sigaction(int signum, const struct sigaction *restrict act,
12 struct sigaction *restrict oldact);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 sigaction():
17 _POSIX_C_SOURCE
18
19 siginfo_t:
20 _POSIX_C_SOURCE >= 199309L
21
23 The sigaction() system call is used to change the action taken by a
24 process on receipt of a specific signal. (See signal(7) for an over‐
25 view of signals.)
26
27 signum specifies the signal and can be any valid signal except SIGKILL
28 and SIGSTOP.
29
30 If act is non-NULL, the new action for signal signum is installed from
31 act. If oldact is non-NULL, the previous action is saved in oldact.
32
33 The sigaction structure is defined as something like:
34
35 struct sigaction {
36 void (*sa_handler)(int);
37 void (*sa_sigaction)(int, siginfo_t *, void *);
38 sigset_t sa_mask;
39 int sa_flags;
40 void (*sa_restorer)(void);
41 };
42
43 On some architectures a union is involved: do not assign to both
44 sa_handler and sa_sigaction.
45
46 The sa_restorer field is not intended for application use. (POSIX does
47 not specify a sa_restorer field.) Some further details of the purpose
48 of this field can be found in sigreturn(2).
49
50 sa_handler specifies the action to be associated with signum and is be
51 one of the following:
52
53 * SIG_DFL for the default action.
54
55 * SIG_IGN to ignore this signal.
56
57 * A pointer to a signal handling function. This function receives the
58 signal number as its only argument.
59
60 If SA_SIGINFO is specified in sa_flags, then sa_sigaction (instead of
61 sa_handler) specifies the signal-handling function for signum. This
62 function receives three arguments, as described below.
63
64 sa_mask specifies a mask of signals which should be blocked (i.e.,
65 added to the signal mask of the thread in which the signal handler is
66 invoked) during execution of the signal handler. In addition, the sig‐
67 nal which triggered the handler will be blocked, unless the SA_NODEFER
68 flag is used.
69
70 sa_flags specifies a set of flags which modify the behavior of the sig‐
71 nal. It is formed by the bitwise OR of zero or more of the following:
72
73 SA_NOCLDSTOP
74 If signum is SIGCHLD, do not receive notification when child
75 processes stop (i.e., when they receive one of SIGSTOP, SIGTSTP,
76 SIGTTIN, or SIGTTOU) or resume (i.e., they receive SIGCONT) (see
77 wait(2)). This flag is meaningful only when establishing a han‐
78 dler for SIGCHLD.
79
80 SA_NOCLDWAIT (since Linux 2.6)
81 If signum is SIGCHLD, do not transform children into zombies
82 when they terminate. See also waitpid(2). This flag is mean‐
83 ingful only when establishing a handler for SIGCHLD, or when
84 setting that signal's disposition to SIG_DFL.
85
86 If the SA_NOCLDWAIT flag is set when establishing a handler for
87 SIGCHLD, POSIX.1 leaves it unspecified whether a SIGCHLD signal
88 is generated when a child process terminates. On Linux, a
89 SIGCHLD signal is generated in this case; on some other imple‐
90 mentations, it is not.
91
92 SA_NODEFER
93 Do not add the signal to the thread's signal mask while the han‐
94 dler is executing, unless the signal is specified in
95 act.sa_mask. Consequently, a further instance of the signal may
96 be delivered to the thread while it is executing the handler.
97 This flag is meaningful only when establishing a signal handler.
98
99 SA_NOMASK is an obsolete, nonstandard synonym for this flag.
100
101 SA_ONSTACK
102 Call the signal handler on an alternate signal stack provided by
103 sigaltstack(2). If an alternate stack is not available, the de‐
104 fault stack will be used. This flag is meaningful only when es‐
105 tablishing a signal handler.
106
107 SA_RESETHAND
108 Restore the signal action to the default upon entry to the sig‐
109 nal handler. This flag is meaningful only when establishing a
110 signal handler.
111
112 SA_ONESHOT is an obsolete, nonstandard synonym for this flag.
113
114 SA_RESTART
115 Provide behavior compatible with BSD signal semantics by making
116 certain system calls restartable across signals. This flag is
117 meaningful only when establishing a signal handler. See sig‐
118 nal(7) for a discussion of system call restarting.
119
120 SA_RESTORER
121 Not intended for application use. This flag is used by C li‐
122 braries to indicate that the sa_restorer field contains the ad‐
123 dress of a "signal trampoline". See sigreturn(2) for more de‐
124 tails.
125
126 SA_SIGINFO (since Linux 2.2)
127 The signal handler takes three arguments, not one. In this
128 case, sa_sigaction should be set instead of sa_handler. This
129 flag is meaningful only when establishing a signal handler.
130
131 The siginfo_t argument to a SA_SIGINFO handler
132 When the SA_SIGINFO flag is specified in act.sa_flags, the signal han‐
133 dler address is passed via the act.sa_sigaction field. This handler
134 takes three arguments, as follows:
135
136 void
137 handler(int sig, siginfo_t *info, void *ucontext)
138 {
139 ...
140 }
141
142 These three arguments are as follows
143
144 sig The number of the signal that caused invocation of the handler.
145
146 info A pointer to a siginfo_t, which is a structure containing fur‐
147 ther information about the signal, as described below.
148
149 ucontext
150 This is a pointer to a ucontext_t structure, cast to void *.
151 The structure pointed to by this field contains signal context
152 information that was saved on the user-space stack by the ker‐
153 nel; for details, see sigreturn(2). Further information about
154 the ucontext_t structure can be found in getcontext(3) and sig‐
155 nal(7). Commonly, the handler function doesn't make any use of
156 the third argument.
157
158 The siginfo_t data type is a structure with the following fields:
159
160 siginfo_t {
161 int si_signo; /* Signal number */
162 int si_errno; /* An errno value */
163 int si_code; /* Signal code */
164 int si_trapno; /* Trap number that caused
165 hardware-generated signal
166 (unused on most architectures) */
167 pid_t si_pid; /* Sending process ID */
168 uid_t si_uid; /* Real user ID of sending process */
169 int si_status; /* Exit value or signal */
170 clock_t si_utime; /* User time consumed */
171 clock_t si_stime; /* System time consumed */
172 union sigval si_value; /* Signal value */
173 int si_int; /* POSIX.1b signal */
174 void *si_ptr; /* POSIX.1b signal */
175 int si_overrun; /* Timer overrun count;
176 POSIX.1b timers */
177 int si_timerid; /* Timer ID; POSIX.1b timers */
178 void *si_addr; /* Memory location which caused fault */
179 long si_band; /* Band event (was int in
180 glibc 2.3.2 and earlier) */
181 int si_fd; /* File descriptor */
182 short si_addr_lsb; /* Least significant bit of address
183 (since Linux 2.6.32) */
184 void *si_lower; /* Lower bound when address violation
185 occurred (since Linux 3.19) */
186 void *si_upper; /* Upper bound when address violation
187 occurred (since Linux 3.19) */
188 int si_pkey; /* Protection key on PTE that caused
189 fault (since Linux 4.6) */
190 void *si_call_addr; /* Address of system call instruction
191 (since Linux 3.5) */
192 int si_syscall; /* Number of attempted system call
193 (since Linux 3.5) */
194 unsigned int si_arch; /* Architecture of attempted system call
195 (since Linux 3.5) */
196 }
197
198 si_signo, si_errno and si_code are defined for all signals. (si_errno
199 is generally unused on Linux.) The rest of the struct may be a union,
200 so that one should read only the fields that are meaningful for the
201 given signal:
202
203 * Signals sent with kill(2) and sigqueue(3) fill in si_pid and si_uid.
204 In addition, signals sent with sigqueue(3) fill in si_int and si_ptr
205 with the values specified by the sender of the signal; see
206 sigqueue(3) for more details.
207
208 * Signals sent by POSIX.1b timers (since Linux 2.6) fill in si_overrun
209 and si_timerid. The si_timerid field is an internal ID used by the
210 kernel to identify the timer; it is not the same as the timer ID re‐
211 turned by timer_create(2). The si_overrun field is the timer overrun
212 count; this is the same information as is obtained by a call to
213 timer_getoverrun(2). These fields are nonstandard Linux extensions.
214
215 * Signals sent for message queue notification (see the description of
216 SIGEV_SIGNAL in mq_notify(3)) fill in si_int/si_ptr, with the
217 sigev_value supplied to mq_notify(3); si_pid, with the process ID of
218 the message sender; and si_uid, with the real user ID of the message
219 sender.
220
221 * SIGCHLD fills in si_pid, si_uid, si_status, si_utime, and si_stime,
222 providing information about the child. The si_pid field is the
223 process ID of the child; si_uid is the child's real user ID. The
224 si_status field contains the exit status of the child (if si_code is
225 CLD_EXITED), or the signal number that caused the process to change
226 state. The si_utime and si_stime contain the user and system CPU
227 time used by the child process; these fields do not include the times
228 used by waited-for children (unlike getrusage(2) and times(2)). In
229 kernels up to 2.6, and since 2.6.27, these fields report CPU time in
230 units of sysconf(_SC_CLK_TCK). In 2.6 kernels before 2.6.27, a bug
231 meant that these fields reported time in units of the (configurable)
232 system jiffy (see time(7)).
233
234 * SIGILL, SIGFPE, SIGSEGV, SIGBUS, and SIGTRAP fill in si_addr with the
235 address of the fault. On some architectures, these signals also fill
236 in the si_trapno field.
237
238 Some suberrors of SIGBUS, in particular BUS_MCEERR_AO and
239 BUS_MCEERR_AR, also fill in si_addr_lsb. This field indicates the
240 least significant bit of the reported address and therefore the ex‐
241 tent of the corruption. For example, if a full page was corrupted,
242 si_addr_lsb contains log2(sysconf(_SC_PAGESIZE)). When SIGTRAP is
243 delivered in response to a ptrace(2) event (PTRACE_EVENT_foo),
244 si_addr is not populated, but si_pid and si_uid are populated with
245 the respective process ID and user ID responsible for delivering the
246 trap. In the case of seccomp(2), the tracee will be shown as deliv‐
247 ering the event. BUS_MCEERR_* and si_addr_lsb are Linux-specific ex‐
248 tensions.
249
250 The SEGV_BNDERR suberror of SIGSEGV populates si_lower and si_upper.
251
252 The SEGV_PKUERR suberror of SIGSEGV populates si_pkey.
253
254 * SIGIO/SIGPOLL (the two names are synonyms on Linux) fills in si_band
255 and si_fd. The si_band event is a bit mask containing the same val‐
256 ues as are filled in the revents field by poll(2). The si_fd field
257 indicates the file descriptor for which the I/O event occurred; for
258 further details, see the description of F_SETSIG in fcntl(2).
259
260 * SIGSYS, generated (since Linux 3.5) when a seccomp filter returns
261 SECCOMP_RET_TRAP, fills in si_call_addr, si_syscall, si_arch, si_er‐
262 rno, and other fields as described in seccomp(2).
263
264 The si_code field
265 The si_code field inside the siginfo_t argument that is passed to a
266 SA_SIGINFO signal handler is a value (not a bit mask) indicating why
267 this signal was sent. For a ptrace(2) event, si_code will contain SIG‐
268 TRAP and have the ptrace event in the high byte:
269
270 (SIGTRAP | PTRACE_EVENT_foo << 8).
271
272 For a non-ptrace(2) event, the values that can appear in si_code are
273 described in the remainder of this section. Since glibc 2.20, the def‐
274 initions of most of these symbols are obtained from <signal.h> by
275 defining feature test macros (before including any header file) as fol‐
276 lows:
277
278 * _XOPEN_SOURCE with the value 500 or greater;
279
280 * _XOPEN_SOURCE and _XOPEN_SOURCE_EXTENDED; or
281
282 * _POSIX_C_SOURCE with the value 200809L or greater.
283
284 For the TRAP_* constants, the symbol definitions are provided only in
285 the first two cases. Before glibc 2.20, no feature test macros were
286 required to obtain these symbols.
287
288 For a regular signal, the following list shows the values which can be
289 placed in si_code for any signal, along with the reason that the signal
290 was generated.
291
292 SI_USER
293 kill(2).
294
295 SI_KERNEL
296 Sent by the kernel.
297
298 SI_QUEUE
299 sigqueue(3).
300
301 SI_TIMER
302 POSIX timer expired.
303
304 SI_MESGQ (since Linux 2.6.6)
305 POSIX message queue state changed; see mq_notify(3).
306
307 SI_ASYNCIO
308 AIO completed.
309
310 SI_SIGIO
311 Queued SIGIO (only in kernels up to Linux 2.2; from Linux
312 2.4 onward SIGIO/SIGPOLL fills in si_code as described be‐
313 low).
314
315 SI_TKILL (since Linux 2.4.19)
316 tkill(2) or tgkill(2).
317
318 The following values can be placed in si_code for a SIGILL signal:
319
320 ILL_ILLOPC
321 Illegal opcode.
322
323 ILL_ILLOPN
324 Illegal operand.
325
326 ILL_ILLADR
327 Illegal addressing mode.
328
329 ILL_ILLTRP
330 Illegal trap.
331
332 ILL_PRVOPC
333 Privileged opcode.
334
335 ILL_PRVREG
336 Privileged register.
337
338 ILL_COPROC
339 Coprocessor error.
340
341 ILL_BADSTK
342 Internal stack error.
343
344 The following values can be placed in si_code for a SIGFPE signal:
345
346 FPE_INTDIV
347 Integer divide by zero.
348
349 FPE_INTOVF
350 Integer overflow.
351
352 FPE_FLTDIV
353 Floating-point divide by zero.
354
355 FPE_FLTOVF
356 Floating-point overflow.
357
358 FPE_FLTUND
359 Floating-point underflow.
360
361 FPE_FLTRES
362 Floating-point inexact result.
363
364 FPE_FLTINV
365 Floating-point invalid operation.
366
367 FPE_FLTSUB
368 Subscript out of range.
369
370 The following values can be placed in si_code for a SIGSEGV signal:
371
372 SEGV_MAPERR
373 Address not mapped to object.
374
375 SEGV_ACCERR
376 Invalid permissions for mapped object.
377
378 SEGV_BNDERR (since Linux 3.19)
379 Failed address bound checks.
380
381 SEGV_PKUERR (since Linux 4.6)
382 Access was denied by memory protection keys. See pkeys(7).
383 The protection key which applied to this access is available
384 via si_pkey.
385
386 The following values can be placed in si_code for a SIGBUS signal:
387
388 BUS_ADRALN
389 Invalid address alignment.
390
391 BUS_ADRERR
392 Nonexistent physical address.
393
394 BUS_OBJERR
395 Object-specific hardware error.
396
397 BUS_MCEERR_AR (since Linux 2.6.32)
398 Hardware memory error consumed on a machine check; action
399 required.
400
401 BUS_MCEERR_AO (since Linux 2.6.32)
402 Hardware memory error detected in process but not consumed;
403 action optional.
404
405 The following values can be placed in si_code for a SIGTRAP signal:
406
407 TRAP_BRKPT
408 Process breakpoint.
409
410 TRAP_TRACE
411 Process trace trap.
412
413 TRAP_BRANCH (since Linux 2.4, IA64 only)
414 Process taken branch trap.
415
416 TRAP_HWBKPT (since Linux 2.4, IA64 only)
417 Hardware breakpoint/watchpoint.
418
419 The following values can be placed in si_code for a SIGCHLD signal:
420
421 CLD_EXITED
422 Child has exited.
423
424 CLD_KILLED
425 Child was killed.
426
427 CLD_DUMPED
428 Child terminated abnormally.
429
430 CLD_TRAPPED
431 Traced child has trapped.
432
433 CLD_STOPPED
434 Child has stopped.
435
436 CLD_CONTINUED (since Linux 2.6.9)
437 Stopped child has continued.
438
439 The following values can be placed in si_code for a SIGIO/SIGPOLL sig‐
440 nal:
441
442 POLL_IN
443 Data input available.
444
445 POLL_OUT
446 Output buffers available.
447
448 POLL_MSG
449 Input message available.
450
451 POLL_ERR
452 I/O error.
453
454 POLL_PRI
455 High priority input available.
456
457 POLL_HUP
458 Device disconnected.
459
460 The following value can be placed in si_code for a SIGSYS signal:
461
462 SYS_SECCOMP (since Linux 3.5)
463 Triggered by a seccomp(2) filter rule.
464
466 sigaction() returns 0 on success; on error, -1 is returned, and errno
467 is set to indicate the error.
468
470 EFAULT act or oldact points to memory which is not a valid part of the
471 process address space.
472
473 EINVAL An invalid signal was specified. This will also be generated if
474 an attempt is made to change the action for SIGKILL or SIGSTOP,
475 which cannot be caught or ignored.
476
478 POSIX.1-2001, POSIX.1-2008, SVr4.
479
481 A child created via fork(2) inherits a copy of its parent's signal dis‐
482 positions. During an execve(2), the dispositions of handled signals
483 are reset to the default; the dispositions of ignored signals are left
484 unchanged.
485
486 According to POSIX, the behavior of a process is undefined after it ig‐
487 nores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
488 kill(2) or raise(3). Integer division by zero has undefined result.
489 On some architectures it will generate a SIGFPE signal. (Also dividing
490 the most negative integer by -1 may generate SIGFPE.) Ignoring this
491 signal might lead to an endless loop.
492
493 POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN.
494 POSIX.1-2001 and later allow this possibility, so that ignoring SIGCHLD
495 can be used to prevent the creation of zombies (see wait(2)). Never‐
496 theless, the historical BSD and System V behaviors for ignoring SIGCHLD
497 differ, so that the only completely portable method of ensuring that
498 terminated children do not become zombies is to catch the SIGCHLD sig‐
499 nal and perform a wait(2) or similar.
500
501 POSIX.1-1990 specified only SA_NOCLDSTOP. POSIX.1-2001 added SA_NOCLD‐
502 STOP, SA_NOCLDWAIT, SA_NODEFER, SA_ONSTACK, SA_RESETHAND, SA_RESTART,
503 and SA_SIGINFO. Use of these latter values in sa_flags may be less
504 portable in applications intended for older UNIX implementations.
505
506 The SA_RESETHAND flag is compatible with the SVr4 flag of the same
507 name.
508
509 The SA_NODEFER flag is compatible with the SVr4 flag of the same name
510 under kernels 1.3.9 and later. On older kernels the Linux implementa‐
511 tion allowed the receipt of any signal, not just the one we are in‐
512 stalling (effectively overriding any sa_mask settings).
513
514 sigaction() can be called with a NULL second argument to query the cur‐
515 rent signal handler. It can also be used to check whether a given sig‐
516 nal is valid for the current machine by calling it with NULL second and
517 third arguments.
518
519 It is not possible to block SIGKILL or SIGSTOP (by specifying them in
520 sa_mask). Attempts to do so are silently ignored.
521
522 See sigsetops(3) for details on manipulating signal sets.
523
524 See signal-safety(7) for a list of the async-signal-safe functions that
525 can be safely called inside from inside a signal handler.
526
527 C library/kernel differences
528 The glibc wrapper function for sigaction() gives an error (EINVAL) on
529 attempts to change the disposition of the two real-time signals used
530 internally by the NPTL threading implementation. See nptl(7) for de‐
531 tails.
532
533 On architectures where the signal trampoline resides in the C library,
534 the glibc wrapper function for sigaction() places the address of the
535 trampoline code in the act.sa_restorer field and sets the SA_RESTORER
536 flag in the act.sa_flags field. See sigreturn(2).
537
538 The original Linux system call was named sigaction(). However, with
539 the addition of real-time signals in Linux 2.2, the fixed-size, 32-bit
540 sigset_t type supported by that system call was no longer fit for pur‐
541 pose. Consequently, a new system call, rt_sigaction(), was added to
542 support an enlarged sigset_t type. The new system call takes a fourth
543 argument, size_t sigsetsize, which specifies the size in bytes of the
544 signal sets in act.sa_mask and oldact.sa_mask. This argument is cur‐
545 rently required to have the value sizeof(sigset_t) (or the error EINVAL
546 results). The glibc sigaction() wrapper function hides these details
547 from us, transparently calling rt_sigaction() when the kernel provides
548 it.
549
550 Undocumented
551 Before the introduction of SA_SIGINFO, it was also possible to get some
552 additional information about the signal. This was done by providing an
553 sa_handler signal handler with a second argument of type struct sigcon‐
554 text, which is the same structure as the one that is passed in the
555 uc_mcontext field of the ucontext structure that is passed (via a
556 pointer) in the third argument of the sa_sigaction handler. See the
557 relevant Linux kernel sources for details. This use is obsolete now.
558
560 When delivering a signal with a SA_SIGINFO handler, the kernel does not
561 always provide meaningful values for all of the fields of the siginfo_t
562 that are relevant for that signal.
563
564 In kernels up to and including 2.6.13, specifying SA_NODEFER in
565 sa_flags prevents not only the delivered signal from being masked dur‐
566 ing execution of the handler, but also the signals specified in
567 sa_mask. This bug was fixed in kernel 2.6.14.
568
570 See mprotect(2).
571
573 kill(1), kill(2), pause(2), pidfd_send_signal(2), restart_syscall(2),
574 seccomp(2), sigaltstack(2), signal(2), signalfd(2), sigpending(2), sig‐
575 procmask(2), sigreturn(2), sigsuspend(2), wait(2), killpg(3), raise(3),
576 siginterrupt(3), sigqueue(3), sigsetops(3), sigvec(3), core(5), sig‐
577 nal(7)
578
580 This page is part of release 5.12 of the Linux man-pages project. A
581 description of the project, information about reporting bugs, and the
582 latest version of this page, can be found at
583 https://www.kernel.org/doc/man-pages/.
584
585
586
587Linux 2021-03-22 SIGACTION(2)