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