1SIGACTION(2) Linux Programmer's Manual SIGACTION(2)
2
3
4
6 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 >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
17
19 The sigaction() system call is used to change the action taken by a
20 process on receipt of a specific signal. (See signal(7) for an over‐
21 view of signals.)
22
23 signum specifies the signal and can be any valid signal except SIGKILL
24 and SIGSTOP.
25
26 If act is non-NULL, the new action for signal signum is installed from
27 act. If oldact is non-NULL, the previous action is saved in oldact.
28
29 The sigaction structure is defined as something like:
30
31 struct sigaction {
32 void (*sa_handler)(int);
33 void (*sa_sigaction)(int, siginfo_t *, void *);
34 sigset_t sa_mask;
35 int sa_flags;
36 void (*sa_restorer)(void);
37 };
38
39 On some architectures a union is involved: do not assign to both
40 sa_handler and sa_sigaction.
41
42 The sa_restorer element is obsolete and should not be used. POSIX does
43 not specify a sa_restorer element.
44
45 sa_handler specifies the action to be associated with signum and may be
46 SIG_DFL for the default action, SIG_IGN to ignore this signal, or a
47 pointer to a signal handling function. This function receives the sig‐
48 nal number as its only argument.
49
50 If SA_SIGINFO is specified in sa_flags, then sa_sigaction (instead of
51 sa_handler) specifies the signal-handling function for signum. This
52 function receives the signal number as its first argument, a pointer to
53 a siginfo_t as its second argument and a pointer to a ucontext_t (cast
54 to void *) as its third argument.
55
56 sa_mask specifies a mask of signals which should be blocked (i.e.,
57 added to the signal mask of the thread in which the signal handler is
58 invoked) during execution of the signal handler. In addition, the sig‐
59 nal which triggered the handler will be blocked, unless the SA_NODEFER
60 flag is used.
61
62 sa_flags specifies a set of flags which modify the behavior of the sig‐
63 nal. It is formed by the bitwise OR of zero or more of the following:
64
65 SA_NOCLDSTOP
66 If signum is SIGCHLD, do not receive notification when child
67 processes stop (i.e., when they receive one of SIGSTOP,
68 SIGTSTP, SIGTTIN or SIGTTOU) or resume (i.e., they receive
69 SIGCONT) (see wait(2)). This flag is only meaningful when
70 establishing a handler for SIGCHLD.
71
72 SA_NOCLDWAIT (since Linux 2.6)
73 If signum is SIGCHLD, do not transform children into zombies
74 when they terminate. See also waitpid(2). This flag is
75 only meaningful when establishing a handler for SIGCHLD, or
76 when setting that signal's disposition to SIG_DFL.
77
78 If the SA_NOCLDWAIT flag is set when establishing a handler
79 for SIGCHLD, POSIX.1 leaves it unspecified whether a SIGCHLD
80 signal is generated when a child process terminates. On
81 Linux, a SIGCHLD signal is generated in this case; on some
82 other implementations, it is not.
83
84 SA_NODEFER
85 Do not prevent the signal from being received from within
86 its own signal handler. This flag is only meaningful when
87 establishing a signal handler. SA_NOMASK is an obsolete,
88 nonstandard synonym for this flag.
89
90 SA_ONSTACK
91 Call the signal handler on an alternate signal stack pro‐
92 vided by sigaltstack(2). If an alternate stack is not
93 available, the default stack will be used. This flag is
94 only meaningful when establishing a signal handler.
95
96 SA_RESETHAND
97 Restore the signal action to the default state once the sig‐
98 nal handler has been called. This flag is only meaningful
99 when establishing a signal handler. SA_ONESHOT is an obso‐
100 lete, nonstandard synonym for this flag.
101
102 SA_RESTART
103 Provide behavior compatible with BSD signal semantics by
104 making certain system calls restartable across signals.
105 This flag is only meaningful when establishing a signal han‐
106 dler. See signal(7) for a discussion of system call
107 restarting.
108
109 SA_SIGINFO (since Linux 2.2)
110 The signal handler takes 3 arguments, not one. In this
111 case, sa_sigaction should be set instead of sa_handler.
112 This flag is only meaningful when establishing a signal han‐
113 dler.
114
115 The siginfo_t argument to sa_sigaction is a struct with the following
116 elements:
117
118 siginfo_t {
119 int si_signo; /* Signal number */
120 int si_errno; /* An errno value */
121 int si_code; /* Signal code */
122 int si_trapno; /* Trap number that caused
123 hardware-generated signal
124 (unused on most architectures) */
125 pid_t si_pid; /* Sending process ID */
126 uid_t si_uid; /* Real user ID of sending process */
127 int si_status; /* Exit value or signal */
128 clock_t si_utime; /* User time consumed */
129 clock_t si_stime; /* System time consumed */
130 sigval_t si_value; /* Signal value */
131 int si_int; /* POSIX.1b signal */
132 void *si_ptr; /* POSIX.1b signal */
133 int si_overrun; /* Timer overrun count; POSIX.1b timers */
134 int si_timerid; /* Timer ID; POSIX.1b timers */
135 void *si_addr; /* Memory location which caused fault */
136 long si_band; /* Band event (was int in
137 glibc 2.3.2 and earlier) */
138 int si_fd; /* File descriptor */
139 short si_addr_lsb; /* Least significant bit of address
140 (since kernel 2.6.32) */
141 }
142
143 si_signo, si_errno and si_code are defined for all signals. (si_errno
144 is generally unused on Linux.) The rest of the struct may be a union,
145 so that one should only read the fields that are meaningful for the
146 given signal:
147
148 * Signals sent with kill(2) and sigqueue(2) fill in si_pid and si_uid.
149 In addition, signals sent with sigqueue(2) fill in si_int and si_ptr
150 with the values specified by the sender the signal; see sigqueue(2)
151 for more details.
152
153 * Signals sent by POSIX.1b timers (since Linux 2.6) fill in si_overrun
154 and si_timerid. The si_timerid field is an internal ID used by the
155 kernel to identify the timer; it is not the same as the timer ID
156 returned by timer_create(2). The si_overrun field is the timer over‐
157 run count; this is the same information as is obtained by a call to
158 timer_getoverrun(2). These fields are nonstandard Linux extensions.
159
160 * Signals sent for message queue notification (see the description of
161 SIGEV_SIGNAL in mq_notify(3)) fill in si_int/si_ptr, with the
162 sigev_value supplied to mq_notify(3); si_pid, with the process ID of
163 the message sender; and si_uid, with the real user ID of the message
164 sender.
165
166 * SIGCHLD fills in si_pid, si_uid, si_status, si_utime and si_stime,
167 providing information about the child. The si_pid field is the
168 process ID of the child; si_uid is the child's real user ID. The
169 si_status field contains the exit status of the child (if si_code is
170 CLD_EXITED), or the signal number that caused the process to change
171 state. The si_utime and si_stime contain the user and system CPU
172 time used by the child process; these fields do not include the times
173 used by waited-for children (unlike getrusage(2) and time(2)). In
174 kernels up to 2.6, and since 2.6.27, these fields report CPU time in
175 units of sysconf(_SC_CLK_TCK). In 2.6 kernels before 2.6.27, a bug
176 meant that these fields reported time in units of the (configurable)
177 system jiffy (see time(7)).
178
179 * SIGILL, SIGFPE, SIGSEGV, SIGBUS, and SIGTRAP fill in si_addr with the
180 address of the fault. On some architectures, these signals also fill
181 in the si_trapno filed. Some suberrors of SIGBUS, in particular
182 BUS_MCEERR_AO and BUS_MCEERR_AR, also fill in si_addr_lsb. This
183 field indicates the least significant bit of the reported address and
184 therefore the extent of the corruption. For example, if a full page
185 was corrupted, si_addr_lsb contains log2(sysconf(_SC_PAGESIZE)).
186 BUS_MCERR_* and si_addr_lsb are Linux-specific extensions.
187
188 * SIGPOLL/SIGIO fills in si_band and si_fd. The si_band event is a bit
189 mask containing the same values as are filled in the revents field by
190 poll(2). The si_fd field indicates the file descriptor for which the
191 I/O event occurred.
192
193 si_code is a value (not a bit mask) indicating why this signal was
194 sent. The following list shows the values which can be placed in
195 si_code for any signal, along with reason that the signal was gener‐
196 ated.
197
198 SI_USER kill(2) or raise(3)
199
200 SI_KERNEL Sent by the kernel.
201
202 SI_QUEUE sigqueue(2)
203
204 SI_TIMER POSIX timer expired
205
206 SI_MESGQ POSIX message queue state changed (since Linux
207 2.6.6); see mq_notify(3)
208
209 SI_ASYNCIO AIO completed
210
211 SI_SIGIO queued SIGIO
212
213 SI_TKILL tkill(2) or tgkill(2) (since Linux 2.4.19)
214
215 The following values can be placed in si_code for a SIGILL signal:
216
217 ILL_ILLOPC illegal opcode
218
219 ILL_ILLOPN illegal operand
220
221 ILL_ILLADR illegal addressing mode
222
223 ILL_ILLTRP illegal trap
224
225 ILL_PRVOPC privileged opcode
226
227 ILL_PRVREG privileged register
228
229 ILL_COPROC coprocessor error
230
231 ILL_BADSTK internal stack error
232
233 The following values can be placed in si_code for a SIGFPE signal:
234
235 FPE_INTDIV integer divide by zero
236
237 FPE_INTOVF integer overflow
238
239 FPE_FLTDIV floating-point divide by zero
240
241 FPE_FLTOVF floating-point overflow
242
243 FPE_FLTUND floating-point underflow
244
245 FPE_FLTRES floating-point inexact result
246
247 FPE_FLTINV floating-point invalid operation
248
249 FPE_FLTSUB subscript out of range
250
251 The following values can be placed in si_code for a SIGSEGV signal:
252
253 SEGV_MAPERR address not mapped to object
254
255 SEGV_ACCERR invalid permissions for mapped object
256
257 The following values can be placed in si_code for a SIGBUS signal:
258
259 BUS_ADRALN invalid address alignment
260
261 BUS_ADRERR nonexistent physical address
262
263 BUS_OBJERR object-specific hardware error
264
265 BUS_MCEERR_AR (since Linux 2.6.32)
266 Hardware memory error consumed on a machine check;
267 action required.
268
269 BUS_MCEERR_AO (since Linux 2.6.32)
270 Hardware memory error detected in process but not
271 consumed; action optional.
272
273 The following values can be placed in si_code for a SIGTRAP signal:
274
275 TRAP_BRKPT process breakpoint
276
277 TRAP_TRACE process trace trap
278
279 TRAP_BRANCH (since Linux 2.4)
280 process taken branch trap
281
282 TRAP_HWBKPT (since Linux 2.4)
283 hardware breakpoint/watchpoint
284
285 The following values can be placed in si_code for a SIGCHLD signal:
286
287 CLD_EXITED child has exited
288
289 CLD_KILLED child was killed
290
291 CLD_DUMPED child terminated abnormally
292
293 CLD_TRAPPED traced child has trapped
294
295 CLD_STOPPED child has stopped
296
297 CLD_CONTINUED stopped child has continued (since Linux 2.6.9)
298
299 The following values can be placed in si_code for a SIGPOLL signal:
300
301 POLL_IN data input available
302
303 POLL_OUT output buffers available
304
305 POLL_MSG input message available
306
307 POLL_ERR I/O error
308
309 POLL_PRI high priority input available
310
311 POLL_HUP device disconnected
312
314 sigaction() returns 0 on success and -1 on error.
315
317 EFAULT act or oldact points to memory which is not a valid part of the
318 process address space.
319
320 EINVAL An invalid signal was specified. This will also be generated if
321 an attempt is made to change the action for SIGKILL or SIGSTOP,
322 which cannot be caught or ignored.
323
325 POSIX.1-2001, SVr4.
326
328 A child created via fork(2) inherits a copy of its parent's signal dis‐
329 positions. During an execve(2), the dispositions of handled signals
330 are reset to the default; the dispositions of ignored signals are left
331 unchanged.
332
333 According to POSIX, the behavior of a process is undefined after it
334 ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
335 kill(2) or raise(3). Integer division by zero has undefined result.
336 On some architectures it will generate a SIGFPE signal. (Also dividing
337 the most negative integer by -1 may generate SIGFPE.) Ignoring this
338 signal might lead to an endless loop.
339
340 POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN.
341 POSIX.1-2001 allows this possibility, so that ignoring SIGCHLD can be
342 used to prevent the creation of zombies (see wait(2)). Nevertheless,
343 the historical BSD and System V behaviors for ignoring SIGCHLD differ,
344 so that the only completely portable method of ensuring that terminated
345 children do not become zombies is to catch the SIGCHLD signal and per‐
346 form a wait(2) or similar.
347
348 POSIX.1-1990 only specified SA_NOCLDSTOP. POSIX.1-2001 added SA_NOCLD‐
349 WAIT, SA_RESETHAND, SA_NODEFER, and SA_SIGINFO. Use of these latter
350 values in sa_flags may be less portable in applications intended for
351 older Unix implementations.
352
353 The SA_RESETHAND flag is compatible with the SVr4 flag of the same
354 name.
355
356 The SA_NODEFER flag is compatible with the SVr4 flag of the same name
357 under kernels 1.3.9 and newer. On older kernels the Linux implementa‐
358 tion allowed the receipt of any signal, not just the one we are
359 installing (effectively overriding any sa_mask settings).
360
361 sigaction() can be called with a null second argument to query the cur‐
362 rent signal handler. It can also be used to check whether a given sig‐
363 nal is valid for the current machine by calling it with null second and
364 third arguments.
365
366 It is not possible to block SIGKILL or SIGSTOP (by specifying them in
367 sa_mask). Attempts to do so are silently ignored.
368
369 See sigsetops(3) for details on manipulating signal sets.
370
371 See signal(7) for a list of the async-signal-safe functions that can be
372 safely called inside from inside a signal handler.
373
374 Undocumented
375 Before the introduction of SA_SIGINFO it was also possible to get some
376 additional information, namely by using a sa_handler with second argu‐
377 ment of type struct sigcontext. See the relevant kernel sources for
378 details. This use is obsolete now.
379
381 In kernels up to and including 2.6.13, specifying SA_NODEFER in
382 sa_flags prevents not only the delivered signal from being masked dur‐
383 ing execution of the handler, but also the signals specified in
384 sa_mask. This bug was fixed in kernel 2.6.14.
385
387 See mprotect(2).
388
390 kill(1), kill(2), killpg(2), pause(2), sigaltstack(2), signal(2), sig‐
391 nalfd(2), sigpending(2), sigprocmask(2), sigqueue(2), sigsuspend(2),
392 wait(2), raise(3), siginterrupt(3), sigsetops(3), sigvec(3), core(5),
393 signal(7)
394
396 This page is part of release 3.25 of the Linux man-pages project. A
397 description of the project, and information about reporting bugs, can
398 be found at http://www.kernel.org/doc/man-pages/.
399
400
401
402Linux 2010-06-16 SIGACTION(2)