]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/sys/sigaction.2
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libc / sys / sigaction.2
1 .\" Copyright (c) 1980, 1990, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     From: @(#)sigaction.2   8.2 (Berkeley) 4/3/94
29 .\" $FreeBSD$
30 .\"
31 .Dd September 27, 2012
32 .Dt SIGACTION 2
33 .Os
34 .Sh NAME
35 .Nm sigaction
36 .Nd software signal facilities
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In signal.h
41 .Bd -literal
42 struct  sigaction {
43         void    (*sa_handler)(int);
44         void    (*sa_sigaction)(int, siginfo_t *, void *);
45         int     sa_flags;               /* see signal options below */
46         sigset_t sa_mask;               /* signal mask to apply */
47 };
48 .Ed
49 .Ft int
50 .Fo sigaction
51 .Fa "int sig"
52 .Fa "const struct sigaction * restrict act"
53 .Fa "struct sigaction * restrict oact"
54 .Fc
55 .Sh DESCRIPTION
56 The system defines a set of signals that may be delivered to a process.
57 Signal delivery resembles the occurrence of a hardware interrupt:
58 the signal is normally blocked from further occurrence, the current process
59 context is saved, and a new one is built.
60 A process may specify a
61 .Em handler
62 to which a signal is delivered, or specify that a signal is to be
63 .Em ignored .
64 A process may also specify that a default action is to be taken
65 by the system when a signal occurs.
66 A signal may also be
67 .Em blocked ,
68 in which case its delivery is postponed until it is
69 .Em unblocked .
70 The action to be taken on delivery is determined at the time
71 of delivery.
72 Normally, signal handlers execute on the current stack
73 of the process.
74 This may be changed, on a per-handler basis,
75 so that signals are taken on a special
76 .Em "signal stack" .
77 .Pp
78 Signal routines normally execute with the signal that caused their
79 invocation
80 .Em blocked ,
81 but other signals may yet occur.
82 A global
83 .Em "signal mask"
84 defines the set of signals currently blocked from delivery
85 to a process.
86 The signal mask for a process is initialized
87 from that of its parent (normally empty).
88 It may be changed with a
89 .Xr sigprocmask 2
90 call, or when a signal is delivered to the process.
91 .Pp
92 When a signal
93 condition arises for a process, the signal is added to a set of
94 signals pending for the process.
95 If the signal is not currently
96 .Em blocked
97 by the process then it is delivered to the process.
98 Signals may be delivered any time a process enters the operating system
99 (e.g., during a system call, page fault or trap, or clock interrupt).
100 If multiple signals are ready to be delivered at the same time,
101 any signals that could be caused by traps are delivered first.
102 Additional signals may be processed at the same time, with each
103 appearing to interrupt the handlers for the previous signals
104 before their first instructions.
105 The set of pending signals is returned by the
106 .Xr sigpending 2
107 system call.
108 When a caught signal
109 is delivered, the current state of the process is saved,
110 a new signal mask is calculated (as described below),
111 and the signal handler is invoked.
112 The call to the handler
113 is arranged so that if the signal handling routine returns
114 normally the process will resume execution in the context
115 from before the signal's delivery.
116 If the process wishes to resume in a different context, then it
117 must arrange to restore the previous context itself.
118 .Pp
119 When a signal is delivered to a process a new signal mask is
120 installed for the duration of the process' signal handler
121 (or until a
122 .Xr sigprocmask 2
123 system call is made).
124 This mask is formed by taking the union of the current signal mask set,
125 the signal to be delivered, and
126 the signal mask associated with the handler to be invoked.
127 .Pp
128 The
129 .Fn sigaction
130 system call
131 assigns an action for a signal specified by
132 .Fa sig .
133 If
134 .Fa act
135 is non-zero, it
136 specifies an action
137 .Dv ( SIG_DFL ,
138 .Dv SIG_IGN ,
139 or a handler routine) and mask
140 to be used when delivering the specified signal.
141 If
142 .Fa oact
143 is non-zero, the previous handling information for the signal
144 is returned to the user.
145 .Pp
146 The above declaration of
147 .Vt "struct sigaction"
148 is not literal.
149 It is provided only to list the accessible members.
150 See
151 .In sys/signal.h
152 for the actual definition.
153 In particular, the storage occupied by sa_handler and sa_sigaction overlaps,
154 and an application can not use both simultaneously.
155 .Pp
156 Once a signal handler is installed, it normally remains installed
157 until another
158 .Fn sigaction
159 system call is made, or an
160 .Xr execve 2
161 is performed.
162 A signal-specific default action may be reset by
163 setting
164 .Va sa_handler
165 to
166 .Dv SIG_DFL .
167 The defaults are process termination, possibly with core dump;
168 no action; stopping the process; or continuing the process.
169 See the signal list below for each signal's default action.
170 If
171 .Va sa_handler
172 is
173 .Dv SIG_DFL ,
174 the default action for the signal is to discard the signal,
175 and if a signal is pending,
176 the pending signal is discarded even if the signal is masked.
177 If
178 .Va sa_handler
179 is set to
180 .Dv SIG_IGN
181 current and pending instances
182 of the signal are ignored and discarded.
183 .Pp
184 Options may be specified by setting
185 .Va sa_flags .
186 The meaning of the various bits is as follows:
187 .Bl -tag -offset indent -width SA_RESETHANDXX
188 .It Dv SA_NOCLDSTOP
189 If this bit is set when installing a catching function
190 for the
191 .Dv SIGCHLD
192 signal,
193 the
194 .Dv SIGCHLD
195 signal will be generated only when a child process exits,
196 not when a child process stops.
197 .It Dv SA_NOCLDWAIT
198 If this bit is set when calling
199 .Fn sigaction
200 for the
201 .Dv SIGCHLD
202 signal, the system will not create zombie processes when children of
203 the calling process exit.
204 If the calling process subsequently issues a
205 .Xr wait 2
206 (or equivalent), it blocks until all of the calling process's child
207 processes terminate, and then returns a value of \-1 with
208 .Va errno
209 set to
210 .Er ECHILD .
211 The same effect of avoiding zombie creation can also be achieved by setting
212 .Va sa_handler
213 for
214 .Dv SIGCHLD
215 to
216 .Dv SIG_IGN .
217 .It Dv SA_ONSTACK
218 If this bit is set, the system will deliver the signal to the process
219 on a
220 .Em "signal stack" ,
221 specified with
222 .Xr sigaltstack 2 .
223 .It Dv SA_NODEFER
224 If this bit is set, further occurrences of the delivered signal are
225 not masked during the execution of the handler.
226 .It Dv SA_RESETHAND
227 If this bit is set, the handler is reset back to
228 .Dv SIG_DFL
229 at the moment the signal is delivered.
230 .It Dv SA_RESTART
231 See paragraph below.
232 .It Dv SA_SIGINFO
233 If this bit is set, the handler function is assumed to be pointed to by the
234 .Va sa_sigaction
235 member of
236 .Vt "struct sigaction"
237 and should match the prototype shown above or as below in
238 .Sx EXAMPLES .
239 This bit should not be set when assigning
240 .Dv SIG_DFL
241 or
242 .Dv SIG_IGN .
243 .El
244 .Pp
245 If a signal is caught during the system calls listed below,
246 the call may be forced to terminate
247 with the error
248 .Er EINTR ,
249 the call may return with a data transfer shorter than requested,
250 or the call may be restarted.
251 Restart of pending calls is requested
252 by setting the
253 .Dv SA_RESTART
254 bit in
255 .Va sa_flags .
256 The affected system calls include
257 .Xr open 2 ,
258 .Xr read 2 ,
259 .Xr write 2 ,
260 .Xr sendto 2 ,
261 .Xr recvfrom 2 ,
262 .Xr sendmsg 2
263 and
264 .Xr recvmsg 2
265 on a communications channel or a slow device (such as a terminal,
266 but not a regular file)
267 and during a
268 .Xr wait 2
269 or
270 .Xr ioctl 2 .
271 However, calls that have already committed are not restarted,
272 but instead return a partial success (for example, a short read count).
273 .Pp
274 After a
275 .Xr fork 2
276 or
277 .Xr vfork 2
278 all signals, the signal mask, the signal stack,
279 and the restart/interrupt flags are inherited by the child.
280 .Pp
281 The
282 .Xr execve 2
283 system call reinstates the default
284 action for all signals which were caught and
285 resets all signals to be caught on the user stack.
286 Ignored signals remain ignored;
287 the signal mask remains the same;
288 signals that restart pending system calls continue to do so.
289 .Pp
290 The following is a list of all signals
291 with names as in the include file
292 .In signal.h :
293 .Bl -column SIGVTALARMXX "create core imagexxx"
294 .It Sy "NAME    Default Action  Description"
295 .It Dv SIGHUP No "      terminate process" "    terminal line hangup"
296 .It Dv SIGINT No "      terminate process" "    interrupt program"
297 .It Dv SIGQUIT No "     create core image" "    quit program"
298 .It Dv SIGILL No "      create core image" "    illegal instruction"
299 .It Dv SIGTRAP No "     create core image" "    trace trap"
300 .It Dv SIGABRT No "     create core image" Ta Xr abort 3
301 call (formerly
302 .Dv SIGIOT )
303 .It Dv SIGEMT No "      create core image" "    emulate instruction executed"
304 .It Dv SIGFPE No "      create core image" "    floating-point exception"
305 .It Dv SIGKILL No "     terminate process" "    kill program"
306 .It Dv SIGBUS No "      create core image" "    bus error"
307 .It Dv SIGSEGV No "     create core image" "    segmentation violation"
308 .It Dv SIGSYS No "      create core image" "    non-existent system call invoked"
309 .It Dv SIGPIPE No "     terminate process" "    write on a pipe with no reader"
310 .It Dv SIGALRM No "     terminate process" "    real-time timer expired"
311 .It Dv SIGTERM No "     terminate process" "    software termination signal"
312 .It Dv SIGURG No "      discard signal" "       urgent condition present on socket"
313 .It Dv SIGSTOP No "     stop process" " stop (cannot be caught or ignored)"
314 .It Dv SIGTSTP No "     stop process" " stop signal generated from keyboard"
315 .It Dv SIGCONT No "     discard signal" "       continue after stop"
316 .It Dv SIGCHLD No "     discard signal" "       child status has changed"
317 .It Dv SIGTTIN No "     stop process" " background read attempted from control terminal"
318 .It Dv SIGTTOU No "     stop process" " background write attempted to control terminal"
319 .It Dv SIGIO No "       discard signal" Tn "    I/O"
320 is possible on a descriptor (see
321 .Xr fcntl 2 )
322 .It Dv SIGXCPU No "     terminate process" "    cpu time limit exceeded (see"
323 .Xr setrlimit 2 )
324 .It Dv SIGXFSZ No "     terminate process" "    file size limit exceeded (see"
325 .Xr setrlimit 2 )
326 .It Dv SIGVTALRM No "   terminate process" "    virtual time alarm (see"
327 .Xr setitimer 2 )
328 .It Dv SIGPROF No "     terminate process" "    profiling timer alarm (see"
329 .Xr setitimer 2 )
330 .It Dv SIGWINCH No "    discard signal" "       Window size change"
331 .It Dv SIGINFO No "     discard signal" "       status request from keyboard"
332 .It Dv SIGUSR1 No "     terminate process" "    User defined signal 1"
333 .It Dv SIGUSR2 No "     terminate process" "    User defined signal 2"
334 .El
335 .Sh NOTE
336 The
337 .Va sa_mask
338 field specified in
339 .Fa act
340 is not allowed to block
341 .Dv SIGKILL
342 or
343 .Dv SIGSTOP .
344 Any attempt to do so will be silently ignored.
345 .Pp
346 The following functions are either reentrant or not interruptible
347 by signals and are async-signal safe.
348 Therefore applications may
349 invoke them, without restriction, from signal-catching functions:
350 .Pp
351 Base Interfaces:
352 .Pp
353 .Fn _exit ,
354 .Fn access ,
355 .Fn alarm ,
356 .Fn cfgetispeed ,
357 .Fn cfgetospeed ,
358 .Fn cfsetispeed ,
359 .Fn cfsetospeed ,
360 .Fn chdir ,
361 .Fn chmod ,
362 .Fn chown ,
363 .Fn close ,
364 .Fn creat ,
365 .Fn dup ,
366 .Fn dup2 ,
367 .Fn execle ,
368 .Fn execve ,
369 .Fn fcntl ,
370 .Fn fork ,
371 .Fn fpathconf ,
372 .Fn fstat ,
373 .Fn fsync ,
374 .Fn getegid ,
375 .Fn geteuid ,
376 .Fn getgid ,
377 .Fn getgroups ,
378 .Fn getpgrp ,
379 .Fn getpid ,
380 .Fn getppid ,
381 .Fn getuid ,
382 .Fn kill ,
383 .Fn link ,
384 .Fn lseek ,
385 .Fn mkdir ,
386 .Fn mkfifo ,
387 .Fn open ,
388 .Fn pathconf ,
389 .Fn pause ,
390 .Fn pipe ,
391 .Fn raise ,
392 .Fn read ,
393 .Fn rename ,
394 .Fn rmdir ,
395 .Fn setgid ,
396 .Fn setpgid ,
397 .Fn setsid ,
398 .Fn setuid ,
399 .Fn sigaction ,
400 .Fn sigaddset ,
401 .Fn sigdelset ,
402 .Fn sigemptyset ,
403 .Fn sigfillset ,
404 .Fn sigismember ,
405 .Fn signal ,
406 .Fn sigpending ,
407 .Fn sigprocmask ,
408 .Fn sigsuspend ,
409 .Fn sleep ,
410 .Fn stat ,
411 .Fn sysconf ,
412 .Fn tcdrain ,
413 .Fn tcflow ,
414 .Fn tcflush ,
415 .Fn tcgetattr ,
416 .Fn tcgetpgrp ,
417 .Fn tcsendbreak ,
418 .Fn tcsetattr ,
419 .Fn tcsetpgrp ,
420 .Fn time ,
421 .Fn times ,
422 .Fn umask ,
423 .Fn uname ,
424 .Fn unlink ,
425 .Fn utime ,
426 .Fn wait ,
427 .Fn waitpid ,
428 .Fn write .
429 .Pp
430 Realtime Interfaces:
431 .Pp
432 .Fn aio_error ,
433 .Fn clock_gettime ,
434 .Fn sigpause ,
435 .Fn timer_getoverrun ,
436 .Fn aio_return ,
437 .Fn fdatasync ,
438 .Fn sigqueue ,
439 .Fn timer_gettime ,
440 .Fn aio_suspend ,
441 .Fn sem_post ,
442 .Fn sigset ,
443 .Fn timer_settime .
444 .Pp
445 .Tn ANSI C
446 Interfaces:
447 .Pp
448 .Fn strcpy ,
449 .Fn strcat ,
450 .Fn strncpy ,
451 .Fn strncat ,
452 and perhaps some others.
453 .Pp
454 Extension Interfaces:
455 .Pp
456 .Fn strlcpy ,
457 .Fn strlcat .
458 .Pp
459 All functions not in the above lists are considered to be unsafe
460 with respect to signals.
461 That is to say, the behaviour of such
462 functions when called from a signal handler is undefined.
463 In general though, signal handlers should do little more than set a
464 flag; most other actions are not safe.
465 .Pp
466 Also, it is good practice to make a copy of the global variable
467 .Va errno
468 and restore it before returning from the signal handler.
469 This protects against the side effect of
470 .Va errno
471 being set by functions called from inside the signal handler.
472 .Sh RETURN VALUES
473 .Rv -std sigaction
474 .Sh EXAMPLES
475 There are three possible prototypes the handler may match:
476 .Bl -tag -offset indent -width short
477 .It Tn ANSI C :
478 .Ft void
479 .Fn handler int ;
480 .It Traditional BSD style:
481 .Ft void
482 .Fn handler int "int code" "struct sigcontext *scp" ;
483 .It Tn POSIX Dv SA_SIGINFO :
484 .Ft void
485 .Fn handler int "siginfo_t *info" "ucontext_t *uap" ;
486 .El
487 .Pp
488 The handler function should match the
489 .Dv SA_SIGINFO
490 prototype if the
491 .Dv SA_SIGINFO
492 bit is set in
493 .Va sa_flags .
494 It then should be pointed to by the
495 .Va sa_sigaction
496 member of
497 .Vt "struct sigaction" .
498 Note that you should not assign
499 .Dv SIG_DFL
500 or
501 .Dv SIG_IGN
502 this way.
503 .Pp
504 If the
505 .Dv SA_SIGINFO
506 flag is not set, the handler function should match
507 either the
508 .Tn ANSI C
509 or traditional
510 .Bx
511 prototype and be pointed to by
512 the
513 .Va sa_handler
514 member of
515 .Vt "struct sigaction" .
516 In practice,
517 .Fx
518 always sends the three arguments of the latter and since the
519 .Tn ANSI C
520 prototype is a subset, both will work.
521 The
522 .Va sa_handler
523 member declaration in
524 .Fx
525 include files is that of
526 .Tn ANSI C
527 (as required by
528 .Tn POSIX ) ,
529 so a function pointer of a
530 .Bx Ns -style
531 function needs to be casted to
532 compile without warning.
533 The traditional
534 .Bx
535 style is not portable and since its capabilities
536 are a full subset of a
537 .Dv SA_SIGINFO
538 handler,
539 its use is deprecated.
540 .Pp
541 The
542 .Fa sig
543 argument is the signal number, one of the
544 .Dv SIG...
545 values from
546 .In signal.h .
547 .Pp
548 The
549 .Fa code
550 argument of the
551 .Bx Ns -style
552 handler and the
553 .Va si_code
554 member of the
555 .Fa info
556 argument to a
557 .Dv SA_SIGINFO
558 handler contain a numeric code explaining the
559 cause of the signal, usually one of the
560 .Dv SI_...
561 values from
562 .In sys/signal.h
563 or codes specific to a signal, i.e., one of the
564 .Dv FPE_...
565 values for
566 .Dv SIGFPE .
567 .Pp
568 The
569 .Fa scp
570 argument to a
571 .Bx Ns -style
572 handler points to an instance of
573 .Vt "struct sigcontext" .
574 .Pp
575 The
576 .Fa uap
577 argument to a
578 .Tn POSIX
579 .Dv SA_SIGINFO
580 handler points to an instance of
581 ucontext_t.
582 .Sh ERRORS
583 The
584 .Fn sigaction
585 system call
586 will fail and no new signal handler will be installed if one
587 of the following occurs:
588 .Bl -tag -width Er
589 .It Bq Er EINVAL
590 The
591 .Fa sig
592 argument
593 is not a valid signal number.
594 .It Bq Er EINVAL
595 An attempt is made to ignore or supply a handler for
596 .Dv SIGKILL
597 or
598 .Dv SIGSTOP .
599 .El
600 .Sh SEE ALSO
601 .Xr kill 1 ,
602 .Xr kill 2 ,
603 .Xr ptrace 2 ,
604 .Xr sigaltstack 2 ,
605 .Xr sigpending 2 ,
606 .Xr sigprocmask 2 ,
607 .Xr sigsuspend 2 ,
608 .Xr wait 2 ,
609 .Xr fpsetmask 3 ,
610 .Xr setjmp 3 ,
611 .Xr siginfo 3 ,
612 .Xr siginterrupt 3 ,
613 .Xr sigsetops 3 ,
614 .Xr ucontext 3 ,
615 .Xr tty 4
616 .Sh STANDARDS
617 The
618 .Fn sigaction
619 system call is expected to conform to
620 .St -p1003.1-90 .
621 The
622 .Dv SA_ONSTACK
623 and
624 .Dv SA_RESTART
625 flags are Berkeley extensions,
626 as are the signals,
627 .Dv SIGTRAP ,
628 .Dv SIGEMT ,
629 .Dv SIGBUS ,
630 .Dv SIGSYS ,
631 .Dv SIGURG ,
632 .Dv SIGIO ,
633 .Dv SIGXCPU ,
634 .Dv SIGXFSZ ,
635 .Dv SIGVTALRM ,
636 .Dv SIGPROF ,
637 .Dv SIGWINCH ,
638 and
639 .Dv SIGINFO .
640 Those signals are available on most
641 .Bx Ns \-derived
642 systems.
643 The
644 .Dv SA_NODEFER
645 and
646 .Dv SA_RESETHAND
647 flags are intended for backwards compatibility with other operating
648 systems.
649 The
650 .Dv SA_NOCLDSTOP ,
651 and
652 .Dv SA_NOCLDWAIT
653 .\" and
654 .\" SA_SIGINFO
655 flags are featuring options commonly found in other operating systems.
656 The flags are approved by
657 .St -susv2 ,
658 along with the option to avoid zombie creation by ignoring
659 .Dv SIGCHLD .