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