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 45struct 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 54The system defines a set of signals that may be delivered to a process. 55Signal delivery resembles the occurrence of a hardware interrupt: 56the signal is blocked from further occurrence, the current process 57context is saved, and a new one is built. A process may specify a 58.Em handler 59to which a signal is delivered, or specify that a signal is to be 60.Em ignored . 61A process may also specify that a default action is to be taken 62by the system when a signal occurs. 63A signal may also be 64.Em blocked , 65in which case its delivery is postponed until it is 66.Em unblocked . 67The action to be taken on delivery is determined at the time 68of delivery. 69Normally, signal handlers execute on the current stack 70of the process. This may be changed, on a per-handler basis, 71so that signals are taken on a special 72.Em "signal stack" . 73.Pp 74Signal routines execute with the signal that caused their 75invocation 76.Em blocked , 77but other signals may yet occur. 78A global 79.Em "signal mask" 80defines the set of signals currently blocked from delivery 81to a process. The signal mask for a process is initialized 82from that of its parent (normally empty). It 83may be changed with a 84.Xr sigprocmask 2 85call, or when a signal is delivered to the process. 86.Pp 87When a signal 88condition arises for a process, the signal is added to a set of 89signals pending for the process. 90If the signal is not currently 91.Em blocked 92by the process then it is delivered to the process. 93Signals 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). 95If multiple signals are ready to be delivered at the same time, 96any signals that could be caused by traps are delivered first. 97Additional signals may be processed at the same time, with each 98appearing to interrupt the handlers for the previous signals 99before their first instructions. 100The set of pending signals is returned by the 101.Xr sigpending 2 102function. 103When a caught signal 104is delivered, the current state of the process is saved, 105a new signal mask is calculated (as described below), 106and the signal handler is invoked. The call to the handler 107is arranged so that if the signal handling routine returns 108normally the process will resume execution in the context 109from before the signal's delivery. 110If the process wishes to resume in a different context, then it 111must arrange to restore the previous context itself. 112.Pp 113When a signal is delivered to a process a new signal mask is 114installed for the duration of the process' signal handler 115(or until a 116.Xr sigprocmask 117call is made). 118This mask is formed by taking the union of the current signal mask set, 119the signal to be delivered, and 120the signal mask associated with the handler to be invoked. 121.Pp 122.Fn Sigaction 123assigns an action for a specific signal. 124If 125.Fa act 126is non-zero, it 127specifies an action 128.Pf ( Dv SIG_DFL , 129.Dv SIG_IGN , 130or a handler routine) and mask 131to be used when delivering the specified signal. 132If 133.Fa oact 134is non-zero, the previous handling information for the signal 135is returned to the user. 136.Pp 137Once a signal handler is installed, it remains installed 138until another 139.Fn sigaction 140call is made, or an 141.Xr execve 2 142is performed. 143A signal-specific default action may be reset by 144setting 145.Fa sa_handler 146to 147.Dv SIG_DFL . 148The defaults are process termination, possibly with core dump; 149no action; stopping the process; or continuing the process. 150See the signal list below for each signal's default action. 151If 152.Fa sa_handler 153is 154.Dv SIG_DFL , 155the default action for the signal is to discard the signal, 156and if a signal is pending, 157the pending signal is discarded even if the signal is masked. 158If 159.Fa sa_handler 160is set to 161.Dv SIG_IGN 162current and pending instances 163of the signal are ignored and discarded. 164.Pp 165Options may be specified by setting 166.Em sa_flags . 167If the 168.Dv SA_NOCLDSTOP 169bit is set when installing a catching function 170for the 171.Dv SIGCHLD 172signal, 173the 174.Dv SIGCHLD 175signal will be generated only when a child process exits, 176not when a child process stops. 177Further, if the 178.Dv SA_ONSTACK 179bit is set in 180.Em sa_flags , 181the system will deliver the signal to the process on a 182.Em "signal stack" , 183specified with 184.Xr sigstack 2 . 185.Pp 186If a signal is caught during the system calls listed below, 187the call may be forced to terminate 188with the error 189.Dv EINTR , 190the call may return with a data transfer shorter than requested, 191or the call may be restarted. 192Restart of pending calls is requested 193by setting the 194.Dv SA_RESTART 195bit in 196.Ar sa_flags . 197The affected system calls include 198.Xr open 2 , 199.Xr read 2 , 200.Xr write 2 , 201.Xr sendto 2 , 202.Xr recvfrom 2 , 203.Xr sendmsg 2 204and 205.Xr recvmsg 2 206on a communications channel or a slow device (such as a terminal, 207but not a regular file) 208and during a 209.Xr wait 2 210or 211.Xr ioctl 2 . 212However, calls that have already committed are not restarted, 213but instead return a partial success (for example, a short read count). 214.Pp 215After a 216.Xr fork 2 217or 218.Xr vfork 2 219all signals, the signal mask, the signal stack, 220and the restart/interrupt flags are inherited by the child. 221.Pp 222.Xr Execve 2 223reinstates the default 224action for all signals which were caught and 225resets all signals to be caught on the user stack. 226Ignored signals remain ignored; 227the signal mask remains the same; 228signals that restart pending system calls continue to do so. 229.Pp 230The following is a list of all signals 231with names as in the include file 232.Aq Pa signal.h : 233.Bl -column SIGVTALARMXX "create core imagexxx" 234.It Sy " NAME " " Default Action " " Description" 235.It Dv SIGHUP No " terminate process" " terminal line hangup" 236.It Dv SIGINT No " terminate process" " interrupt program" 237.It Dv SIGQUIT No " create core image" " quit program" 238.It Dv SIGILL No " create core image" " illegal instruction" 239.It Dv SIGTRAP No " create core image" " trace trap" 240.It Dv SIGABRT No " create core image" Xr abort 2 241call (formerly 242.Dv SIGIOT ) 243.It Dv SIGEMT No " create core image" " emulate instruction executed" 244.It Dv SIGFPE No " create core image" " floating-point exception" 245.It Dv SIGKILL No " terminate process" " kill program" 246.It Dv SIGBUS No " create core image" " bus error" 247.It Dv SIGSEGV No " create core image" " segmentation violation" 248.It Dv SIGSYS No " create core image" " system call given invalid argument" 249.It Dv SIGPIPE No " terminate process" " write on a pipe with no reader" 250.It Dv SIGALRM No " terminate process" " real-time timer expired" 251.It Dv SIGTERM No " terminate process" " software termination signal" 252.It Dv SIGURG No " discard signal" " urgent condition present on socket" 253.It Dv SIGSTOP No " stop process" " stop (cannot be caught or ignored)" 254.It Dv SIGTSTP No " stop process" " stop signal generated from keyboard" 255.It Dv SIGCONT No " discard signal" " continue after stop" 256.It Dv SIGCHLD No " discard signal" " child status has changed" 257.It Dv SIGTTIN No " stop process" " background read attempted from control terminal" 258.It Dv SIGTTOU No " stop process" " background write attempted to control terminal" 259.It Dv SIGIO No " discard signal" Tn " I/O" 260is possible on a descriptor (see 261.Xr fcntl 2 ) 262.It Dv SIGXCPU No " terminate process" " cpu time limit exceeded (see" 263.Xr setrlimit 2 ) 264.It Dv SIGXFSZ No " terminate process" " file size limit exceeded (see" 265.Xr setrlimit 2 ) 266.It Dv SIGVTALRM No " terminate process" " virtual time alarm (see" 267.Xr setitimer 2 ) 268.It Dv SIGPROF No " terminate process" " profiling timer alarm (see" 269.Xr setitimer 2 ) 270.It Dv SIGWINCH No " discard signal" " Window size change" 271.It Dv SIGINFO No " discard signal" " status request from keyboard" 272.It Dv SIGUSR1 No " terminate process" " User defined signal 1" 273.It Dv SIGUSR2 No " terminate process" " User defined signal 2" 274.El 275.Sh NOTE 276The mask specified in 277.Fa act 278is not allowed to block 279.Dv SIGKILL 280or 281.Dv SIGSTOP . 282This is done silently by the system. 283.Sh RETURN VALUES 284A 0 value indicated that the call succeeded. A \-1 return value 285indicates an error occurred and 286.Va errno 287is set to indicated the reason. 288.Sh EXAMPLE 289The handler routine can be declared: 290.Bd -literal -offset indent 291void handler(sig, code, scp) 292int sig, code; 293struct sigcontext *scp; 294.Ed 295.Pp 296Here 297.Fa sig 298is the signal number, into which the hardware faults and traps are 299mapped. 300.Fa Code 301is a parameter that is either a constant 302or the code provided by 303the hardware. 304.Fa Scp 305is a pointer to the 306.Fa sigcontext 307structure (defined in 308.Aq Pa signal.h ) , 309used to restore the context from before the signal. 310.Sh ERRORS 311.Fn Sigaction 312will fail and no new signal handler will be installed if one 313of the following occurs: 314.Bl -tag -width Er 315.It Bq Er EFAULT 316Either 317.Fa act 318or 319.Fa oact 320points to memory that is not a valid part of the process 321address space. 322.It Bq Er EINVAL 323.Fa Sig 324is not a valid signal number. 325.It Bq Er EINVAL 326An attempt is made to ignore or supply a handler for 327.Dv SIGKILL 328or 329.Dv SIGSTOP . 330.El 331.Sh STANDARDS 332The 333.Nm sigaction 334function is defined by 335.St -p1003.1-88 . 336The 337.Dv SA_ONSTACK 338and 339.Dv SA_RESTART 340flags are Berkeley extensions, 341as are the signals, 342.Dv SIGTRAP , 343.Dv SIGEMT , 344.Dv SIGBUS , 345.Dv SIGSYS , 346.Dv SIGURG , 347.Dv SIGIO , 348.Dv SIGXCPU , 349.Dv SIGXFSZ , 350.Dv SIGVTALRM , 351.Dv SIGPROF , 352.Dv SIGWINCH , 353and 354.Dv SIGINFO . 355Those signals are available on most 356.Tn BSD Ns \-derived 357systems. 358.Sh SEE ALSO 359.Xr kill 1 , 360.Xr ptrace 2 , 361.Xr kill 2 , 362.Xr sigaction 2 , 363.Xr sigprocmask 2 , 364.Xr sigsuspend 2 , 365.Xr sigblock 2 , 366.Xr sigsetmask 2 , 367.Xr sigpause 2 , 368.Xr sigstack 2 , 369.Xr sigvec 3 , 370.Xr setjmp 3 , 371.Xr siginterrupt 3 , 372.Xr sigsetops 3 , 373.Xr tty 4 374