xref: /netbsd-src/lib/libc/sys/sigaction.2 (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1.\"	$NetBSD: sigaction.2,v 1.34 2003/05/11 19:22:18 kleink Exp $
2.\"
3.\" Copyright (c) 1980, 1990, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"	@(#)sigaction.2	8.2 (Berkeley) 4/3/94
35.\"
36.Dd May 11, 2003
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.Ft int
47.Fn sigaction "int sig" "const struct sigaction *act" "struct sigaction *oact"
48.Sh DESCRIPTION
49The system defines a set of signals that may be delivered to a process.
50Signal delivery resembles the occurrence of a hardware interrupt:
51the signal is blocked from further occurrence, the current process
52context is saved, and a new one is built.
53A process may specify a
54.Em handler
55to which a signal is delivered, or specify that a signal is to be
56.Em ignored .
57A process may also specify that a default action is to be taken
58by the system when a signal occurs.
59A signal may also be
60.Em blocked ,
61in which case its delivery is postponed until it is
62.Em unblocked .
63The action to be taken on delivery is determined at the time of delivery.
64Normally, signal handlers execute on the current stack of the process.
65This may be changed, on a per-handler basis, so that signals are
66taken on a special
67.Em "signal stack" .
68.Pp
69Signal routines execute with the signal that caused their
70invocation
71.Em blocked ,
72but other signals may yet occur.
73A global
74.Em "signal mask"
75defines the set of signals currently blocked from delivery
76to a process.
77The signal mask for a process is initialized from that of its parent
78(normally empty).
79It may be changed with a
80.Xr sigprocmask 2
81call, or when a signal is delivered to the process.
82Signal masks are represented using the
83.Em sigset_t
84type; the
85.Xr sigsetops 3
86interface is used to modify such data.
87.Pp
88When a signal
89condition arises for a process, the signal is added to a set of
90signals pending for the process.
91If the signal is not currently
92.Em blocked
93by the process then it is delivered to the process.
94Signals may be delivered any time a process enters the operating system
95(e.g., during a system call, page fault or trap, or clock interrupt).
96If multiple signals are ready to be delivered at the same time,
97any signals that could be caused by traps are delivered first.
98Additional signals may be processed at the same time, with each
99appearing to interrupt the handlers for the previous signals
100before their first instructions.
101The set of pending signals is returned by the
102.Xr sigpending 2
103function.
104When a caught signal
105is delivered, the current state of the process is saved,
106a new signal mask is calculated (as described below),
107and the signal handler is invoked.
108The call to the handler is arranged so that if the signal handling
109routine returns normally the process will resume execution in the
110context from before the signal's delivery.
111If the process wishes to resume in a different context, then it
112must arrange to restore the previous context itself.
113.Pp
114.Em "struct sigaction"
115includes the following members:
116.Bd -literal -offset indent
117void      (*sa_handler)(int)
118sigset_t  sa_mask
119int       sa_flags
120.Ed
121.Pp
122When a signal is delivered to a process a new signal mask is
123installed for the duration of the process' signal handler
124(or until a
125.Xr sigprocmask 2
126call is made).
127This mask is formed by taking the union of the current signal mask,
128the signal to be delivered, and
129the signal mask associated with the handler to be invoked,
130.Em sa_mask .
131.Pp
132.Fn sigaction
133assigns an action for a specific signal.
134If
135.Fa act
136is non-zero, it
137specifies an action
138.Pf ( Dv SIG_DFL ,
139.Dv SIG_IGN ,
140or a handler routine) and mask
141to be used when delivering the specified signal.
142If
143.Fa oact
144is non-zero, the previous handling information for the signal
145is returned to the user.
146.Pp
147Once a signal handler is installed, it remains installed
148until another
149.Fn sigaction
150call is made, or an
151.Xr execve 2
152is performed.
153A signal-specific default action may be reset by
154setting
155.Fa sa_handler
156to
157.Dv SIG_DFL .
158Alternately, if the
159.Dv SA_RESETHAND
160bit is set the default action will be reinstated when the signal
161is first posted.
162The defaults are process termination, possibly with core dump;
163no action; stopping the process; or continuing the process.
164See the signal list below for each signal's default action.
165If
166.Fa sa_handler
167is set to
168.Dv SIG_DFL ,
169the default action for the signal is to discard the signal,
170and if a signal is pending,
171the pending signal is discarded even if the signal is masked.
172If
173.Fa sa_handler
174is set to
175.Dv SIG_IGN ,
176current and pending instances
177of the signal are ignored and discarded.
178.Pp
179Options may be specified by setting
180.Em sa_flags .
181If the
182.Dv SA_NOCLDSTOP
183bit is set when installing a catching function
184for the
185.Dv SIGCHLD
186signal,
187the
188.Dv SIGCHLD
189signal will be generated only when a child process exits,
190not when a child process stops.
191Further, if the
192.Dv SA_ONSTACK
193bit is set in
194.Em sa_flags ,
195the system will deliver the signal to the process on a
196.Em "signal stack" ,
197specified with
198.Xr sigaltstack 2 .
199Finally, if the
200.Dv SA_NOCLDWAIT
201bit is set in
202.Em sa_flags ,
203the system will not create a zombie when the child exits, but the child
204process will be automatically waited for.
205.Pp
206If a signal is caught during the system calls listed below,
207the call may be forced to terminate
208with the error
209.Er EINTR ,
210the call may return with a data transfer shorter than requested,
211or the call may be restarted.
212Restarting of pending calls is requested
213by setting the
214.Dv SA_RESTART
215bit in
216.Ar sa_flags .
217The affected system calls include
218.Xr open 2 ,
219.Xr read 2 ,
220.Xr write 2 ,
221.Xr sendto 2 ,
222.Xr recvfrom 2 ,
223.Xr sendmsg 2
224and
225.Xr recvmsg 2
226on a communications channel or a slow device (such as a terminal,
227but not a regular file)
228and during a
229.Xr wait 2
230or
231.Xr ioctl 2 .
232However, calls that have already committed are not restarted,
233but instead return a partial success (for example, a short read count).
234.Pp
235After a
236.Xr fork 2
237or
238.Xr vfork 2
239all signals, the signal mask, the signal stack,
240and the restart/interrupt flags are inherited by the child.
241.Pp
242The
243.Xr execve 2
244system call reinstates the default
245action for all signals which were caught and
246resets all signals to be caught on the user stack.
247Ignored signals remain ignored;
248the signal mask remains the same;
249signals that restart pending system calls continue to do so.
250.Pp
251See
252.Xr signal 7
253for comprehensive list of supported signals.
254.Sh NOTES
255The mask specified in
256.Fa act
257is not allowed to block
258.Dv SIGKILL
259or
260.Dv SIGSTOP .
261This is enforced silently by the system.
262.Sh RETURN VALUES
263A 0 value indicates that the call succeeded.
264A \-1 return value indicates an error occurred and
265.Va errno
266is set to indicate the reason.
267.Sh EXAMPLES
268The handler routine can be declared:
269.Bd -literal -offset indent
270void
271handler(sig, code, scp)
272	int sig, code;
273	struct sigcontext *scp;
274.Ed
275.Pp
276Here
277.Fa sig
278is the signal number, into which the hardware faults and traps are
279mapped.
280.Fa code
281is a parameter that is either a constant
282or the code provided by the hardware.
283.Fa scp
284is a pointer to the
285.Fa sigcontext
286structure (defined in
287.Aq Pa signal.h ) ,
288used to restore the context from before the signal.
289.Pp
290For POSIX compliance, the
291.Fa sa_handler
292is declared to be (void (*)(int)) and the above handler will need to be
293casted to that type.
294Future versions of
295.Nx
296will replace the
297.Fa sigcontext
298interface with the
299.Fa siginfo
300interface.
301.Sh ERRORS
302.Fn sigaction
303will fail and no new signal handler will be installed if one
304of the following occurs:
305.Bl -tag -width Er
306.It Bq Er EFAULT
307Either
308.Fa act
309or
310.Fa oact
311points to memory that is not a valid part of the process
312address space.
313.It Bq Er EINVAL
314.Fa sig
315is not a valid signal number.
316.It Bq Er EINVAL
317An attempt is made to ignore or supply a handler for
318.Dv SIGKILL
319or
320.Dv SIGSTOP .
321.It Bq Er EINVAL
322The
323.Em sa_flags
324word contains bits other than
325.Dv SA_ONSTACK ,
326.Dv SA_RESTART ,
327.Dv SA_RESETHAND ,
328.Dv SA_NODEFER ,
329.Dv SA_SIGINFO ,
330.Dv SA_NOCLDWAIT ,
331and
332.Dv SA_NOCLDSTOP .
333.El
334.Sh SEE ALSO
335.Xr kill 1 ,
336.Xr kill 2 ,
337.Xr ptrace 2 ,
338.Xr sigaltstack 2 ,
339.Xr sigprocmask 2 ,
340.Xr sigsuspend 2 ,
341.Xr setjmp 3 ,
342.Xr sigsetops 3 ,
343.Xr tty 4 ,
344.Xr signal 7
345.Sh STANDARDS
346The
347.Fn sigaction
348function conforms to
349.St -p1003.1-90 .
350The
351.Dv SA_ONSTACK
352and
353.Dv SA_RESTART
354flags are Berkeley extensions, available on most
355.Bx Ns \-derived
356systems.
357