xref: /csrg-svn/lib/libc/sys/sigaction.2 (revision 66697)
161185Sbostic.\" Copyright (c) 1980, 1990, 1993
261185Sbostic.\"	The Regents of the University of California.  All rights reserved.
344870Skarels.\"
450487Scael.\" %sccs.include.redist.roff%
544870Skarels.\"
6*66697Sbostic.\"	@(#)sigaction.2	8.2 (Berkeley) 04/03/94
744870Skarels.\"
848835Scael.Dd
948835Scael.Dt SIGACTION 2
1050487Scael.Os
1148835Scael.Sh NAME
1248835Scael.Nm sigaction
1348835Scael.Nd software signal facilities
1448835Scael.Sh SYNOPSIS
1548835Scael.Fd #include <signal.h>
1648835Scael.Bd -literal
1748835Scaelstruct sigaction {
1848835Scael	void     (*sa_handler)();
1948835Scael	sigset_t sa_mask;
2048835Scael	int	 sa_flags;
2148835Scael};
2248835Scael.Ed
2348835Scael.Fn sigaction "int sig" "struct sigaction *act" "struct sigaction *oact"
2448835Scael.Sh DESCRIPTION
2544870SkarelsThe system defines a set of signals that may be delivered to a process.
2658267SmckusickSignal delivery resembles the occurrence of a hardware interrupt:
2744870Skarelsthe signal is blocked from further occurrence, the current process
2844870Skarelscontext is saved, and a new one is built.  A process may specify a
2948835Scael.Em handler
3044870Skarelsto which a signal is delivered, or specify that a signal is to be
3148835Scael.Em ignored .
3244870SkarelsA process may also specify that a default action is to be taken
3344870Skarelsby the system when a signal occurs.
3444870SkarelsA signal may also be
3548835Scael.Em blocked ,
3644870Skarelsin which case its delivery is postponed until it is
3748835Scael.Em unblocked .
3844870SkarelsThe action to be taken on delivery is determined at the time
3944870Skarelsof delivery.
4044870SkarelsNormally, signal handlers execute on the current stack
4144870Skarelsof the process.  This may be changed, on a per-handler basis,
4244870Skarelsso that signals are taken on a special
4348835Scael.Em "signal stack" .
4448835Scael.Pp
4544870SkarelsSignal routines execute with the signal that caused their
4644870Skarelsinvocation
4748835Scael.Em blocked ,
4844870Skarelsbut other signals may yet occur.
4944870SkarelsA global
5048835Scael.Em "signal mask"
5144870Skarelsdefines the set of signals currently blocked from delivery
5244870Skarelsto a process.  The signal mask for a process is initialized
5344870Skarelsfrom that of its parent (normally empty).  It
5444870Skarelsmay be changed with a
5548835Scael.Xr sigprocmask 2
5644870Skarelscall, or when a signal is delivered to the process.
5748835Scael.Pp
5844870SkarelsWhen a signal
5944870Skarelscondition arises for a process, the signal is added to a set of
6044870Skarelssignals pending for the process.
6144870SkarelsIf the signal is not currently
6248835Scael.Em blocked
6344870Skarelsby the process then it is delivered to the process.
6444870SkarelsSignals may be delivered any time a process enters the operating system
6544870Skarels(e.g., during a system call, page fault or trap, or clock interrupt).
6644870SkarelsIf multiple signals are ready to be delivered at the same time,
6744870Skarelsany signals that could be caused by traps are delivered first.
6844870SkarelsAdditional signals may be processed at the same time, with each
6944870Skarelsappearing to interrupt the handlers for the previous signals
7044870Skarelsbefore their first instructions.
7144870SkarelsThe set of pending signals is returned by the
7248835Scael.Xr sigpending 2
7344870Skarelsfunction.
7444870SkarelsWhen a caught signal
7544870Skarelsis delivered, the current state of the process is saved,
7644870Skarelsa new signal mask is calculated (as described below),
7744870Skarelsand the signal handler is invoked.  The call to the handler
7844870Skarelsis arranged so that if the signal handling routine returns
7944870Skarelsnormally the process will resume execution in the context
8044870Skarelsfrom before the signal's delivery.
8144870SkarelsIf the process wishes to resume in a different context, then it
8244870Skarelsmust arrange to restore the previous context itself.
8348835Scael.Pp
8444870SkarelsWhen a signal is delivered to a process a new signal mask is
8544870Skarelsinstalled for the duration of the process' signal handler
8644870Skarels(or until a
8748835Scael.Xr sigprocmask
8844870Skarelscall is made).
8944870SkarelsThis mask is formed by taking the union of the current signal mask set,
9044870Skarelsthe signal to be delivered, and
9144870Skarelsthe signal mask associated with the handler to be invoked.
9248835Scael.Pp
9348835Scael.Fn Sigaction
9444870Skarelsassigns an action for a specific signal.
9544870SkarelsIf
9648835Scael.Fa act
9744870Skarelsis non-zero, it
9848835Scaelspecifies an action
9948835Scael.Pf ( Dv SIG_DFL ,
10048835Scael.Dv SIG_IGN ,
10148835Scaelor a handler routine) and mask
10244870Skarelsto be used when delivering the specified signal.
10344870SkarelsIf
10448835Scael.Fa oact
10544870Skarelsis non-zero, the previous handling information for the signal
10644870Skarelsis returned to the user.
10748835Scael.Pp
10844870SkarelsOnce a signal handler is installed, it remains installed
10944870Skarelsuntil another
11048835Scael.Fn sigaction
11144870Skarelscall is made, or an
11248835Scael.Xr execve 2
11344870Skarelsis performed.
11448835ScaelA signal-specific default action may be reset by
11548835Scaelsetting
11648835Scael.Fa sa_handler
11748835Scaelto
11848835Scael.Dv SIG_DFL .
11948835ScaelThe defaults are process termination, possibly with core dump;
12044870Skarelsno action; stopping the process; or continuing the process.
12144870SkarelsSee the signal list below for each signal's default action.
12244870SkarelsIf
12348835Scael.Fa sa_handler
12448835Scaelis
12558267Smckusick.Dv SIG_DFL ,
12658267Smckusickthe default action for the signal is to discard the signal,
12758267Smckusickand if a signal is pending,
12858267Smckusickthe pending signal is discarded even if the signal is masked.
12958267SmckusickIf
13058267Smckusick.Fa sa_handler
13158267Smckusickis set to
13248835Scael.Dv SIG_IGN
13348835Scaelcurrent and pending instances
13448835Scaelof the signal are ignored and discarded.
13548835Scael.Pp
13644870SkarelsOptions may be specified by setting
13748835Scael.Em sa_flags .
13848835ScaelIf the
13948835Scael.Dv SA_NOCLDSTOP
14048835Scaelbit is set when installing a catching function
14148835Scaelfor the
14248835Scael.Dv SIGCHLD
14348835Scaelsignal,
14448835Scaelthe
14548835Scael.Dv SIGCHLD
14648835Scaelsignal will be generated only when a child process exits,
14744870Skarelsnot when a child process stops.
14848835ScaelFurther, if the
14948835Scael.Dv SA_ONSTACK
15048835Scaelbit is set in
15148835Scael.Em sa_flags ,
15244870Skarelsthe system will deliver the signal to the process on a
15348835Scael.Em "signal stack" ,
15444870Skarelsspecified with
15558267Smckusick.Xr sigstack 2 .
15648835Scael.Pp
15748835ScaelIf a signal is caught during the system calls listed below,
15848835Scaelthe call may be forced to terminate
15948835Scaelwith the error
16048835Scael.Dv EINTR ,
16158267Smckusickthe call may return with a data transfer shorter than requested,
16244870Skarelsor the call may be restarted.
16344870SkarelsRestart of pending calls is requested
16448835Scaelby setting the
16548835Scael.Dv SA_RESTART
16648835Scaelbit in
16748835Scael.Ar sa_flags .
16844870SkarelsThe affected system calls include
16958267Smckusick.Xr open 2 ,
17048835Scael.Xr read 2 ,
17148835Scael.Xr write 2 ,
17248835Scael.Xr sendto 2 ,
17348835Scael.Xr recvfrom 2 ,
17448835Scael.Xr sendmsg 2
17544870Skarelsand
17648835Scael.Xr recvmsg 2
17744870Skarelson a communications channel or a slow device (such as a terminal,
17844870Skarelsbut not a regular file)
17944870Skarelsand during a
18048835Scael.Xr wait 2
18144870Skarelsor
18248835Scael.Xr ioctl 2 .
18344870SkarelsHowever, calls that have already committed are not restarted,
18444870Skarelsbut instead return a partial success (for example, a short read count).
18548835Scael.Pp
18644870SkarelsAfter a
18748835Scael.Xr fork 2
18844870Skarelsor
18948835Scael.Xr vfork 2
19044870Skarelsall signals, the signal mask, the signal stack,
19148835Scaeland the restart/interrupt flags are inherited by the child.
19248835Scael.Pp
19348835Scael.Xr Execve 2
19448835Scaelreinstates the default
19548835Scaelaction for all signals which were caught and
19644870Skarelsresets all signals to be caught on the user stack.
19744870SkarelsIgnored signals remain ignored;
19844870Skarelsthe signal mask remains the same;
19944870Skarelssignals that restart pending system calls continue to do so.
20048835Scael.Pp
20144870SkarelsThe following is a list of all signals
20244870Skarelswith names as in the include file
20348835Scael.Aq Pa signal.h :
20448835Scael.Bl -column SIGVTALARMXX "create core imagexxx"
20558475Smckusick.It Sy "  NAME  " "	Default Action  " "	Description"
20648835Scael.It Dv SIGHUP No "	terminate process" "	terminal line hangup"
20748835Scael.It Dv SIGINT No "	terminate process" "	interrupt program"
20848835Scael.It Dv SIGQUIT No "	create core image" "	quit program"
20948835Scael.It Dv SIGILL No "	create core image" "	illegal instruction"
21048835Scael.It Dv SIGTRAP No "	create core image" "	trace trap"
21148835Scael.It Dv SIGABRT No "	create core image" Xr 	abort 2
21248835Scaelcall (formerly
21348835Scael.Dv SIGIOT )
21448835Scael.It Dv SIGEMT No "	create core image" "	emulate instruction executed"
21548835Scael.It Dv SIGFPE No "	create core image" "	floating-point exception"
21648835Scael.It Dv SIGKILL No "	terminate process" "	kill program"
21748835Scael.It Dv SIGBUS No "	create core image" "	bus error"
21848835Scael.It Dv SIGSEGV No "	create core image" "	segmentation violation"
21948835Scael.It Dv SIGSYS No "	create core image" "	system call given invalid argument"
22048835Scael.It Dv SIGPIPE No "	terminate process" "	write on a pipe with no reader"
22148835Scael.It Dv SIGALRM No "	terminate process" "	real-time timer expired"
22248835Scael.It Dv SIGTERM No "	terminate process" "	software termination signal"
22348835Scael.It Dv SIGURG No "	discard signal" "	urgent condition present on socket"
22448835Scael.It Dv SIGSTOP No "	stop process" "	stop (cannot be caught or ignored)"
22548835Scael.It Dv SIGTSTP No "	stop process" "	stop signal generated from keyboard"
22648835Scael.It Dv SIGCONT No "	discard signal" "	continue after stop"
22748835Scael.It Dv SIGCHLD No "	discard signal" "	child status has changed"
22848835Scael.It Dv SIGTTIN No "	stop process" "	background read attempted from control terminal"
22948835Scael.It Dv SIGTTOU No "	stop process" "	background write attempted to control terminal"
23048835Scael.It Dv SIGIO No "	discard signal" Tn "	I/O"
23148835Scaelis possible on a descriptor (see
23248835Scael.Xr fcntl 2 )
23348835Scael.It Dv SIGXCPU No "	terminate process" "	cpu time limit exceeded (see"
23448835Scael.Xr setrlimit 2 )
23548835Scael.It Dv SIGXFSZ No "	terminate process" "	file size limit exceeded (see"
23648835Scael.Xr setrlimit 2 )
23748835Scael.It Dv SIGVTALRM No "	terminate process" "	virtual time alarm (see"
23848835Scael.Xr setitimer 2 )
23948835Scael.It Dv SIGPROF No "	terminate process" "	profiling timer alarm (see"
24048835Scael.Xr setitimer 2 )
24148835Scael.It Dv SIGWINCH No "	discard signal" "	Window size change"
24248835Scael.It Dv SIGINFO No "	discard signal" "	status request from keyboard"
24348835Scael.It Dv SIGUSR1 No "	terminate process" "	User defined signal 1"
24448835Scael.It Dv SIGUSR2 No "	terminate process" "	User defined signal 2"
24548835Scael.El
24648835Scael.Sh NOTE
24744870SkarelsThe mask specified in
24848835Scael.Fa act
24948835Scaelis not allowed to block
25048835Scael.Dv SIGKILL
25148835Scaelor
25258267Smckusick.Dv SIGSTOP .
25344870SkarelsThis is done silently by the system.
25448835Scael.Sh RETURN VALUES
25544870SkarelsA 0 value indicated that the call succeeded.  A \-1 return value
25644870Skarelsindicates an error occurred and
25748835Scael.Va errno
25844870Skarelsis set to indicated the reason.
25958267Smckusick.Sh EXAMPLE
26058267SmckusickThe handler routine can be declared:
26158267Smckusick.Bd -literal -offset indent
26258267Smckusickvoid handler(sig, code, scp)
26358267Smckusickint sig, code;
26458267Smckusickstruct sigcontext *scp;
26558267Smckusick.Ed
26658267Smckusick.Pp
26758267SmckusickHere
26858267Smckusick.Fa sig
26958267Smckusickis the signal number, into which the hardware faults and traps are
27058267Smckusickmapped.
27158475Smckusick.Fa Code
27258267Smckusickis a parameter that is either a constant
27358267Smckusickor the code provided by
27458267Smckusickthe hardware.
27558267Smckusick.Fa Scp
27658267Smckusickis a pointer to the
27758267Smckusick.Fa sigcontext
27858267Smckusickstructure (defined in
27958267Smckusick.Aq Pa signal.h ) ,
28058267Smckusickused to restore the context from before the signal.
28158267Smckusick.Sh ERRORS
28248835Scael.Fn Sigaction
28344870Skarelswill fail and no new signal handler will be installed if one
28444870Skarelsof the following occurs:
28558267Smckusick.Bl -tag -width Er
28658267Smckusick.It Bq Er EFAULT
28744870SkarelsEither
28848835Scael.Fa act
28944870Skarelsor
29048835Scael.Fa oact
29144870Skarelspoints to memory that is not a valid part of the process
29244870Skarelsaddress space.
29358267Smckusick.It Bq Er EINVAL
29448835Scael.Fa Sig
29544870Skarelsis not a valid signal number.
29658267Smckusick.It Bq Er EINVAL
29748835ScaelAn attempt is made to ignore or supply a handler for
29858475Smckusick.Dv SIGKILL
29948835Scaelor
30058475Smckusick.Dv SIGSTOP .
30158267Smckusick.El
30258475Smckusick.Sh STANDARDS
30344870SkarelsThe
30450487Scael.Nm sigaction
30548835Scaelfunction is defined by
30648835Scael.St -p1003.1-88 .
30748835ScaelThe
30848835Scael.Dv SA_ONSTACK
30948835Scaeland
31048835Scael.Dv SA_RESTART
31148835Scaelflags are Berkeley extensions,
31248835Scaelas are the signals,
31348835Scael.Dv SIGTRAP ,
31448835Scael.Dv SIGEMT ,
31548835Scael.Dv SIGBUS ,
31648835Scael.Dv SIGSYS ,
31748835Scael.Dv SIGURG ,
31848835Scael.Dv SIGIO ,
31948835Scael.Dv SIGXCPU ,
32048835Scael.Dv SIGXFSZ ,
32148835Scael.Dv SIGVTALRM ,
32248835Scael.Dv SIGPROF ,
32348835Scael.Dv SIGWINCH ,
32448835Scaeland
32548835Scael.Dv SIGINFO .
32658267SmckusickThose signals are available on most
32748835Scael.Tn BSD Ns \-derived
32848835Scaelsystems.
32948835Scael.Sh SEE ALSO
33048835Scael.Xr kill 1 ,
33148835Scael.Xr ptrace 2 ,
33248835Scael.Xr kill 2 ,
33348835Scael.Xr sigaction 2 ,
33448835Scael.Xr sigprocmask 2 ,
33548835Scael.Xr sigsuspend 2 ,
33648835Scael.Xr sigblock 2 ,
33748835Scael.Xr sigsetmask 2 ,
33848835Scael.Xr sigpause 2 ,
33958267Smckusick.Xr sigstack 2 ,
34048835Scael.Xr sigvec 2 ,
34148835Scael.Xr setjmp 3 ,
34258267Smckusick.Xr siginterrupt 3 ,
343*66697Sbostic.Xr sigsetops 3 ,
34448835Scael.Xr tty 4
345