]> git.saurik.com Git - apple/xnu.git/blob - bsd/man/man2/sigaction.2
24435274ad092efeefb42755ff112f3b1c4f8851
[apple/xnu.git] / bsd / man / man2 / sigaction.2
1 .\" $NetBSD: sigaction.2,v 1.7 1995/10/12 15:41:16 jtc Exp $
2 .\"
3 .\" Copyright (c) 1980, 1990, 1993
4 .\" The Regents of the University of California. All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\"
34 .\" @(#)sigaction.2 8.2 (Berkeley) 4/3/94
35 .\"
36 .Dd April 3, 1994
37 .Dt SIGACTION 2
38 .Os
39 .Sh NAME
40 .Nm sigaction
41 .Nd software signal facilities
42 .Sh SYNOPSIS
43 .Fd #include <signal.h>
44 .Bd -literal
45 struct sigaction {
46 void (*sa_handler)();
47 sigset_t sa_mask;
48 int sa_flags;
49 };
50 .Ed
51 .Ft int
52 .Fn sigaction "int sig" "const struct sigaction *act" "struct sigaction *oact"
53 .Sh DESCRIPTION
54 The system defines a set of signals that may be delivered to a process.
55 Signal delivery resembles the occurrence of a hardware interrupt:
56 the signal is blocked from further occurrence, the current process
57 context is saved, and a new one is built. A process may specify a
58 .Em handler
59 to which a signal is delivered, or specify that a signal is to be
60 .Em ignored .
61 A process may also specify that a default action is to be taken
62 by the system when a signal occurs.
63 A signal may also be
64 .Em blocked ,
65 in which case its delivery is postponed until it is
66 .Em unblocked .
67 The action to be taken on delivery is determined at the time
68 of delivery.
69 Normally, signal handlers execute on the current stack
70 of the process. This may be changed, on a per-handler basis,
71 so that signals are taken on a special
72 .Em "signal stack" .
73 .Pp
74 Signal routines execute with the signal that caused their
75 invocation
76 .Em blocked ,
77 but other signals may yet occur.
78 A global
79 .Em "signal mask"
80 defines the set of signals currently blocked from delivery
81 to a process. The signal mask for a process is initialized
82 from that of its parent (normally empty). It
83 may be changed with a
84 .Xr sigprocmask 2
85 call, or when a signal is delivered to the process.
86 .Pp
87 When a signal
88 condition arises for a process, the signal is added to a set of
89 signals pending for the process.
90 If the signal is not currently
91 .Em blocked
92 by the process then it is delivered to the process.
93 Signals may be delivered any time a process enters the operating system
94 (e.g., during a system call, page fault or trap, or clock interrupt).
95 If multiple signals are ready to be delivered at the same time,
96 any signals that could be caused by traps are delivered first.
97 Additional signals may be processed at the same time, with each
98 appearing to interrupt the handlers for the previous signals
99 before their first instructions.
100 The set of pending signals is returned by the
101 .Xr sigpending 2
102 function.
103 When a caught signal
104 is delivered, the current state of the process is saved,
105 a new signal mask is calculated (as described below),
106 and the signal handler is invoked. The call to the handler
107 is arranged so that if the signal handling routine returns
108 normally the process will resume execution in the context
109 from before the signal's delivery.
110 If the process wishes to resume in a different context, then it
111 must arrange to restore the previous context itself.
112 .Pp
113 When a signal is delivered to a process a new signal mask is
114 installed for the duration of the process' signal handler
115 (or until a
116 .Xr sigprocmask
117 call is made).
118 This mask is formed by taking the union of the current signal mask set,
119 the signal to be delivered, and
120 the signal mask associated with the handler to be invoked.
121 .Pp
122 .Fn Sigaction
123 assigns an action for a specific signal.
124 If
125 .Fa act
126 is non-zero, it
127 specifies an action
128 .Pf ( Dv SIG_DFL ,
129 .Dv SIG_IGN ,
130 or a handler routine) and mask
131 to be used when delivering the specified signal.
132 If
133 .Fa oact
134 is non-zero, the previous handling information for the signal
135 is returned to the user.
136 .Pp
137 Once a signal handler is installed, it remains installed
138 until another
139 .Fn sigaction
140 call is made, or an
141 .Xr execve 2
142 is performed.
143 A signal-specific default action may be reset by
144 setting
145 .Fa sa_handler
146 to
147 .Dv SIG_DFL .
148 The defaults are process termination, possibly with core dump;
149 no action; stopping the process; or continuing the process.
150 See the signal list below for each signal's default action.
151 If
152 .Fa sa_handler
153 is
154 .Dv SIG_DFL ,
155 the default action for the signal is to discard the signal,
156 and if a signal is pending,
157 the pending signal is discarded even if the signal is masked.
158 If
159 .Fa sa_handler
160 is set to
161 .Dv SIG_IGN
162 current and pending instances
163 of the signal are ignored and discarded.
164 .Pp
165 Options may be specified by setting
166 .Em sa_flags .
167 If the
168 .Dv SA_NOCLDSTOP
169 bit is set when installing a catching function
170 for the
171 .Dv SIGCHLD
172 signal,
173 the
174 .Dv SIGCHLD
175 signal will be generated only when a child process exits,
176 not when a child process stops.
177 Further, if the
178 .Dv SA_ONSTACK
179 bit is set in
180 .Em sa_flags ,
181 the system will deliver the signal to the process on a
182 .Em "signal stack" ,
183 specified with
184 .Xr sigstack 2 .
185 .Pp
186 Finally, the
187 .Dv SA_SIGINFO
188 option causes the 2nd argument for the signal handler to be a pointer
189 to a
190 .Em siginfo_t
191 as described in
192 .Pa <sys/siginfo.h> .
193 The
194 .Em siginfo_t
195 is a part of
196 .St -p1003.1b .
197 and provides much more information about the causes and
198 attributes of the signal that is being delivered.
199 .Pp
200 If a signal is caught during the system calls listed below,
201 the call may be forced to terminate
202 with the error
203 .Dv EINTR ,
204 the call may return with a data transfer shorter than requested,
205 or the call may be restarted.
206 Restart of pending calls is requested
207 by setting the
208 .Dv SA_RESTART
209 bit in
210 .Ar sa_flags .
211 The affected system calls include
212 .Xr open 2 ,
213 .Xr read 2 ,
214 .Xr write 2 ,
215 .Xr sendto 2 ,
216 .Xr recvfrom 2 ,
217 .Xr sendmsg 2
218 and
219 .Xr recvmsg 2
220 on a communications channel or a slow device (such as a terminal,
221 but not a regular file)
222 and during a
223 .Xr wait 2
224 or
225 .Xr ioctl 2 .
226 However, calls that have already committed are not restarted,
227 but instead return a partial success (for example, a short read count).
228 .Pp
229 After a
230 .Xr fork 2
231 or
232 .Xr vfork 2
233 all signals, the signal mask, the signal stack,
234 and the restart/interrupt flags are inherited by the child.
235 .Pp
236 .Xr Execve 2
237 reinstates the default
238 action for all signals which were caught and
239 resets all signals to be caught on the user stack.
240 Ignored signals remain ignored;
241 the signal mask remains the same;
242 signals that restart pending system calls continue to do so.
243 .Pp
244 The following is a list of all signals
245 with names as in the include file
246 .Aq Pa signal.h :
247 .Bl -column SIGVTALARMXX "create core imagexxx"
248 .It Sy " NAME " " Default Action " " Description"
249 .It Dv SIGHUP No " terminate process" " terminal line hangup"
250 .It Dv SIGINT No " terminate process" " interrupt program"
251 .It Dv SIGQUIT No " create core image" " quit program"
252 .It Dv SIGILL No " create core image" " illegal instruction"
253 .It Dv SIGTRAP No " create core image" " trace trap"
254 .It Dv SIGABRT No " create core image" Xr abort 2
255 call (formerly
256 .Dv SIGIOT )
257 .It Dv SIGEMT No " create core image" " emulate instruction executed"
258 .It Dv SIGFPE No " create core image" " floating-point exception"
259 .It Dv SIGKILL No " terminate process" " kill program"
260 .It Dv SIGBUS No " create core image" " bus error"
261 .It Dv SIGSEGV No " create core image" " segmentation violation"
262 .It Dv SIGSYS No " create core image" " system call given invalid argument"
263 .It Dv SIGPIPE No " terminate process" " write on a pipe with no reader"
264 .It Dv SIGALRM No " terminate process" " real-time timer expired"
265 .It Dv SIGTERM No " terminate process" " software termination signal"
266 .It Dv SIGURG No " discard signal" " urgent condition present on socket"
267 .It Dv SIGSTOP No " stop process" " stop (cannot be caught or ignored)"
268 .It Dv SIGTSTP No " stop process" " stop signal generated from keyboard"
269 .It Dv SIGCONT No " discard signal" " continue after stop"
270 .It Dv SIGCHLD No " discard signal" " child status has changed"
271 .It Dv SIGTTIN No " stop process" " background read attempted from control terminal"
272 .It Dv SIGTTOU No " stop process" " background write attempted to control terminal"
273 .It Dv SIGIO No " discard signal" Tn " I/O"
274 is possible on a descriptor (see
275 .Xr fcntl 2 )
276 .It Dv SIGXCPU No " terminate process" " cpu time limit exceeded (see"
277 .Xr setrlimit 2 )
278 .It Dv SIGXFSZ No " terminate process" " file size limit exceeded (see"
279 .Xr setrlimit 2 )
280 .It Dv SIGVTALRM No " terminate process" " virtual time alarm (see"
281 .Xr setitimer 2 )
282 .It Dv SIGPROF No " terminate process" " profiling timer alarm (see"
283 .Xr setitimer 2 )
284 .It Dv SIGWINCH No " discard signal" " Window size change"
285 .It Dv SIGINFO No " discard signal" " status request from keyboard"
286 .It Dv SIGUSR1 No " terminate process" " User defined signal 1"
287 .It Dv SIGUSR2 No " terminate process" " User defined signal 2"
288 .El
289 .Sh NOTE
290 The mask specified in
291 .Fa act
292 is not allowed to block
293 .Dv SIGKILL
294 or
295 .Dv SIGSTOP .
296 This is done silently by the system.
297 .Sh RETURN VALUES
298 A 0 value indicated that the call succeeded. A \-1 return value
299 indicates an error occurred and
300 .Va errno
301 is set to indicated the reason.
302 .Sh EXAMPLE
303 The handler routine can be declared:
304 .Bd -literal -offset indent
305 void handler(sig, sip, scp)
306 int sig;
307 siginfo_t *sip;
308 struct sigcontext *scp;
309 .Ed
310 .Pp
311 Here
312 .Fa sig
313 is the signal number, into which the hardware faults and traps are
314 mapped.
315 If the
316 .Dv SA_SIGINFO
317 option is set,
318 .Fa sip
319 is a pointer to a
320 .Dv siginfo_t
321 as described in
322 .Pa <sys/siginfo.h> .
323 If
324 .Dv SA_SIGINFO
325 is not set, this is NULL.
326 .Fa Scp
327 is a pointer to the
328 .Fa sigcontext
329 structure (defined in
330 .Aq Pa signal.h ) ,
331 used to restore the context from before the signal.
332 .Sh ERRORS
333 .Fn Sigaction
334 will fail and no new signal handler will be installed if one
335 of the following occurs:
336 .Bl -tag -width Er
337 .It Bq Er EFAULT
338 Either
339 .Fa act
340 or
341 .Fa oact
342 points to memory that is not a valid part of the process
343 address space.
344 .It Bq Er EINVAL
345 .Fa Sig
346 is not a valid signal number.
347 .It Bq Er EINVAL
348 An attempt is made to ignore or supply a handler for
349 .Dv SIGKILL
350 or
351 .Dv SIGSTOP .
352 .El
353 .Sh STANDARDS
354 The
355 .Nm sigaction
356 function is defined by
357 .St -p1003.1-88 .
358 The
359 .Dv SA_ONSTACK
360 and
361 .Dv SA_RESTART
362 flags are Berkeley extensions,
363 as are the signals,
364 .Dv SIGTRAP ,
365 .Dv SIGEMT ,
366 .Dv SIGBUS ,
367 .Dv SIGSYS ,
368 .Dv SIGURG ,
369 .Dv SIGIO ,
370 .Dv SIGXCPU ,
371 .Dv SIGXFSZ ,
372 .Dv SIGVTALRM ,
373 .Dv SIGPROF ,
374 .Dv SIGWINCH ,
375 and
376 .Dv SIGINFO .
377 Those signals are available on most
378 .Tn BSD Ns \-derived
379 systems.
380 .Sh SEE ALSO
381 .Xr kill 1 ,
382 .Xr ptrace 2 ,
383 .Xr kill 2 ,
384 .Xr sigaction 2 ,
385 .Xr sigprocmask 2 ,
386 .Xr sigsuspend 2 ,
387 .Xr sigblock 2 ,
388 .Xr sigsetmask 2 ,
389 .Xr sigpause 2 ,
390 .Xr sigstack 2 ,
391 .Xr sigvec 3 ,
392 .Xr setjmp 3 ,
393 .Xr siginterrupt 3 ,
394 .Xr sigsetops 3 ,
395 .Xr tty 4