OSDN Git Service

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