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 September 14, 2021 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 SIGCKPTEXITXX "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.It Dv SIGCKPT Ta checkpoint process Ta checkpoint 343.It Dv SIGCKPTEXIT Ta terminate process Ta checkpoint and exit 344.El 345.Sh NOTE 346The 347.Fa sa_mask 348field specified in 349.Fa act 350is not allowed to block 351.Dv SIGKILL 352or 353.Dv SIGSTOP . 354Any attempt to do so will be silently ignored. 355.Pp 356The following functions are either reentrant or not interruptible 357by signals and are async-signal safe. 358Therefore applications may 359invoke them, without restriction, from signal-catching functions 360or from a child process after calling 361.Xr fork 2 362in a multi-threaded process: 363.Pp 364Base Interfaces: 365.Pp 366.Fn _Exit , 367.Fn _exit , 368.Fn access , 369.Fn alarm , 370.Fn cfgetispeed , 371.Fn cfgetospeed , 372.Fn cfsetispeed , 373.Fn cfsetospeed , 374.Fn chdir , 375.Fn chmod , 376.Fn chown , 377.Fn close , 378.Fn creat , 379.Fn dup , 380.Fn dup2 , 381.Fn execle , 382.Fn execve , 383.Fn faccessat , 384.Fn fchmodat , 385.Fn fchownat , 386.Fn fcntl , 387.Fn fork , 388.Fn fpathconf , 389.Fn fstat , 390.Fn fsync , 391.Fn getegid , 392.Fn geteuid , 393.Fn getgid , 394.Fn getgroups , 395.Fn getpgrp , 396.Fn getpid , 397.Fn getppid , 398.Fn getuid , 399.Fn kill , 400.Fn link , 401.Fn lseek , 402.Fn mkdir , 403.Fn mkdirat , 404.Fn mkfifo , 405.Fn mkfifoat , 406.Fn mknod , 407.Fn mknodat , 408.Fn open , 409.Fn openat , 410.Fn pathconf , 411.Fn pause , 412.Fn pipe , 413.Fn raise , 414.Fn read , 415.Fn readlink , 416.Fn readlinkat , 417.Fn rename , 418.Fn renameat , 419.Fn rmdir , 420.Fn setgid , 421.Fn setpgid , 422.Fn setsid , 423.Fn setuid , 424.Fn sigaction , 425.Fn sigaddset , 426.Fn sigdelset , 427.Fn sigemptyset , 428.Fn sigfillset , 429.Fn sigismember , 430.Fn signal , 431.Fn sigpending , 432.Fn sigprocmask , 433.Fn sigsuspend , 434.Fn sleep , 435.Fn stat , 436.Fn symlink , 437.Fn symlinkat , 438.Fn sysconf , 439.Fn tcdrain , 440.Fn tcflow , 441.Fn tcflush , 442.Fn tcgetattr , 443.Fn tcgetpgrp , 444.Fn tcsendbreak , 445.Fn tcsetattr , 446.Fn tcsetpgrp , 447.Fn time , 448.Fn times , 449.Fn umask , 450.Fn uname , 451.Fn unlink , 452.Fn unlinkat , 453.Fn utime , 454.Fn wait , 455.Fn waitpid , 456.Fn write . 457.Pp 458Realtime Interfaces: 459.Pp 460.Fn aio_error , 461.Fn clock_gettime , 462.Fn sigpause , 463.\".Fn timer_getoverrun , 464.Fn aio_return , 465.Fn fdatasync , 466.\".Fn sigqueue , 467.\".Fn timer_gettime , 468.Fn aio_suspend , 469.Fn sem_post . 470.\".Fn sigset . 471.\".Fn timer_settime . 472.Pp 473All functions not in the above lists are considered to be unsafe 474with respect to signals. 475That is to say, the behaviour of such 476functions is undefined when they are called from a signal handler 477that interrupted an unsafe function. 478In general though, signal handlers should do little more than set a 479flag; most other actions are not safe. 480.Pp 481Also, it is good practice to make a copy of the global variable 482.Va errno 483and restore it before returning from the signal handler. 484This protects against the side effect of 485.Va errno 486being set by functions called from inside the signal handler. 487.Sh RETURN VALUES 488.Rv -std sigaction 489.Sh EXAMPLES 490There are three possible prototypes the handler may match: 491.Bl -tag -offset indent -width short 492.It Tn ANSI C : 493.Ft void 494.Fn handler int ; 495.It Traditional BSD style: 496.Ft void 497.Fn handler int "int code" "struct sigcontext *scp" ; 498.It Tn POSIX Dv SA_SIGINFO : 499.Ft void 500.Fn handler int "siginfo_t *info" "ucontext_t *uap" ; 501.El 502.Pp 503The handler function should match the 504.Dv SA_SIGINFO 505prototype if the 506.Dv SA_SIGINFO 507bit is set in 508.Fa sa_flags . 509It then should be pointed to by the 510.Fa sa_sigaction 511member of 512.Vt "struct sigaction" . 513Note that you should not assign 514.Dv SIG_DFL 515or 516.Dv SIG_IGN 517this way. 518.Pp 519If the 520.Dv SA_SIGINFO 521flag is not set, the handler function should match 522either the 523.Tn ANSI C 524or traditional 525.Bx 526prototype and be pointed to by 527the 528.Fa sa_handler 529member of 530.Vt "struct sigaction" . 531In practice, 532.Dx 533always sends the three arguments of the latter and since the 534.Tn ANSI C 535prototype is a subset, both will work. 536The 537.Fa sa_handler 538member declaration in 539.Fx 540include files is that of 541.Tn ANSI C 542(as required by 543.Tn POSIX ) , 544so a function pointer of a 545.Bx Ns -style 546function needs to be cast to 547compile without warning. 548The traditional 549.Bx 550style is not portable and since its capabilities 551are a full subset of a 552.Dv SA_SIGINFO 553handler, 554its use is deprecated. 555.Pp 556The 557.Fa sig 558argument is the signal number, one of the 559.Dv SIG... 560values from 561.In signal.h . 562.Pp 563The 564.Fa code 565argument of the 566.Bx Ns -style 567handler and the 568.Fa si_code 569member of the 570.Fa info 571argument to a 572.Dv SA_SIGINFO 573handler contain a numeric code explaining the 574cause of the signal, usually one of the 575.Dv SI_... 576values from 577.In sys/signal.h 578or codes specific to a signal, i.e.\& one of the 579.Dv FPE_... 580values for 581.Dv SIGFPE . 582.Pp 583The 584.Fa scp 585argument to a 586.Bx Ns -style 587handler points to an instance of 588.Vt "struct sigcontext" . 589.Pp 590The 591.Fa uap 592argument to a 593.Tn POSIX 594.Dv SA_SIGINFO 595handler points to an instance of 596.Vt ucontext_t . 597.Sh ERRORS 598The 599.Fn sigaction 600system call 601will fail and no new signal handler will be installed if one 602of the following occurs: 603.Bl -tag -width Er 604.It Bq Er EFAULT 605Either 606.Fa act 607or 608.Fa oact 609points to memory that is not a valid part of the process 610address space. 611.It Bq Er EINVAL 612The 613.Fa sig 614argument 615is not a valid signal number. 616.It Bq Er EINVAL 617An attempt is made to ignore or supply a handler for 618.Dv SIGKILL 619or 620.Dv SIGSTOP . 621.El 622.Sh SEE ALSO 623.Xr kill 1 , 624.Xr kill 2 , 625.Xr ptrace 2 , 626.Xr sigaltstack 2 , 627.Xr sigpending 2 , 628.Xr sigprocmask 2 , 629.Xr sigsuspend 2 , 630.Xr wait 2 , 631.Xr fpsetmask 3 , 632.Xr setjmp 3 , 633.Xr siginterrupt 3 , 634.Xr sigsetops 3 , 635.Xr ucontext 3 , 636.Xr tty 4 637.Sh STANDARDS 638The 639.Fn sigaction 640system call is expected to conform to 641.St -p1003.1-90 . 642The 643.Dv SA_ONSTACK 644and 645.Dv SA_RESTART 646flags are Berkeley extensions, 647as are the signals, 648.Dv SIGTRAP , 649.Dv SIGEMT , 650.Dv SIGBUS , 651.Dv SIGSYS , 652.Dv SIGURG , 653.Dv SIGIO , 654.Dv SIGXCPU , 655.Dv SIGXFSZ , 656.Dv SIGVTALRM , 657.Dv SIGPROF , 658.Dv SIGWINCH , 659and 660.Dv SIGINFO . 661Those signals are available on most 662.Bx Ns \-derived 663systems. 664The 665.Dv SA_NODEFER 666and 667.Dv SA_RESETHAND 668flags are intended for backwards compatibility with other operating 669systems. 670The 671.Dv SA_NOCLDSTOP , 672and 673.Dv SA_NOCLDWAIT 674.\" and 675.\" SA_SIGINFO 676flags are featuring options commonly found in other operating systems. 677The flags are approved by 678.St -susv2 , 679along with the option to avoid zombie creation by ignoring 680.Dv SIGCHLD . 681