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