1.\" $OpenBSD: signal.3,v 1.26 2003/07/14 21:23:57 dhartmei Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.Dd April 19, 1994 31.Dt SIGNAL 3 32.Os 33.Sh NAME 34.Nm signal 35.Nd simplified software signal facilities 36.Sh SYNOPSIS 37.Fd #include <signal.h> 38.Ft void 39.\" can not do this with just .Fn yet 40.\" .br 41.\" .Po 42.Fn (*signal "int sigcatch" "void (*func)(int sigraised))) (int" 43.\" .Pc Ns \*(lp\*(rp 44.Sh DESCRIPTION 45The 46.Fn signal 47facility 48is a simplified interface to the more general 49.Xr sigaction 2 50facility. 51.Pp 52Signals allow the manipulation of a process from outside its 53domain as well as allowing the process to manipulate itself or 54copies of itself (children). 55There are two general types of signals: 56those that cause termination of a process and those that do not. 57Signals which cause termination of a program might result from 58an irrecoverable error or might be the result of a user at a terminal 59typing the 60.Dq interrupt 61character. 62.Pp 63Signals are used when a process is stopped because it wishes to access 64its control terminal while in the background (see 65.Xr tty 4 ) . 66Signals are optionally generated 67when a process resumes after being stopped, 68when the status of child processes changes, 69or when input is ready at the control terminal. 70Most signals result in the termination of the process receiving them 71if no action 72is taken; some signals instead cause the process receiving them 73to be stopped, or are simply discarded if the process has not 74requested otherwise. 75.Pp 76Except for the 77.Dv SIGKILL 78and 79.Dv SIGSTOP 80signals, the 81.Fn signal 82function allows for any signal to be caught, to be ignored, or to generate 83an interrupt. 84These signals are defined in the file 85.Aq Pa signal.h : 86.Bl -column SIGVTALARMXX "create core imagexxx" 87.It Sy "Name Default Action Description" 88.It Dv SIGHUP No " terminate process" " terminal line hangup" 89.It Dv SIGINT No " terminate process" " interrupt program" 90.It Dv SIGQUIT No " create core image" " quit program" 91.It Dv SIGILL No " create core image" " illegal instruction" 92.It Dv SIGTRAP No " create core image" " trace trap" 93.It Dv SIGABRT No " create core image" Xr abort 3 94call (formerly 95.Dv SIGIOT ) 96.It Dv SIGEMT No " create core image" " emulate instruction executed" 97.It Dv SIGFPE No " create core image" " floating-point exception" 98.It Dv SIGKILL No " terminate process" " kill program" 99.It Dv SIGBUS No " create core image" " bus error" 100.It Dv SIGSEGV No " create core image" " segmentation violation" 101.It Dv SIGSYS No " create core image" " system call given invalid argument" 102.It Dv SIGPIPE No " terminate process" " write on a pipe with no reader" 103.It Dv SIGALRM No " terminate process" " real-time timer expired" 104.It Dv SIGTERM No " terminate process" " software termination signal" 105.It Dv SIGURG No " discard signal" " urgent condition present on socket" 106.It Dv SIGSTOP No " stop process" " stop (cannot be caught or ignored)" 107.It Dv SIGTSTP No " stop process" " stop signal generated from keyboard" 108.It Dv SIGCONT No " discard signal" " continue after stop" 109.It Dv SIGCHLD No " discard signal" " child status has changed" 110.It Dv SIGTTIN No " stop process" " background read attempted from control terminal" 111.It Dv SIGTTOU No " stop process" " background write attempted to control terminal" 112.It Dv SIGIO No " discard signal" Tn " I/O" 113is possible on a descriptor (see 114.Xr fcntl 2 ) 115.It Dv SIGXCPU No " terminate process" " CPU time limit exceeded (see" 116.Xr setrlimit 2 ) 117.It Dv SIGXFSZ No " terminate process" " file size limit exceeded (see" 118.Xr setrlimit 2 ) 119.It Dv SIGVTALRM No " terminate process" " virtual time alarm (see" 120.Xr setitimer 2 ) 121.It Dv SIGPROF No " terminate process" " profiling timer alarm (see" 122.Xr setitimer 2 ) 123.It Dv SIGWINCH No " discard signal" " window size change" 124.It Dv SIGINFO No " discard signal" " status request from keyboard" 125.It Dv SIGUSR1 No " terminate process" " user-defined signal 1" 126.It Dv SIGUSR2 No " terminate process" " user-defined signal 2" 127.El 128.Pp 129The 130.Fa func 131argument is a function to be called as the action upon receipt of the signal 132.Fa sigcatch . 133The function will be called with one argument, 134.Fa sigraised , 135which is the signal raised (thus the same function, 136.Fa func , 137can be used by more than one signal). 138To set the default action of the signal to occur as listed above, 139.Fa func 140should be 141.Dv SIG_DFL . 142A 143.Dv SIG_DFL 144resets the default action. 145To ignore the signal, 146.Fa func 147should be 148.Dv SIG_IGN . 149This will cause subsequent instances of the signal to be ignored 150and pending instances to be discarded. 151If 152.Dv SIG_IGN 153is not used, 154further occurrences of the signal are 155automatically blocked and 156.Fa func 157is called. 158.Pp 159If the 160.Fa func 161is set to 162.Dv SIG_IGN 163for the 164.Dv SIGCHLD 165signal, the system will not create zombie processes when children of 166the calling process exit. 167If the calling process subsequently issues a 168.Xr wait 2 169(or equivalent), it blocks until all of the calling process's child 170processes terminate, and then returns a value of \-1 with 171.Va errno 172set to 173.Dv ECHILD . 174.Bf -symbolic 175This differs from historical 176.Bx 177behavior but is consistent with 178.At V 179as well as the 180.St -xpg4.2 . 181.Ef 182.Pp 183The handled signal is unblocked when 184.Fa func 185returns and 186the process continues from where it left off when the signal occurred. 187.Bf -symbolic 188Unlike previous signal facilities, the handler 189func() remains installed after a signal has been delivered. 190.Ef 191.Pp 192For some system calls, if a signal is caught while the call is 193executing and the call is prematurely terminated, 194the call is automatically restarted. 195(The handler is installed using the 196.Dv SA_RESTART 197flag with 198.Xr sigaction 2 . ) 199The affected system calls include 200.Xr read 2 , 201.Xr write 2 , 202.Xr sendto 2 , 203.Xr recvfrom 2 , 204.Xr sendmsg 2 , 205and 206.Xr recvmsg 2 207on a communications channel or a low-speed device 208and during a 209.Xr ioctl 2 210or 211.Xr wait 2 . 212However, calls that have already committed are not restarted, 213but instead return a partial success (for example, a short read count). 214.Pp 215When a process which has installed signal handlers forks, 216the child process inherits the signals. 217All caught signals may be reset to their default action by a call 218to the 219.Xr execve 2 220function; 221ignored signals remain ignored. 222.Pp 223The following functions are either reentrant or not interruptible 224by signals and are async-signal safe. 225Therefore applications may 226invoke them, without restriction, from signal-catching functions: 227.Pp 228Base Interfaces: 229.Pp 230.Fn _exit , 231.Fn access , 232.Fn alarm , 233.Fn cfgetispeed , 234.Fn cfgetospeed , 235.Fn cfsetispeed , 236.Fn cfsetospeed , 237.Fn chdir , 238.Fn chmod , 239.Fn chown , 240.Fn close , 241.Fn creat , 242.Fn dup , 243.Fn dup2 , 244.Fn execle , 245.Fn execve , 246.Fn fcntl , 247.Fn fork , 248.Fn fpathconf , 249.Fn fstat , 250.Fn fsync , 251.Fn getegid , 252.Fn geteuid , 253.Fn getgid , 254.Fn getgroups , 255.Fn getpgrp , 256.Fn getpid , 257.Fn getppid , 258.Fn getuid , 259.Fn kill , 260.Fn link , 261.Fn lseek , 262.Fn mkdir , 263.Fn mkfifo , 264.Fn open , 265.Fn pathconf , 266.Fn pause , 267.Fn pipe , 268.Fn raise , 269.Fn read , 270.Fn rename , 271.Fn rmdir , 272.Fn setgid , 273.Fn setpgid , 274.Fn setsid , 275.Fn setuid , 276.Fn sigaction , 277.Fn sigaddset , 278.Fn sigdelset , 279.Fn sigemptyset , 280.Fn sigfillset , 281.Fn sigismember , 282.Fn signal , 283.Fn sigpending , 284.Fn sigprocmask , 285.Fn sigsuspend , 286.Fn sleep , 287.Fn stat , 288.Fn sysconf , 289.Fn tcdrain , 290.Fn tcflow , 291.Fn tcflush , 292.Fn tcgetattr , 293.Fn tcgetpgrp , 294.Fn tcsendbreak , 295.Fn tcsetattr , 296.Fn tcsetpgrp , 297.Fn time , 298.Fn times , 299.Fn umask , 300.Fn uname , 301.Fn unlink , 302.Fn utime , 303.Fn wait , 304.Fn waitpid , 305.Fn write . 306.Pp 307Realtime Interfaces: 308.Pp 309.Fn aio_error , 310.Fn clock_gettime , 311.Fn sigpause , 312.Fn timer_getoverrun , 313.Fn aio_return , 314.Fn fdatasync , 315.Fn sigqueue , 316.Fn timer_gettime , 317.Fn aio_suspend , 318.Fn sem_post , 319.Fn sigset , 320.Fn timer_settime . 321.Pp 322ANSI C Interfaces: 323.Pp 324.Fn strcpy , 325.Fn strcat , 326.Fn strncpy , 327.Fn strncat , 328and perhaps some others. 329.Pp 330Extension Interfaces: 331.Pp 332.Fn strlcpy , 333.Fn strlcat , 334.Fn syslog_r . 335.Pp 336Most functions not in the above lists are considered to be unsafe 337with respect to signals. 338That is to say, the behaviour of such functions when called from a 339signal handler is undefined. 340In general though, signal handlers should do little more than set a 341flag; most other actions are not safe. 342.Pp 343Additionally, inside the signal handler it is also considered more safe to 344make a copy of the global variable 345.Va errno 346and restore it before returning from the signal handler. 347.Pp 348A few other functions are signal race safe in 349.Ox 350but probably not on other systems: 351.Pp 352.Bl -tag -offset indent -compact -width foofoofoofoo 353.It Fn snprintf 354Safe as long as $n positional arguments are not used. 355.It Fn syslog_r 356Safe if the 357.Va syslog_data 358struct is initialized as a local variable. 359.El 360.Sh RETURN VALUES 361The previous action is returned on a successful call. 362Otherwise, 363.Fa SIG_ERR 364is returned and the global variable 365.Va errno 366is set to indicate the error. 367.Sh ERRORS 368.Fn signal 369will fail and no action will take place if one of the 370following occur: 371.Bl -tag -width Er 372.It Bq Er EINVAL 373A specified signal 374is not a valid signal number. 375.It Bq Er EINVAL 376An attempt is made to ignore or supply a handler for 377.Dv SIGKILL 378or 379.Ev SIGSTOP . 380.El 381.Sh SEE ALSO 382.Xr kill 1 , 383.Xr kill 2 , 384.Xr ptrace 2 , 385.Xr sigaction 2 , 386.Xr sigaltstack 2 , 387.Xr sigprocmask 2 , 388.Xr sigsuspend 2 , 389.Xr setjmp 3 , 390.Xr tty 4 391.Sh HISTORY 392This 393.Fn signal 394facility appeared in 395.Bx 4.0 . 396