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