]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/sigaction.2
xnu-1228.9.59.tar.gz
[apple/xnu.git] / bsd / man / man2 / sigaction.2
CommitLineData
9bccf70c
A
1.\" Copyright (c) 1980, 1990, 1993
2.\" The Regents of the University of California. 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.\"
55e303ae
A
32.\" From: @(#)sigaction.2 8.2 (Berkeley) 4/3/94
33.\" $FreeBSD: src/lib/libc/sys/sigaction.2,v 1.48 2003/03/24 16:07:19 charnier Exp $
9bccf70c
A
34.\"
35.Dd April 3, 1994
36.Dt SIGACTION 2
37.Os
38.Sh NAME
39.Nm sigaction
40.Nd software signal facilities
55e303ae
A
41.Sh LIBRARY
42.Lb libc
9bccf70c 43.Sh SYNOPSIS
55e303ae 44.In signal.h
9bccf70c 45.Bd -literal
55e303ae
A
46struct sigaction {
47 union {
48 void (*__sa_handler)(int);
49 void (*__sa_sigaction)(int, struct __siginfo *, void *);
50 } __sigaction_u; /* signal handler */
51 int sa_flags; /* see signal options below */
52 sigset_t sa_mask; /* signal mask to apply */
9bccf70c 53};
55e303ae
A
54
55#define sa_handler __sigaction_u.__sa_handler
56#define sa_sigaction __sigaction_u.__sa_sigaction
9bccf70c
A
57.Ed
58.Ft int
55e303ae
A
59.Fo sigaction
60.Fa "int sig"
2d21ac55
A
61.Fa "const struct sigaction *restrict act"
62.Fa "struct sigaction *restrict oact"
55e303ae 63.Fc
9bccf70c
A
64.Sh DESCRIPTION
65The system defines a set of signals that may be delivered to a process.
66Signal delivery resembles the occurrence of a hardware interrupt:
55e303ae 67the signal is normally blocked from further occurrence, the current process
9bccf70c
A
68context is saved, and a new one is built. A process may specify a
69.Em handler
55e303ae 70to which a signal is delivered, or specify that a signal is to be
9bccf70c
A
71.Em ignored .
72A process may also specify that a default action is to be taken
73by the system when a signal occurs.
74A signal may also be
75.Em blocked ,
76in which case its delivery is postponed until it is
77.Em unblocked .
78The action to be taken on delivery is determined at the time
79of delivery.
80Normally, signal handlers execute on the current stack
81of the process. This may be changed, on a per-handler basis,
82so that signals are taken on a special
83.Em "signal stack" .
84.Pp
55e303ae 85Signal routines normally execute with the signal that caused their
9bccf70c
A
86invocation
87.Em blocked ,
88but other signals may yet occur.
55e303ae 89A global
9bccf70c
A
90.Em "signal mask"
91defines the set of signals currently blocked from delivery
92to a process. The signal mask for a process is initialized
93from that of its parent (normally empty). It
94may be changed with a
95.Xr sigprocmask 2
96call, or when a signal is delivered to the process.
97.Pp
98When a signal
99condition arises for a process, the signal is added to a set of
100signals pending for the process.
101If the signal is not currently
102.Em blocked
103by the process then it is delivered to the process.
104Signals may be delivered any time a process enters the operating system
105(e.g., during a system call, page fault or trap, or clock interrupt).
106If multiple signals are ready to be delivered at the same time,
107any signals that could be caused by traps are delivered first.
108Additional signals may be processed at the same time, with each
109appearing to interrupt the handlers for the previous signals
110before their first instructions.
111The set of pending signals is returned by the
112.Xr sigpending 2
55e303ae 113system call.
9bccf70c
A
114When a caught signal
115is delivered, the current state of the process is saved,
55e303ae 116a new signal mask is calculated (as described below),
9bccf70c
A
117and the signal handler is invoked. The call to the handler
118is arranged so that if the signal handling routine returns
119normally the process will resume execution in the context
120from before the signal's delivery.
121If the process wishes to resume in a different context, then it
122must arrange to restore the previous context itself.
123.Pp
124When a signal is delivered to a process a new signal mask is
125installed for the duration of the process' signal handler
126(or until a
55e303ae
A
127.Xr sigprocmask 2
128system call is made).
9bccf70c 129This mask is formed by taking the union of the current signal mask set,
55e303ae 130the signal to be delivered, and
9bccf70c
A
131the signal mask associated with the handler to be invoked.
132.Pp
55e303ae
A
133The
134.Fn sigaction
135system call
136assigns an action for a signal specified by
137.Fa sig .
9bccf70c
A
138If
139.Fa act
140is non-zero, it
141specifies an action
55e303ae 142.Dv ( SIG_DFL ,
9bccf70c
A
143.Dv SIG_IGN ,
144or a handler routine) and mask
145to be used when delivering the specified signal.
55e303ae 146If
9bccf70c
A
147.Fa oact
148is non-zero, the previous handling information for the signal
149is returned to the user.
150.Pp
55e303ae 151Once a signal handler is installed, it normally remains installed
9bccf70c
A
152until another
153.Fn sigaction
55e303ae 154system call is made, or an
9bccf70c
A
155.Xr execve 2
156is performed.
157A signal-specific default action may be reset by
158setting
159.Fa sa_handler
160to
161.Dv SIG_DFL .
162The defaults are process termination, possibly with core dump;
163no action; stopping the process; or continuing the process.
164See the signal list below for each signal's default action.
165If
166.Fa sa_handler
167is
168.Dv SIG_DFL ,
169the default action for the signal is to discard the signal,
170and if a signal is pending,
171the pending signal is discarded even if the signal is masked.
172If
173.Fa sa_handler
174is set to
175.Dv SIG_IGN
176current and pending instances
177of the signal are ignored and discarded.
178.Pp
179Options may be specified by setting
55e303ae
A
180.Va sa_flags .
181The meaning of the various bits is as follows:
182.Bl -tag -offset indent -width SA_RESETHANDXX
183.It Dv SA_NOCLDSTOP
184If this bit is set when installing a catching function
9bccf70c
A
185for the
186.Dv SIGCHLD
187signal,
188the
189.Dv SIGCHLD
190signal will be generated only when a child process exits,
191not when a child process stops.
55e303ae
A
192.It Dv SA_NOCLDWAIT
193If this bit is set when calling
194.Fn sigaction
195for the
196.Dv SIGCHLD
197signal, the system will not create zombie processes when children of
198the calling process exit. If the calling process subsequently issues
199a
200.Xr wait 2
201(or equivalent), it blocks until all of the calling process's child
202processes terminate, and then returns a value of -1 with errno set to
203.Er ECHILD .
204.It Dv SA_ONSTACK
205If this bit is set, the system will deliver the signal to the process
206on a
9bccf70c
A
207.Em "signal stack" ,
208specified with
55e303ae
A
209.Xr sigaltstack 2 .
210.It Dv SA_NODEFER
211If this bit is set, further occurrences of the delivered signal are
212not masked during the execution of the handler.
213.It Dv SA_RESETHAND
214If this bit is set, the handler is reset back to
215.Dv SIG_DFL
216at the moment the signal is delivered.
217.It Dv SA_RESTART
218See paragraph below.
219.It Dv SA_SIGINFO
220If this bit is set, the handler function is assumed to be pointed to by the
221.Dv sa_sigaction
222member of struct sigaction and should match the prototype shown above or as
223below in
224.Sx EXAMPLES .
225This bit should not be set when assigning
226.Dv SIG_DFL
227or
228.Dv SIG_IGN .
229.El
9bccf70c
A
230.Pp
231If a signal is caught during the system calls listed below,
232the call may be forced to terminate
233with the error
55e303ae 234.Er EINTR ,
9bccf70c
A
235the call may return with a data transfer shorter than requested,
236or the call may be restarted.
237Restart of pending calls is requested
238by setting the
239.Dv SA_RESTART
240bit in
55e303ae 241.Va sa_flags .
9bccf70c
A
242The affected system calls include
243.Xr open 2 ,
244.Xr read 2 ,
245.Xr write 2 ,
246.Xr sendto 2 ,
247.Xr recvfrom 2 ,
248.Xr sendmsg 2
249and
250.Xr recvmsg 2
251on a communications channel or a slow device (such as a terminal,
252but not a regular file)
253and during a
254.Xr wait 2
255or
256.Xr ioctl 2 .
257However, calls that have already committed are not restarted,
258but instead return a partial success (for example, a short read count).
259.Pp
260After a
261.Xr fork 2
262or
263.Xr vfork 2
264all signals, the signal mask, the signal stack,
265and the restart/interrupt flags are inherited by the child.
266.Pp
55e303ae
A
267The
268.Xr execve 2
269system call reinstates the default
9bccf70c
A
270action for all signals which were caught and
271resets all signals to be caught on the user stack.
272Ignored signals remain ignored;
273the signal mask remains the same;
274signals that restart pending system calls continue to do so.
275.Pp
276The following is a list of all signals
277with names as in the include file
278.Aq Pa signal.h :
279.Bl -column SIGVTALARMXX "create core imagexxx"
55e303ae 280.It Sy "NAME Default Action Description"
9bccf70c
A
281.It Dv SIGHUP No " terminate process" " terminal line hangup"
282.It Dv SIGINT No " terminate process" " interrupt program"
283.It Dv SIGQUIT No " create core image" " quit program"
284.It Dv SIGILL No " create core image" " illegal instruction"
285.It Dv SIGTRAP No " create core image" " trace trap"
55e303ae 286.It Dv SIGABRT No " create core image" Ta Xr abort 3
9bccf70c
A
287call (formerly
288.Dv SIGIOT )
289.It Dv SIGEMT No " create core image" " emulate instruction executed"
290.It Dv SIGFPE No " create core image" " floating-point exception"
291.It Dv SIGKILL No " terminate process" " kill program"
292.It Dv SIGBUS No " create core image" " bus error"
293.It Dv SIGSEGV No " create core image" " segmentation violation"
55e303ae 294.It Dv SIGSYS No " create core image" " non-existent system call invoked"
9bccf70c
A
295.It Dv SIGPIPE No " terminate process" " write on a pipe with no reader"
296.It Dv SIGALRM No " terminate process" " real-time timer expired"
297.It Dv SIGTERM No " terminate process" " software termination signal"
298.It Dv SIGURG No " discard signal" " urgent condition present on socket"
299.It Dv SIGSTOP No " stop process" " stop (cannot be caught or ignored)"
300.It Dv SIGTSTP No " stop process" " stop signal generated from keyboard"
301.It Dv SIGCONT No " discard signal" " continue after stop"
302.It Dv SIGCHLD No " discard signal" " child status has changed"
303.It Dv SIGTTIN No " stop process" " background read attempted from control terminal"
304.It Dv SIGTTOU No " stop process" " background write attempted to control terminal"
305.It Dv SIGIO No " discard signal" Tn " I/O"
306is possible on a descriptor (see
307.Xr fcntl 2 )
308.It Dv SIGXCPU No " terminate process" " cpu time limit exceeded (see"
309.Xr setrlimit 2 )
310.It Dv SIGXFSZ No " terminate process" " file size limit exceeded (see"
311.Xr setrlimit 2 )
312.It Dv SIGVTALRM No " terminate process" " virtual time alarm (see"
313.Xr setitimer 2 )
314.It Dv SIGPROF No " terminate process" " profiling timer alarm (see"
315.Xr setitimer 2 )
316.It Dv SIGWINCH No " discard signal" " Window size change"
317.It Dv SIGINFO No " discard signal" " status request from keyboard"
318.It Dv SIGUSR1 No " terminate process" " User defined signal 1"
319.It Dv SIGUSR2 No " terminate process" " User defined signal 2"
320.El
321.Sh NOTE
55e303ae
A
322The
323.Fa sa_mask
324field specified in
9bccf70c
A
325.Fa act
326is not allowed to block
327.Dv SIGKILL
328or
329.Dv SIGSTOP .
55e303ae
A
330Any attempt to do so will be silently ignored.
331.Pp
332The following functions are either reentrant or not interruptible
333by signals and are async-signal safe.
334Therefore applications may
335invoke them, without restriction, from signal-catching functions:
336.Pp
337Base Interfaces:
338.Pp
339.Fn _exit ,
340.Fn access ,
341.Fn alarm ,
342.Fn cfgetispeed ,
343.Fn cfgetospeed ,
344.Fn cfsetispeed ,
345.Fn cfsetospeed ,
346.Fn chdir ,
347.Fn chmod ,
348.Fn chown ,
349.Fn close ,
350.Fn creat ,
351.Fn dup ,
352.Fn dup2 ,
353.Fn execle ,
354.Fn execve ,
355.Fn fcntl ,
356.Fn fork ,
357.Fn fpathconf ,
358.Fn fstat ,
359.Fn fsync ,
360.Fn getegid ,
361.Fn geteuid ,
362.Fn getgid ,
363.Fn getgroups ,
364.Fn getpgrp ,
365.Fn getpid ,
366.Fn getppid ,
367.Fn getuid ,
368.Fn kill ,
369.Fn link ,
370.Fn lseek ,
371.Fn mkdir ,
372.Fn mkfifo ,
373.Fn open ,
374.Fn pathconf ,
375.Fn pause ,
376.Fn pipe ,
377.Fn raise ,
378.Fn read ,
379.Fn rename ,
380.Fn rmdir ,
381.Fn setgid ,
382.Fn setpgid ,
383.Fn setsid ,
384.Fn setuid ,
385.Fn sigaction ,
386.Fn sigaddset ,
387.Fn sigdelset ,
388.Fn sigemptyset ,
389.Fn sigfillset ,
390.Fn sigismember ,
391.Fn signal ,
392.Fn sigpending ,
393.Fn sigprocmask ,
394.Fn sigsuspend ,
395.Fn sleep ,
396.Fn stat ,
397.Fn sysconf ,
398.Fn tcdrain ,
399.Fn tcflow ,
400.Fn tcflush ,
401.Fn tcgetattr ,
402.Fn tcgetpgrp ,
403.Fn tcsendbreak ,
404.Fn tcsetattr ,
405.Fn tcsetpgrp ,
406.Fn time ,
407.Fn times ,
408.Fn umask ,
409.Fn uname ,
410.Fn unlink ,
411.Fn utime ,
412.Fn wait ,
413.Fn waitpid ,
414.Fn write .
415.Pp
416Realtime Interfaces:
417.Pp
418.Fn aio_error ,
419.Fn clock_gettime ,
420.Fn sigpause ,
421.Fn timer_getoverrun ,
422.Fn aio_return ,
423.Fn fdatasync ,
424.Fn sigqueue ,
425.Fn timer_gettime ,
426.Fn aio_suspend ,
427.Fn sem_post ,
428.Fn sigset ,
429.Fn timer_settime .
430.Pp
431ANSI C Interfaces:
432.Pp
433.Fn strcpy ,
434.Fn strcat ,
435.Fn strncpy ,
436.Fn strncat ,
437and perhaps some others.
438.Pp
439Extension Interfaces:
440.Pp
441.Fn strlcpy ,
442.Fn strlcat .
443.Pp
444All functions not in the above lists are considered to be unsafe
445with respect to signals. That is to say, the behaviour of such
446functions when called from a signal handler is undefined.
447In general though, signal handlers should do little more than set a
448flag; most other actions are not safe.
449.Pp
450Also, it is good practice to make a copy of the global variable
9bccf70c 451.Va errno
55e303ae
A
452and restore it before returning from the signal handler.
453This protects against the side effect of
454.Va errno
455being set by functions called from inside the signal handler.
456.Sh RETURN VALUES
457.Rv -std sigaction
458.Sh EXAMPLES
459There are three possible prototypes the handler may match:
460.Bl -tag -offset indent -width short
461.It ANSI C:
462.Ft void
463.Fn handler int ;
464.It POSIX SA_SIGINFO:
465.Ft void
466.Fn handler int "siginfo_t *info" "ucontext_t *uap" ;
467.El
468.Pp
469The handler function should match the SA_SIGINFO prototype if the
470SA_SIGINFO bit is set in flags.
471It then should be pointed to by the
472.Dv sa_sigaction
473member of
474.Dv struct sigaction .
475Note that you should not assign SIG_DFL or SIG_IGN this way.
476.Pp
477If the SA_SIGINFO flag is not set, the handler function should match
478either the ANSI C or traditional
479.Bx
480prototype and be pointed to by
481the
482.Dv sa_handler
483member of
484.Dv struct sigaction .
485In practice,
486.Fx
487always sends the three arguments of the latter and since the ANSI C
488prototype is a subset, both will work.
489The
490.Dv sa_handler
491member declaration in
492.Fx
493include files is that of ANSI C (as required by POSIX),
494so a function pointer of a
495.Bx Ns -style
496function needs to be casted to
497compile without warning.
498The traditional
499.Bx
500style is not portable and since its capabilities
501are a full subset of a SA_SIGINFO handler,
502its use is deprecated.
9bccf70c 503.Pp
55e303ae 504The
9bccf70c 505.Fa sig
55e303ae
A
506argument is the signal number, one of the
507.Dv SIG...
508values from <signal.h>.
509.Pp
510The
511.Fa code
512argument of the
513.Bx Ns -style
514handler and the
515.Dv si_code
516member of the
517.Dv info
518argument to a SA_SIGINFO handler contain a numeric code explaining the
519cause of the signal, usually one of the
520.Dv SI_...
521values from
522<sys/signal.h> or codes specific to a signal, i.e. one of the
523.Dv FPE_...
524values for SIGFPE.
525.Pp
526The
527.Fa uap
528argument to a POSIX SA_SIGINFO handler points to an instance of
529ucontext_t.
9bccf70c 530.Sh ERRORS
55e303ae
A
531The
532.Fn sigaction
533system call
9bccf70c
A
534will fail and no new signal handler will be installed if one
535of the following occurs:
536.Bl -tag -width Er
2d21ac55 537.\" ===========
9bccf70c
A
538.It Bq Er EFAULT
539Either
540.Fa act
55e303ae 541or
9bccf70c
A
542.Fa oact
543points to memory that is not a valid part of the process
544address space.
2d21ac55 545.\" ===========
9bccf70c 546.It Bq Er EINVAL
55e303ae
A
547The
548.Fa sig
549argument
9bccf70c 550is not a valid signal number.
2d21ac55 551.\" ===========
9bccf70c
A
552.It Bq Er EINVAL
553An attempt is made to ignore or supply a handler for
554.Dv SIGKILL
555or
556.Dv SIGSTOP .
2d21ac55
A
557.\" ===========
558.It Bq Er EINVAL
559An attempt was made to set the action to SIG_DFL
560for a signal that cannot be caught or ignored (or both).
9bccf70c
A
561.El
562.Sh STANDARDS
563The
55e303ae
A
564.Fn sigaction
565system call is expected to conform to
566.St -p1003.1-90 .
9bccf70c
A
567The
568.Dv SA_ONSTACK
569and
570.Dv SA_RESTART
571flags are Berkeley extensions,
572as are the signals,
573.Dv SIGTRAP ,
574.Dv SIGEMT ,
575.Dv SIGBUS ,
576.Dv SIGSYS ,
577.Dv SIGURG ,
578.Dv SIGIO ,
579.Dv SIGXCPU ,
580.Dv SIGXFSZ ,
581.Dv SIGVTALRM ,
582.Dv SIGPROF ,
583.Dv SIGWINCH ,
584and
585.Dv SIGINFO .
586Those signals are available on most
55e303ae 587.Bx Ns \-derived
9bccf70c 588systems.
55e303ae
A
589The
590.Dv SA_NODEFER
591and
592.Dv SA_RESETHAND
593flags are intended for backwards compatibility with other operating
594systems. The
595.Dv SA_NOCLDSTOP ,
596and
597.Dv SA_NOCLDWAIT
598.\" and
599.\" SA_SIGINFO
600flags are featuring options commonly found in other operating systems.
9bccf70c
A
601.Sh SEE ALSO
602.Xr kill 1 ,
9bccf70c 603.Xr kill 2 ,
55e303ae
A
604.Xr ptrace 2 ,
605.Xr sigaltstack 2 ,
9bccf70c 606.Xr sigblock 2 ,
9bccf70c 607.Xr sigpause 2 ,
55e303ae
A
608.Xr sigpending 2 ,
609.Xr sigprocmask 2 ,
610.Xr sigsetmask 2 ,
611.Xr sigsuspend 2 ,
612.Xr sigvec 2 ,
613.Xr wait 2 ,
614.Xr fpsetmask 3 ,
9bccf70c
A
615.Xr setjmp 3 ,
616.Xr siginterrupt 3 ,
617.Xr sigsetops 3 ,
55e303ae 618.Xr ucontext 3 ,
9bccf70c 619.Xr tty 4