sigaltstack - set and/or get signal stack context
#include <signal.h>
int sigaltstack(const stack_t *_Nullable restrict ss,
stack_t *_Nullable restrict old_ss);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
sigaltstack() allows a thread to define a new alternate signal stack and/or retrieve the state of an existing alternate signal stack. An alternate signal stack is used during the execution of a signal handler if the establishment of that handler (see sigaction(2)) requested it.
The normal sequence of events for using an alternate signal stack is the following:
Allocate an area of memory to be used for the alternate signal stack.
Use sigaltstack() to inform the system of the existence and location of the alternate signal stack.
When establishing a signal handler using sigaction(2), inform the system that the signal handler should be executed on the alternate signal stack by specifying the SA_ONSTACK flag.
The ss
argument is used to specify a new alternate signal
stack, while the old_ss
argument is used to retrieve
information about the currently established signal stack. If we are
interested in performing just one of these tasks, then the other
argument can be specified as NULL.
The stack_t
type used to type the arguments of this function
is defined as follows:
typedef struct {
void *ss_sp; /* Base address of stack */
int ss_flags; /* Flags */
size_t ss_size; /* Number of bytes in stack */
} stack_t;
To establish a new alternate signal stack, the fields of this structure are set as follows:
ss.ss_flags
This field contains either 0, or the following flag:
Clear the alternate signal stack settings on entry to the signal handler. When the signal handler returns, the previous alternate signal stack settings are restored.
This flag was added in order to make it safe to switch away from the signal handler with swapcontext(3). Without this flag, a subsequently handled signal will corrupt the state of the switched-away signal handler. On kernels where this flag is not supported, sigaltstack() fails with the error EINVAL when this flag is supplied.
ss.ss_sp
This field specifies the starting address of the stack. When a signal
handler is invoked on the alternate stack, the kernel automatically
aligns the address given in ss.ss_sp
to a suitable address
boundary for the underlying hardware architecture.
ss.ss_size
This field specifies the size of the stack. The constant SIGSTKSZ is defined to be large enough to cover the usual size requirements for an alternate signal stack, and the constant MINSIGSTKSZ defines the minimum size required to execute a signal handler.
To disable an existing stack, specify ss.ss_flags
as
SS_DISABLE. In this case, the kernel ignores any other
flags in ss.ss_flags
and the remaining fields in
ss
.
If old_ss
is not NULL, then it is used to return information
about the alternate signal stack which was in effect prior to the call
to sigaltstack(). The old_ss.ss_sp
and
old_ss.ss_size
fields return the starting address and size of
that stack. The old_ss.ss_flags
may return either of the
following values:
The thread is currently executing on the alternate signal stack. (Note that it is not possible to change the alternate signal stack if the thread is currently executing on it.)
The alternate signal stack is currently disabled.
Alternatively, this value is returned if the thread is currently executing on an alternate signal stack that was established using the SS_AUTODISARM flag. In this case, it is safe to switch away from the signal handler with swapcontext(3). It is also possible to set up a different alternative signal stack using a further call to sigaltstack().
The alternate signal stack has been marked to be autodisarmed as described above.
By specifying ss
as NULL, and old_ss
as a non-NULL
value, one can obtain the current settings for the alternate signal
stack without changing them.
sigaltstack() returns 0 on success, or -1 on failure
with errno
set to indicate the error.
The following code segment demonstrates the use of sigaltstack() (and sigaction(2)) to install an alternate signal stack that is employed by a handler for the SIGSEGV signal:
stack_t ss;
ss.ss_sp = malloc(SIGSTKSZ);
if (ss.ss_sp == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) == -1) {
perror("sigaltstack");
exit(EXIT_FAILURE);
}
sa.sa_flags = SA_ONSTACK;
sa.sa_handler = handler(); /* Address of a signal handler */
sigemptyset(&sa.sa_mask);
if (sigaction(SIGSEGV, &sa, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}