1.\" $OpenBSD: sigaction.2,v 1.44 2007/05/31 19:19:34 jmc Exp $ 2.\" $NetBSD: sigaction.2,v 1.7 1995/10/12 15:41:16 jtc Exp $ 3.\" 4.\" Copyright (c) 1980, 1990, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)sigaction.2 8.2 (Berkeley) 4/3/94 32.\" 33.Dd $Mdocdate: May 31 2007 $ 34.Dt SIGACTION 2 35.Os 36.Sh NAME 37.Nm sigaction 38.Nd software signal facilities 39.Sh SYNOPSIS 40.Fd #include <signal.h> 41.Pp 42.Bd -literal 43struct sigaction { 44 union { /* signal handler */ 45 void (*__sa_handler)(int); 46 void (*__sa_sigaction)(int, siginfo_t *, void *); 47 } __sigaction_u; 48 sigset_t sa_mask; /* signal mask to apply */ 49 int sa_flags; /* see signal options below */ 50}; 51.Ed 52.Pp 53.Fd #define sa_handler __sigaction_u.__sa_handler 54.Fd #define sa_sigaction __sigaction_u.__sa_sigaction 55.Ft int 56.Fn sigaction "int sig" "const struct sigaction *act" "struct sigaction *oact" 57.Sh DESCRIPTION 58The system defines a set of signals that may be delivered to a process. 59Signal delivery resembles the occurrence of a hardware interrupt: 60the signal is normally blocked from further occurrence, the current process 61context is saved, and a new one is built. 62A process may specify a 63.Em handler 64to which a signal is delivered, or specify that a signal is to be 65.Em ignored . 66A process may also specify that a default action is to be taken 67by the system when a signal occurs. 68A signal may also be 69.Em blocked , 70in which case its delivery is postponed until it is 71.Em unblocked . 72The action to be taken on delivery is determined at the time 73of delivery. 74Normally, signal handlers execute on the current stack 75of the process. 76This may be changed, on a per-handler basis, 77so that signals are taken on a special 78.Em "signal stack" . 79.Pp 80Signal routines normally execute with the signal that caused their 81invocation 82.Em blocked , 83but other signals may yet occur. 84A global 85.Em "signal mask" 86defines the set of signals currently blocked from delivery 87to a process. 88The signal mask for a process is initialized from that of its 89parent (normally empty). 90It may be changed with a 91.Xr sigprocmask 2 92call, or when a signal is delivered to the process. 93.Pp 94When a signal 95condition arises for a process, the signal is added to a set of 96signals pending for the process. 97If the signal is not currently 98.Em blocked 99by the process then it is delivered to the process. 100Signals may be delivered any time a process enters the operating system 101(e.g., during a system call, page fault or trap, or clock interrupt). 102If multiple signals are ready to be delivered at the same time, 103any signals that could be caused by traps are delivered first. 104Additional signals may be processed at the same time, with each 105appearing to interrupt the handlers for the previous signals 106before their first instructions. 107The set of pending signals is returned by the 108.Xr sigpending 2 109function. 110When a caught signal 111is delivered, the current state of the process is saved, 112a new signal mask is calculated (as described below), 113and the signal handler is invoked. 114The call to the handler is arranged so that if the signal handling routine 115returns normally the process will resume execution in the context from 116before the signal's delivery. 117If the process wishes to resume in a different context, then it 118must arrange to restore the previous context itself. 119.Pp 120When a signal is delivered to a process a new signal mask is 121installed for the duration of the process' signal handler 122(or until a 123.Xr sigprocmask 2 124call is made). 125This mask is formed by taking the union of the current signal mask set, 126the signal to be delivered, and the signal mask 127.Em sa_mask 128associated with the handler to be invoked. 129.Pp 130.Fn sigaction 131assigns an action for a signal specified by 132.Fa sig . 133If 134.Fa act 135is non-zero, it 136specifies an action 137.Pf ( Dv SIG_DFL , 138.Dv SIG_IGN , 139or a handler routine) and mask 140to be used when delivering the specified signal. 141If 142.Fa oact 143is non-zero, the previous handling information for the signal 144is returned to the user. 145.Pp 146Once a signal handler is installed, it normally remains installed 147until another 148.Fn sigaction 149call is made, or an 150.Xr execve 2 151is performed. 152The value of 153.Fa sa_handler 154(or, if the 155.Dv SA_SIGINFO 156flag is set, the value of 157.Fa sa_sigaction 158instead) indicates what action should be performed when a 159signal arrives. 160A signal-specific default action may be reset by 161setting 162.Fa sa_handler 163to 164.Dv SIG_DFL . 165Alternately, if the 166.Dv SA_RESETHAND 167flag is set the default action will be reinstated when the signal 168is first posted. 169The defaults are process termination, possibly with core dump; 170no action; stopping the process; or continuing the process. 171See the signal list below for each signal's default action. 172If 173.Fa sa_handler 174is 175.Dv SIG_DFL , 176the default action for the signal is to discard the signal, 177and if a signal is pending, 178the pending signal is discarded even if the signal is masked. 179If 180.Fa sa_handler 181is set to 182.Dv SIG_IGN , 183current and pending instances 184of the signal are ignored and discarded. 185If 186.Fa sig 187is 188.Dv SIGCHLD 189and 190.Fa sa_handler 191is set to 192.Dv SIG_IGN , 193the 194.Dv SA_NOCLDWAIT 195flag (described below) is implied. 196.Pp 197Options may be specified by setting 198.Em sa_flags . 199The meaning of the various bits is as follows: 200.Bl -tag -offset indent -width SA_RESETHANDXX 201.It Dv SA_NOCLDSTOP 202If this bit is set when installing a catching function 203for the 204.Dv SIGCHLD 205signal, 206the 207.Dv SIGCHLD 208signal will be generated only when a child process exits, 209not when a child process stops. 210.It Dv SA_NOCLDWAIT 211If this bit is set when calling 212.Fn sigaction 213for the 214.Dv SIGCHLD 215signal, the system will not create zombie processes when children of 216the calling process exit. 217If the calling process subsequently issues a 218.Xr wait 2 219(or equivalent), it blocks until all of the calling process's child 220processes terminate, and then returns a value of \-1 with 221.Va errno 222set to 223.Er ECHILD . 224.It Dv SA_ONSTACK 225If this bit is set, the system will deliver the signal to the process 226on a 227.Em "signal stack" , 228specified with 229.Xr sigaltstack 2 . 230.It Dv SA_NODEFER 231If this bit is set, further occurrences of the delivered signal are 232not masked during the execution of the handler. 233.It Dv SA_RESETHAND 234If this bit is set, the handler is reset back to 235.Dv SIG_DFL 236at the moment the signal is delivered. 237.It Dv SA_SIGINFO 238If this bit is set, the 2nd argument of the handler is set to 239be a pointer to a 240.Em siginfo_t 241structure as described in 242.Aq Pa sys/siginfo.h . 243The 244.Em siginfo_t 245structure is a part of 246.St -p1003.1b . 247It provides much more information about the causes and 248attributes of the signal that is being delivered. 249.It Dv SA_RESTART 250If a signal is caught during the system calls listed below, 251the call may be forced to terminate 252with the error 253.Er EINTR , 254the call may return with a data transfer shorter than requested, 255or the call may be restarted. 256Restarting of pending calls is requested 257by setting the 258.Dv SA_RESTART 259bit in 260.Ar sa_flags . 261The affected system calls include 262.Xr read 2 , 263.Xr write 2 , 264.Xr sendto 2 , 265.Xr recvfrom 2 , 266.Xr sendmsg 2 267and 268.Xr recvmsg 2 269on a communications channel or a slow device (such as a terminal, 270but not a regular file) 271and during a 272.Xr wait 2 273or 274.Xr ioctl 2 . 275However, calls that have already committed are not restarted, 276but instead return a partial success (for example, a short read count). 277.El 278.Pp 279After a 280.Xr fork 2 281or 282.Xr vfork 2 , 283all signals, the signal mask, the signal stack, 284and the restart/interrupt flags are inherited by the child. 285.Pp 286.Xr execve 2 287reinstates the default 288action for all signals which were caught and 289resets all signals to be caught on the user stack. 290Ignored signals remain ignored; 291the signal mask remains the same; 292signals that restart pending system calls continue to do so. 293.Pp 294The following is a list of all signals 295with names as in the include file 296.Aq Pa signal.h : 297.Bl -column SIGVTALARMXX "create core imagexxx" 298.It Sy " NAME " " Default Action " " Description" 299.It Dv SIGHUP No " terminate process" " terminal line hangup" 300.It Dv SIGINT No " terminate process" " interrupt program" 301.It Dv SIGQUIT No " create core image" " quit program" 302.It Dv SIGILL No " create core image" " illegal instruction" 303.It Dv SIGTRAP No " create core image" " trace trap" 304.It Dv SIGABRT No " create core image" Xr abort 3 305call (formerly 306.Dv SIGIOT ) 307.It Dv SIGEMT No " create core image" " emulate instruction executed" 308.It Dv SIGFPE No " create core image" " floating-point exception" 309.It Dv SIGKILL No " terminate process" " kill program (cannot be caught or ignored)" 310.It Dv SIGBUS No " create core image" " bus error" 311.It Dv SIGSEGV No " create core image" " segmentation violation" 312.It Dv SIGSYS No " create core image" " system call given invalid argument" 313.It Dv SIGPIPE No " terminate process" " write on a pipe with no reader" 314.It Dv SIGALRM No " terminate process" " real-time timer expired" 315.It Dv SIGTERM No " terminate process" " software termination signal" 316.It Dv SIGURG No " discard signal" " urgent condition present on socket" 317.It Dv SIGSTOP No " stop process" " stop (cannot be caught or ignored)" 318.It Dv SIGTSTP No " stop process" " stop signal generated from keyboard" 319.It Dv SIGCONT No " discard signal" " continue after stop" 320.It Dv SIGCHLD No " discard signal" " child status has changed" 321.It Dv SIGTTIN No " stop process" " background read attempted from control terminal" 322.It Dv SIGTTOU No " stop process" " background write attempted to control terminal" 323.It Dv SIGIO No " discard signal" Tn " I/O" 324is possible on a descriptor (see 325.Xr fcntl 2 ) 326.It Dv SIGXCPU No " terminate process" " CPU time limit exceeded (see" 327.Xr setrlimit 2 ) 328.It Dv SIGXFSZ No " terminate process" " file size limit exceeded (see" 329.Xr setrlimit 2 ) 330.It Dv SIGVTALRM No " terminate process" " virtual time alarm (see" 331.Xr setitimer 2 ) 332.It Dv SIGPROF No " terminate process" " profiling timer alarm (see" 333.Xr setitimer 2 ) 334.It Dv SIGWINCH No " discard signal" " window size change" 335.It Dv SIGINFO No " discard signal" " status request from keyboard" 336.It Dv SIGUSR1 No " terminate process" " user defined signal 1" 337.It Dv SIGUSR2 No " terminate process" " user defined signal 2" 338.El 339.Sh NOTE 340The 341.Fa sa_mask 342field specified in 343.Fa act 344is not allowed to block 345.Dv SIGKILL 346or 347.Dv SIGSTOP . 348Any attempt to do so will be silently ignored. 349.Pp 350The following functions are either reentrant or not interruptible 351by signals and are async-signal safe. 352Therefore applications may invoke them, without restriction, from 353signal-catching functions: 354.Bd -ragged -offset indent 355.Xr _exit 2 , 356.Xr access 2 , 357.Xr alarm 3 , 358.Xr cfgetispeed 3 , 359.Xr cfgetospeed 3 , 360.Xr cfsetispeed 3 , 361.Xr cfsetospeed 3 , 362.Xr chdir 2 , 363.Xr chmod 2 , 364.Xr chown 2 , 365.Xr close 2 , 366.Xr creat 3 , 367.Xr dup 2 , 368.Xr dup2 2 , 369.Xr execle 3 , 370.Xr execve 2 , 371.Xr fcntl 2 , 372.Xr fork 2 , 373.Xr fpathconf 2 , 374.Xr fstat 2 , 375.Xr fsync 2 , 376.Xr getegid 2 , 377.Xr geteuid 2 , 378.Xr getgid 2 , 379.Xr getgroups 2 , 380.Xr getpgrp 2 , 381.Xr getpid 2 , 382.Xr getppid 2 , 383.Xr getuid 2 , 384.Xr kill 2 , 385.Xr link 2 , 386.Xr lseek 2 , 387.Xr mkdir 2 , 388.Xr mkfifo 2 , 389.Xr open 2 , 390.Xr pathconf 2 , 391.Xr pause 3 , 392.Xr pipe 2 , 393.Xr raise 3 , 394.Xr read 2 , 395.Xr rename 2 , 396.Xr rmdir 2 , 397.Xr setgid 2 , 398.Xr setpgid 2 , 399.Xr setsid 2 , 400.Xr setuid 2 , 401.Xr sigaction 2 , 402.Xr sigaddset 3 , 403.Xr sigdelset 3 , 404.Xr sigemptyset 3 , 405.Xr sigfillset 3 , 406.Xr sigismember 3 , 407.Xr signal 3 , 408.Xr sigpause 3 , 409.Xr sigpending 2 , 410.Xr sigprocmask 2 , 411.Xr sigsuspend 2 , 412.Xr sleep 3 , 413.Xr stat 2 , 414.Xr sysconf 3 , 415.Xr tcdrain 3 , 416.Xr tcflow 3 , 417.Xr tcflush 3 , 418.Xr tcgetattr 3 , 419.Xr tcgetpgrp 3 , 420.Xr tcsendbreak 3 , 421.Xr tcsetattr 3 , 422.Xr tcsetpgrp 3 , 423.Xr time 3 , 424.Xr times 3 , 425.Xr umask 2 , 426.Xr uname 3 , 427.Xr unlink 2 , 428.Xr utime 3 , 429.Xr wait 2 , 430.Xr waitpid 2 , 431.Xr write 2 . 432.\" .Fn aio_error , 433.\" .Fn clock_gettime , 434.\" .Fn timer_getoverrun , 435.\" .Fn aio_return , 436.\" .Fn fdatasync , 437.\" .Fn sigqueue , 438.\" .Fn timer_gettime , 439.\" .Fn aio_suspend , 440.\" .Fn sem_post , 441.\" .Fn timer_settime . 442.Ed 443.Pp 444Please see 445.Xr signal 3 446for a more detailed list. 447.Pp 448All functions not in the above list are considered to be unsafe 449with respect to signals. 450That is to say, the behaviour of such functions when called from 451a signal handler is undefined. 452In general though, signal handlers should do little more than set a 453flag; most other actions are not safe. 454.Pp 455Additionally, it is advised that signal handlers guard against 456modification of the external symbol 457.Va errno 458by the above functions, saving it at entry and restoring 459it on return, thus: 460.Bd -literal -offset indent 461void 462handler(int sig) 463{ 464 int save_errno = errno; 465 466 ... 467 errno = save_errno; 468} 469.Ed 470.Sh RETURN VALUES 471A 0 value indicates that the call succeeded. 472A \-1 return value indicates an error occurred and 473.Va errno 474is set to indicate the reason. 475.Sh EXAMPLES 476The handler routine can be declared: 477.Bd -literal -offset indent 478void 479handler(int sig) 480.Pp 481.Ed 482If the 483.Dv SA_SIGINFO 484option is enabled, the canonical way to declare it is: 485.Bd -literal -offset indent 486void 487handler(int sig, siginfo_t *sip, struct sigcontext *scp) 488.Ed 489.Pp 490Here 491.Fa sig 492is the signal number, into which the hardware faults and traps are mapped. 493If the 494.Dv SA_SIGINFO 495option is set, 496.Fa sip 497is a pointer to a 498.Dv siginfo_t 499as described in 500.Aq Pa sys/siginfo.h . 501If 502.Dv SA_SIGINFO 503is not set, this pointer will be 504.Dv NULL 505instead. 506The function specified in 507.Fa sa_sigaction 508will be called instead of the function specified by 509.Fa sa_handler 510(Note that in some implementations these are in fact the same). 511.Fa scp 512is a pointer to the 513.Fa sigcontext 514structure (defined in 515.Aq Pa signal.h ) , 516used to restore the context from before the signal. 517.Sh ERRORS 518.Fn sigaction 519will fail and no new signal handler will be installed if one 520of the following occurs: 521.Bl -tag -width Er 522.It Bq Er EFAULT 523Either 524.Fa act 525or 526.Fa oact 527points to memory that is not a valid part of the process 528address space. 529.It Bq Er EINVAL 530.Fa sig 531is not a valid signal number. 532.It Bq Er EINVAL 533An attempt is made to ignore or supply a handler for 534.Dv SIGKILL 535or 536.Dv SIGSTOP . 537.El 538.Sh SEE ALSO 539.Xr kill 1 , 540.Xr kill 2 , 541.Xr ptrace 2 , 542.Xr sigaltstack 2 , 543.Xr sigprocmask 2 , 544.Xr sigsuspend 2 , 545.Xr wait 2 , 546.Xr setjmp 3 , 547.Xr sigblock 3 , 548.Xr sigpause 3 , 549.Xr sigsetops 3 , 550.Xr sigvec 3 , 551.Xr tty 4 552.Sh STANDARDS 553The 554.Fn sigaction 555function conforms to 556.St -p1003.1-90 . 557The 558.Dv SA_ONSTACK 559and 560.Dv SA_RESTART 561flags are Berkeley extensions, as are the signals 562.Dv SIGTRAP , 563.Dv SIGEMT , 564.Dv SIGBUS , 565.Dv SIGSYS , 566.Dv SIGURG , 567.Dv SIGIO , 568.Dv SIGXCPU , 569.Dv SIGXFSZ , 570.Dv SIGVTALRM , 571.Dv SIGPROF , 572.Dv SIGWINCH , 573and 574.Dv SIGINFO . 575These signals are available on most 576.Tn BSD Ns \-derived 577systems. 578The 579.Dv SA_NODEFER 580and 581.Dv SA_RESETHAND 582flags are intended for backwards compatibility with other operating 583systems. 584The 585.Dv SA_NOCLDSTOP , 586.Dv SA_NOCLDWAIT , 587and 588.Dv SA_SIGINFO 589flags are options commonly found in other operating systems. 590The following functions are either reentrant or not interruptible 591by signals and are async-signal safe. 592Therefore applications may 593invoke them, without restriction, from signal-catching functions: 594.Pp 595Base Interfaces: 596.Pp 597.Fn _exit , 598.Fn access , 599.Fn alarm , 600.Fn cfgetispeed , 601.Fn cfgetospeed , 602.Fn cfsetispeed , 603.Fn cfsetospeed , 604.Fn chdir , 605.Fn chmod , 606.Fn chown , 607.Fn close , 608.Fn creat , 609.Fn dup , 610.Fn dup2 , 611.Fn execle , 612.Fn execve , 613.Fn fcntl , 614.Fn fork , 615.Fn fpathconf , 616.Fn fstat , 617.Fn fsync , 618.Fn getegid , 619.Fn geteuid , 620.Fn getgid , 621.Fn getgroups , 622.Fn getpgrp , 623.Fn getpid , 624.Fn getppid , 625.Fn getuid , 626.Fn kill , 627.Fn link , 628.Fn lseek , 629.Fn mkdir , 630.Fn mkfifo , 631.Fn open , 632.Fn pathconf , 633.Fn pause , 634.Fn pipe , 635.Fn raise , 636.Fn read , 637.Fn rename , 638.Fn rmdir , 639.Fn setgid , 640.Fn setpgid , 641.Fn setsid , 642.Fn setuid , 643.Fn sigaction , 644.Fn sigaddset , 645.Fn sigdelset , 646.Fn sigemptyset , 647.Fn sigfillset , 648.Fn sigismember , 649.Fn signal , 650.Fn sigpending , 651.Fn sigprocmask , 652.Fn sigsuspend , 653.Fn sleep , 654.Fn stat , 655.Fn sysconf , 656.Fn tcdrain , 657.Fn tcflow , 658.Fn tcflush , 659.Fn tcgetattr , 660.Fn tcgetpgrp , 661.Fn tcsendbreak , 662.Fn tcsetattr , 663.Fn tcsetpgrp , 664.Fn time , 665.Fn times , 666.Fn umask , 667.Fn uname , 668.Fn unlink , 669.Fn utime , 670.Fn wait , 671.Fn waitpid , 672.Fn write . 673.Pp 674.\" Realtime Interfaces: 675.\" .Pp 676.\" .Fn aio_error , 677.\" .Fn aio_return , 678.\" .Fn aio_suspend , 679.\" .Fn clock_gettime , 680.\" .Fn fdatasync , 681.\" .Fn sem_post , 682.\" .Fn sigpause , 683.\" .Fn sigqueue , 684.\" .Fn sigset , 685.\" .Fn timer_getoverrun , 686.\" .Fn timer_gettime , 687.\" .Fn timer_settime . 688.\" .Pp 689ANSI C Interfaces: 690.Pp 691.Fn strcat , 692.Fn strcpy , 693.Fn strncat , 694.Fn strncpy , 695and perhaps some others. 696.Pp 697Extension Interfaces: 698.Pp 699.Fn strlcat , 700.Fn strlcpy . 701.Pp 702Most functions not in the above lists are considered to be unsafe 703with respect to signals. 704That is to say, the behaviour of such functions when called from 705a signal handler is undefined. 706.Pp 707Additionally, inside the signal handler it is also considered safer to 708make a copy of the global variable 709.Va errno 710and restore it before returning from the signal handler. 711.Pp 712A few other functions are signal race safe in 713.Ox 714but probably not on other systems: 715.Pp 716.Bl -tag -offset indent -compact -width foofoofoofoo 717.It Fn snprintf 718Safe. 719.It Fn vsnprintf 720Safe. 721.It Fn syslog_r 722Safe if the 723.Va syslog_data 724struct is initialized as a local variable. 725.El 726