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 non-standard 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, non-standard 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 int si_band; /* Band event */
137 int si_fd; /* File descriptor */
138 }
139
140 si_signo, si_errno and si_code are defined for all signals. (si_errno
141 is generally unused on Linux.) The rest of the struct may be a union,
142 so that one should only read the fields that are meaningful for the
143 given signal:
144
145 * POSIX.1b signals and SIGCHLD fill in si_pid and si_uid.
146
147 * POSIX.1b timers (since Linux 2.6) fill in si_overrun and si_timerid.
148 The si_timerid field is an internal ID used by the kernel to identify
149 the timer; it is not the same as the timer ID returned by timer_cre‐
150 ate(2).
151
152 * SIGCHLD fills in si_status, si_utime and si_stime. The si_utime and
153 si_stime fields do not include the times used by waited for children
154 (unlike getrusage(2) and time(2). In kernels up to 2.6, and since
155 2.6.27, these fields report CPU time in units of
156 sysconf(_SC_CLK_TCK). In 2.6 kernels before 2.6.27, a bug meant that
157 these fields reported time in units of the (configurable) system
158 jiffy (see time(7)).
159
160
161 * si_int and si_ptr are specified by the sender of the POSIX.1b signal.
162 See sigqueue(2) for more details.
163
164 * SIGILL, SIGFPE, SIGSEGV, and SIGBUS fill in si_addr with the address
165 of the fault. SIGPOLL fills in si_band and si_fd.
166
167 si_code is a value (not a bit mask) indicating why this signal was
168 sent. The following list shows the values which can be placed in
169 si_code for any signal, along with reason that the signal was gener‐
170 ated.
171
172 SI_USER kill(2) or raise(3)
173
174 SI_KERNEL Sent by the kernel.
175
176 SI_QUEUE sigqueue(2)
177
178 SI_TIMER POSIX timer expired
179
180 SI_MESGQ POSIX message queue state changed (since Linux
181 2.6.6); see mq_notify(3)
182
183 SI_ASYNCIO AIO completed
184
185 SI_SIGIO queued SIGIO
186
187 SI_TKILL tkill(2) or tgkill(2) (since Linux 2.4.19)
188
189 The following values can be placed in si_code for a SIGILL signal:
190
191 ILL_ILLOPC illegal opcode
192
193 ILL_ILLOPN illegal operand
194
195 ILL_ILLADR illegal addressing mode
196
197 ILL_ILLTRP illegal trap
198
199 ILL_PRVOPC privileged opcode
200
201 ILL_PRVREG privileged register
202
203 ILL_COPROC coprocessor error
204
205 ILL_BADSTK internal stack error
206
207 The following values can be placed in si_code for a SIGFPE signal:
208
209 FPE_INTDIV integer divide by zero
210
211 FPE_INTOVF integer overflow
212
213 FPE_FLTDIV floating-point divide by zero
214
215 FPE_FLTOVF floating-point overflow
216
217 FPE_FLTUND floating-point underflow
218
219 FPE_FLTRES floating-point inexact result
220
221 FPE_FLTINV floating-point invalid operation
222
223 FPE_FLTSUB subscript out of range
224
225 The following values can be placed in si_code for a SIGSEGV signal:
226
227 SEGV_MAPERR address not mapped to object
228
229 SEGV_ACCERR invalid permissions for mapped object
230
231 The following values can be placed in si_code for a SIGBUS signal:
232
233 BUS_ADRALN invalid address alignment
234
235 BUS_ADRERR nonexistent physical address
236
237 BUS_OBJERR object-specific hardware error
238
239 The following values can be placed in si_code for a SIGTRAP signal:
240
241 TRAP_BRKPT process breakpoint
242
243 TRAP_TRACE process trace trap
244
245 The following values can be placed in si_code for a SIGCHLD signal:
246
247 CLD_EXITED child has exited
248
249 CLD_KILLED child was killed
250
251 CLD_DUMPED child terminated abnormally
252
253 CLD_TRAPPED traced child has trapped
254
255 CLD_STOPPED child has stopped
256
257 CLD_CONTINUED stopped child has continued (since Linux 2.6.9)
258
259 The following values can be placed in si_code for a SIGPOLL signal:
260
261 POLL_IN data input available
262
263 POLL_OUT output buffers available
264
265 POLL_MSG input message available
266
267 POLL_ERR i/o error
268
269 POLL_PRI high priority input available
270
271 POLL_HUP device disconnected
272
274 sigaction() returns 0 on success and -1 on error.
275
277 EFAULT act or oldact points to memory which is not a valid part of the
278 process address space.
279
280 EINVAL An invalid signal was specified. This will also be generated if
281 an attempt is made to change the action for SIGKILL or SIGSTOP,
282 which cannot be caught or ignored.
283
285 POSIX.1-2001, SVr4.
286
288 A child created via fork(2) inherits a copy of its parent's signal dis‐
289 positions. During an execve(2), the dispositions of handled signals
290 are reset to the default; the dispositions of ignored signals are left
291 unchanged.
292
293 According to POSIX, the behavior of a process is undefined after it
294 ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
295 kill(2) or raise(3). Integer division by zero has undefined result.
296 On some architectures it will generate a SIGFPE signal. (Also dividing
297 the most negative integer by -1 may generate SIGFPE.) Ignoring this
298 signal might lead to an endless loop.
299
300 POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN.
301 POSIX.1-2001 allows this possibility, so that ignoring SIGCHLD can be
302 used to prevent the creation of zombies (see wait(2)). Nevertheless,
303 the historical BSD and System V behaviors for ignoring SIGCHLD differ,
304 so that the only completely portable method of ensuring that terminated
305 children do not become zombies is to catch the SIGCHLD signal and per‐
306 form a wait(2) or similar.
307
308 POSIX.1-1990 only specified SA_NOCLDSTOP. POSIX.1-2001 added SA_NOCLD‐
309 WAIT, SA_RESETHAND, SA_NODEFER, and SA_SIGINFO. Use of these latter
310 values in sa_flags may be less portable in applications intended for
311 older Unix implementations.
312
313 The SA_RESETHAND flag is compatible with the SVr4 flag of the same
314 name.
315
316 The SA_NODEFER flag is compatible with the SVr4 flag of the same name
317 under kernels 1.3.9 and newer. On older kernels the Linux implementa‐
318 tion allowed the receipt of any signal, not just the one we are
319 installing (effectively overriding any sa_mask settings).
320
321 sigaction() can be called with a null second argument to query the cur‐
322 rent signal handler. It can also be used to check whether a given sig‐
323 nal is valid for the current machine by calling it with null second and
324 third arguments.
325
326 It is not possible to block SIGKILL or SIGSTOP (by specifying them in
327 sa_mask). Attempts to do so are silently ignored.
328
329 See sigsetops(3) for details on manipulating signal sets.
330
331 See signal(7) for a list of the async-signal-safe functions that can be
332 safely called inside from inside a signal handler.
333
334 Undocumented
335 Before the introduction of SA_SIGINFO it was also possible to get some
336 additional information, namely by using a sa_handler with second argu‐
337 ment of type struct sigcontext. See the relevant kernel sources for
338 details. This use is obsolete now.
339
341 In kernels up to and including 2.6.13, specifying SA_NODEFER in
342 sa_flags prevents not only the delivered signal from being masked dur‐
343 ing execution of the handler, but also the signals specified in
344 sa_mask. This bug was fixed in kernel 2.6.14.
345
347 See mprotect(2).
348
350 kill(1), kill(2), killpg(2), pause(2), sigaltstack(2), signal(2), sig‐
351 nalfd(2), sigpending(2), sigprocmask(2), sigqueue(2), sigsuspend(2),
352 wait(2), raise(3), siginterrupt(3), sigsetops(3), sigvec(3), core(5),
353 signal(7)
354
356 This page is part of release 3.22 of the Linux man-pages project. A
357 description of the project, and information about reporting bugs, can
358 be found at http://www.kernel.org/doc/man-pages/.
359
360
361
362Linux 2009-07-25 SIGACTION(2)