.Os changes, miscellaneous cleanups
[unix-history] / usr / src / lib / libc / sys / sigaction.2
CommitLineData
87714767
MK
1.\" Copyright (c) 1980, 1990 The Regents of the University of California.
2.\" All rights reserved.
3.\"
faf7e3e0 4.\" %sccs.include.redist.roff%
87714767 5.\"
faf7e3e0 6.\" @(#)sigaction.2 6.3 (Berkeley) %G%
87714767 7.\"
e17b717a
CL
8.Dd
9.Dt SIGACTION 2
faf7e3e0 10.Os
e17b717a
CL
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
87714767
MK
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
e17b717a 29.Em handler
87714767 30to which a signal is delivered, or specify that a signal is to be
e17b717a 31.Em ignored .
87714767
MK
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
e17b717a 35.Em blocked ,
87714767 36in which case its delivery is postponed until it is
e17b717a 37.Em unblocked .
87714767
MK
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
e17b717a
CL
43.Em "signal stack" .
44.Pp
87714767
MK
45Signal routines execute with the signal that caused their
46invocation
e17b717a 47.Em blocked ,
87714767
MK
48but other signals may yet occur.
49A global
e17b717a 50.Em "signal mask"
87714767
MK
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
e17b717a 55.Xr sigprocmask 2
87714767 56call, or when a signal is delivered to the process.
e17b717a 57.Pp
87714767
MK
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
e17b717a 62.Em blocked
87714767
MK
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
e17b717a 72.Xr sigpending 2
87714767
MK
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.
e17b717a 83.Pp
87714767
MK
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
e17b717a 87.Xr sigprocmask
87714767
MK
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.
e17b717a
CL
92.Pp
93.Fn Sigaction
87714767
MK
94assigns an action for a specific signal.
95If
e17b717a 96.Fa act
87714767 97is non-zero, it
e17b717a
CL
98specifies an action
99.Pf ( Dv SIG_DFL ,
100.Dv SIG_IGN ,
101or a handler routine) and mask
87714767
MK
102to be used when delivering the specified signal.
103If
e17b717a 104.Fa oact
87714767
MK
105is non-zero, the previous handling information for the signal
106is returned to the user.
e17b717a 107.Pp
87714767
MK
108Once a signal handler is installed, it remains installed
109until another
e17b717a 110.Fn sigaction
87714767 111call is made, or an
e17b717a 112.Xr execve 2
87714767 113is performed.
e17b717a
CL
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;
87714767
MK
120no action; stopping the process; or continuing the process.
121See the signal list below for each signal's default action.
122If
e17b717a
CL
123.Fa sa_handler
124is
125.Dv SIG_IGN
126current and pending instances
127of the signal are ignored and discarded.
128.Pp
87714767 129Options may be specified by setting
e17b717a
CL
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,
87714767 140not when a child process stops.
e17b717a
CL
141Further, if the
142.Dv SA_ONSTACK
143bit is set in
144.Em sa_flags ,
87714767 145the system will deliver the signal to the process on a
e17b717a 146.Em "signal stack" ,
87714767 147specified with
e17b717a
CL
148.Xr sigstack 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 ,
87714767
MK
154or the call may be restarted.
155Restart of pending calls is requested
e17b717a
CL
156by setting the
157.Dv SA_RESTART
158bit in
159.Ar sa_flags .
87714767 160The affected system calls include
e17b717a
CL
161.Xr read 2 ,
162.Xr write 2 ,
163.Xr sendto 2 ,
164.Xr recvfrom 2 ,
165.Xr sendmsg 2
87714767 166and
e17b717a 167.Xr recvmsg 2
87714767
MK
168on a communications channel or a slow device (such as a terminal,
169but not a regular file)
170and during a
e17b717a 171.Xr wait 2
87714767 172or
e17b717a 173.Xr ioctl 2 .
87714767
MK
174However, calls that have already committed are not restarted,
175but instead return a partial success (for example, a short read count).
e17b717a 176.Pp
87714767 177After a
e17b717a 178.Xr fork 2
87714767 179or
e17b717a 180.Xr vfork 2
87714767 181all signals, the signal mask, the signal stack,
e17b717a
CL
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
87714767
MK
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.
e17b717a 191.Pp
87714767
MK
192The following is a list of all signals
193with names as in the include file
e17b717a
CL
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
87714767 238The mask specified in
e17b717a
CL
239.Fa act
240is not allowed to block
241.Dv SIGKILL
242or
243.Dv SIGSTOP
87714767 244This is done silently by the system.
e17b717a 245.Sh RETURN VALUES
87714767
MK
246A 0 value indicated that the call succeeded. A \-1 return value
247indicates an error occurred and
e17b717a 248.Va errno
87714767 249is set to indicated the reason.
e17b717a
CL
250.Sh ERROR
251.Fn Sigaction
87714767
MK
252will fail and no new signal handler will be installed if one
253of the following occurs:
e17b717a
CL
254.Tw Er
255.Tl Bq Er EFAULT
87714767 256Either
e17b717a 257.Fa act
87714767 258or
e17b717a 259.Fa oact
87714767
MK
260points to memory that is not a valid part of the process
261address space.
e17b717a
CL
262.Tl Bq Er EINVAL
263.Fa Sig
87714767 264is not a valid signal number.
e17b717a
CL
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
faf7e3e0 273.Nm sigaction
e17b717a
CL
274function is defined by
275.St -p1003.1-88 .
87714767 276The
e17b717a
CL
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 sigprocmask 2 ,
304.Em sigsetops 2 ,
305.Xr sigsuspend 2 ,
306.Xr sigblock 2 ,
307.Xr sigsetmask 2 ,
308.Xr sigpause 2 ,
309.Xr sigstack 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
87714767 324Here
e17b717a 325.Fa sig
87714767
MK
326is the signal number, into which the hardware faults and traps are
327mapped as defined below.
e17b717a 328.Em Code
87714767 329is a parameter that is either a constant
e17b717a 330as given below or the code provided by
87714767 331the hardware (Compatibility mode faults are distinguished from the
e17b717a
CL
332other
333.Dv SIGILL
334traps by having
335.Dv PSL_CM
336set in the psl).
337.Fa Scp
87714767 338is a pointer to the
e17b717a 339.Fa sigcontext
87714767 340structure (defined in
e17b717a 341.Aq Pa signal.h ) ,
87714767 342used to restore the context from before the signal.