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