Manpage of SIGACTION


Section: Linux Programmer's Manual (2)
Updated: 2017-09-15


sigaction, rt_sigaction - examine and change a signal action  


#include <signal.h>int sigaction(int signum, const struct sigaction *act,              struct sigaction *oldact);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

sigaction(): _POSIX_C_SOURCE

siginfo_t: _POSIX_C_SOURCE >= 199309L  


The sigaction() system call is used to change the action taken by a process on receipt of a specific signal. (See signal(7) for an overview of signals.)

signumspecifies the signal and can be any valid signal except SIGKILLand SIGSTOP.

If actis non-NULL, the new action for signal signumis installed from act. If oldactis non-NULL, the previous action is saved in oldact.

The sigactionstructure 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 both sa_handlerand sa_sigaction.

The sa_restorerfield is not intended for application use. (POSIX does not specify a sa_restorerfield.) Some further details of purpose of this field can be found in sigreturn(2).

sa_handlerspecifies the action to be associated with signumand may be SIG_DFLfor the default action, SIG_IGNto ignore this signal, or a pointer to a signal handling function. This function receives the signal number as its only argument.

If SA_SIGINFOis specified in sa_flags, then sa_sigaction(instead of sa_handler) specifies the signal-handling function for signum. This function receives the signal number as its first argument, a pointer to a siginfo_tas its second argument and a pointer to a ucontext_t(cast to void *) as its third argument. (Commonly, the handler function doesn't make any use of the third argument. See getcontext(3) for further information about ucontext_t.)

sa_maskspecifies 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 the SA_NODEFERflag is used.

sa_flagsspecifies 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:

If signumis SIGCHLD, do not receive notification when child processes stop (i.e., when they receive one of SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU) or resume (i.e., they receive SIGCONT) (see wait(2)). This flag is meaningful only when establishing a handler for SIGCHLD.
SA_NOCLDWAIT (since Linux 2.6)
If signumis SIGCHLD, do not transform children into zombies when they terminate. See also waitpid(2). This flag is meaningful only when establishing a handler for SIGCHLD, or when setting that signal's disposition to SIG_DFL.
If the SA_NOCLDWAITflag is set when establishing a handler for SIGCHLD, POSIX.1 leaves it unspecified whether a SIGCHLDsignal is generated when a child process terminates. On Linux, a SIGCHLDsignal is generated in this case; on some other implementations, it is not.
Do not prevent the signal from being received from within its own signal handler. This flag is meaningful only when establishing a signal handler. SA_NOMASKis an obsolete, nonstandard synonym for this flag.
Call the signal handler on an alternate signal stack provided by sigaltstack(2). If an alternate stack is not available, the default stack will be used. This flag is meaningful only when establishing a signal handler.
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.
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. See signal(7) for a discussion of system call restarting.
Not intended for application use. This flag is used by C libraries to indicate that the sa_restorerfield contains the address of a "signal trampoline". See sigreturn(2) for more details.
SA_SIGINFO (since Linux 2.2)
The signal handler takes three arguments, not one. In this case, sa_sigactionshould be set instead of sa_handler. This flag is meaningful only when establishing a signal handler.

The siginfo_t argument to a SA_SIGINFO handler

When the SA_SIGINFOflag is specified in act.sa_flags, the signal handler address is passed via the act.sa_sigactionfield. This handler takes three arguments, as follows:

void handler(int sig, siginfo_t *info, void *ucontext) {
    ... }

The siginfo_tdata 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 */
    sigval_t 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 (was int 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 and si_codeare defined for all signals. (si_errnois 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 with kill(2) and sigqueue(3) fill in si_pid and si_uid. In addition, signals sent with sigqueue(3) fill in si_int and si_ptrwith the values specified by the sender of the signal; see sigqueue(3) for more details.
Signals sent by POSIX.1b timers (since Linux 2.6) fill in si_overrunand si_timerid. The si_timeridfield is an internal ID used by the kernel to identify the timer; it is not the same as the timer ID returned by timer_create(2). The si_overrunfield is the timer overrun count; this is the same information as is obtained by a call to timer_getoverrun(2). These fields are nonstandard Linux extensions.
Signals sent for message queue notification (see the description of SIGEV_SIGNALin mq_notify(3)) fill in si_int/si_ptr, with the sigev_valuesupplied to mq_notify(3); si_pid, with the process ID of the message sender; and si_uid, with the real user ID of the message sender.
SIGCHLDfills in si_pid, si_uid, si_status, si_utime, and si_stime, providing information about the child. The si_pidfield is the process ID of the child; si_uidis the child's real user ID. The si_statusfield contains the exit status of the child (if si_codeis CLD_EXITED), or the signal number that caused the process to change state. The si_utimeand si_stimecontain the user and system CPU time used by the child process; these fields do not include the times used by waited-for children (unlike getrusage(2) and times(2)). In kernels up to 2.6, and since 2.6.27, these fields report CPU time in units of sysconf(_SC_CLK_TCK). In 2.6 kernels before 2.6.27, a bug meant that these fields reported time in units of the (configurable) system jiffy (see time(7)).
SIGILL, SIGFPE, SIGSEGV, SIGBUS, and SIGTRAPfill in si_addrwith the address of the fault. On some architectures, these signals also fill in the si_trapnofield.
Some suberrors of SIGBUS, in particular BUS_MCEERR_AOand BUS_MCEERR_AR, also fill in si_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_lsbcontains log2(sysconf(_SC_PAGESIZE)). When SIGTRAPis delivered in response to a ptrace(2) event (PTRACE_EVENT_foo), si_addris not populated, but si_pidand si_uidare populated with the respective process ID and user ID responsible for delivering the trap. In the case of seccomp(2), the tracee will be shown as delivering the event. BUS_MCEERR_*and si_addr_lsbare Linux-specific extensions.
The SEGV_BNDERRsuberror of SIGSEGVpopulates si_lowerand si_upper.
The SEGV_PKUERRsuberror of SIGSEGVpopulates si_pkey.
SIGIO/SIGPOLL(the two names are synonyms on Linux) fills in si_band and si_fd. The si_bandevent is a bit mask containing the same values as are filled in the reventsfield by poll(2). The si_fdfield indicates the file descriptor for which the I/O event occurred; for further details, see the description of F_SETSIGin fcntl(2).
SIGSYS, generated (since Linux 3.5) when a seccomp filter returns SECCOMP_RET_TRAP, fills in si_call_addr, si_syscall, si_arch, si_errno, and other fields as described in seccomp(2).

The si_code field

The si_codefield inside the siginfo_targument that is passed to a SA_SIGINFOsignal handler is a value (not a bit mask) indicating why this signal was sent. For a ptrace(2) event, si_codewill contain SIGTRAPand have the ptrace event in the high byte:


For a non-ptrace(2) event, the values that can appear in si_codeare 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 including anyheader file) as follows:

_XOPEN_SOURCEwith the value 500 or greater;
_POSIX_C_SOURCEwith the value 200809L or greater.

For the TRAP_*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 in si_codefor any signal, along with the reason that the signal was generated.

Sent by the kernel.
POSIX timer expired.
SI_MESGQ (since Linux 2.6.6)
POSIX message queue state changed; see mq_notify(3).
AIO completed.
Queued SIGIO(only in kernels up to Linux 2.2; from Linux 2.4 onward SIGIO/SIGPOLLfills in si_codeas described below).
SI_TKILL (since Linux 2.4.19)
tkill(2) or tgkill(2).

The following values can be placed in si_codefor a SIGILLsignal:

Illegal opcode.
Illegal operand.
Illegal addressing mode.
Illegal trap.
Privileged opcode.
Privileged register.
Coprocessor error.
Internal stack error.

The following values can be placed in si_codefor a SIGFPEsignal:

Integer divide by zero.
Integer overflow.
Floating-point divide by zero.
Floating-point overflow.
Floating-point underflow.
Floating-point inexact result.
Floating-point invalid operation.
Subscript out of range.

The following values can be placed in si_codefor a SIGSEGVsignal:

Address not mapped to object.
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. See pkeys(7). The protection key which applied to this access is available via si_pkey.

The following values can be placed in si_codefor a SIGBUSsignal:

Invalid address alignment.
Nonexistent physical address.
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 in si_codefor a SIGTRAPsignal:

Process breakpoint.
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 in si_codefor a SIGCHLDsignal:

Child has exited.
Child was killed.
Child terminated abnormally.
Traced child has trapped.
Child has stopped.
CLD_CONTINUED (since Linux 2.6.9)
Stopped child has continued.

The following values can be placed in si_codefor a SIGIO/SIGPOLLsignal:

Data input available.
Output buffers available.
Input message available.
I/O error.
High priority input available.
Device disconnected.

The following value can be placed in si_codefor a SIGSYSsignal:

SYS_SECCOMP (since Linux 3.5)
Triggered by a seccomp(2) filter rule.


sigaction() returns 0 on success; on error, -1 is returned, and errnois set to indicate the error.  


act or oldactpoints to memory which is not a valid part of the process address space.
An invalid signal was specified. This will also be generated if an attempt is made to change the action for SIGKILL or SIGSTOP, which cannot be caught or ignored.


POSIX.1-2001, POSIX.1-2008, SVr4.  


A child created via fork(2) inherits a copy of its parent's signal dispositions. During an execve(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 a SIGFPE, SIGILL, or SIGSEGVsignal that was not generated by kill(2) or raise(3). Integer division by zero has undefined result. On some architectures it will generate a SIGFPEsignal. (Also dividing the most negative integer by -1 may generate SIGFPE.) Ignoring this signal might lead to an endless loop.

POSIX.1-1990 disallowed setting the action for SIGCHLDto SIG_IGN. POSIX.1-2001 and later allow this possibility, so that ignoring SIGCHLDcan be used to prevent the creation of zombies (see wait(2)). Nevertheless, the historical BSD and System V behaviors for ignoring SIGCHLDdiffer, so that the only completely portable method of ensuring that terminated children do not become zombies is to catch the SIGCHLDsignal and perform a wait(2) or similar.

POSIX.1-1990 specified only SA_NOCLDSTOP. POSIX.1-2001 added SA_NOCLDSTOP, SA_NOCLDWAIT, SA_NODEFER, SA_ONSTACK, SA_RESETHAND, SA_RESTART, and SA_SIGINFO. Use of these latter values in sa_flagsmay be less portable in applications intended for older UNIX implementations.

The SA_RESETHANDflag is compatible with the SVr4 flag of the same name.

The SA_NODEFERflag is compatible with the SVr4 flag of the same name under kernels 1.3.9 and newer. On older kernels the Linux implementation allowed the receipt of any signal, not just the one we are installing (effectively overriding any sa_masksettings).

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 block SIGKILL or SIGSTOP(by specifying them in sa_mask). Attempts to do so are silently ignored.

See sigsetops(3) for details on manipulating signal sets.

See signal-safety(7) for a list of the async-signal-safe functions that can be safely called inside from inside a signal handler.  

C library/kernel differences

The glibc wrapper function for sigaction() gives an error (EINVAL) on attempts to change the disposition of the two real-time signals used internally by the NPTL threading implementation. See nptl(7) for details.

The original Linux system call was named sigaction(). However, with the addition of real-time signals in Linux 2.2, the fixed-size, 32-bit sigset_ttype supported by that system call was no longer fit for purpose. Consequently, a new system call, rt_sigaction(), was added to support an enlarged sigset_ttype. The new system call takes a fourth argument, size_t sigsetsize, which specifies the size in bytes of the signal sets in act.sa_maskand oldact.sa_mask. This argument is currently required to have the value sizeof(sigset_t)(or the error EINVALresults). The glibc sigaction() wrapper function hides these details from us, transparently calling rt_sigaction() when the kernel provides it.  


Before the introduction of SA_SIGINFO, it was also possible to get some additional information, namely by using a sa_handlerwith second argument of type struct sigcontext.See the relevant Linux kernel sources for details. This use is obsolete now.  


In kernels up to and including 2.6.13, specifying SA_NODEFERin sa_flagsprevents not only the delivered signal from being masked during execution of the handler, but also the signals specified in sa_mask. This bug was fixed in kernel 2.6.14.  


See mprotect(2).  


kill(1), kill(2), pause(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)



The siginfo_t argument to a SA_SIGINFO handler
The si_code field
C library/kernel differences

This document was created by man2html, using the manual pages.
Time: 19:53:41 GMT, October 26, 2017 Click Here!