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