]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/sigaction.2
s/parameter/argument/ when talking about the things given
[thirdparty/man-pages.git] / man2 / sigaction.2
1 '\" t
2 .\" Copyright (c) 1994,1995 Mike Battersby <mib@deakin.edu.au>
3 .\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" based on work by faith@cs.unc.edu
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\" Modified, aeb, 960424
27 .\" Modified Fri Jan 31 17:31:20 1997 by Eric S. Raymond <esr@thyrsus.com>
28 .\" Modified Thu Nov 26 02:12:45 1998 by aeb - add SIGCHLD stuff.
29 .\" Modified Sat May 8 17:40:19 1999 by Matthew Wilcox
30 .\" add POSIX.1b signals
31 .\" Modified Sat Dec 29 01:44:52 2001 by Evan Jones <ejones@uwaterloo.ca>
32 .\" SA_ONSTACK
33 .\" Modified 2004-11-11 by Michael Kerrisk <mtk.manpages@gmail.com>
34 .\" Added mention of SIGCONT under SA_NOCLDSTOP
35 .\" Added SA_NOCLDWAIT
36 .\" Modified 2004-11-17 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" Updated discussion for POSIX.1-2001 and SIGCHLD and sa_flags.
38 .\" Formatting fixes
39 .\" 2004-12-09, mtk, added SI_TKILL + other minor changes
40 .\" 2005-09-15, mtk, split sigpending(), sigprocmask(), sigsuspend()
41 .\" out of this page into separate pages.
42 .\"
43 .TH SIGACTION 2 2008-07-08 "Linux" "Linux Programmer's Manual"
44 .SH NAME
45 sigaction \- examine and change a signal action
46 .SH SYNOPSIS
47 .nf
48 .B #include <signal.h>
49 .sp
50 .BI "int sigaction(int " signum ", const struct sigaction *" act ,
51 .BI " struct sigaction *" oldact );
52 .fi
53 .SH DESCRIPTION
54 The
55 .BR sigaction ()
56 system call is used to change the action taken by a process on
57 receipt of a specific signal.
58 .PP
59 .I signum
60 specifies the signal and can be any valid signal except
61 .B SIGKILL
62 and
63 .BR SIGSTOP .
64 .PP
65 If
66 .I act
67 is non-null, the new action for signal
68 .I signum
69 is installed from
70 .IR act .
71 If
72 .I oldact
73 is non-null, the previous action is saved in
74 .IR oldact .
75 .PP
76 The
77 .I sigaction
78 structure is defined as something like:
79 .sp
80 .in +4n
81 .nf
82 struct sigaction {
83 void (*sa_handler)(int);
84 void (*sa_sigaction)(int, siginfo_t *, void *);
85 sigset_t sa_mask;
86 int sa_flags;
87 void (*sa_restorer)(void);
88 };
89 .fi
90 .in
91 .PP
92 On some architectures a union is involved: do not assign to both
93 .I sa_handler
94 and
95 .IR sa_sigaction .
96 .PP
97 The
98 .I sa_restorer
99 element is obsolete and should not be used.
100 POSIX does not specify a
101 .I sa_restorer
102 element.
103 .PP
104 .I sa_handler
105 specifies the action to be associated with
106 .I signum
107 and may be
108 .B SIG_DFL
109 for the default action,
110 .B SIG_IGN
111 to ignore this signal, or a pointer to a signal handling function.
112 This function receives the signal number as its only argument.
113 .PP
114 If
115 .B SA_SIGINFO
116 is specified in
117 .IR sa_flags ,
118 then
119 .I sa_sigaction
120 (instead of
121 .IR sa_handler )
122 specifies the signal-handling function for
123 .IR signum .
124 This function receives the signal number as its first argument, a
125 pointer to a
126 .I siginfo_t
127 as its second argument and a pointer to a
128 .I ucontext_t
129 (cast to \fIvoid\ *\fP) as its third argument.
130 .PP
131 .I sa_mask
132 gives a mask of signals which should be blocked during execution of
133 the signal handler.
134 In addition, the signal which triggered the handler
135 will be blocked, unless the
136 .B SA_NODEFER
137 flag is used.
138 .PP
139 .I sa_flags
140 specifies a set of flags which modify the behavior of the signal.
141 It is formed by the bitwise OR of zero or more of the following:
142 .RS 4
143 .TP
144 .B SA_NOCLDSTOP
145 If
146 .I signum
147 is
148 .BR SIGCHLD ,
149 do not receive notification when child processes stop (i.e., when they
150 receive one of
151 .BR SIGSTOP ", " SIGTSTP ", " SIGTTIN
152 or
153 .BR SIGTTOU )
154 or resume (i.e., they receive
155 .BR SIGCONT )
156 (see
157 .BR wait (2)).
158 This flag is only meaningful when establishing a handler for
159 .BR SIGCHLD .
160 .TP
161 .BR SA_NOCLDWAIT " (Since Linux 2.6)"
162 .\" To be precise: Linux 2.5.60 -- MTK
163 If
164 .I signum
165 is
166 .BR SIGCHLD ,
167 do not transform children into zombies when they terminate.
168 See also
169 .BR waitpid (2).
170 This flag is only meaningful when establishing a handler for
171 .BR SIGCHLD ,
172 or when setting that signal's disposition to
173 .BR SIG_DFL .
174
175 If the
176 .B SA_NOCLDWAIT
177 flag is set when establishing a handler for
178 .BR SIGCHLD ,
179 POSIX.1 leaves it unspecified whether a
180 .B SIGCHLD
181 signal is generated when a child process terminates.
182 On Linux, a
183 .B SIGCHLD
184 signal is generated in this case;
185 on some other implementations, it is not.
186 .TP
187 .B SA_NODEFER
188 Do not prevent the signal from being received from within its own signal
189 handler.
190 This flag is only meaningful when establishing a signal handler.
191 .B SA_NOMASK
192 is an obsolete, non-standard synonym for this flag.
193 .TP
194 .B SA_ONSTACK
195 Call the signal handler on an alternate signal stack provided by
196 .BR sigaltstack (2).
197 If an alternate stack is not available, the default stack will be used.
198 This flag is only meaningful when establishing a signal handler.
199 .TP
200 .BR SA_RESETHAND
201 Restore the signal action to the default state once the signal handler
202 has been called.
203 This flag is only meaningful when establishing a signal handler.
204 .B SA_ONESHOT
205 is an obsolete, non-standard synonym for this flag.
206 .TP
207 .B SA_RESTART
208 Provide behavior compatible with BSD signal semantics by making certain
209 system calls restartable across signals.
210 This flag is only meaningful when establishing a signal handler.
211 See
212 .BR signal (7)
213 for a discussion of system call restarting.
214 .TP
215 .BR SA_SIGINFO " (since Linux 2.2)"
216 The signal handler takes 3 arguments, not one.
217 In this case,
218 .I sa_sigaction
219 should be set instead of
220 .IR sa_handler .
221 This flag is only meaningful when establishing a signal handler.
222 .\" (The
223 .\" .I sa_sigaction
224 .\" field was added in Linux 2.1.86.)
225 .RE
226 .PP
227 The
228 .I siginfo_t
229 argument to
230 .I sa_sigaction
231 is a struct with the following elements:
232 .sp
233 .in +4n
234 .nf
235 siginfo_t {
236 int si_signo; /* Signal number */
237 int si_errno; /* An errno value */
238 int si_code; /* Signal code */
239 int si_trapno; /* Trap number that caused
240 hardware-generated signal
241 (unused on most architectures) */
242 .\" FIXME
243 .\" si_trapno seems to be only used on SPARC and Alpha;
244 .\" this page could use a little more detail on its purpose there.
245 pid_t si_pid; /* Sending process ID */
246 uid_t si_uid; /* Real user ID of sending process */
247 int si_status; /* Exit value or signal */
248 clock_t si_utime; /* User time consumed */
249 clock_t si_stime; /* System time consumed */
250 sigval_t si_value; /* Signal value */
251 int si_int; /* POSIX.1b signal */
252 void *si_ptr; /* POSIX.1b signal */
253 int si_overrun; /* Timer overrun count; POSIX.1b timers */
254 int si_timerid; /* Timer ID; POSIX.1b timers */
255 .\" In the kernel: si_tid
256 void *si_addr; /* Memory location which caused fault */
257 int si_band; /* Band event */
258 int si_fd; /* File descriptor */
259 }
260 .fi
261 .in
262
263 .IR si_signo ", " si_errno " and " si_code
264 are defined for all signals.
265 .RI ( si_errno
266 is generally unused on Linux.)
267 The rest of the struct may be a union, so that one should only
268 read the fields that are meaningful for the given signal:
269 .IP * 2
270 POSIX.1b signals and
271 .B SIGCHLD
272 fill in
273 .IR si_pid " and " si_uid .
274 .IP *
275 POSIX.1b timers (since Linux 2.6) fill in
276 .I si_overrun
277 and
278 .IR si_timerid .
279 The
280 .I si_timerid
281 field is an internal ID used by the kernel to identify
282 the timer; it is not the same as the timer ID returned by
283 .BR timer_create (3).
284 .IP *
285 .B SIGCHLD
286 fills in
287 .IR si_status ", " si_utime " and " si_stime .
288 .\" FIXME .
289 .\" When si_utime and si_stime where originally implemented, the
290 .\" measurement unit was HZ, which was the same as clock ticks
291 .\" (sysconf(_SC_CLK_TCK)). In 2.6, HZ became configurable, and
292 .\" was *still* used as the unit to return the info these fields,
293 .\" with the result that the field values depended on the the
294 .\" configured HZ. Of course, the should have been measured in
295 .\" USER_HZ instead, so that sysconf(_SC_CLK_TCK) could be used to
296 .\" convert to seconds. I have a queued patch to fix this:
297 .\" http://thread.gmane.org/gmane.linux.kernel/698061/ .
298 .\" Maybe it will make it into 2.6.27.
299 .\" But note that these fields still don't return the times of
300 .\" waited for children (as is done by getrusage() and times()
301 .\" and wait4()). Solaris 8 does include child times.
302 .IP *
303 .IR si_int " and " si_ptr
304 are specified by the sender of the POSIX.1b signal.
305 See
306 .BR sigqueue (2)
307 for more details.
308 .IP *
309 .BR SIGILL ,
310 .BR SIGFPE ,
311 .BR SIGSEGV ,
312 and
313 .B SIGBUS
314 fill in
315 .I si_addr
316 with the address of the fault.
317 .B SIGPOLL
318 fills in
319 .IR si_band " and " si_fd .
320 .PP
321 .I si_code
322 is a value (not a bit mask)
323 indicating why this signal was sent.
324 The following list shows the values which can be placed in
325 .I si_code
326 for any signal, along with reason that the signal was generated.
327 .RS 4
328 .TP 15
329 .B SI_USER
330 .BR kill (2)
331 or
332 .BR raise (3)
333 .TP
334 .B SI_KERNEL
335 Sent by the kernel.
336 .TP
337 .B SI_QUEUE
338 .BR sigqueue (2)
339 .TP
340 .B SI_TIMER
341 POSIX timer expired
342 .TP
343 .B SI_MESGQ
344 POSIX message queue state changed (since Linux 2.6.6); see
345 .BR mq_notify (3)
346 .TP
347 .B SI_ASYNCIO
348 AIO completed
349 .TP
350 .B SI_SIGIO
351 queued SIGIO
352 .TP
353 .B SI_TKILL
354 .BR tkill (2)
355 or
356 .BR tgkill (2)
357 (since Linux 2.4.19)
358 .\" SI_DETHREAD is defined in 2.6.9 sources, but isn't implemented
359 .\" It appears to have been an idea that was tried during 2.5.6
360 .\" through to 2.5.24 and then was backed out.
361 .RE
362 .PP
363 The following values can be placed in
364 .I si_code
365 for a
366 .B SIGILL
367 signal:
368 .RS 4
369 .TP 15
370 .B ILL_ILLOPC
371 illegal opcode
372 .TP
373 .B ILL_ILLOPN
374 illegal operand
375 .TP
376 .B ILL_ILLADR
377 illegal addressing mode
378 .TP
379 .B ILL_ILLTRP
380 illegal trap
381 .TP
382 .B ILL_PRVOPC
383 privileged opcode
384 .TP
385 .B ILL_PRVREG
386 privileged register
387 .TP
388 .B ILL_COPROC
389 coprocessor error
390 .TP
391 .B ILL_BADSTK
392 internal stack error
393 .RE
394 .PP
395 The following values can be placed in
396 .I si_code
397 for a
398 .B SIGFPE
399 signal:
400 .RS 4
401 .TP 15
402 .B FPE_INTDIV
403 integer divide by zero
404 .TP
405 .B FPE_INTOVF
406 integer overflow
407 .TP
408 .B FPE_FLTDIV
409 floating point divide by zero
410 .TP
411 .B FPE_FLTOVF
412 floating point overflow
413 .TP
414 .B FPE_FLTUND
415 floating point underflow
416 .TP
417 .B FPE_FLTRES
418 floating point inexact result
419 .TP
420 .B FPE_FLTINV
421 floating point invalid operation
422 .TP
423 .B FPE_FLTSUB
424 subscript out of range
425 .RE
426 .PP
427 The following values can be placed in
428 .I si_code
429 for a
430 .B SIGSEGV
431 signal:
432 .RS 4
433 .TP 15
434 .B SEGV_MAPERR
435 address not mapped to object
436 .TP
437 .B SEGV_ACCERR
438 invalid permissions for mapped object
439 .RE
440 .PP
441 The following values can be placed in
442 .I si_code
443 for a
444 .B SIGBUS
445 signal:
446 .RS 4
447 .TP 15
448 .B BUS_ADRALN
449 invalid address alignment
450 .TP
451 .B BUS_ADRERR
452 nonexistent physical address
453 .TP
454 .B BUS_OBJERR
455 object-specific hardware error
456 .RE
457 .PP
458 The following values can be placed in
459 .I si_code
460 for a
461 .B SIGTRAP
462 signal:
463 .RS 4
464 .TP 15
465 .B TRAP_BRKPT
466 process breakpoint
467 .TP
468 .B TRAP_TRACE
469 process trace trap
470 .RE
471 .PP
472 The following values can be placed in
473 .I si_code
474 for a
475 .B SIGCHLD
476 signal:
477 .RS 4
478 .TP 15
479 .B CLD_EXITED
480 child has exited
481 .TP
482 .B CLD_KILLED
483 child was killed
484 .TP
485 .B CLD_DUMPED
486 child terminated abnormally
487 .TP
488 .B CLD_TRAPPED
489 traced child has trapped
490 .TP
491 .B CLD_STOPPED
492 child has stopped
493 .TP
494 .B CLD_CONTINUED
495 stopped child has continued (since Linux 2.6.9)
496 .RE
497 .PP
498 The following values can be placed in
499 .I si_code
500 for a
501 .B SIGPOLL
502 signal:
503 .RS 4
504 .TP 15
505 .B POLL_IN
506 data input available
507 .TP
508 .B POLL_OUT
509 output buffers available
510 .TP
511 .B POLL_MSG
512 input message available
513 .TP
514 .B POLL_ERR
515 i/o error
516 .TP
517 .B POLL_PRI
518 high priority input available
519 .TP
520 .B POLL_HUP
521 device disconnected
522 .RE
523 .SH "RETURN VALUE"
524 .BR sigaction ()
525 returns 0 on success and \-1 on error.
526 .SH ERRORS
527 .TP
528 .B EFAULT
529 .IR act " or " oldact
530 points to memory which is not a valid part of the process address space.
531 .TP
532 .B EINVAL
533 An invalid signal was specified.
534 This will also be generated if an attempt
535 is made to change the action for
536 .BR SIGKILL " or " SIGSTOP ", "
537 which cannot be caught or ignored.
538 .SH "CONFORMING TO"
539 POSIX.1-2001, SVr4.
540 .\" SVr4 does not document the EINTR condition.
541 .SH NOTES
542 .PP
543 According to POSIX, the behavior of a process is undefined after it
544 ignores a
545 .BR SIGFPE ,
546 .BR SIGILL ,
547 or
548 .B SIGSEGV
549 signal that was not generated by
550 .BR kill (2)
551 or
552 .BR raise (3).
553 Integer division by zero has undefined result.
554 On some architectures it will generate a
555 .B SIGFPE
556 signal.
557 (Also dividing the most negative integer by \-1 may generate
558 .BR SIGFPE .)
559 Ignoring this signal might lead to an endless loop.
560 .PP
561 POSIX.1-1990 disallowed setting the action for
562 .B SIGCHLD
563 to
564 .BR SIG_IGN .
565 POSIX.1-2001 allows this possibility, so that ignoring
566 .B SIGCHLD
567 can be used to prevent the creation of zombies (see
568 .BR wait (2)).
569 Nevertheless, the historical BSD and System V behaviors for ignoring
570 .B SIGCHLD
571 differ, so that the only completely portable method of ensuring that
572 terminated children do not become zombies is to catch the
573 .B SIGCHLD
574 signal and perform a
575 .BR wait (2)
576 or similar.
577 .PP
578 POSIX.1-1990 only specified
579 .BR SA_NOCLDSTOP .
580 POSIX.1-2001 added
581 .BR SA_NOCLDWAIT ,
582 .BR SA_RESETHAND ,
583 .BR SA_NODEFER ,
584 and
585 .BR SA_SIGINFO .
586 Use of these latter values in
587 .I sa_flags
588 may be less portable in applications intended for older
589 Unix implementations.
590 .PP
591 The
592 .B SA_RESETHAND
593 flag is compatible with the SVr4 flag of the same name.
594 .PP
595 The
596 .B SA_NODEFER
597 flag is compatible with the SVr4 flag of the same name under kernels
598 1.3.9 and newer.
599 On older kernels the Linux implementation
600 allowed the receipt of any signal, not just the one we are installing
601 (effectively overriding any
602 .I sa_mask
603 settings).
604 .PP
605 .BR sigaction ()
606 can be called with a null second argument to query the current signal
607 handler.
608 It can also be used to check whether a given signal is valid for
609 the current machine by calling it with null second and third arguments.
610 .PP
611 It is not possible to block
612 .BR SIGKILL " or " SIGSTOP
613 (by specifying them in
614 .IR sa_mask ).
615 Attempts to do so are silently ignored.
616 .PP
617 See
618 .BR sigsetops (3)
619 for details on manipulating signal sets.
620 .PP
621 See
622 .BR signal (7)
623 for a list of the async-signal-safe functions that can be
624 safely called inside from inside a signal handler.
625 .SS Undocumented
626 Before the introduction of
627 .B SA_SIGINFO
628 it was also possible to get some additional information,
629 namely by using a
630 .I sa_handler
631 with second argument of type
632 .IR "struct sigcontext".
633 See the relevant kernel sources for details.
634 This use is obsolete now.
635 .SH BUGS
636 In kernels up to and including 2.6.13, specifying
637 .B SA_NODEFER
638 in
639 .I sa_flags
640 prevents not only the delivered signal from being masked during
641 execution of the handler, but also the signals specified in
642 .IR sa_mask .
643 This bug was fixed in kernel 2.6.14.
644 .SH EXAMPLE
645 See
646 .BR mprotect (2).
647 .SH "SEE ALSO"
648 .BR kill (1),
649 .BR kill (2),
650 .BR killpg (2),
651 .BR pause (2),
652 .BR sigaltstack (2),
653 .BR signal (2),
654 .BR signalfd (2),
655 .BR sigpending (2),
656 .BR sigprocmask (2),
657 .BR sigqueue (2),
658 .BR sigsuspend (2),
659 .BR wait (2),
660 .BR raise (3),
661 .BR siginterrupt (3),
662 .BR sigsetops (3),
663 .BR sigvec (3),
664 .BR core (5),
665 .BR signal (7)