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, struct sigaction
12 *oldact);
13
15 The sigaction() system call is used to change the action taken by a
16 process on receipt of a specific signal.
17
18 signum specifies the signal and can be any valid signal except SIGKILL
19 and SIGSTOP.
20
21 If act is non-null, the new action for signal signum is installed from
22 act. If oldact is non-null, the previous action is saved in oldact.
23
24 The sigaction structure is defined as something like
25
26 struct sigaction {
27 void (*sa_handler)(int);
28 void (*sa_sigaction)(int, siginfo_t *, void *);
29 sigset_t sa_mask;
30 int sa_flags;
31 void (*sa_restorer)(void);
32 }
33
34 On some architectures a union is involved: do not assign to both
35 sa_handler and sa_sigaction.
36
37 The sa_restorer element is obsolete and should not be used. POSIX does
38 not specify a sa_restorer element.
39
40 sa_handler specifies the action to be associated with signum and may be
41 SIG_DFL for the default action, SIG_IGN to ignore this signal, or a
42 pointer to a signal handling function. This function receives the sig‐
43 nal number as its only argument.
44
45 If SA_SIGINFO is specified in sa_flags, then sa_sigaction (instead of
46 sa_handler) specifies the signal-handling function for signum. This
47 function receives the signal number as its first argument, a pointer to
48 a siginfo_t as its second argument and a pointer to a ucontext_t (cast
49 to void *) as its third argument.
50
51 sa_mask gives a mask of signals which should be blocked during execu‐
52 tion of the signal handler. In addition, the signal which triggered
53 the handler will be blocked, unless the SA_NODEFER flag is used.
54
55 sa_flags specifies a set of flags which modify the behaviour of the
56 signal handling process. It is formed by the bitwise OR of zero or more
57 of the following:
58
59 SA_NOCLDSTOP
60 If signum is SIGCHLD, do not receive notification when
61 child processes stop (i.e., when they receive one of
62 SIGSTOP, SIGTSTP, SIGTTIN or SIGTTOU) or resume (i.e.,
63 they receive SIGCONT) (see wait(2)).
64
65 SA_NOCLDWAIT
66 (Linux 2.6 and later) If signum is SIGCHLD, do not trans‐
67 form children into zombies when they terminate. See also
68 waitpid(2).
69
70 SA_RESETHAND
71 Restore the signal action to the default state once the
72 signal handler has been called. SA_ONESHOT is an obso‐
73 lete, non-standard synonym for this flag.
74
75 SA_ONSTACK
76 Call the signal handler on an alternate signal stack pro‐
77 vided by sigaltstack(2). If an alternate stack is not
78 available, the default stack will be used.
79
80 SA_RESTART
81 Provide behaviour compatible with BSD signal semantics by
82 making certain system calls restartable across signals.
83
84 SA_NODEFER
85 Do not prevent the signal from being received from within
86 its own signal handler. SA_NOMASK is an obsolete, non-
87 standard synonym for this flag.
88
89 SA_SIGINFO
90 The signal handler takes 3 arguments, not one. In this
91 case, sa_sigaction should be set instead of sa_handler.
92 (The sa_sigaction field was added in Linux 2.1.86.)
93
94 The siginfo_t parameter to sa_sigaction is a struct with the following
95 elements
96
97 siginfo_t {
98 int si_signo; /* Signal number */
99 int si_errno; /* An errno value */
100 int si_code; /* Signal code */
101 pid_t si_pid; /* Sending process ID */
102 uid_t si_uid; /* Real user ID of sending process */
103 int si_status; /* Exit value or signal */
104 clock_t si_utime; /* User time consumed */
105 clock_t si_stime; /* System time consumed */
106 sigval_t si_value; /* Signal value */
107 int si_int; /* POSIX.1b signal */
108 void * si_ptr; /* POSIX.1b signal */
109 void * si_addr; /* Memory location which caused fault */
110 int si_band; /* Band event */
111 int si_fd; /* File descriptor */
112 }
113
114 si_signo, si_errno and si_code are defined for all signals. (si_signo
115 is unused on Linux.) The rest of the struct may be a union, so that
116 one should only read the fields that are meaningful for the given sig‐
117 nal. POSIX.1b signals and SIGCHLD fill in si_pid and si_uid. SIGCHLD
118 also fills in si_status, si_utime and si_stime. si_int and si_ptr are
119 specified by the sender of the POSIX.1b signal. SIGILL, SIGFPE,
120 SIGSEGV, and SIGBUS fill in si_addr with the address of the fault.
121 SIGPOLL fills in si_band and si_fd.
122
123 si_code indicates why this signal was sent. It is a value, not a bit‐
124 mask. The values which are possible for any signal are listed in this
125 table:
126
127 ┌───────────────────────────────────────────────────────────────────┐
128 │ si_code │
129 ├───────────┬───────────────────────────────────────────────────────┤
130 │Value │ Signal origin │
131 ├───────────┼───────────────────────────────────────────────────────┤
132 │SI_USER │ kill(), sigsend(), or raise() │
133 ├───────────┼───────────────────────────────────────────────────────┤
134 │SI_KERNEL │ The kernel │
135 ├───────────┼───────────────────────────────────────────────────────┤
136 │SI_QUEUE │ sigqueue() │
137 ├───────────┼───────────────────────────────────────────────────────┤
138 │SI_TIMER │ POSIX timer expired │
139 ├───────────┼───────────────────────────────────────────────────────┤
140 │SI_MESGQ │ POSIX message queue state changed (since Linux 2.6.6) │
141 ├───────────┼───────────────────────────────────────────────────────┤
142 │SI_ASYNCIO │ AIO completed │
143 ├───────────┼───────────────────────────────────────────────────────┤
144 │SI_SIGIO │ queued SIGIO │
145 ├───────────┼───────────────────────────────────────────────────────┤
146 │SI_TKILL │ tkill() or tgkill() (since Linux 2.4.19) │
147 └───────────┴───────────────────────────────────────────────────────┘
148
149 ┌─────────────────────────────────────┐
150 │ SIGILL │
151 ├───────────┬─────────────────────────┤
152 │ILL_ILLOPC │ illegal opcode │
153 ├───────────┼─────────────────────────┤
154 │ILL_ILLOPN │ illegal operand │
155 ├───────────┼─────────────────────────┤
156 │ILL_ILLADR │ illegal addressing mode │
157 ├───────────┼─────────────────────────┤
158 │ILL_ILLTRP │ illegal trap │
159 ├───────────┼─────────────────────────┤
160 │ILL_PRVOPC │ privileged opcode │
161 ├───────────┼─────────────────────────┤
162 │ILL_PRVREG │ privileged register │
163 ├───────────┼─────────────────────────┤
164 │ILL_COPROC │ coprocessor error │
165 ├───────────┼─────────────────────────┤
166 │ILL_BADSTK │ internal stack error │
167 └───────────┴─────────────────────────┘
168
169 ┌──────────────────────────────────────────────┐
170 │ SIGFPE │
171 ├───────────┬──────────────────────────────────┤
172 │FPE_INTDIV │ integer divide by zero │
173 ├───────────┼──────────────────────────────────┤
174 │FPE_INTOVF │ integer overflow │
175 ├───────────┼──────────────────────────────────┤
176 │FPE_FLTDIV │ floating point divide by zero │
177 ├───────────┼──────────────────────────────────┤
178 │FPE_FLTOVF │ floating point overflow │
179 ├───────────┼──────────────────────────────────┤
180 │FPE_FLTUND │ floating point underflow │
181 ├───────────┼──────────────────────────────────┤
182 │FPE_FLTRES │ floating point inexact result │
183 ├───────────┼──────────────────────────────────┤
184 │FPE_FLTINV │ floating point invalid operation │
185 ├───────────┼──────────────────────────────────┤
186 │FPE_FLTSUB │ subscript out of range │
187 └───────────┴──────────────────────────────────┘
188
189 ┌────────────────────────────────────────────────────┐
190 │ SIGSEGV │
191 ├────────────┬───────────────────────────────────────┤
192 │SEGV_MAPERR │ address not mapped to object │
193 ├────────────┼───────────────────────────────────────┤
194 │SEGV_ACCERR │ invalid permissions for mapped object │
195 └────────────┴───────────────────────────────────────┘
196
197 ┌────────────────────────────────────────────┐
198 │ SIGBUS │
199 ├───────────┬────────────────────────────────┤
200 │BUS_ADRALN │ invalid address alignment │
201 ├───────────┼────────────────────────────────┤
202 │BUS_ADRERR │ non-existent physical address │
203 ├───────────┼────────────────────────────────┤
204 │BUS_OBJERR │ object specific hardware error │
205 └───────────┴────────────────────────────────┘
206
207 ┌────────────────────────────────┐
208 │ SIGTRAP │
209 ├───────────┬────────────────────┤
210 │TRAP_BRKPT │ process breakpoint │
211 ├───────────┼────────────────────┤
212 │TRAP_TRACE │ process trace trap │
213 └───────────┴────────────────────┘
214
215 ┌────────────────────────────────────────────────────────────────┐
216 │ SIGCHLD │
217 ├──────────────┬─────────────────────────────────────────────────┤
218 │CLD_EXITED │ child has exited │
219 ├──────────────┼─────────────────────────────────────────────────┤
220 │CLD_KILLED │ child was killed │
221 ├──────────────┼─────────────────────────────────────────────────┤
222 │CLD_DUMPED │ child terminated abnormally │
223 ├──────────────┼─────────────────────────────────────────────────┤
224 │CLD_TRAPPED │ traced child has trapped │
225 ├──────────────┼─────────────────────────────────────────────────┤
226 │CLD_STOPPED │ child has stopped │
227 ├──────────────┼─────────────────────────────────────────────────┤
228 │CLD_CONTINUED │ stopped child has continued (since Linux 2.6.9) │
229 └──────────────┴─────────────────────────────────────────────────┘
230
231 ┌─────────────────────────────────────────┐
232 │ SIGPOLL │
233 ├─────────┬───────────────────────────────┤
234 │POLL_IN │ data input available │
235 ├─────────┼───────────────────────────────┤
236 │POLL_OUT │ output buffers available │
237 ├─────────┼───────────────────────────────┤
238 │POLL_MSG │ input message available │
239 ├─────────┼───────────────────────────────┤
240 │POLL_ERR │ i/o error │
241 ├─────────┼───────────────────────────────┤
242 │POLL_PRI │ high priority input available │
243 ├─────────┼───────────────────────────────┤
244 │POLL_HUP │ device disconnected │
245 └─────────┴───────────────────────────────┘
246
248 sigaction() returns 0 on success and -1 on error.
249
251 EFAULT act or oldact points to memory which is not a valid part of the
252 process address space.
253
254 EINVAL An invalid signal was specified. This will also be generated if
255 an attempt is made to change the action for SIGKILL or SIGSTOP,
256 which cannot be caught or ignored.
257
259 According to POSIX, the behaviour of a process is undefined after it
260 ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
261 kill() or raise(). Integer division by zero has undefined result. On
262 some architectures it will generate a SIGFPE signal. (Also dividing
263 the most negative integer by -1 may generate SIGFPE.) Ignoring this
264 signal might lead to an endless loop.
265
266 POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN.
267 POSIX.1-2001 allows this possibility, so that ignoring SIGCHLD can be
268 used to prevent the creation of zombies (see wait(2)). Nevertheless,
269 the historical BSD and System V behaviours for ignoring SIGCHLD differ,
270 so that the only completely portable method of ensuring that terminated
271 children do not become zombies is to catch the SIGCHLD signal and per‐
272 form a wait(2) or similar.
273
274 POSIX.1-1990 only specified SA_NOCLDSTOP. POSIX.1-2001 added SA_NOCLD‐
275 WAIT, SA_RESETHAND, SA_NODEFER, and SA_SIGINFO. Use of these latter
276 values in sa_flags may be less portable in applications intended for
277 older Unix implementations.
278
279 Support for SA_SIGINFO was added in Linux 2.2.
280
281 The SA_RESETHAND flag is compatible with the SVr4 flag of the same
282 name.
283
284 The SA_NODEFER flag is compatible with the SVr4 flag of the same name
285 under kernels 1.3.9 and newer. On older kernels the Linux implementa‐
286 tion allowed the receipt of any signal, not just the one we are
287 installing (effectively overriding any sa_mask settings).
288
289 sigaction() can be called with a null second argument to query the cur‐
290 rent signal handler. It can also be used to check whether a given sig‐
291 nal is valid for the current machine by calling it with null second and
292 third arguments.
293
294 It is not possible to block SIGKILL or SIGSTOP (by specifying them in
295 sa_mask). Attempts to do so are silently ignored.
296
297 See sigsetops(3) for details on manipulating signal sets.
298
300 In kernels up to and including 2.6.13, specifying SA_NODEFER in
301 sa_flags preventing not only the delivered signal from being masked
302 during execution of the handler, but also the signals specified in
303 sa_mask. This bug is was fixed in kernel 2.6.14.
304
306 POSIX.1-2001, SVr4.
307
309 Before the introduction of SA_SIGINFO it was also possible to get some
310 additional information, namely by using a sa_handler with second argu‐
311 ment of type struct sigcontext. See the relevant kernel sources for
312 details. This use is obsolete now.
313
315 kill(1), kill(2), pause(2), sigaltstack(2), signal(2), sigpending(2),
316 sigprocmask(2), sigqueue(2), sigsuspend(2), wait(2), killpg(3),
317 raise(3), siginterrupt(3), sigsetops(3), sigvec(3), core(5), signal(7)
318
319
320
321Linux 2.6.14 2005-09-15 SIGACTION(2)