xref: /netbsd/lib/libc/sys/sigaction.2 (revision c4a72b64)
1.\"	$NetBSD: sigaction.2,v 1.28 2002/07/15 07:48:57 jdolecek 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 November 1, 1997
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.Fd #include \*[Lt]signal.h\*[Gt]
46.Bd -literal
47struct sigaction {
48	void     (*sa_handler)(int);
49	sigset_t sa_mask;
50	int	 sa_flags;
51};
52.Ed
53.Ft int
54.Fn sigaction "int sig" "const struct sigaction *act" "struct sigaction *oact"
55.Sh DESCRIPTION
56The system defines a set of signals that may be delivered to a process.
57Signal delivery resembles the occurrence of a hardware interrupt:
58the signal is blocked from further occurrence, the current process
59context is saved, and a new one is built.
60A process may specify a
61.Em handler
62to which a signal is delivered, or specify that a signal is to be
63.Em ignored .
64A process may also specify that a default action is to be taken
65by the system when a signal occurs.
66A signal may also be
67.Em blocked ,
68in which case its delivery is postponed until it is
69.Em unblocked .
70The action to be taken on delivery is determined at the time of delivery.
71Normally, signal handlers execute on the current stack of the process.
72This may be changed, on a per-handler basis, so that signals are
73taken on a special
74.Em "signal stack" .
75.Pp
76Signal routines execute with the signal that caused their
77invocation
78.Em blocked ,
79but other signals may yet occur.
80A global
81.Em "signal mask"
82defines the set of signals currently blocked from delivery
83to a process.
84The signal mask for a process is initialized from that of its parent
85(normally empty).
86It may be changed with a
87.Xr sigprocmask 2
88call, or when a signal is delivered to the process.
89.Pp
90When a signal
91condition arises for a process, the signal is added to a set of
92signals pending for the process.
93If the signal is not currently
94.Em blocked
95by the process then it is delivered to the process.
96Signals may be delivered any time a process enters the operating system
97(e.g., during a system call, page fault or trap, or clock interrupt).
98If multiple signals are ready to be delivered at the same time,
99any signals that could be caused by traps are delivered first.
100Additional signals may be processed at the same time, with each
101appearing to interrupt the handlers for the previous signals
102before their first instructions.
103The set of pending signals is returned by the
104.Xr sigpending 2
105function.
106When a caught signal
107is delivered, the current state of the process is saved,
108a new signal mask is calculated (as described below),
109and the signal handler is invoked.
110The call to the handler is arranged so that if the signal handling
111routine returns normally the process will resume execution in the
112context from before the signal's delivery.
113If the process wishes to resume in a different context, then it
114must arrange to restore the previous context itself.
115.Pp
116When a signal is delivered to a process a new signal mask is
117installed for the duration of the process' signal handler
118(or until a
119.Xr sigprocmask 2
120call is made).
121This mask is formed by taking the union of the current signal mask,
122the signal to be delivered, and
123the signal mask associated with the handler to be invoked,
124.Em sa_mask .
125.Pp
126.Fn sigaction
127assigns an action for a specific signal.
128If
129.Fa act
130is non-zero, it
131specifies an action
132.Pf ( Dv SIG_DFL ,
133.Dv SIG_IGN ,
134or a handler routine) and mask
135to be used when delivering the specified signal.
136If
137.Fa oact
138is non-zero, the previous handling information for the signal
139is returned to the user.
140.Pp
141Once a signal handler is installed, it remains installed
142until another
143.Fn sigaction
144call is made, or an
145.Xr execve 2
146is performed.
147A signal-specific default action may be reset by
148setting
149.Fa sa_handler
150to
151.Dv SIG_DFL .
152Alternately, if the
153.Dv SA_RESETHAND
154bit is set the default action will be reinstated when the signal
155is first posted.
156The defaults are process termination, possibly with core dump;
157no action; stopping the process; or continuing the process.
158See the signal list below for each signal's default action.
159If
160.Fa sa_handler
161is set to
162.Dv SIG_DFL ,
163the default action for the signal is to discard the signal,
164and if a signal is pending,
165the pending signal is discarded even if the signal is masked.
166If
167.Fa sa_handler
168is set to
169.Dv SIG_IGN ,
170current and pending instances
171of the signal are ignored and discarded.
172.Pp
173Options may be specified by setting
174.Em sa_flags .
175If the
176.Dv SA_NOCLDSTOP
177bit is set when installing a catching function
178for the
179.Dv SIGCHLD
180signal,
181the
182.Dv SIGCHLD
183signal will be generated only when a child process exits,
184not when a child process stops.
185Further, if the
186.Dv SA_ONSTACK
187bit is set in
188.Em sa_flags ,
189the system will deliver the signal to the process on a
190.Em "signal stack" ,
191specified with
192.Xr sigaltstack 2 .
193Finally, if the
194.Dv SA_NOCLDWAIT
195bit is set in
196.Em sa_flags ,
197the system will not create a zombie when the child exits, but the child
198process will be automatically waited for.
199.Pp
200If a signal is caught during the system calls listed below,
201the call may be forced to terminate
202with the error
203.Er EINTR ,
204the call may return with a data transfer shorter than requested,
205or the call may be restarted.
206Restarting of pending calls is requested
207by setting the
208.Dv SA_RESTART
209bit in
210.Ar sa_flags .
211The affected system calls include
212.Xr open 2 ,
213.Xr read 2 ,
214.Xr write 2 ,
215.Xr sendto 2 ,
216.Xr recvfrom 2 ,
217.Xr sendmsg 2
218and
219.Xr recvmsg 2
220on a communications channel or a slow device (such as a terminal,
221but not a regular file)
222and during a
223.Xr wait 2
224or
225.Xr ioctl 2 .
226However, calls that have already committed are not restarted,
227but instead return a partial success (for example, a short read count).
228.Pp
229After a
230.Xr fork 2
231or
232.Xr vfork 2
233all signals, the signal mask, the signal stack,
234and the restart/interrupt flags are inherited by the child.
235.Pp
236The
237.Xr execve 2
238system call reinstates the default
239action for all signals which were caught and
240resets all signals to be caught on the user stack.
241Ignored signals remain ignored;
242the signal mask remains the same;
243signals that restart pending system calls continue to do so.
244.Pp
245See
246.Xr signal 7
247for comprehensive list of supported signals.
248.Sh NOTES
249The mask specified in
250.Fa act
251is not allowed to block
252.Dv SIGKILL
253or
254.Dv SIGSTOP .
255This is enforced silently by the system.
256.Sh RETURN VALUES
257A 0 value indicates that the call succeeded.
258A \-1 return value indicates an error occurred and
259.Va errno
260is set to indicate the reason.
261.Sh EXAMPLES
262The handler routine can be declared:
263.Bd -literal -offset indent
264void
265handler(sig, code, scp)
266	int sig, code;
267	struct sigcontext *scp;
268.Ed
269.Pp
270Here
271.Fa sig
272is the signal number, into which the hardware faults and traps are
273mapped.
274.Fa code
275is a parameter that is either a constant
276or the code provided by the hardware.
277.Fa scp
278is a pointer to the
279.Fa sigcontext
280structure (defined in
281.Aq Pa signal.h ) ,
282used to restore the context from before the signal.
283.Pp
284For POSIX compliance, the
285.Fa sa_handler
286is declared to be (void (*)(int)) and the above handler will need to be
287casted to that type.
288Future versions of
289.Nx
290will replace the
291.Fa sigcontext
292interface with the
293.Fa siginfo
294interface.
295.Sh ERRORS
296.Fn sigaction
297will fail and no new signal handler will be installed if one
298of the following occurs:
299.Bl -tag -width Er
300.It Bq Er EFAULT
301Either
302.Fa act
303or
304.Fa oact
305points to memory that is not a valid part of the process
306address space.
307.It Bq Er EINVAL
308.Fa sig
309is not a valid signal number.
310.It Bq Er EINVAL
311An attempt is made to ignore or supply a handler for
312.Dv SIGKILL
313or
314.Dv SIGSTOP .
315.El
316.Sh SEE ALSO
317.Xr kill 1 ,
318.Xr kill 2 ,
319.Xr ptrace 2 ,
320.Xr sigaltstack 2 ,
321.Xr sigprocmask 2 ,
322.Xr sigsuspend 2 ,
323.Xr setjmp 3 ,
324.Xr sigsetops 3 ,
325.Xr tty 4 ,
326.Xr signal 7
327.Sh STANDARDS
328The
329.Fn sigaction
330function conforms to
331.St -p1003.1-90 .
332The
333.Dv SA_ONSTACK
334and
335.Dv SA_RESTART
336flags are Berkeley extensions, available on most
337.Bx Ns \-derived
338systems.
339