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