xref: /netbsd-src/lib/libc/sys/sigaction.2 (revision 5f7096188587a2c7c95fa3c69b78e1ec9c7923d0)
1.\" Copyright (c) 1980, 1990 The Regents of the University of California.
2.\" 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	6.3 (Berkeley) 7/23/91
33.\"	$Id: sigaction.2,v 1.3 1993/10/15 00:05:58 jtc Exp $
34.\"
35.Dd July 23, 1991
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
44struct sigaction {
45	void     (*sa_handler)();
46	sigset_t sa_mask;
47	int	 sa_flags;
48};
49.Ed
50.Fn sigaction "int sig" "struct sigaction *act" "struct sigaction *oact"
51.Sh DESCRIPTION
52The system defines a set of signals that may be delivered to a process.
53Signal delivery resembles the occurence of a hardware interrupt:
54the signal is blocked from further occurrence, the current process
55context is saved, and a new one is built.  A process may specify a
56.Em handler
57to which a signal is delivered, or specify that a signal is to be
58.Em ignored .
59A process may also specify that a default action is to be taken
60by the system when a signal occurs.
61A signal may also be
62.Em blocked ,
63in which case its delivery is postponed until it is
64.Em unblocked .
65The action to be taken on delivery is determined at the time
66of delivery.
67Normally, signal handlers execute on the current stack
68of the process.  This may be changed, on a per-handler basis,
69so that signals are taken on a special
70.Em "signal stack" .
71.Pp
72Signal routines execute with the signal that caused their
73invocation
74.Em blocked ,
75but other signals may yet occur.
76A global
77.Em "signal mask"
78defines the set of signals currently blocked from delivery
79to a process.  The signal mask for a process is initialized
80from that of its parent (normally empty).  It
81may be changed with a
82.Xr sigprocmask 2
83call, or when a signal is delivered to the process.
84.Pp
85When a signal
86condition arises for a process, the signal is added to a set of
87signals pending for the process.
88If the signal is not currently
89.Em blocked
90by the process then it is delivered to the process.
91Signals may be delivered any time a process enters the operating system
92(e.g., during a system call, page fault or trap, or clock interrupt).
93If multiple signals are ready to be delivered at the same time,
94any signals that could be caused by traps are delivered first.
95Additional signals may be processed at the same time, with each
96appearing to interrupt the handlers for the previous signals
97before their first instructions.
98The set of pending signals is returned by the
99.Xr sigpending 2
100function.
101When a caught signal
102is delivered, the current state of the process is saved,
103a new signal mask is calculated (as described below),
104and the signal handler is invoked.  The call to the handler
105is arranged so that if the signal handling routine returns
106normally the process will resume execution in the context
107from before the signal's delivery.
108If the process wishes to resume in a different context, then it
109must arrange to restore the previous context itself.
110.Pp
111When a signal is delivered to a process a new signal mask is
112installed for the duration of the process' signal handler
113(or until a
114.Xr sigprocmask
115call is made).
116This mask is formed by taking the union of the current signal mask set,
117the signal to be delivered, and
118the signal mask associated with the handler to be invoked.
119.Pp
120.Fn Sigaction
121assigns an action for a specific signal.
122If
123.Fa act
124is non-zero, it
125specifies an action
126.Pf ( Dv SIG_DFL ,
127.Dv SIG_IGN ,
128or a handler routine) and mask
129to be used when delivering the specified signal.
130If
131.Fa oact
132is non-zero, the previous handling information for the signal
133is returned to the user.
134.Pp
135Once a signal handler is installed, it remains installed
136until another
137.Fn sigaction
138call is made, or an
139.Xr execve 2
140is performed.
141A signal-specific default action may be reset by
142setting
143.Fa sa_handler
144to
145.Dv SIG_DFL .
146The defaults are process termination, possibly with core dump;
147no action; stopping the process; or continuing the process.
148See the signal list below for each signal's default action.
149If
150.Fa sa_handler
151is
152.Dv SIG_IGN
153current and pending instances
154of the signal are ignored and discarded.
155.Pp
156Options may be specified by setting
157.Em sa_flags .
158If the
159.Dv SA_NOCLDSTOP
160bit is set when installing a catching function
161for the
162.Dv SIGCHLD
163signal,
164the
165.Dv SIGCHLD
166signal will be generated only when a child process exits,
167not when a child process stops.
168Further, if the
169.Dv SA_ONSTACK
170bit is set in
171.Em sa_flags ,
172the system will deliver the signal to the process on a
173.Em "signal stack" ,
174specified with
175.Xr sigstack 2 .
176.Pp
177If a signal is caught during the system calls listed below,
178the call may be forced to terminate
179with the error
180.Dv EINTR ,
181or the call may be restarted.
182Restart of pending calls is requested
183by setting the
184.Dv SA_RESTART
185bit in
186.Ar sa_flags .
187The affected system calls include
188.Xr read 2 ,
189.Xr write 2 ,
190.Xr sendto 2 ,
191.Xr recvfrom 2 ,
192.Xr sendmsg 2
193and
194.Xr recvmsg 2
195on a communications channel or a slow device (such as a terminal,
196but not a regular file)
197and during a
198.Xr wait 2
199or
200.Xr ioctl 2 .
201However, calls that have already committed are not restarted,
202but instead return a partial success (for example, a short read count).
203.Pp
204After a
205.Xr fork 2
206or
207.Xr vfork 2
208all signals, the signal mask, the signal stack,
209and the restart/interrupt flags are inherited by the child.
210.Pp
211.Xr Execve 2
212reinstates the default
213action for all signals which were caught and
214resets all signals to be caught on the user stack.
215Ignored signals remain ignored;
216the signal mask remains the same;
217signals that restart pending system calls continue to do so.
218.Pp
219The following is a list of all signals
220with names as in the include file
221.Aq Pa signal.h :
222.Bl -column SIGVTALARMXX "create core imagexxx"
223.It Sy "  NAME  " "	  Default Action  " "	              Description"
224.It Dv SIGHUP No "	terminate process" "	terminal line hangup"
225.It Dv SIGINT No "	terminate process" "	interrupt program"
226.It Dv SIGQUIT No "	create core image" "	quit program"
227.It Dv SIGILL No "	create core image" "	illegal instruction"
228.It Dv SIGTRAP No "	create core image" "	trace trap"
229.It Dv SIGABRT No "	create core image" Xr 	abort 2
230call (formerly
231.Dv SIGIOT )
232.It Dv SIGEMT No "	create core image" "	emulate instruction executed"
233.It Dv SIGFPE No "	create core image" "	floating-point exception"
234.It Dv SIGKILL No "	terminate process" "	kill program"
235.It Dv SIGBUS No "	create core image" "	bus error"
236.It Dv SIGSEGV No "	create core image" "	segmentation violation"
237.It Dv SIGSYS No "	create core image" "	system call given invalid argument"
238.It Dv SIGPIPE No "	terminate process" "	write on a pipe with no reader"
239.It Dv SIGALRM No "	terminate process" "	real-time timer expired"
240.It Dv SIGTERM No "	terminate process" "	software termination signal"
241.It Dv SIGURG No "	discard signal" "	urgent condition present on socket"
242.It Dv SIGSTOP No "	stop process" "	stop (cannot be caught or ignored)"
243.It Dv SIGTSTP No "	stop process" "	stop signal generated from keyboard"
244.It Dv SIGCONT No "	discard signal" "	continue after stop"
245.It Dv SIGCHLD No "	discard signal" "	child status has changed"
246.It Dv SIGTTIN No "	stop process" "	background read attempted from control terminal"
247.It Dv SIGTTOU No "	stop process" "	background write attempted to control terminal"
248.It Dv SIGIO No "	discard signal" Tn "	I/O"
249is possible on a descriptor (see
250.Xr fcntl 2 )
251.It Dv SIGXCPU No "	terminate process" "	cpu time limit exceeded (see"
252.Xr setrlimit 2 )
253.It Dv SIGXFSZ No "	terminate process" "	file size limit exceeded (see"
254.Xr setrlimit 2 )
255.It Dv SIGVTALRM No "	terminate process" "	virtual time alarm (see"
256.Xr setitimer 2 )
257.It Dv SIGPROF No "	terminate process" "	profiling timer alarm (see"
258.Xr setitimer 2 )
259.It Dv SIGWINCH No "	discard signal" "	Window size change"
260.It Dv SIGINFO No "	discard signal" "	status request from keyboard"
261.It Dv SIGUSR1 No "	terminate process" "	User defined signal 1"
262.It Dv SIGUSR2 No "	terminate process" "	User defined signal 2"
263.El
264.Sh NOTE
265The mask specified in
266.Fa act
267is not allowed to block
268.Dv SIGKILL
269or
270.Dv SIGSTOP
271This is done silently by the system.
272.Sh RETURN VALUES
273A 0 value indicated that the call succeeded.  A \-1 return value
274indicates an error occurred and
275.Va errno
276is set to indicated the reason.
277.Sh ERROR
278.Fn Sigaction
279will fail and no new signal handler will be installed if one
280of the following occurs:
281.Tw Er
282.Tl Bq Er EFAULT
283Either
284.Fa act
285or
286.Fa oact
287points to memory that is not a valid part of the process
288address space.
289.Tl Bq Er EINVAL
290.Fa Sig
291is not a valid signal number.
292.Tl Bq Er EINVAL
293An attempt is made to ignore or supply a handler for
294.Em SIGKIL
295or
296.Dv SIGSTOP
297.Tl
298.Sh STANDARD
299The
300.Nm sigaction
301function is defined by
302.St -p1003.1-88 .
303The
304.Dv SA_ONSTACK
305and
306.Dv SA_RESTART
307flags are Berkeley extensions,
308as are the signals,
309.Dv SIGTRAP ,
310.Dv SIGEMT ,
311.Dv SIGBUS ,
312.Dv SIGSYS ,
313.Dv SIGURG ,
314.Dv SIGIO ,
315.Dv SIGXCPU ,
316.Dv SIGXFSZ ,
317.Dv SIGVTALRM ,
318.Dv SIGPROF ,
319.Dv SIGWINCH ,
320and
321.Dv SIGINFO .
322Most of those signals are available on most
323.Tn BSD Ns \-derived
324systems.
325.Sh SEE ALSO
326.Xr kill 1 ,
327.Xr ptrace 2 ,
328.Xr kill 2 ,
329.Xr sigaction 2 ,
330.Xr sigprocmask 2 ,
331.Xr sigsetops 2 ,
332.Xr sigsuspend 2 ,
333.Xr sigblock 2 ,
334.Xr sigsetmask 2 ,
335.Xr sigpause 2 ,
336.Xr sigstack 2 ,
337.Xr sigvec 2 ,
338.Xr setjmp 3 ,
339.Xr siginterrupt 3 ,
340.Xr tty 4
341.Sh EXAMPLE
342On a
343.Tn VAX\-11,
344the handler routine can be declared:
345.Bd -literal -offset indent
346void handler(sig, code, scp)
347int sig, code;
348struct sigcontext *scp;
349.Ed
350.Pp
351Here
352.Fa sig
353is the signal number, into which the hardware faults and traps are
354mapped as defined below.
355.Em Code
356is a parameter that is either a constant
357as given below or the code provided by
358the hardware (Compatibility mode faults are distinguished from the
359other
360.Dv SIGILL
361traps by having
362.Dv PSL_CM
363set in the psl).
364.Fa Scp
365is a pointer to the
366.Fa sigcontext
367structure (defined in
368.Aq Pa signal.h ) ,
369used to restore the context from before the signal.
370