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