Linux Blog


Section: Linux Programmer's Manual (2)
Updated: 2001-09-27
Index Return to Main Contents


sigaltstack - set and/or get signal stack context  


#include <signal.h>

int sigaltstack(const stack_t *ss, stack_t *oss);  




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 oss 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. Each of these arguments is a structure of the following type:

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, ss.ss_flags is set to zero, and ss.ss_sp and ss.ss_size specify the starting address and 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 remaining fields in ss are ignored.

If oss 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 oss.ss_sp and oss.ss_size fields return the starting address and size of that stack. The oss.ss_flags may return either of the following values:

The alternate signal stack is currently disabled.




Either ss or oss is not NULL and points to an area outside of the process's address space.
ss is not NULL and the ss_flags field contains a non-zero value other than SS_DISABLE.
The specified size of the new alternate signal stack (ss.ss_size) was less than MINSTKSZ.


SUSv2, SVr4, POSIX.1-2001.  


The most common usage of an alternate signal stack is to handle the SIGSEGV signal that is generated if the space available for the normal process stack is exhausted: in this case, a signal handler for SIGSEGV cannot be invoked on the process stack; if we wish to handle it, we must use an alternate signal stack. Establishing an alternate signal stack is useful if a process expects that it may exhaust its standard stack. This may occur, for example, because the stack grows so large that it encounters the upwardly growing heap, or it reaches a limit established by a call to setrlimit(RLIMIT_STACK, &rlim). If the standard stack is exhausted, the kernel sends the process a SIGSEGV signal. In these circumstances the only way to catch this signal is on an alternate signal stack. On most hardware architectures supported by Linux, stacks grow downwards. sigaltstack() automatically takes account of the direction of stack growth. Functions called from a signal handler executing on an alternate signal stack will also use the alternate signal stack. (This also applies to any handlers invoked for other signals while the process is executing on the alternate signal stack.) Unlike the standard stack, the system does not automatically extend the alternate signal stack. Exceeding the allocated size of the alternate signal stack will lead to unpredictable results. A successful call to execve(2) removes any existing alternate signal stack. sigaltstack() supersedes the older sigstack() call. For backwards compatibility, glibc also provides sigstack(). All new applications should be written using sigaltstack().  




The following code segment demonstrates the use of sigaltstack():

stack_t ss;

ss.ss_sp = malloc(SIGSTKSZ);
if (ss.ss_sp == NULL)
    /* Handle error */;
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
    /* Handle error */;






Random Man Pages: