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