xref: /original-bsd/lib/libc/sys/sigaction.2 (revision e58c8952)
1.\" Copyright (c) 1980, 1990, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" %sccs.include.redist.roff%
5.\"
6.\"	@(#)sigaction.2	8.2 (Berkeley) 04/03/94
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 occurrence 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_DFL ,
126the default action for the signal is to discard the signal,
127and if a signal is pending,
128the pending signal is discarded even if the signal is masked.
129If
130.Fa sa_handler
131is set to
132.Dv SIG_IGN
133current and pending instances
134of the signal are ignored and discarded.
135.Pp
136Options may be specified by setting
137.Em sa_flags .
138If the
139.Dv SA_NOCLDSTOP
140bit is set when installing a catching function
141for the
142.Dv SIGCHLD
143signal,
144the
145.Dv SIGCHLD
146signal will be generated only when a child process exits,
147not when a child process stops.
148Further, if the
149.Dv SA_ONSTACK
150bit is set in
151.Em sa_flags ,
152the system will deliver the signal to the process on a
153.Em "signal stack" ,
154specified with
155.Xr sigstack 2 .
156.Pp
157If a signal is caught during the system calls listed below,
158the call may be forced to terminate
159with the error
160.Dv EINTR ,
161the call may return with a data transfer shorter than requested,
162or the call may be restarted.
163Restart of pending calls is requested
164by setting the
165.Dv SA_RESTART
166bit in
167.Ar sa_flags .
168The affected system calls include
169.Xr open 2 ,
170.Xr read 2 ,
171.Xr write 2 ,
172.Xr sendto 2 ,
173.Xr recvfrom 2 ,
174.Xr sendmsg 2
175and
176.Xr recvmsg 2
177on a communications channel or a slow device (such as a terminal,
178but not a regular file)
179and during a
180.Xr wait 2
181or
182.Xr ioctl 2 .
183However, calls that have already committed are not restarted,
184but instead return a partial success (for example, a short read count).
185.Pp
186After a
187.Xr fork 2
188or
189.Xr vfork 2
190all signals, the signal mask, the signal stack,
191and the restart/interrupt flags are inherited by the child.
192.Pp
193.Xr Execve 2
194reinstates the default
195action for all signals which were caught and
196resets all signals to be caught on the user stack.
197Ignored signals remain ignored;
198the signal mask remains the same;
199signals that restart pending system calls continue to do so.
200.Pp
201The following is a list of all signals
202with names as in the include file
203.Aq Pa signal.h :
204.Bl -column SIGVTALARMXX "create core imagexxx"
205.It Sy "  NAME  " "	Default Action  " "	Description"
206.It Dv SIGHUP No "	terminate process" "	terminal line hangup"
207.It Dv SIGINT No "	terminate process" "	interrupt program"
208.It Dv SIGQUIT No "	create core image" "	quit program"
209.It Dv SIGILL No "	create core image" "	illegal instruction"
210.It Dv SIGTRAP No "	create core image" "	trace trap"
211.It Dv SIGABRT No "	create core image" Xr 	abort 2
212call (formerly
213.Dv SIGIOT )
214.It Dv SIGEMT No "	create core image" "	emulate instruction executed"
215.It Dv SIGFPE No "	create core image" "	floating-point exception"
216.It Dv SIGKILL No "	terminate process" "	kill program"
217.It Dv SIGBUS No "	create core image" "	bus error"
218.It Dv SIGSEGV No "	create core image" "	segmentation violation"
219.It Dv SIGSYS No "	create core image" "	system call given invalid argument"
220.It Dv SIGPIPE No "	terminate process" "	write on a pipe with no reader"
221.It Dv SIGALRM No "	terminate process" "	real-time timer expired"
222.It Dv SIGTERM No "	terminate process" "	software termination signal"
223.It Dv SIGURG No "	discard signal" "	urgent condition present on socket"
224.It Dv SIGSTOP No "	stop process" "	stop (cannot be caught or ignored)"
225.It Dv SIGTSTP No "	stop process" "	stop signal generated from keyboard"
226.It Dv SIGCONT No "	discard signal" "	continue after stop"
227.It Dv SIGCHLD No "	discard signal" "	child status has changed"
228.It Dv SIGTTIN No "	stop process" "	background read attempted from control terminal"
229.It Dv SIGTTOU No "	stop process" "	background write attempted to control terminal"
230.It Dv SIGIO No "	discard signal" Tn "	I/O"
231is possible on a descriptor (see
232.Xr fcntl 2 )
233.It Dv SIGXCPU No "	terminate process" "	cpu time limit exceeded (see"
234.Xr setrlimit 2 )
235.It Dv SIGXFSZ No "	terminate process" "	file size limit exceeded (see"
236.Xr setrlimit 2 )
237.It Dv SIGVTALRM No "	terminate process" "	virtual time alarm (see"
238.Xr setitimer 2 )
239.It Dv SIGPROF No "	terminate process" "	profiling timer alarm (see"
240.Xr setitimer 2 )
241.It Dv SIGWINCH No "	discard signal" "	Window size change"
242.It Dv SIGINFO No "	discard signal" "	status request from keyboard"
243.It Dv SIGUSR1 No "	terminate process" "	User defined signal 1"
244.It Dv SIGUSR2 No "	terminate process" "	User defined signal 2"
245.El
246.Sh NOTE
247The mask specified in
248.Fa act
249is not allowed to block
250.Dv SIGKILL
251or
252.Dv SIGSTOP .
253This is done silently by the system.
254.Sh RETURN VALUES
255A 0 value indicated that the call succeeded.  A \-1 return value
256indicates an error occurred and
257.Va errno
258is set to indicated the reason.
259.Sh EXAMPLE
260The handler routine can be declared:
261.Bd -literal -offset indent
262void handler(sig, code, scp)
263int sig, code;
264struct sigcontext *scp;
265.Ed
266.Pp
267Here
268.Fa sig
269is the signal number, into which the hardware faults and traps are
270mapped.
271.Fa Code
272is a parameter that is either a constant
273or the code provided by
274the hardware.
275.Fa Scp
276is a pointer to the
277.Fa sigcontext
278structure (defined in
279.Aq Pa signal.h ) ,
280used to restore the context from before the signal.
281.Sh ERRORS
282.Fn Sigaction
283will fail and no new signal handler will be installed if one
284of the following occurs:
285.Bl -tag -width Er
286.It Bq Er EFAULT
287Either
288.Fa act
289or
290.Fa oact
291points to memory that is not a valid part of the process
292address space.
293.It Bq Er EINVAL
294.Fa Sig
295is not a valid signal number.
296.It Bq Er EINVAL
297An attempt is made to ignore or supply a handler for
298.Dv SIGKILL
299or
300.Dv SIGSTOP .
301.El
302.Sh STANDARDS
303The
304.Nm sigaction
305function is defined by
306.St -p1003.1-88 .
307The
308.Dv SA_ONSTACK
309and
310.Dv SA_RESTART
311flags are Berkeley extensions,
312as are the signals,
313.Dv SIGTRAP ,
314.Dv SIGEMT ,
315.Dv SIGBUS ,
316.Dv SIGSYS ,
317.Dv SIGURG ,
318.Dv SIGIO ,
319.Dv SIGXCPU ,
320.Dv SIGXFSZ ,
321.Dv SIGVTALRM ,
322.Dv SIGPROF ,
323.Dv SIGWINCH ,
324and
325.Dv SIGINFO .
326Those signals are available on most
327.Tn BSD Ns \-derived
328systems.
329.Sh SEE ALSO
330.Xr kill 1 ,
331.Xr ptrace 2 ,
332.Xr kill 2 ,
333.Xr sigaction 2 ,
334.Xr sigprocmask 2 ,
335.Xr sigsuspend 2 ,
336.Xr sigblock 2 ,
337.Xr sigsetmask 2 ,
338.Xr sigpause 2 ,
339.Xr sigstack 2 ,
340.Xr sigvec 2 ,
341.Xr setjmp 3 ,
342.Xr siginterrupt 3 ,
343.Xr sigsetops 3 ,
344.Xr tty 4
345