1.\" $OpenBSD: sigaction.2,v 1.72 2015/10/11 09:51:26 guenther 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: October 11 2015 $ 34.Dt SIGACTION 2 35.Os 36.Sh NAME 37.Nm sigaction 38.Nd software signal facilities 39.Sh SYNOPSIS 40.In signal.h 41.Bd -literal 42struct sigaction { 43 union { /* signal handler */ 44 void (*__sa_handler)(int); 45 void (*__sa_sigaction)(int, siginfo_t *, void *); 46 } __sigaction_u; 47 sigset_t sa_mask; /* signal mask to apply */ 48 int sa_flags; /* see signal options below */ 49}; 50.Ed 51.Pp 52.Fd #define sa_handler __sigaction_u.__sa_handler 53.Fd #define sa_sigaction __sigaction_u.__sa_sigaction 54.Ft int 55.Fn sigaction "int sig" "const struct sigaction *act" "struct sigaction *oact" 56.Sh DESCRIPTION 57The system defines a set of signals that may be delivered to a process. 58Signal delivery resembles the occurrence of a hardware interrupt: 59the signal is normally blocked from further occurrence, the current process 60context is saved, and a new one is built. 61A process may specify a 62.Em handler 63to which a signal is delivered, or specify that a signal is to be 64.Em ignored . 65A process may also specify that a default action is to be taken 66by the system when a signal occurs. 67A signal may also be 68.Em blocked , 69in which case its delivery is postponed until it is 70.Em unblocked . 71The action to be taken on delivery is determined at the time 72of delivery. 73Normally, signal handlers execute on the current stack 74of the process. 75This may be changed, on a per-handler basis, 76so that signals are taken on a special 77.Em "signal stack" . 78.Pp 79Signal routines normally execute with the signal that caused their 80invocation 81.Em blocked , 82but other signals may yet occur. 83A global 84.Em "signal mask" 85defines the set of signals currently blocked from delivery 86to a process. 87The signal mask for a process is initialized from that of its 88parent (normally empty). 89It may be changed with a 90.Xr sigprocmask 2 91call, or when a signal is delivered to the process. 92.Pp 93When a signal 94condition arises for a process, the signal is added to a set of 95signals pending for the process. 96If the signal is not currently 97.Em blocked 98by the process then it is delivered to the process. 99Signals may be delivered any time a process enters the operating system 100(e.g., during a system call, page fault or trap, or clock interrupt). 101If multiple signals are ready to be delivered at the same time, 102any signals that could be caused by traps are delivered first. 103Additional signals may be processed at the same time, with each 104appearing to interrupt the handlers for the previous signals 105before their first instructions. 106The set of pending signals is returned by the 107.Xr sigpending 2 108function. 109When a caught signal 110is delivered, the current state of the process is saved, 111a new signal mask is calculated (as described below), 112and the signal handler is invoked. 113The call to the handler is arranged so that if the signal handling routine 114returns normally the process will resume execution in the context from 115before the signal's delivery. 116If the process wishes to resume in a different context, then it 117must arrange to restore the previous context itself. 118.Pp 119When a signal is delivered to a process a new signal mask is 120installed for the duration of the process' signal handler 121(or until a 122.Xr sigprocmask 2 123call is made). 124This mask is formed by taking the union of the current signal mask set, 125the signal to be delivered, and the signal mask 126.Fa sa_mask 127associated with the handler to be invoked, but always excluding 128.Dv SIGKILL 129and 130.Dv SIGSTOP . 131.Pp 132.Fn sigaction 133assigns an action for a signal specified by 134.Fa sig . 135If 136.Fa act 137is non-zero, it 138specifies an action 139.Pf ( Dv SIG_DFL , 140.Dv SIG_IGN , 141or a handler routine) and mask 142to be used when delivering the specified signal. 143If 144.Fa oact 145is non-zero, the previous handling information for the signal 146is returned to the user. 147.Pp 148Once a signal handler is installed, it normally remains installed 149until another 150.Fn sigaction 151call is made, or an 152.Xr execve 2 153is performed. 154The value of 155.Fa sa_handler 156(or, if the 157.Dv SA_SIGINFO 158flag is set, the value of 159.Fa sa_sigaction 160instead) indicates what action should be performed when a 161signal arrives. 162A signal-specific default action may be reset by 163setting 164.Fa sa_handler 165to 166.Dv SIG_DFL . 167Alternately, if the 168.Dv SA_RESETHAND 169flag is set the default action will be reinstated when the signal 170is first posted. 171The defaults are process termination, possibly with core dump; 172no action; stopping the process; or continuing the process. 173See the signal list below for each signal's default action. 174If 175.Fa sa_handler 176is 177.Dv SIG_DFL , 178the default action for the signal is to discard the signal, 179and if a signal is pending, 180the pending signal is discarded even if the signal is masked. 181If 182.Fa sa_handler 183is set to 184.Dv SIG_IGN , 185current and pending instances 186of the signal are ignored and discarded. 187If 188.Fa sig 189is 190.Dv SIGCHLD 191and 192.Fa sa_handler 193is set to 194.Dv SIG_IGN , 195the 196.Dv SA_NOCLDWAIT 197flag (described below) is implied. 198.Pp 199Options may be specified by setting 200.Fa sa_flags . 201The meaning of the various bits is as follows: 202.Bl -tag -offset indent -width SA_RESETHANDXX 203.It Dv SA_NOCLDSTOP 204If this bit is set when installing a catching function 205for the 206.Dv SIGCHLD 207signal, 208the 209.Dv SIGCHLD 210signal will be generated only when a child process exits, 211not when a child process stops. 212.It Dv SA_NOCLDWAIT 213If this bit is set when calling 214.Fn sigaction 215for the 216.Dv SIGCHLD 217signal, the system will not create zombie processes when children of 218the calling process exit, 219though existing zombies will remain. 220If the calling process subsequently issues a 221.Xr waitpid 2 222(or equivalent) and there are no previously existing zombie child 223processes that match the 224.Xr waitpid 2 225criteria, 226it blocks until all of the calling process's child 227processes that would match terminate, 228and then returns a value of \-1 with 229.Va errno 230set to 231.Er ECHILD . 232.It Dv SA_ONSTACK 233If this bit is set, the system will deliver the signal to the process 234on a 235.Em "signal stack" , 236specified with 237.Xr sigaltstack 2 . 238.It Dv SA_NODEFER 239If this bit is set, further occurrences of the delivered signal are 240not masked during the execution of the handler. 241.It Dv SA_RESETHAND 242If this bit is set, the handler is reset back to 243.Dv SIG_DFL 244at the moment the signal is delivered. 245.It Dv SA_SIGINFO 246If this bit is set, the 2nd argument of the handler is set to 247be a pointer to a 248.Em siginfo_t 249structure as described in 250.In sys/siginfo.h . 251It provides much more information about the causes and 252attributes of the signal that is being delivered. 253.It Dv SA_RESTART 254If a signal is caught during the system calls listed below, 255the call may be forced to terminate 256with the error 257.Er EINTR , 258the call may return with a data transfer shorter than requested, 259or the call may be restarted. 260Restarting of pending calls is requested 261by setting the 262.Dv SA_RESTART 263bit in 264.Fa sa_flags . 265The affected system calls include 266.Xr read 2 , 267.Xr write 2 , 268.Xr sendto 2 , 269.Xr recvfrom 2 , 270.Xr sendmsg 2 271and 272.Xr recvmsg 2 273on a communications channel or a slow device (such as a terminal, 274but not a regular file) 275and during a 276.Xr wait 2 277or 278.Xr ioctl 2 . 279However, calls that have already committed are not restarted, 280but instead return a partial success (for example, a short read count). 281.El 282.Pp 283After a 284.Xr fork 2 285or 286.Xr vfork 2 , 287all signals, the signal mask, the signal stack, 288and the restart/interrupt flags are inherited by the child. 289.Pp 290.Xr execve 2 291reinstates the default 292action for 293.Dv SIGCHLD 294and all signals which were caught; all other signals remain ignored. 295All signals are reset to be caught on the user stack and 296the signal mask remains the same; 297signals that restart pending system calls continue to do so. 298.Pp 299The following is a list of all signals 300with names as in the include file 301.In signal.h : 302.Bl -column "SIGVTALARM" "create core image" "Description" 303.It Sy "Name" Ta Sy "Default Action" Ta Sy "Description" 304.It Dv SIGHUP Ta "terminate process" Ta "terminal line hangup" 305.It Dv SIGINT Ta "terminate process" Ta "interrupt program" 306.It Dv SIGQUIT Ta "create core image" Ta "quit program" 307.It Dv SIGILL Ta "create core image" Ta "illegal instruction" 308.It Dv SIGTRAP Ta "create core image" Ta "trace trap" 309.It Dv SIGABRT Ta "create core image" Ta "abort(3) call (formerly SIGIOT)" 310.It Dv SIGEMT Ta "create core image" Ta "emulate instruction executed" 311.It Dv SIGFPE Ta "create core image" Ta "floating-point exception" 312.It Dv SIGKILL Ta "terminate process" Ta "kill program (cannot be caught or ignored)" 313.It Dv SIGBUS Ta "create core image" Ta "bus error" 314.It Dv SIGSEGV Ta "create core image" Ta "segmentation violation" 315.It Dv SIGSYS Ta "create core image" Ta "system call given invalid argument" 316.It Dv SIGPIPE Ta "terminate process" Ta "write on a pipe with no reader" 317.It Dv SIGALRM Ta "terminate process" Ta "real-time timer expired" 318.It Dv SIGTERM Ta "terminate process" Ta "software termination signal" 319.It Dv SIGURG Ta "discard signal" Ta "urgent condition present on socket" 320.It Dv SIGSTOP Ta "stop process" Ta "stop (cannot be caught or ignored)" 321.It Dv SIGTSTP Ta "stop process" Ta "stop signal generated from keyboard" 322.It Dv SIGCONT Ta "discard signal" Ta "continue after stop" 323.It Dv SIGCHLD Ta "discard signal" Ta "child status has changed" 324.It Dv SIGTTIN Ta "stop process" Ta "background read attempted from control terminal" 325.It Dv SIGTTOU Ta "stop process" Ta "background write attempted to control terminal" 326.It Dv SIGIO Ta "discard signal" Ta "I/O is possible on a descriptor (see" 327.Xr fcntl 2 ) 328.It Dv SIGXCPU Ta "terminate process" Ta "CPU time limit exceeded (see" 329.Xr setrlimit 2 ) 330.It Dv SIGXFSZ Ta "terminate process" Ta "file size limit exceeded (see" 331.Xr setrlimit 2 ) 332.It Dv SIGVTALRM Ta "terminate process" Ta "virtual time alarm (see" 333.Xr setitimer 2 ) 334.It Dv SIGPROF Ta "terminate process" Ta "profiling timer alarm (see" 335.Xr setitimer 2 ) 336.It Dv SIGWINCH Ta "discard signal" Ta "window size change" 337.It Dv SIGINFO Ta "discard signal" Ta "status request from keyboard" 338.It Dv SIGUSR1 Ta "terminate process" Ta "user defined signal 1" 339.It Dv SIGUSR2 Ta "terminate process" Ta "user defined signal 2" 340.It Dv SIGTHR Ta "discard signal" Ta "thread AST" 341.El 342.Sh RETURN VALUES 343.Rv -std 344.Sh EXAMPLES 345The handler routine can be declared: 346.Bd -literal -offset indent 347void 348handler(int sig) 349.Pp 350.Ed 351If the 352.Dv SA_SIGINFO 353option is enabled, the canonical way to declare it is: 354.Bd -literal -offset indent 355void 356handler(int sig, siginfo_t *sip, void *ctx) 357.Ed 358.Pp 359Here 360.Fa sig 361is the signal number, into which the hardware faults and traps are mapped. 362If the 363.Dv SA_SIGINFO 364option is set, 365.Fa sip 366is a pointer to a 367.Dv siginfo_t 368as described in 369.In sys/siginfo.h . 370If 371.Dv SA_SIGINFO 372is not set, this pointer will be 373.Dv NULL 374instead. 375The function specified in 376.Fa sa_sigaction 377will be called instead of the function specified by 378.Fa sa_handler 379(note that in some implementations these are in fact the same). 380.Fa ctx 381may be cast to a pointer to 382.Fa ucontext_t 383which can be used to restore the thread's context from before the signal. 384On 385.Ox , 386.Fa ucontext_t 387is an alias for the 388.Fa sigcontext 389structure defined in 390.In signal.h . 391The contents of this structure are machine-dependent. 392.Sh ERRORS 393.Fn sigaction 394will fail and no new signal handler will be installed if one 395of the following occurs: 396.Bl -tag -width Er 397.It Bq Er EFAULT 398Either 399.Fa act 400or 401.Fa oact 402points to memory that is not a valid part of the process 403address space. 404.It Bq Er EINVAL 405.Fa sig 406is not a valid signal number. 407.It Bq Er EINVAL 408An attempt is made to ignore or supply a handler for 409.Dv SIGKILL 410or 411.Dv SIGSTOP . 412.El 413.Sh SEE ALSO 414.Xr kill 1 , 415.Xr kill 2 , 416.Xr ptrace 2 , 417.Xr sigaltstack 2 , 418.Xr sigprocmask 2 , 419.Xr sigsuspend 2 , 420.Xr wait 2 , 421.Xr setjmp 3 , 422.Xr sigblock 3 , 423.Xr sigpause 3 , 424.Xr sigsetops 3 , 425.Xr sigvec 3 , 426.Xr tty 4 427.Sh STANDARDS 428The 429.Fn sigaction 430function conforms to 431.St -p1003.1-2008 . 432.Pp 433The 434.Dv SA_ONSTACK 435flag and the 436.Dv SIGPROF , 437.Dv SIGSYS , 438.Dv SIGTRAP , 439.Dv SIGVTALRM , 440.Dv SIGXCPU , 441and 442.Dv SIGXFSZ 443signals conform to the X/Open System Interfaces option of that standard. 444The standard marks 445.Dv SIGPROF 446as obsolescent. 447The signals 448.Dv SIGEMT , 449.Dv SIGINFO , 450.Dv SIGIO , 451and 452.Dv SIGWINCH 453are Berkeley extensions. 454These signals are available on most 455.Bx Ns -derived 456systems. 457The 458.Dv SIGTHR 459signal is specific to 460.Ox 461and is part of the 462implementation of thread cancellation; 463.Fa sigaction 464and other signal interfaces may reject attempts to use or alter the 465handling of 466.Dv SIGTHR . 467.Pp 468The following functions are either reentrant or not interruptible 469by signals and are async-signal-safe. 470Therefore applications may 471invoke them, without restriction, from signal-catching functions: 472.Pp 473Standard Interfaces: 474.Pp 475.Fn _exit , 476.Fn _Exit , 477.Fn abort , 478.Fn accept , 479.Fn access , 480.Fn alarm , 481.Fn bind , 482.Fn cfgetispeed , 483.Fn cfgetospeed , 484.Fn cfsetispeed , 485.Fn cfsetospeed , 486.Fn chdir , 487.Fn chmod , 488.Fn chown , 489.Fn clock_gettime , 490.Fn close , 491.Fn connect , 492.Fn creat , 493.Fn dup , 494.Fn dup2 , 495.Fn execl , 496.Fn execle , 497.Fn execv , 498.Fn execve , 499.Fn faccessat , 500.Fn fchdir , 501.Fn fchmod , 502.Fn fchmodat , 503.Fn fchown , 504.Fn fchownat , 505.Fn fcntl , 506.Fn fdatasync , 507.Fn fork , 508.Fn fpathconf , 509.Fn fstat , 510.Fn fstatat , 511.Fn fsync , 512.Fn ftruncate , 513.Fn futimens , 514.Fn futimes , 515.Fn getegid , 516.Fn geteuid , 517.Fn getgid , 518.Fn getgroups , 519.Fn getpeername , 520.Fn getpgrp , 521.Fn getpid , 522.Fn getppid , 523.Fn getsockname , 524.Fn getsockopt , 525.Fn getuid , 526.Fn kill , 527.Fn link , 528.Fn linkat , 529.Fn listen , 530.Fn lseek , 531.Fn lstat , 532.Fn mkdir , 533.Fn mkdirat , 534.Fn mkfifo , 535.Fn mkfifoat , 536.Fn mknod , 537.Fn mknodat , 538.Fn open , 539.Fn openat , 540.Fn pathconf , 541.Fn pause , 542.Fn pipe , 543.Fn poll , 544.Fn pselect , 545.Fn pthread_sigmask , 546.Fn raise , 547.Fn read , 548.Fn readlink , 549.Fn readlinkat , 550.Fn recv , 551.Fn recvfrom , 552.Fn recvmsg , 553.Fn rename , 554.Fn renameat , 555.Fn rmdir , 556.Fn select , 557.Fn send , 558.Fn sendmsg , 559.Fn sendto , 560.Fn setgid , 561.Fn setpgid , 562.Fn setsid , 563.Fn setsockopt , 564.Fn setuid , 565.Fn shutdown , 566.Fn sigaction , 567.Fn sigaddset , 568.Fn sigdelset , 569.Fn sigemptyset , 570.Fn sigfillset , 571.Fn sigismember , 572.Fn signal , 573.Fn sigpause , 574.Fn sigpending , 575.Fn sigprocmask , 576.Fn sigsuspend , 577.Fn sleep , 578.Fn sockatmark , 579.Fn socket , 580.Fn socketpair , 581.Fn stat , 582.Fn strcat , 583.Fn strcpy , 584.Fn strncat , 585.Fn strncpy , 586.Fn symlink , 587.Fn symlinkat , 588.Fn sysconf , 589.Fn tcdrain , 590.Fn tcflow , 591.Fn tcflush , 592.Fn tcgetattr , 593.Fn tcgetpgrp , 594.Fn tcsendbreak , 595.Fn tcsetattr , 596.Fn tcsetpgrp , 597.Fn time , 598.Fn times , 599.Fn umask , 600.Fn uname , 601.Fn unlink , 602.Fn unlinkat , 603.Fn utime , 604.Fn utimensat , 605.Fn utimes , 606.Fn wait , 607.Fn waitpid , 608.Fn write , 609and perhaps some others. 610.\" unimplemented functions that should be async-sig-safe, if we had them 611.\" POSIX Issue 7 additions 612.\" .Pp 613.\" .Fn fexecve . 614.\" 615.\" Realtime Interfaces: 616.\" .Pp 617.\" .Fn aio_error , 618.\" .Fn aio_return , 619.\" .Fn aio_suspend , 620.\" .Fn sem_post , 621.\" .Fn sigqueue , 622.\" .Fn timer_getoverrun , 623.\" .Fn timer_gettime , 624.\" .Fn timer_settime . 625.Pp 626Extension Interfaces: 627.Pp 628.Fn accept4 , 629.Fn chflags , 630.Fn chflagsat , 631.Fn dup3 , 632.Fn fchflags , 633.Fn getentropy , 634.Fn getresgid , 635.Fn getresuid , 636.Fn pipe2 , 637.Fn ppoll , 638.Fn sendsyslog , 639.Fn setresgid , 640.Fn setresuid , 641.Fn strlcat , 642.Fn strlcpy , 643.Fn wait3 , 644.Fn wait4 . 645.Pp 646In addition, access and updates to 647.Va errno 648are guaranteed to be safe. 649Most functions not in the above lists are considered to be unsafe 650with respect to signals. 651That is to say, the behaviour of such functions when called from 652a signal handler is undefined. 653In general though, signal handlers should do little more than set a 654flag, ideally of type volatile sig_atomic_t; most other actions are not safe. 655.Pp 656Additionally, it is advised that signal handlers guard against 657modification of the external symbol 658.Va errno 659by the above functions, saving it at entry and restoring 660it on return, thus: 661.Bd -literal -offset indent 662void 663handler(int sig) 664{ 665 int save_errno = errno; 666 667 ... 668 errno = save_errno; 669} 670.Ed 671.Pp 672The functions below are async-signal-safe in 673.Ox 674except when used with floating-point arguments or directives, 675but are probably unsafe on other systems: 676.Pp 677.Bl -tag -offset indent -compact -width foofoofoofoo 678.It Fn dprintf 679Safe. 680.It Fn vdprintf 681Safe. 682.It Fn snprintf 683Safe. 684.It Fn vsnprintf 685Safe. 686.It Fn syslog_r 687Safe if the 688.Va syslog_data 689struct is initialized as a local variable. 690.El 691