SIGACTION(3P)
PROLOG π
okay so bestie, welcome to sigaction(3P) β yes, from the POSIX Programmer's Manual π. This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. fr fr, that's the whole prolog. π«Ά
NAME πΈ
sigaction β examine and change a signal action. that's the vibe. π
SYNOPSIS β¨
#include <signal.h>
int sigaction(int sig, const struct sigaction *restrict act,
struct sigaction *restrict oact);
DESCRIPTION π¦
The sigaction() function allows the calling process to examine and/or specify the action to be associated with a specific signal. The argument sig specifies the signal; acceptable values are defined in <signal.h>. iykyk. β¨
The structure sigaction, used to describe an action to be taken, is defined in the <signal.h> header to include at least the following members:
void(*) (int)βsa_handler: Pointer to a signal-catching function or one of the macrosSIG_IGNorSIG_DFL.sigset_tβsa_mask: Additional set of signals to be blocked during execution of signal-catching function.intβsa_flags: Special flags to affect behavior of signal.void(*) (int, siginfo_t *, void *)βsa_sigaction: Pointer to a signal-catching function.
The storage occupied by sa_handler and sa_sigaction may overlap, and a conforming application shall not use both simultaneously. not me warning you twice. π
If the argument act is not a null pointer, it points to a structure specifying the action to be associated with the specified signal. If the argument oact is not a null pointer, the action previously associated with the signal is stored in the location pointed to by the argument oact. If the argument act is a null pointer, signal handling is unchanged; thus, the call can be used to enquire about the current handling of a given signal. The SIGKILL and SIGSTOP signals shall not be added to the signal mask using this mechanism; this restriction shall be enforced by the system without causing an error to be indicated. periodt. π₯
If the SA_SIGINFO flag (see below) is cleared in the sa_flags field of the sigaction structure, the sa_handler field identifies the action to be associated with the specified signal. If the SA_SIGINFO flag is set in the sa_flags field, the sa_sigaction field specifies a signal-catching function. got it? π
The sa_flags field can be used to modify the behavior of the specified signal.
The following flags, defined in the <signal.h> header, can be set in sa_flags:
SA_NOCLDSTOPβ Do not generateSIGCHLDwhen children stop or stopped children continue.- If
sigisSIGCHLDand theSA_NOCLDSTOPflag is not set insa_flags, and the implementation supports theSIGCHLDsignal, then aSIGCHLDsignal shall be generated for the calling process whenever any of its child processes stop and aSIGCHLDsignal may be generated for the calling process whenever any of its stopped child processes are continued. IfsigisSIGCHLDand theSA_NOCLDSTOPflag is set insa_flags, then the implementation shall not generate aSIGCHLDsignal in this way.
- If
SA_ONSTACKβ If set and an alternate signal stack has been declared withsigaltstack(), the signal shall be delivered to the calling process on that stack. Otherwise, the signal shall be delivered on the current stack.SA_RESETHANDβ If set, the disposition of the signal shall be reset toSIG_DFLand theSA_SIGINFOflag shall be cleared on entry to the signal handler.
Note:SIGILLandSIGTRAPcannot be automatically reset when delivered; the system silently enforces this restriction.
- Otherwise, the disposition of the signal shall not be modified on entry to the signal handler.
- In addition, if this flag is set,
sigaction()may behave as if theSA_NODEFERflag were also set.
SA_RESTARTβ This flag affects the behavior of interruptible functions; that is, those specified to fail witherrnoset to[EINTR]. If set, and a function specified as interruptible is interrupted by this signal, the function shall restart and shall not fail with[EINTR]unless otherwise specified. If an interruptible function which uses a timeout is restarted, the duration of the timeout following the restart is set to an unspecified value that does not exceed the original timeout value. If the flag is not set, interruptible functions interrupted by this signal shall fail witherrnoset to[EINTR].SA_SIGINFOβ If cleared and the signal is caught, the signal-catching function shall be entered as:
void func(int signo);
where signo is the only argument to the signal-catching function. In this case, the application shall use the sa_handler member to describe the signal-catching function and the application shall not modify the sa_sigaction member.
If SA_SIGINFO is set and the signal is caught, the signal-catching function shall be entered as:
void func(int signo, siginfo_t *info, void *context);
where two additional arguments are passed to the signal-catching function. The second argument shall point to an object of type siginfo_t explaining the reason why the signal was generated; the third argument can be cast to a pointer to an object of type ucontext_t to refer to the receiving thread's context that was interrupted when the signal was delivered. In this case, the application shall use the sa_sigaction member to describe the signal-catching function and the application shall not modify the sa_handler member.
The si_signo member contains the system-generated signal number.
The si_errno member may contain implementation-defined additional error information; if non-zero, it contains an error number identifying the condition that caused the signal to be generated.
The si_code member contains a code identifying the cause of the signal, as described in Section 2.4.3, Signal Actions.
SA_NOCLDWAITβ Ifsigdoes not equalSIGCHLD, the behavior is unspecified. Otherwise, the behavior of theSA_NOCLDWAITflag is as specified in Consequences of Process Termination.SA_NODEFERβ If set andsigis caught,sigshall not be added to the thread's signal mask on entry to the signal handler unless it is included insa_mask. Otherwise,sigshall always be added to the thread's signal mask on entry to the signal handler.
When a signal is caught by a signal-catching function installed by sigaction(), a new signal mask is calculated and installed for the duration of the signal-catching function (or until a call to either sigprocmask() or sigsuspend() is made). This mask is formed by taking the union of the current signal mask and the value of the sa_mask for the signal being delivered, and unless SA_NODEFER or SA_RESETHAND is set, then including the signal being delivered. If and when the user's signal handler returns normally, the original signal mask is restored. slay. π
Once an action is installed for a specific signal, it shall remain installed until another action is explicitly requested (by another call to sigaction()), until the SA_RESETHAND flag causes resetting of the handler, or until one of the exec functions is called.
If the previous action for sig had been established by signal(), the values of the fields returned in the structure pointed to by oact are unspecified, and in particular oact->sa_handler is not necessarily the same value passed to signal(). However, if a pointer to the same structure or a copy thereof is passed to a subsequent call to sigaction() via the act argument, handling of the signal shall be as if the original call to signal() were repeated. no cap. π«Ά
If sigaction() fails, no new signal handler is installed.
It is unspecified whether an attempt to set the action for a signal that cannot be caught or ignored to SIG_DFL is ignored or causes an error to be returned with errno set to [EINVAL].
If SA_SIGINFO is not set in sa_flags, then the disposition of subsequent occurrences of sig when it is already pending is implementation-defined; the signal-catching function shall be invoked with a single argument. If SA_SIGINFO is set in sa_flags, then subsequent occurrences of sig generated by sigqueue() or as a result of any signal-generating function that supports the specification of an application-defined value (when sig is already pending) shall be queued in FIFO order until delivered or accepted; the signal-catching function shall be invoked with three arguments. The application specified value is passed to the signal-catching function as the si_value member of the siginfo_t structure.
The result of the use of sigaction() and a sigwait() function concurrently within a process on the same signal is unspecified. π
π RETURN VALUE π
okay so bestie, if everything goes right? sigaction() returns 0. slay. but if it doesn't? it returns -1, sets errno to tell you why it's upset, and β super important β no new signal-catching function shall be installed. so nothing changed, no half-installed chaos. periodt. β¨
π ERRORS π
so sigaction() shall fail if:
EINVALβ thesigargument is not a valid signal number, OR someone tried to catch a signal that cannot be caught, OR tried to ignore a signal that cannot be ignored. you can't do that. the signal said no. π
and it may fail if:
EINVALβ an attempt was made to set the action toSIG_DFLfor a signal that cannot be caught or ignored (or both). like, respectfully, stop trying. π«
plus, on systems that do not support the XSI option, sigaction() may fail if the SA_SIGINFO flag is set in the sa_flags field of the sigaction structure for a signal that's not in the range SIGRTMIN to SIGRTMAX. iykyk.
The following sections are informative.
πΈ EXAMPLES πΈ
Establishing a Signal Handler
this is the moment, the tutorial, the main character arc. β¨ this example shows off sigaction() establishing a handler for the SIGINT signal:
#include <signal.h>
static void handler(int signum)
{
/* Take appropriate actions for signal delivery */
}
int main()
{
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART; /* Restart functions if
interrupted by handler */
if (sigaction(SIGINT, &sa, NULL) == -1)
/* Handle error */;
/* Further code */
}
look at her go. SA_RESTART in the chat means functions get restarted if the handler interrupts them. iconic. π
π APPLICATION USAGE π
so sigaction() supersedes the signal() function, and you should use it in preference. and listen β in particular, sigaction() and signal() should not be used in the same process to control the same signal. pick one bestie, don't make them fight. π€
the behavior of async-signal-safe functions, as defined in their respective DESCRIPTION sections, is as specified by this volume of POSIX.1β2017, regardless of invocation from a signal-catching function. that's the only intended meaning of the statement that async-signal-safe functions may be used in signal-catching functions without restrictions. so don't get it twisted β applications must still consider all effects of such functions on things like data structures, files, and process state. in particular, application developers need to consider the restrictions on interactions when interrupting sleep() and interactions among multiple handles for a file description. and the fact that any specific function is listed as async-signal-safe does not necessarily mean that invocation of that function from a signal-catching function is recommended. being allowed β being a good idea, fr fr. π
in order to prevent errors arising from interrupting non-async-signal-safe function calls, applications should protect calls to these functions either by blocking the appropriate signals or through the use of some programmatic semaphore (see semget(), sem_init(), sem_open(), and so on). note in particular that even the ``safe'' functions may modify errno β the signal-catching function, if not executing as an independent thread, should save and restore its value in order to avoid the possibility that delivery of a signal in between an error return from a function that sets errno and the subsequent examination of errno could result in the signal-catching function changing the value of errno. naturally, the same principles apply to the async-signal-safety of application routines and asynchronous data access. π
note that longjmp() and siglongjmp() are not in the list of async-signal-safe functions. why? because the code executing after longjmp() and siglongjmp() can call any unsafe functions with the same danger as calling those unsafe functions directly from the signal handler. applications that use longjmp() and siglongjmp() from within signal handlers require rigorous protection in order to be portable. many of the other functions that are excluded from the list are traditionally implemented using either malloc() or free() functions or the standard I/O library, both of which traditionally use data structures in a non-async-signal-safe manner. and since any combination of different functions using a common data structure can cause async-signal-safety problems, this volume of POSIX.1β2017 does not define the behavior when any unsafe function is called in a signal handler that interrupts an unsafe function. π¨
usually, the signal is executed on the stack that was in effect before the signal was delivered. an alternate stack may be specified to receive a subset of the signals being caught. π§
when the signal handler returns, the receiving thread resumes execution at the point it was interrupted β unless the signal handler makes other arrangements. and if longjmp() or _longjmp() is used to leave the signal handler, then the signal mask must be explicitly restored. don't forget, bestie. π
this volume of POSIX.1β2017 defines the third argument of a signal handling function when SA_SIGINFO is set as a void * instead of a ucontext_t *, but without requiring type checking. new applications should explicitly cast the third argument of the signal handling function to ucontext_t *. do the cast. be safe. π
the BSD optional four argument signal handling function is not supported by this volume of POSIX.1β2017. the BSD declaration would be:
void handler(int sig, int code, struct sigcontext *scp,
char *addr);
where sig is the signal number, code is additional information on certain signals, scp is a pointer to the sigcontext structure, and addr is additional address information. much the same information is available in the objects pointed to by the second argument of the signal handler specified when SA_SIGINFO is set. so like, you're not missing out, the info's still there. π
since the sigaction() function is allowed but not required to set SA_NODEFER when the application sets the SA_RESETHAND flag, applications which depend on the SA_RESETHAND functionality for the newly installed signal handler must always explicitly set SA_NODEFER when they set SA_RESETHAND in order to be portable. always. explicitly. no shortcuts. β
see also the rationale for Realtime Signal Generation and Delivery in the Rationale (Informative) volume of POSIX.1β2017, Section B.2.4.2, Signal Generation and Delivery. π
π¦ RATIONALE π¦
okay so even though this volume of POSIX.1β2017 requires that signals that cannot be ignored shall not be added to the signal mask when a signal-catching function is entered, there's no explicit requirement that subsequent calls to sigaction() reflect this in the information returned in the oact argument. in other words, if SIGKILL is included in the sa_mask field of act, it is unspecified whether or not a subsequent call to sigaction() returns with SIGKILL included in the sa_mask field of oact. unspecified means don't count on it either way. π€·
the SA_NOCLDSTOP flag, when supplied in the act->sa_flags parameter, allows overloading SIGCHLD with the System V semantics that each SIGCLD signal indicates a single terminated child. most conforming applications that catch SIGCHLD are expected to install signal-catching functions that repeatedly call the waitpid() function with the WNOHANG flag set, acting on each child for which status is returned, until waitpid() returns zero. if stopped children are not of interest, the use of the SA_NOCLDSTOP flag can prevent the overhead from invoking the signal-catching routine when they stop. efficiency queen. π
some historical implementations also define other mechanisms for stopping processes, such as the ptrace() function. these implementations usually do not generate a SIGCHLD signal when processes stop due to this mechanism β however, that is beyond the scope of this volume of POSIX.1β2017. noted, moving on. π
this volume of POSIX.1β2017 requires that calls to sigaction() that supply a NULL act argument succeed, even in the case of signals that cannot be caught or ignored (that is, SIGKILL or SIGSTOP). the System V signal() and BSD sigvec() functions return [EINVAL] in these cases and, in this respect, their behavior varies from sigaction(). so sigaction is the chill one here. β¨
this volume of POSIX.1β2017 requires that sigaction() properly save and restore a signal action set up by the ISO C standard signal() function. however, there is no guarantee that the reverse is true, nor could there be given the greater amount of information conveyed by the sigaction structure. because of this, applications should avoid using both functions for the same signal in the same process. and since this cannot always be avoided in case of general-purpose library routines, they should always be implemented with sigaction(). rules are rules. π
it was intended that the signal() function should be implementable as a library routine using sigaction(). cute, right? π«Ά
the POSIX Realtime Extension extends the sigaction() function as specified by the POSIX.1β1990 standard to allow the application to request on a per-signal basis, via an additional signal action flag, that the extra parameters β including the application-defined signal value, if any β be passed to the signal-catching function. extra info, delivered directly to your handler. obsessed. π
π FUTURE DIRECTIONS π
None.
that's it. nothing coming. the future is a blank canvas bestie. π¦
π SEE ALSO π
- Section 2.4, Signal Concepts
exec_Exit()kill()_longjmp()longjmp()pthread_sigmask()raise()semget()sem_init()sem_open()sigaddset()sigaltstack()sigdelset()sigemptyset()sigfillset()sigismember()signal()sigsuspend()wait()waitid()
The Base Definitions volume of POSIX.1β2017, <signal.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html . π
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page bestie!! πβ¨ this one came from the IEEE/The Open Group girlies, dated 2017, and it lives in the man pages as SIGACTION(3P). go install a handler, save your errno, and slay. periodt. ππ¦π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms