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.\" 30.Dd December 1, 2023 31.Dt SIGACTION 2 32.Os 33.Sh NAME 34.Nm sigaction 35.Nd software signal facilities 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In signal.h 40.Bd -literal 41struct sigaction { 42 void (*sa_handler)(int); 43 void (*sa_sigaction)(int, siginfo_t *, void *); 44 int sa_flags; /* see signal options below */ 45 sigset_t sa_mask; /* signal mask to apply */ 46}; 47.Ed 48.Pp 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-NULL, it specifies an action 147.Dv ( SIG_DFL , 148.Dv SIG_IGN , 149or a handler routine) and mask to be used when delivering the specified signal. 150If 151.Fa oact 152is non-NULL, the previous handling information for the signal 153is returned to the user. 154.Pp 155The above declaration of 156.Vt "struct sigaction" 157is not literal. 158It is provided only to list the accessible members. 159See 160.In sys/signal.h 161for the actual definition. 162In particular, the storage occupied by 163.Va sa_handler 164and 165.Va sa_sigaction 166overlaps, and it is nonsensical for an application to attempt to use both 167simultaneously. 168.Pp 169Once a signal handler is installed, it normally remains installed 170until another 171.Fn sigaction 172system call is made, or an 173.Xr execve 2 174is performed. 175A signal-specific default action may be reset by 176setting 177.Va sa_handler 178to 179.Dv SIG_DFL . 180The defaults are process termination, possibly with core dump; 181no action; stopping the process; or continuing the process. 182See the signal list below for each signal's default action. 183If 184.Va sa_handler 185is 186.Dv SIG_DFL , 187the default action for the signal is to discard the signal, 188and if a signal is pending, 189the pending signal is discarded even if the signal is masked. 190If 191.Va sa_handler 192is set to 193.Dv SIG_IGN 194current and pending instances 195of the signal are ignored and discarded. 196.Pp 197Options may be specified by setting 198.Va 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 . 224The same effect of avoiding zombie creation can also be achieved by setting 225.Va sa_handler 226for 227.Dv SIGCHLD 228to 229.Dv SIG_IGN . 230.It Dv SA_ONSTACK 231If this bit is set, the system will deliver the signal to the process 232on a 233.Em "signal stack" , 234specified by each thread with 235.Xr sigaltstack 2 . 236.It Dv SA_NODEFER 237If this bit is set, further occurrences of the delivered signal are 238not masked during the execution of the handler. 239.It Dv SA_RESETHAND 240If this bit is set, the handler is reset back to 241.Dv SIG_DFL 242at the moment the signal is delivered. 243.It Dv SA_RESTART 244See paragraph below. 245.It Dv SA_SIGINFO 246If this bit is set, the handler function is assumed to be pointed to by the 247.Va sa_sigaction 248member of 249.Vt "struct sigaction" 250and should match the prototype shown above or as below in 251.Sx EXAMPLES . 252This bit should not be set when assigning 253.Dv SIG_DFL 254or 255.Dv SIG_IGN . 256.El 257.Pp 258If a signal is caught during the system calls listed below, 259the call may be forced to terminate 260with the error 261.Er EINTR , 262the call may return with a data transfer shorter than requested, 263or the call may be restarted. 264Restart of pending calls is requested 265by setting the 266.Dv SA_RESTART 267bit in 268.Va sa_flags . 269The affected system calls include 270.Xr open 2 , 271.Xr read 2 , 272.Xr write 2 , 273.Xr sendto 2 , 274.Xr recvfrom 2 , 275.Xr sendmsg 2 276and 277.Xr recvmsg 2 278on a communications channel or a slow device (such as a terminal, 279but not a regular file) 280and during a 281.Xr wait 2 282or 283.Xr ioctl 2 . 284However, calls that have already committed are not restarted, 285but instead return a partial success (for example, a short read count). 286.Pp 287After a 288.Xr pthread_create 3 289the signal mask is inherited by the new thread and 290the set of pending signals and the signal stack for the new thread are empty. 291.Pp 292After a 293.Xr fork 2 294or 295.Xr vfork 2 296all signals, the signal mask, the signal stack, 297and the restart/interrupt flags are inherited by the child. 298.Pp 299The 300.Xr execve 2 301system call reinstates the default 302action for all signals which were caught and 303resets all signals to be caught on the user stack. 304Ignored signals remain ignored; 305the signal mask remains the same; 306signals that restart pending system calls continue to do so. 307.Pp 308The following is a list of all signals 309with names as in the include file 310.In signal.h : 311.Bl -column SIGVTALARMXX "create core imagexxx" 312.It Sy NAME Ta Sy Default Action Ta Sy Description 313.It Dv SIGHUP Ta terminate process Ta terminal line hangup 314.It Dv SIGINT Ta terminate process Ta interrupt program 315.It Dv SIGQUIT Ta create core image Ta quit program 316.It Dv SIGILL Ta create core image Ta illegal instruction 317.It Dv SIGTRAP Ta create core image Ta trace trap 318.It Dv SIGABRT Ta create core image Ta Xr abort 3 call (formerly Dv SIGIOT ) 319.It Dv SIGEMT Ta create core image Ta emulate instruction executed 320.It Dv SIGFPE Ta create core image Ta floating-point exception 321.It Dv SIGKILL Ta terminate process Ta kill program 322.It Dv SIGBUS Ta create core image Ta bus error 323.It Dv SIGSEGV Ta create core image Ta segmentation violation 324.It Dv SIGSYS Ta create core image Ta non-existent system call invoked 325.It Dv SIGPIPE Ta terminate process Ta write on a pipe with no reader 326.It Dv SIGALRM Ta terminate process Ta real-time timer expired 327.It Dv SIGTERM Ta terminate process Ta software termination signal 328.It Dv SIGURG Ta discard signal Ta urgent condition present on socket 329.It Dv SIGSTOP Ta stop process Ta stop (cannot be caught or ignored) 330.It Dv SIGTSTP Ta stop process Ta stop signal generated from keyboard 331.It Dv SIGCONT Ta discard signal Ta continue after stop 332.It Dv SIGCHLD Ta discard signal Ta child status has changed 333.It Dv SIGTTIN Ta stop process Ta background read attempted from control terminal 334.It Dv SIGTTOU Ta stop process Ta background write attempted to control terminal 335.It Dv SIGIO Ta discard signal Ta I/O is possible on a descriptor (see Xr fcntl 2 ) 336.It Dv SIGXCPU Ta terminate process Ta cpu time limit exceeded (see Xr setrlimit 2 ) 337.It Dv SIGXFSZ Ta terminate process Ta file size limit exceeded (see Xr setrlimit 2 ) 338.It Dv SIGVTALRM Ta terminate process Ta virtual time alarm (see Xr setitimer 2 ) 339.It Dv SIGPROF Ta terminate process Ta profiling timer alarm (see Xr setitimer 2 ) 340.It Dv SIGWINCH Ta discard signal Ta window size change 341.It Dv SIGINFO Ta discard signal Ta status request from keyboard 342.It Dv SIGUSR1 Ta terminate process Ta user defined signal 1 343.It Dv SIGUSR2 Ta terminate process Ta user defined signal 2 344.El 345.Sh NOTE 346The 347.Va 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 accept , 369.Fn access , 370.Fn alarm , 371.Fn bind , 372.Fn cfgetispeed , 373.Fn cfgetospeed , 374.Fn cfsetispeed , 375.Fn cfsetospeed , 376.Fn chdir , 377.Fn chmod , 378.Fn chown , 379.Fn close , 380.Fn connect , 381.Fn creat , 382.Fn dup , 383.Fn dup2 , 384.Fn execl , 385.Fn execle , 386.Fn execv , 387.Fn execve , 388.Fn faccessat , 389.Fn fchdir , 390.Fn fchmod , 391.Fn fchmodat , 392.Fn fchown , 393.Fn fchownat , 394.Fn fcntl , 395.Fn _Fork , 396.Fn fstat , 397.Fn fstatat , 398.Fn fsync , 399.Fn ftruncate , 400.Fn getegid , 401.Fn geteuid , 402.Fn getgid , 403.Fn getgroups , 404.Fn getpeername , 405.Fn getpgrp , 406.Fn getpid , 407.Fn getppid , 408.Fn getsockname , 409.Fn getsockopt , 410.Fn getuid , 411.Fn kill , 412.Fn link , 413.Fn linkat , 414.Fn listen , 415.Fn lseek , 416.Fn lstat , 417.Fn mkdir , 418.Fn mkdirat , 419.Fn mkfifo , 420.Fn mkfifoat , 421.Fn mknod , 422.Fn mknodat , 423.Fn open , 424.Fn openat , 425.Fn pause , 426.Fn pipe , 427.Fn poll , 428.Fn pselect , 429.Fn pthread_sigmask , 430.Fn raise , 431.Fn read , 432.Fn readlink , 433.Fn readlinkat , 434.Fn recv , 435.Fn recvfrom , 436.Fn recvmsg , 437.Fn rename , 438.Fn renameat , 439.Fn rmdir , 440.Fn select , 441.Fn send , 442.Fn sendmsg , 443.Fn sendto , 444.Fn setgid , 445.Fn setpgid , 446.Fn setsid , 447.Fn setsockopt , 448.Fn setuid , 449.Fn shutdown , 450.Fn sigaction , 451.Fn sigaddset , 452.Fn sigdelset , 453.Fn sigemptyset , 454.Fn sigfillset , 455.Fn sigismember , 456.Fn signal , 457.Fn sigpending , 458.Fn sigprocmask , 459.Fn sigsuspend , 460.Fn sleep , 461.Fn sockatmark , 462.Fn socket , 463.Fn socketpair , 464.Fn stat , 465.Fn symlink , 466.Fn symlinkat , 467.Fn tcdrain , 468.Fn tcflow , 469.Fn tcflush , 470.Fn tcgetattr , 471.Fn tcgetpgrp , 472.Fn tcsendbreak , 473.Fn tcsetattr , 474.Fn tcsetpgrp , 475.Fn time , 476.Fn times , 477.Fn umask , 478.Fn uname , 479.Fn unlink , 480.Fn unlinkat , 481.Fn utime , 482.Fn wait , 483.Fn waitpid , 484.Fn write . 485.Pp 486X/Open Systems Interfaces: 487.Pp 488.Fn sigpause , 489.Fn sigset , 490.Fn utimes . 491.Pp 492Realtime Interfaces: 493.Pp 494.Fn aio_error , 495.Fn clock_gettime , 496.Fn timer_getoverrun , 497.Fn aio_return , 498.Fn fdatasync , 499.Fn sigqueue , 500.Fn timer_gettime , 501.Fn aio_suspend , 502.Fn sem_post , 503.Fn timer_settime . 504.Pp 505Base Interfaces not specified as async-signal safe by 506.Tn POSIX : 507.Pp 508.Fn fpathconf , 509.Fn pathconf , 510.Fn sysconf . 511.Pp 512Base Interfaces not specified as async-signal safe by 513.Tn POSIX , 514but planned to be: 515.Pp 516.Fn ffs , 517.Fn htonl , 518.Fn htons , 519.Fn memccpy , 520.Fn memchr , 521.Fn memcmp , 522.Fn memcpy , 523.Fn memmove , 524.Fn memset , 525.Fn ntohl , 526.Fn ntohs , 527.Fn stpcpy , 528.Fn stpncpy , 529.Fn strcat , 530.Fn strchr , 531.Fn strcmp , 532.Fn strcpy , 533.Fn strcspn , 534.Fn strlen , 535.Fn strncat , 536.Fn strncmp , 537.Fn strncpy , 538.Fn strnlen , 539.Fn strpbrk , 540.Fn strrchr , 541.Fn strspn , 542.Fn strstr , 543.Fn strtok_r , 544.Fn wcpcpy , 545.Fn wcpncpy , 546.Fn wcscat , 547.Fn wcschr , 548.Fn wcscmp , 549.Fn wcscpy , 550.Fn wcscspn , 551.Fn wcslen , 552.Fn wcsncat , 553.Fn wcsncmp , 554.Fn wcsncpy , 555.Fn wcsnlen , 556.Fn wcspbrk , 557.Fn wcsrchr , 558.Fn wcsspn , 559.Fn wcsstr , 560.Fn wcstok , 561.Fn wmemchr , 562.Fn wmemcmp , 563.Fn wmemcpy , 564.Fn wmemmove , 565.Fn wmemset . 566.Pp 567Extension Interfaces: 568.Pp 569.Fn accept4 , 570.Fn bindat , 571.Fn close_range , 572.Fn closefrom , 573.Fn connectat , 574.Fn eaccess , 575.Fn ffsl , 576.Fn ffsll , 577.Fn flock , 578.Fn fls , 579.Fn flsl , 580.Fn flsll , 581.Fn futimesat , 582.Fn pipe2 , 583.Fn strlcat . 584.Fn strlcpy , 585.Fn strsep . 586.Pp 587In addition, reading or writing 588.Va errno 589is async-signal safe. 590.Pp 591All functions not in the above lists are considered to be unsafe 592with respect to signals. 593That is to say, the behaviour of such 594functions is undefined when they are called from a signal handler 595that interrupted an unsafe function. 596In general though, signal handlers should do little more than set a 597flag; most other actions are not safe. 598.Pp 599Also, it is good practice to make a copy of the global variable 600.Va errno 601and restore it before returning from the signal handler. 602This protects against the side effect of 603.Va errno 604being set by functions called from inside the signal handler. 605.Sh RETURN VALUES 606.Rv -std sigaction 607.Sh EXAMPLES 608There are three possible prototypes the handler may match: 609.Bl -tag -offset indent -width short 610.It Tn ANSI C : 611.Ft void 612.Fn handler int ; 613.It Traditional BSD style: 614.Ft void 615.Fn handler int "int code" "struct sigcontext *scp" ; 616.It Tn POSIX Dv SA_SIGINFO : 617.Ft void 618.Fn handler int "siginfo_t *info" "ucontext_t *uap" ; 619.El 620.Pp 621The handler function should match the 622.Dv SA_SIGINFO 623prototype if the 624.Dv SA_SIGINFO 625bit is set in 626.Va sa_flags . 627It then should be pointed to by the 628.Va sa_sigaction 629member of 630.Vt "struct sigaction" . 631Note that you should not assign 632.Dv SIG_DFL 633or 634.Dv SIG_IGN 635this way. 636.Pp 637If the 638.Dv SA_SIGINFO 639flag is not set, the handler function should match 640either the 641.Tn ANSI C 642or traditional 643.Bx 644prototype and be pointed to by 645the 646.Va sa_handler 647member of 648.Vt "struct sigaction" . 649In practice, 650.Fx 651always sends the three arguments of the latter and since the 652.Tn ANSI C 653prototype is a subset, both will work. 654The 655.Va sa_handler 656member declaration in 657.Fx 658include files is that of 659.Tn ANSI C 660(as required by 661.Tn POSIX ) , 662so a function pointer of a 663.Bx Ns -style 664function needs to be casted to 665compile without warning. 666The traditional 667.Bx 668style is not portable and since its capabilities 669are a full subset of a 670.Dv SA_SIGINFO 671handler, 672its use is deprecated. 673.Pp 674The 675.Fa sig 676argument is the signal number, one of the 677.Dv SIG... 678values from 679.In signal.h . 680.Pp 681The 682.Fa code 683argument of the 684.Bx Ns -style 685handler and the 686.Va si_code 687member of the 688.Fa info 689argument to a 690.Dv SA_SIGINFO 691handler contain a numeric code explaining the 692cause of the signal, usually one of the 693.Dv SI_... 694values from 695.In sys/signal.h 696or codes specific to a signal, i.e., one of the 697.Dv FPE_... 698values for 699.Dv SIGFPE . 700.Pp 701The 702.Fa scp 703argument to a 704.Bx Ns -style 705handler points to an instance of 706.Vt "struct sigcontext" . 707.Pp 708The 709.Fa uap 710argument to a 711.Tn POSIX 712.Dv SA_SIGINFO 713handler points to an instance of 714ucontext_t. 715.Sh ERRORS 716The 717.Fn sigaction 718system call 719will fail and no new signal handler will be installed if one 720of the following occurs: 721.Bl -tag -width Er 722.It Bq Er EINVAL 723The 724.Fa sig 725argument 726is not a valid signal number. 727.It Bq Er EINVAL 728An attempt is made to ignore or supply a handler for 729.Dv SIGKILL 730or 731.Dv SIGSTOP . 732.El 733.Sh SEE ALSO 734.Xr kill 1 , 735.Xr kill 2 , 736.Xr ptrace 2 , 737.Xr setitimer 2 , 738.Xr setrlimit 2 , 739.Xr sigaltstack 2 , 740.Xr sigpending 2 , 741.Xr sigprocmask 2 , 742.Xr sigsuspend 2 , 743.Xr wait 2 , 744.Xr fpsetmask 3 , 745.Xr setjmp 3 , 746.Xr siginfo 3 , 747.Xr siginterrupt 3 , 748.Xr sigsetops 3 , 749.Xr ucontext 3 , 750.Xr tty 4 751.Sh STANDARDS 752The 753.Fn sigaction 754system call is expected to conform to 755.St -p1003.1-90 . 756The 757.Dv SA_ONSTACK 758and 759.Dv SA_RESTART 760flags are Berkeley extensions, 761as are the signals, 762.Dv SIGTRAP , 763.Dv SIGEMT , 764.Dv SIGBUS , 765.Dv SIGSYS , 766.Dv SIGURG , 767.Dv SIGIO , 768.Dv SIGXCPU , 769.Dv SIGXFSZ , 770.Dv SIGVTALRM , 771.Dv SIGPROF , 772.Dv SIGWINCH , 773and 774.Dv SIGINFO . 775Those signals are available on most 776.Bx Ns \-derived 777systems. 778The 779.Dv SA_NODEFER 780and 781.Dv SA_RESETHAND 782flags are intended for backwards compatibility with other operating 783systems. 784The 785.Dv SA_NOCLDSTOP , 786and 787.Dv SA_NOCLDWAIT 788.\" and 789.\" SA_SIGINFO 790flags are featuring options commonly found in other operating systems. 791The flags are approved by 792.St -susv2 , 793along with the option to avoid zombie creation by ignoring 794.Dv SIGCHLD . 795