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.Dd December 1, 2023 29.Dt SIGACTION 2 30.Os 31.Sh NAME 32.Nm sigaction 33.Nd software signal facilities 34.Sh LIBRARY 35.Lb libc 36.Sh SYNOPSIS 37.In signal.h 38.Bd -literal 39struct sigaction { 40 void (*sa_handler)(int); 41 void (*sa_sigaction)(int, siginfo_t *, void *); 42 int sa_flags; /* see signal options below */ 43 sigset_t sa_mask; /* signal mask to apply */ 44}; 45.Ed 46.Pp 47.Ft int 48.Fo sigaction 49.Fa "int sig" 50.Fa "const struct sigaction * restrict act" 51.Fa "struct sigaction * restrict oact" 52.Fc 53.Sh DESCRIPTION 54The system defines a set of signals that may be delivered to a process. 55Signal delivery resembles the occurrence of a hardware interrupt: 56the signal is normally blocked from further occurrence, the current thread 57context is saved, and a new one is built. 58A process may specify a 59.Em handler 60to which a signal is delivered, or specify that a signal is to be 61.Em ignored . 62A process may also specify that a default action is to be taken 63by the system when a signal occurs. 64A signal may also be 65.Em blocked 66for a thread, 67in which case it will not be delivered to that thread until it is 68.Em unblocked . 69The action to be taken on delivery is determined at the time 70of delivery. 71Normally, signal handlers execute on the current stack 72of the thread. 73This may be changed, on a per-handler basis, 74so that signals are taken on a special 75.Em "signal stack" . 76.Pp 77Signal routines normally execute with the signal that caused their 78invocation 79.Em blocked , 80but other signals may yet occur. 81A global 82.Em "signal mask" 83defines the set of signals currently blocked from delivery 84to a thread. 85The signal mask for a thread is initialized 86from that of its parent (normally empty). 87It may be changed with a 88.Xr sigprocmask 2 89or 90.Xr pthread_sigmask 3 91call, or when a signal is delivered to the thread. 92.Pp 93When a signal 94condition arises for a process or thread, the signal is added to a set of 95signals pending for the process or thread. 96Whether the signal is directed at the process in general or at a specific 97thread depends on how it is generated. 98For signals directed at a specific thread, 99if the signal is not currently 100.Em blocked 101by the thread then it is delivered to the thread. 102For signals directed at the process, 103if the signal is not currently 104.Em blocked 105by all threads then it is delivered to one thread that does not have it blocked 106(the selection of which is unspecified). 107Signals may be delivered any time a thread enters the operating system 108(e.g., during a system call, page fault or trap, or clock interrupt). 109If multiple signals are ready to be delivered at the same time, 110any signals that could be caused by traps are delivered first. 111Additional signals may be processed at the same time, with each 112appearing to interrupt the handlers for the previous signals 113before their first instructions. 114The set of pending signals is returned by the 115.Xr sigpending 2 116system call. 117When a caught signal 118is delivered, the current state of the thread is saved, 119a new signal mask is calculated (as described below), 120and the signal handler is invoked. 121The call to the handler 122is arranged so that if the signal handling routine returns 123normally the thread will resume execution in the context 124from before the signal's delivery. 125If the thread wishes to resume in a different context, then it 126must arrange to restore the previous context itself. 127.Pp 128When a signal is delivered to a thread a new signal mask is 129installed for the duration of the process' signal handler 130(or until a 131.Xr sigprocmask 2 132system call is made). 133This mask is formed by taking the union of the current signal mask set, 134the signal to be delivered, and 135the signal mask associated with the handler to be invoked. 136.Pp 137The 138.Fn sigaction 139system call 140assigns an action for a signal specified by 141.Fa sig . 142If 143.Fa act 144is non-NULL, it specifies an action 145.Dv ( SIG_DFL , 146.Dv SIG_IGN , 147or a handler routine) and mask to be used when delivering the specified signal. 148If 149.Fa oact 150is non-NULL, the previous handling information for the signal 151is returned to the user. 152.Pp 153The above declaration of 154.Vt "struct sigaction" 155is not literal. 156It is provided only to list the accessible members. 157See 158.In sys/signal.h 159for the actual definition. 160In particular, the storage occupied by 161.Va sa_handler 162and 163.Va sa_sigaction 164overlaps, and it is nonsensical for an application to attempt to use both 165simultaneously. 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.Va 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.Va 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.Va 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.Va 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.Va 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.Va 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.Va 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.Va 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 accept , 367.Fn access , 368.Fn alarm , 369.Fn bind , 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 connect , 379.Fn creat , 380.Fn dup , 381.Fn dup2 , 382.Fn execl , 383.Fn execle , 384.Fn execv , 385.Fn execve , 386.Fn faccessat , 387.Fn fchdir , 388.Fn fchmod , 389.Fn fchmodat , 390.Fn fchown , 391.Fn fchownat , 392.Fn fcntl , 393.Fn _Fork , 394.Fn fstat , 395.Fn fstatat , 396.Fn fsync , 397.Fn ftruncate , 398.Fn getegid , 399.Fn geteuid , 400.Fn getgid , 401.Fn getgroups , 402.Fn getpeername , 403.Fn getpgrp , 404.Fn getpid , 405.Fn getppid , 406.Fn getsockname , 407.Fn getsockopt , 408.Fn getuid , 409.Fn kill , 410.Fn link , 411.Fn linkat , 412.Fn listen , 413.Fn lseek , 414.Fn lstat , 415.Fn mkdir , 416.Fn mkdirat , 417.Fn mkfifo , 418.Fn mkfifoat , 419.Fn mknod , 420.Fn mknodat , 421.Fn open , 422.Fn openat , 423.Fn pause , 424.Fn pipe , 425.Fn poll , 426.Fn pselect , 427.Fn pthread_sigmask , 428.Fn raise , 429.Fn read , 430.Fn readlink , 431.Fn readlinkat , 432.Fn recv , 433.Fn recvfrom , 434.Fn recvmsg , 435.Fn rename , 436.Fn renameat , 437.Fn rmdir , 438.Fn select , 439.Fn send , 440.Fn sendmsg , 441.Fn sendto , 442.Fn setgid , 443.Fn setpgid , 444.Fn setsid , 445.Fn setsockopt , 446.Fn setuid , 447.Fn shutdown , 448.Fn sigaction , 449.Fn sigaddset , 450.Fn sigdelset , 451.Fn sigemptyset , 452.Fn sigfillset , 453.Fn sigismember , 454.Fn signal , 455.Fn sigpending , 456.Fn sigprocmask , 457.Fn sigsuspend , 458.Fn sleep , 459.Fn sockatmark , 460.Fn socket , 461.Fn socketpair , 462.Fn stat , 463.Fn symlink , 464.Fn symlinkat , 465.Fn tcdrain , 466.Fn tcflow , 467.Fn tcflush , 468.Fn tcgetattr , 469.Fn tcgetpgrp , 470.Fn tcsendbreak , 471.Fn tcsetattr , 472.Fn tcsetpgrp , 473.Fn time , 474.Fn times , 475.Fn umask , 476.Fn uname , 477.Fn unlink , 478.Fn unlinkat , 479.Fn utime , 480.Fn wait , 481.Fn waitpid , 482.Fn write . 483.Pp 484X/Open Systems Interfaces: 485.Pp 486.Fn sigpause , 487.Fn sigset , 488.Fn utimes . 489.Pp 490Realtime Interfaces: 491.Pp 492.Fn aio_error , 493.Fn clock_gettime , 494.Fn timer_getoverrun , 495.Fn aio_return , 496.Fn fdatasync , 497.Fn sigqueue , 498.Fn timer_gettime , 499.Fn aio_suspend , 500.Fn sem_post , 501.Fn timer_settime . 502.Pp 503Base Interfaces not specified as async-signal safe by 504.Tn POSIX : 505.Pp 506.Fn fpathconf , 507.Fn pathconf , 508.Fn sysconf . 509.Pp 510Base Interfaces not specified as async-signal safe by 511.Tn POSIX , 512but planned to be: 513.Pp 514.Fn ffs , 515.Fn htonl , 516.Fn htons , 517.Fn memccpy , 518.Fn memchr , 519.Fn memcmp , 520.Fn memcpy , 521.Fn memmove , 522.Fn memset , 523.Fn ntohl , 524.Fn ntohs , 525.Fn stpcpy , 526.Fn stpncpy , 527.Fn strcat , 528.Fn strchr , 529.Fn strcmp , 530.Fn strcpy , 531.Fn strcspn , 532.Fn strlen , 533.Fn strncat , 534.Fn strncmp , 535.Fn strncpy , 536.Fn strnlen , 537.Fn strpbrk , 538.Fn strrchr , 539.Fn strspn , 540.Fn strstr , 541.Fn strtok_r , 542.Fn wcpcpy , 543.Fn wcpncpy , 544.Fn wcscat , 545.Fn wcschr , 546.Fn wcscmp , 547.Fn wcscpy , 548.Fn wcscspn , 549.Fn wcslen , 550.Fn wcsncat , 551.Fn wcsncmp , 552.Fn wcsncpy , 553.Fn wcsnlen , 554.Fn wcspbrk , 555.Fn wcsrchr , 556.Fn wcsspn , 557.Fn wcsstr , 558.Fn wcstok , 559.Fn wmemchr , 560.Fn wmemcmp , 561.Fn wmemcpy , 562.Fn wmemmove , 563.Fn wmemset . 564.Pp 565Extension Interfaces: 566.Pp 567.Fn accept4 , 568.Fn bindat , 569.Fn close_range , 570.Fn closefrom , 571.Fn connectat , 572.Fn eaccess , 573.Fn ffsl , 574.Fn ffsll , 575.Fn flock , 576.Fn fls , 577.Fn flsl , 578.Fn flsll , 579.Fn futimesat , 580.Fn pipe2 , 581.Fn strlcat . 582.Fn strlcpy , 583.Fn strsep . 584.Pp 585In addition, reading or writing 586.Va errno 587is async-signal safe. 588.Pp 589All functions not in the above lists are considered to be unsafe 590with respect to signals. 591That is to say, the behaviour of such 592functions is undefined when they are called from a signal handler 593that interrupted an unsafe function. 594In general though, signal handlers should do little more than set a 595flag; most other actions are not safe. 596.Pp 597Also, it is good practice to make a copy of the global variable 598.Va errno 599and restore it before returning from the signal handler. 600This protects against the side effect of 601.Va errno 602being set by functions called from inside the signal handler. 603.Sh RETURN VALUES 604.Rv -std sigaction 605.Sh EXAMPLES 606There are three possible prototypes the handler may match: 607.Bl -tag -offset indent -width short 608.It Tn ANSI C : 609.Ft void 610.Fn handler int ; 611.It Traditional BSD style: 612.Ft void 613.Fn handler int "int code" "struct sigcontext *scp" ; 614.It Tn POSIX Dv SA_SIGINFO : 615.Ft void 616.Fn handler int "siginfo_t *info" "ucontext_t *uap" ; 617.El 618.Pp 619The handler function should match the 620.Dv SA_SIGINFO 621prototype if the 622.Dv SA_SIGINFO 623bit is set in 624.Va sa_flags . 625It then should be pointed to by the 626.Va sa_sigaction 627member of 628.Vt "struct sigaction" . 629Note that you should not assign 630.Dv SIG_DFL 631or 632.Dv SIG_IGN 633this way. 634.Pp 635If the 636.Dv SA_SIGINFO 637flag is not set, the handler function should match 638either the 639.Tn ANSI C 640or traditional 641.Bx 642prototype and be pointed to by 643the 644.Va sa_handler 645member of 646.Vt "struct sigaction" . 647In practice, 648.Fx 649always sends the three arguments of the latter and since the 650.Tn ANSI C 651prototype is a subset, both will work. 652The 653.Va sa_handler 654member declaration in 655.Fx 656include files is that of 657.Tn ANSI C 658(as required by 659.Tn POSIX ) , 660so a function pointer of a 661.Bx Ns -style 662function needs to be casted to 663compile without warning. 664The traditional 665.Bx 666style is not portable and since its capabilities 667are a full subset of a 668.Dv SA_SIGINFO 669handler, 670its use is deprecated. 671.Pp 672The 673.Fa sig 674argument is the signal number, one of the 675.Dv SIG... 676values from 677.In signal.h . 678.Pp 679The 680.Fa code 681argument of the 682.Bx Ns -style 683handler and the 684.Va si_code 685member of the 686.Fa info 687argument to a 688.Dv SA_SIGINFO 689handler contain a numeric code explaining the 690cause of the signal, usually one of the 691.Dv SI_... 692values from 693.In sys/signal.h 694or codes specific to a signal, i.e., one of the 695.Dv FPE_... 696values for 697.Dv SIGFPE . 698.Pp 699The 700.Fa scp 701argument to a 702.Bx Ns -style 703handler points to an instance of 704.Vt "struct sigcontext" . 705.Pp 706The 707.Fa uap 708argument to a 709.Tn POSIX 710.Dv SA_SIGINFO 711handler points to an instance of 712ucontext_t. 713.Sh ERRORS 714The 715.Fn sigaction 716system call 717will fail and no new signal handler will be installed if one 718of the following occurs: 719.Bl -tag -width Er 720.It Bq Er EINVAL 721The 722.Fa sig 723argument 724is not a valid signal number. 725.It Bq Er EINVAL 726An attempt is made to ignore or supply a handler for 727.Dv SIGKILL 728or 729.Dv SIGSTOP . 730.El 731.Sh SEE ALSO 732.Xr kill 1 , 733.Xr kill 2 , 734.Xr ptrace 2 , 735.Xr setitimer 2 , 736.Xr setrlimit 2 , 737.Xr sigaltstack 2 , 738.Xr sigpending 2 , 739.Xr sigprocmask 2 , 740.Xr sigsuspend 2 , 741.Xr wait 2 , 742.Xr fpsetmask 3 , 743.Xr setjmp 3 , 744.Xr siginfo 3 , 745.Xr siginterrupt 3 , 746.Xr sigsetops 3 , 747.Xr ucontext 3 , 748.Xr tty 4 749.Sh STANDARDS 750The 751.Fn sigaction 752system call is expected to conform to 753.St -p1003.1-90 . 754The 755.Dv SA_ONSTACK 756and 757.Dv SA_RESTART 758flags are Berkeley extensions, 759as are the signals, 760.Dv SIGTRAP , 761.Dv SIGEMT , 762.Dv SIGBUS , 763.Dv SIGSYS , 764.Dv SIGURG , 765.Dv SIGIO , 766.Dv SIGXCPU , 767.Dv SIGXFSZ , 768.Dv SIGVTALRM , 769.Dv SIGPROF , 770.Dv SIGWINCH , 771and 772.Dv SIGINFO . 773Those signals are available on most 774.Bx Ns \-derived 775systems. 776The 777.Dv SA_NODEFER 778and 779.Dv SA_RESETHAND 780flags are intended for backwards compatibility with other operating 781systems. 782The 783.Dv SA_NOCLDSTOP , 784and 785.Dv SA_NOCLDWAIT 786.\" and 787.\" SA_SIGINFO 788flags are featuring options commonly found in other operating systems. 789The flags are approved by 790.St -susv2 , 791along with the option to avoid zombie creation by ignoring 792.Dv SIGCHLD . 793