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