NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |VERSIONS |STANDARDS |HISTORY |NOTES |BUGS |EXAMPLES |SEE ALSO |COLOPHON | |
sigaction(2) System Calls Manualsigaction(2)sigaction, rt_sigaction - examine and change a signal action
Standard C library (libc,-lc)
#include <signal.h>int sigaction(intsignum,const struct sigaction *_Nullable restrictact,struct sigaction *_Nullable restrictoldact); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):sigaction(): _POSIX_C_SOURCEsiginfo_t: _POSIX_C_SOURCE >= 199309L
Thesigaction() system call is used to change the action taken by a process on receipt of a specific signal. (Seesignal(7) for an overview of signals.)signum specifies the signal and can be any valid signal exceptSIGKILLandSIGSTOP. Ifact is non-NULL, the new action for signalsignum is installed fromact. Ifoldact is non-NULL, the previous action is saved inoldact. Thesigaction structure is defined as something like: struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); }; On some architectures a union is involved: do not assign to bothsa_handler andsa_sigaction. Thesa_restorer field is not intended for application use. (POSIX does not specify asa_restorer field.) Some further details of the purpose of this field can be found insigreturn(2).sa_handler specifies the action to be associated withsignum and can be one of the following: •SIG_DFLfor the default action. •SIG_IGNto ignore this signal. • A pointer to a signal handling function. This function receives the signal number as its only argument. IfSA_SIGINFOis specified insa_flags, thensa_sigaction (instead ofsa_handler) specifies the signal-handling function forsignum. This function receives three arguments, as described below.sa_mask specifies a mask of signals which should be blocked (i.e., added to the signal mask of the thread in which the signal handler is invoked) during execution of the signal handler. In addition, the signal which triggered the handler will be blocked, unless theSA_NODEFERflag is used.sa_flags specifies a set of flags which modify the behavior of the signal. It is formed by the bitwise OR of zero or more of the following:SA_NOCLDSTOP Ifsignum isSIGCHLD, do not receive notification when child processes stop (i.e., when they receive one ofSIGSTOP,SIGTSTP,SIGTTIN, orSIGTTOU) or resume (i.e., they receiveSIGCONT) (seewait(2)). This flag is meaningful only when establishing a handler forSIGCHLD.SA_NOCLDWAIT(since Linux 2.6) Ifsignum isSIGCHLD, do not transform children into zombies when they terminate. See alsowaitpid(2). This flag is meaningful only when establishing a handler forSIGCHLD, or when setting that signal's disposition toSIG_DFL. If theSA_NOCLDWAITflag is set when establishing a handler forSIGCHLD, POSIX.1 leaves it unspecified whether aSIGCHLDsignal is generated when a child process terminates. On Linux, aSIGCHLDsignal is generated in this case; on some other implementations, it is not.SA_NODEFER Do not add the signal to the thread's signal mask while the handler is executing, unless the signal is specified inact.sa_mask. Consequently, a further instance of the signal may be delivered to the thread while it is executing the handler. This flag is meaningful only when establishing a signal handler.SA_NOMASKis an obsolete, nonstandard synonym for this flag.SA_ONSTACK Call the signal handler on an alternate signal stack provided bysigaltstack(2). If an alternate stack is not available, the default stack will be used. This flag is meaningful only when establishing a signal handler.SA_RESETHAND Restore the signal action to the default upon entry to the signal handler. This flag is meaningful only when establishing a signal handler.SA_ONESHOTis an obsolete, nonstandard synonym for this flag.SA_RESTART Provide behavior compatible with BSD signal semantics by making certain system calls restartable across signals. This flag is meaningful only when establishing a signal handler. Seesignal(7) for a discussion of system call restarting.SA_RESTORERNot intended for application use. This flag is used by C libraries to indicate that thesa_restorer field contains the address of a "signal trampoline". Seesigreturn(2) for more details.SA_SIGINFO(since Linux 2.2) The signal handler takes three arguments, not one. In this case,sa_sigaction should be set instead ofsa_handler. This flag is meaningful only when establishing a signal handler.SA_UNSUPPORTED(since Linux 5.11) Used to dynamically probe for flag bit support. If an attempt to register a handler succeeds with this flag set inact->sa_flags alongside other flags that are potentially unsupported by the kernel, and an immediately subsequentsigaction() call specifying the same signal number and with a non-NULLoldact argument yieldsSA_UNSUPPORTEDclear inoldact->sa_flags, thenoldact->sa_flags may be used as a bitmask describing which of the potentially unsupported flags are, in fact, supported. See the section "Dynamically probing for flag bit support" below for more details.SA_EXPOSE_TAGBITS(since Linux 5.11) Normally, when delivering a signal, an architecture- specific set of tag bits are cleared from thesi_addr field ofsiginfo_t. If this flag is set, an architecture- specific subset of the tag bits will be preserved insi_addr. Programs that need to be compatible with Linux versions older than 5.11 must useSA_UNSUPPORTEDto probe for support.The siginfo_t argument to a SA_SIGINFO handler When theSA_SIGINFOflag is specified inact.sa_flags, the signal handler address is passed via theact.sa_sigaction field. This handler takes three arguments, as follows: void handler(int sig, siginfo_t *info, void *ucontext) { ... } These three arguments are as followssig The number of the signal that caused invocation of the handler.info A pointer to asiginfo_t, which is a structure containing further information about the signal, as described below.ucontext This is a pointer to aucontext_t structure, cast tovoid *. The structure pointed to by this field contains signal context information that was saved on the user-space stack by the kernel; for details, seesigreturn(2). Further information about theucontext_t structure can be found ingetcontext(3) andsignal(7). Commonly, the handler function doesn't make any use of the third argument. Thesiginfo_t data type is a structure with the following fields: siginfo_t { int si_signo; /* Signal number */ int si_errno; /* An errno value */ int si_code; /* Signal code */ int si_trapno; /* Trap number that caused hardware-generated signal (unused on most architectures) */ pid_t si_pid; /* Sending process ID */ uid_t si_uid; /* Real user ID of sending process */ int si_status; /* Exit value or signal */ clock_t si_utime; /* User time consumed */ clock_t si_stime; /* System time consumed */ union sigval si_value; /* Signal value */ int si_int; /* POSIX.1b signal */ void *si_ptr; /* POSIX.1b signal */ int si_overrun; /* Timer overrun count; POSIX.1b timers */ int si_timerid; /* Timer ID; POSIX.1b timers */ void *si_addr; /* Memory location which caused fault */ long si_band; /* Band event (wasint in glibc 2.3.2 and earlier) */ int si_fd; /* File descriptor */ short si_addr_lsb; /* Least significant bit of address (since Linux 2.6.32) */ void *si_lower; /* Lower bound when address violation occurred (since Linux 3.19) */ void *si_upper; /* Upper bound when address violation occurred (since Linux 3.19) */ int si_pkey; /* Protection key on PTE that caused fault (since Linux 4.6) */ void *si_call_addr; /* Address of system call instruction (since Linux 3.5) */ int si_syscall; /* Number of attempted system call (since Linux 3.5) */ unsigned int si_arch; /* Architecture of attempted system call (since Linux 3.5) */ }si_signo,si_errno andsi_code are defined for all signals. (si_errno is generally unused on Linux.) The rest of the struct may be a union, so that one should read only the fields that are meaningful for the given signal: • Signals sent withkill(2) andsigqueue(3) fill insi_pid andsi_uid. In addition, signals sent withsigqueue(3) fill insi_int andsi_ptr with the values specified by the sender of the signal; seesigqueue(3) for more details. • Signals sent by POSIX.1b timers (since Linux 2.6) fill insi_overrun andsi_timerid. Thesi_timerid field is an internal ID used by the kernel to identify the timer; it is not the same as the timer ID returned bytimer_create(2). Thesi_overrun field is the timer overrun count; this is the same information as is obtained by a call totimer_getoverrun(2). These fields are nonstandard Linux extensions. • Signals sent for message queue notification (see the description ofSIGEV_SIGNALinmq_notify(3)) fill insi_int/si_ptr, with thesigev_value supplied tomq_notify(3);si_pid, with the process ID of the message sender; andsi_uid, with the real user ID of the message sender. •SIGCHLDfills insi_pid,si_uid,si_status,si_utime, andsi_stime, providing information about the child. Thesi_pid field is the process ID of the child;si_uid is the child's real user ID. Thesi_status field contains the exit status of the child (ifsi_code isCLD_EXITED), or the signal number that caused the process to change state. Thesi_utime andsi_stime contain the user and system CPU time used by the child process; these fields do not include the times used by waited-for children (unlikegetrusage(2) andtimes(2)). Up to Linux 2.6, and since Linux 2.6.27, these fields report CPU time in units ofsysconf(_SC_CLK_TCK). In Linux 2.6 kernels before Linux 2.6.27, a bug meant that these fields reported time in units of the (configurable) system jiffy (seetime(7)). •SIGILL,SIGFPE,SIGSEGV,SIGBUS, andSIGTRAPfill insi_addr with the address of the fault. On some architectures, these signals also fill in thesi_trapno field. Some suberrors ofSIGBUS, in particularBUS_MCEERR_AOandBUS_MCEERR_AR, also fill insi_addr_lsb. This field indicates the least significant bit of the reported address and therefore the extent of the corruption. For example, if a full page was corrupted,si_addr_lsb containslog2(sysconf(_SC_PAGESIZE)). WhenSIGTRAPis delivered in response to aptrace(2) event (PTRACE_EVENT_foo),si_addr is not populated, butsi_pid andsi_uid are populated with the respective process ID and user ID responsible for delivering the trap. In the case ofseccomp(2), the tracee will be shown as delivering the event.BUS_MCEERR_*andsi_addr_lsb are Linux-specific extensions. TheSEGV_BNDERRsuberror ofSIGSEGVpopulatessi_lower andsi_upper. TheSEGV_PKUERRsuberror ofSIGSEGVpopulatessi_pkey. •SIGIO/SIGPOLL(the two names are synonyms on Linux) fills insi_band andsi_fd. Thesi_band event is a bit mask containing the same values as are filled in therevents field bypoll(2). Thesi_fd field indicates the file descriptor for which the I/O event occurred; for further details, see the description ofF_SETSIGinfcntl(2). •SIGSYS, generated (since Linux 3.5) when a seccomp filter returnsSECCOMP_RET_TRAP, fills insi_call_addr,si_syscall,si_arch,si_errno, and other fields as described inseccomp(2).The si_code field Thesi_code field inside thesiginfo_t argument that is passed to aSA_SIGINFOsignal handler is a value (not a bit mask) indicating why this signal was sent. For aptrace(2) event,si_code will containSIGTRAPand have the ptrace event in the high byte: (SIGTRAP | PTRACE_EVENT_foo << 8). For a non-ptrace(2) event, the values that can appear insi_code are described in the remainder of this section. Since glibc 2.20, the definitions of most of these symbols are obtained from<signal.h> by defining feature test macros (before includingany header file) as follows: •_XOPEN_SOURCEwith the value 500 or greater; •_XOPEN_SOURCEand_XOPEN_SOURCE_EXTENDED; or •_POSIX_C_SOURCEwith the value 200809L or greater. For theTRAP_*constants, the symbol definitions are provided only in the first two cases. Before glibc 2.20, no feature test macros were required to obtain these symbols. For a regular signal, the following list shows the values which can be placed insi_code for any signal, along with the reason that the signal was generated.SI_USERkill(2).SI_KERNEL Sent by the kernel.SI_QUEUEsigqueue(3).SI_TIMER POSIX, orsetitimer(2) oralarm(2) timer expired.SI_MESGQ(since Linux 2.6.6) POSIX message queue state changed; seemq_notify(3).SI_ASYNCIO AIO completed.SI_SIGIO QueuedSIGIO(only up to Linux 2.2; from Linux 2.4 onwardSIGIO/SIGPOLLfills insi_code as described below).SI_TKILL(since Linux 2.4.19)tkill(2) ortgkill(2). The following values can be placed insi_code for aSIGILLsignal:ILL_ILLOPC Illegal opcode.ILL_ILLOPN Illegal operand.ILL_ILLADR Illegal addressing mode.ILL_ILLTRP Illegal trap.ILL_PRVOPC Privileged opcode.ILL_PRVREG Privileged register.ILL_COPROC Coprocessor error.ILL_BADSTK Internal stack error. The following values can be placed insi_code for aSIGFPEsignal:FPE_INTDIV Integer divide by zero.FPE_INTOVF Integer overflow.FPE_FLTDIV Floating-point divide by zero.FPE_FLTOVF Floating-point overflow.FPE_FLTUND Floating-point underflow.FPE_FLTRES Floating-point inexact result.FPE_FLTINV Floating-point invalid operation.FPE_FLTSUB Subscript out of range. The following values can be placed insi_code for aSIGSEGV signal:SEGV_MAPERR Address not mapped to object.SEGV_ACCERR Invalid permissions for mapped object.SEGV_BNDERR(since Linux 3.19) Failed address bound checks.SEGV_PKUERR(since Linux 4.6) Access was denied by memory protection keys. Seepkeys(7). The protection key which applied to this access is available viasi_pkey. The following values can be placed insi_code for aSIGBUSsignal:BUS_ADRALN Invalid address alignment.BUS_ADRERR Nonexistent physical address.BUS_OBJERR Object-specific hardware error.BUS_MCEERR_AR(since Linux 2.6.32) Hardware memory error consumed on a machine check; action required.BUS_MCEERR_AO(since Linux 2.6.32) Hardware memory error detected in process but not consumed; action optional. The following values can be placed insi_code for aSIGTRAP signal:TRAP_BRKPT Process breakpoint.TRAP_TRACE Process trace trap.TRAP_BRANCH(since Linux 2.4, IA64 only) Process taken branch trap.TRAP_HWBKPT(since Linux 2.4, IA64 only) Hardware breakpoint/watchpoint. The following values can be placed insi_code for aSIGCHLD signal:CLD_EXITED Child has exited.CLD_KILLED Child was killed.CLD_DUMPED Child terminated abnormally.CLD_TRAPPED Traced child has trapped.CLD_STOPPED Child has stopped.CLD_CONTINUED(since Linux 2.6.9) Stopped child has continued. The following values can be placed insi_code for aSIGIO/SIGPOLL signal:POLL_IN Data input available.POLL_OUT Output buffers available.POLL_MSG Input message available.POLL_ERR I/O error.POLL_PRI High priority input available.POLL_HUP Device disconnected. The following value can be placed insi_code for aSIGSYSsignal:SYS_SECCOMP(since Linux 3.5) Triggered by aseccomp(2) filter rule.Dynamically probing for flag bit support Thesigaction() call on Linux accepts unknown bits set inact->sa_flags without error. The behavior of the kernel starting with Linux 5.11 is that a secondsigaction() will clear unknown bits fromoldact->sa_flags. However, historically, a secondsigaction() call would typically leave those bits set inoldact->sa_flags. This means that support for new flags cannot be detected simply by testing for a flag insa_flags, and a program must test thatSA_UNSUPPORTEDhas been cleared before relying on the contents ofsa_flags. Since the behavior of the signal handler cannot be guaranteed unless the check passes, it is wise to either block the affected signal while registering the handler and performing the check in this case, or where this is not possible, for example if the signal is synchronous, to issue the secondsigaction() in the signal handler itself. In kernels that do not support a specific flag, the kernel's behavior is as if the flag was not set, even if the flag was set inact->sa_flags. The flagsSA_NOCLDSTOP,SA_NOCLDWAIT,SA_SIGINFO,SA_ONSTACK,SA_RESTART,SA_NODEFER,SA_RESETHAND, and, if defined by the architecture,SA_RESTORERmay not be reliably probed for using this mechanism, because they were introduced before Linux 5.11. However, in general, programs may assume that these flags are supported, since they have all been supported since Linux 2.6, which was released in the year 2003. See EXAMPLES below for a demonstration of the use ofSA_UNSUPPORTED.sigaction() returns 0 on success; on error, -1 is returned, anderrno is set to indicate the error.
EFAULTact oroldact points to memory which is not a valid part of the process address space.EINVALAn invalid signal was specified. This will also be generated if an attempt is made to change the action forSIGKILLorSIGSTOP, which cannot be caught or ignored.
C library/kernel differences The glibc wrapper function forsigaction() gives an error (EINVAL) on attempts to change the disposition of the two real-time signals used internally by the NPTL threading implementation. Seenptl(7) for details. On architectures where the signal trampoline resides in the C library, the glibc wrapper function forsigaction() places the address of the trampoline code in theact.sa_restorer field and sets theSA_RESTORERflag in theact.sa_flags field. Seesigreturn(2). The original Linux system call was namedsigaction(). However, with the addition of real-time signals in Linux 2.2, the fixed- size, 32-bitsigset_t type supported by that system call was no longer fit for purpose. Consequently, a new system call,rt_sigaction(), was added to support an enlargedsigset_t type. The new system call takes a fourth argument,size_t sigsetsize, which specifies the size in bytes of the signal sets inact.sa_mask andoldact.sa_mask. This argument is currently required to have the valuesizeof(sigset_t) (or the errorEINVAL results). The glibcsigaction() wrapper function hides these details from us, transparently callingrt_sigaction() when the kernel provides it.
POSIX.1-2008.
POSIX.1-2001, SVr4. POSIX.1-1990 disallowed setting the action forSIGCHLDtoSIG_IGN. POSIX.1-2001 and later allow this possibility, so that ignoringSIGCHLDcan be used to prevent the creation of zombies (seewait(2)). Nevertheless, the historical BSD and System V behaviors for ignoringSIGCHLDdiffer, so that the only completely portable method of ensuring that terminated children do not become zombies is to catch theSIGCHLDsignal and perform await(2) or similar. POSIX.1-1990 specified onlySA_NOCLDSTOP. POSIX.1-2001 addedSA_NOCLDWAIT,SA_NODEFER,SA_ONSTACK,SA_RESETHAND,SA_RESTART, andSA_SIGINFOas XSI extensions. POSIX.1-2008 movedSA_NODEFER,SA_RESETHAND,SA_RESTART, andSA_SIGINFOto the base specifications. Use of these latter values insa_flags may be less portable in applications intended for older UNIX implementations. TheSA_RESETHANDflag is compatible with the SVr4 flag of the same name. TheSA_NODEFERflag is compatible with the SVr4 flag of the same name under kernels 1.3.9 and later. On older kernels the Linux implementation allowed the receipt of any signal, not just the one we are installing (effectively overriding anysa_mask settings).
A child created viafork(2) inherits a copy of its parent's signal dispositions. During anexecve(2), the dispositions of handled signals are reset to the default; the dispositions of ignored signals are left unchanged. According to POSIX, the behavior of a process is undefined after it ignores aSIGFPE,SIGILL, orSIGSEGVsignal that was not generated bykill(2) orraise(3). Integer division by zero has undefined result. On some architectures it will generate aSIGFPE signal. (Also dividing the most negative integer by -1 may generateSIGFPE.) Ignoring this signal might lead to an endless loop.sigaction() can be called with a NULL second argument to query the current signal handler. It can also be used to check whether a given signal is valid for the current machine by calling it with NULL second and third arguments. It is not possible to blockSIGKILLorSIGSTOP(by specifying them insa_mask). Attempts to do so are silently ignored. Seesigsetops(3) for details on manipulating signal sets. Seesignal-safety(7) for a list of the async-signal-safe functions that can be safely called inside from inside a signal handler. POSIX only guaranteesSI_TIMERfor signals created bytimer_create(2). Implementations are free to also provide it for other types of timers. The Linux behaviour matches NetBSD.Undocumented Before the introduction ofSA_SIGINFO, it was also possible to get some additional information about the signal. This was done by providing ansa_handler signal handler with a second argument of typestruct sigcontext, which is the same structure as the one that is passed in theuc_mcontext field of theucontext structure that is passed (via a pointer) in the third argument of thesa_sigaction handler. See the relevant Linux kernel sources for details. This use is obsolete now.
When delivering a signal resulting from a hardware exception with aSA_SIGINFOhandler, the kernel does not always provide meaningful values for all of the fields of thesiginfo_t that are relevant for that signal. For example, when the x86int instruction is called with a forbidden argument (any number other than 3 or 128), aSIGSEGVsignal is delivered, but thesiginfo_t passed to the signal handler has all its fields besidessi_signo andsi_code set to zero, even if other fields should be set (as an example,si_addr should be non-zero for allSIGSEGVsignals). Up to and including Linux 2.6.13, specifyingSA_NODEFERinsa_flags prevents not only the delivered signal from being masked during execution of the handler, but also the signals specified insa_mask. This bug was fixed in Linux 2.6.14.
Seemprotect(2).Probing for flag support The following example program exits with statusEXIT_SUCCESSifSA_EXPOSE_TAGBITSis determined to be supported, andEXIT_FAILURE otherwise. #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> static void handler(int signo, siginfo_t *info, void *context) { struct sigaction oldact; if (sigaction(SIGSEGV, NULL, &oldact) == -1 || (oldact.sa_flags & SA_UNSUPPORTED) || !(oldact.sa_flags & SA_EXPOSE_TAGBITS)) { _exit(EXIT_FAILURE); } _exit(EXIT_SUCCESS); } int main(void) { struct sigaction act = { 0 }; act.sa_flags = SA_SIGINFO | SA_UNSUPPORTED | SA_EXPOSE_TAGBITS; act.sa_sigaction = &handler; if (sigaction(SIGSEGV, &act, NULL) == -1) { perror("sigaction"); exit(EXIT_FAILURE); } raise(SIGSEGV); }kill(1),kill(2),pause(2),pidfd_send_signal(2),restart_syscall(2),seccomp(2),sigaltstack(2),signal(2),signalfd(2),sigpending(2),sigprocmask(2),sigreturn(2),sigsuspend(2),wait(2),killpg(3),raise(3),siginterrupt(3),sigqueue(3),sigsetops(3),sigvec(3),core(5),signal(7)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17sigaction(2)Pages that refer to this page:env(1), kill(1), kill(1@@procps-ng), pgrep(1), alarm(2), clock_nanosleep(2), clone(2), F_GETSIG(2const), getitimer(2), pidfd_open(2), pidfd_send_signal(2), PR_MCE_KILL_SET(2const), PR_SET_PDEATHSIG(2const), PR_SET_SYSCALL_USER_DISPATCH(2const), ptrace(2), restart_syscall(2), rt_sigqueueinfo(2), seccomp(2), seccomp_unotify(2), semop(2), send(2), sigaltstack(2), signal(2), signalfd(2), sigpending(2), sigprocmask(2), sigreturn(2), sigsuspend(2), sigwaitinfo(2), syscalls(2), timer_getoverrun(2), wait(2), wait4(2), abort(3), bsd_signal(3), getcontext(3), makecontext(3), posix_spawn(3), profil(3), psignal(3), pthread_kill(3), pthread_sigmask(3), pthread_sigqueue(3), raise(3), seccomp_init(3), sigevent(3type), siginterrupt(3), sigpause(3), sigqueue(3), sigset(3), sigsetops(3), sigvec(3), sigwait(3), system(3), sysv_signal(3), core(5), proc_pid_timers(5), proc_sys_vm(5), fifo(7), inotify(7), nptl(7), pid_namespaces(7), pkeys(7), signal(7), signal-safety(7), socket(7), system_data_types(7), user_namespaces(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |