1SIGALTSTACK(2) Linux Programmer's Manual SIGALTSTACK(2)
2
3
4
6 sigaltstack - set and/or get signal stack context
7
9 #include <signal.h>
10
11 int sigaltstack(const stack_t *ss, stack_t *oss);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 sigaltstack():
16 _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
17 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
18 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
19
21 sigaltstack() allows a process to define a new alternate signal stack
22 and/or retrieve the state of an existing alternate signal stack. An
23 alternate signal stack is used during the execution of a signal handler
24 if the establishment of that handler (see sigaction(2)) requested it.
25
26 The normal sequence of events for using an alternate signal stack is
27 the following:
28
29 1. Allocate an area of memory to be used for the alternate signal
30 stack.
31
32 2. Use sigaltstack() to inform the system of the existence and location
33 of the alternate signal stack.
34
35 3. When establishing a signal handler using sigaction(2), inform the
36 system that the signal handler should be executed on the alternate
37 signal stack by specifying the SA_ONSTACK flag.
38
39 The ss argument is used to specify a new alternate signal stack, while
40 the oss argument is used to retrieve information about the currently
41 established signal stack. If we are interested in performing just one
42 of these tasks then the other argument can be specified as NULL. Each
43 of these arguments is a structure of the following type:
44
45 typedef struct {
46 void *ss_sp; /* Base address of stack */
47 int ss_flags; /* Flags */
48 size_t ss_size; /* Number of bytes in stack */
49 } stack_t;
50
51 To establish a new alternate signal stack, ss.ss_flags is set to zero,
52 and ss.ss_sp and ss.ss_size specify the starting address and size of
53 the stack. The constant SIGSTKSZ is defined to be large enough to
54 cover the usual size requirements for an alternate signal stack, and
55 the constant MINSIGSTKSZ defines the minimum size required to execute a
56 signal handler.
57
58 When a signal handler is invoked on the alternate stack, the kernel
59 automatically aligns the address given in ss.ss_sp to a suitable
60 address boundary for the underlying hardware architecture.
61
62 To disable an existing stack, specify ss.ss_flags as SS_DISABLE. In
63 this case, the remaining fields in ss are ignored.
64
65 If oss is not NULL, then it is used to return information about the
66 alternate signal stack which was in effect prior to the call to sigalt‐
67 stack(). The oss.ss_sp and oss.ss_size fields return the starting
68 address and size of that stack. The oss.ss_flags may return either of
69 the following values:
70
71 SS_ONSTACK
72 The process is currently executing on the alternate signal
73 stack. (Note that it is not possible to change the alternate
74 signal stack if the process is currently executing on it.)
75
76 SS_DISABLE
77 The alternate signal stack is currently disabled.
78
80 sigaltstack() returns 0 on success, or -1 on failure with errno set to
81 indicate the error.
82
84 EFAULT Either ss or oss is not NULL and points to an area outside of
85 the process's address space.
86
87 EINVAL ss is not NULL and the ss_flags field contains a nonzero value
88 other than SS_DISABLE.
89
90 ENOMEM The specified size of the new alternate signal stack
91 (ss.ss_size) was less than MINSTKSZ.
92
93 EPERM An attempt was made to change the alternate signal stack while
94 it was active (i.e., the process was already executing on the
95 current alternate signal stack).
96
98 SUSv2, SVr4, POSIX.1-2001.
99
101 The most common usage of an alternate signal stack is to handle the
102 SIGSEGV signal that is generated if the space available for the normal
103 process stack is exhausted: in this case, a signal handler for SIGSEGV
104 cannot be invoked on the process stack; if we wish to handle it, we
105 must use an alternate signal stack.
106
107 Establishing an alternate signal stack is useful if a process expects
108 that it may exhaust its standard stack. This may occur, for example,
109 because the stack grows so large that it encounters the upwardly grow‐
110 ing heap, or it reaches a limit established by a call to setr‐
111 limit(RLIMIT_STACK, &rlim). If the standard stack is exhausted, the
112 kernel sends the process a SIGSEGV signal. In these circumstances the
113 only way to catch this signal is on an alternate signal stack.
114
115 On most hardware architectures supported by Linux, stacks grow down‐
116 ward. sigaltstack() automatically takes account of the direction of
117 stack growth.
118
119 Functions called from a signal handler executing on an alternate signal
120 stack will also use the alternate signal stack. (This also applies to
121 any handlers invoked for other signals while the process is executing
122 on the alternate signal stack.) Unlike the standard stack, the system
123 does not automatically extend the alternate signal stack. Exceeding
124 the allocated size of the alternate signal stack will lead to unpre‐
125 dictable results.
126
127 A successful call to execve(2) removes any existing alternate signal
128 stack. A child process created via fork(2) inherits a copy of its par‐
129 ent's alternate signal stack settings.
130
131 sigaltstack() supersedes the older sigstack() call. For backward com‐
132 patibility, glibc also provides sigstack(). All new applications
133 should be written using sigaltstack().
134
135 History
136 4.2BSD had a sigstack() system call. It used a slightly different
137 struct, and had the major disadvantage that the caller had to know the
138 direction of stack growth.
139
141 The following code segment demonstrates the use of sigaltstack():
142
143 stack_t ss;
144
145 ss.ss_sp = malloc(SIGSTKSZ);
146 if (ss.ss_sp == NULL)
147 /* Handle error */;
148 ss.ss_size = SIGSTKSZ;
149 ss.ss_flags = 0;
150 if (sigaltstack(&ss, NULL) == -1)
151 /* Handle error */;
152
154 execve(2), setrlimit(2), sigaction(2), siglongjmp(3), sigsetjmp(3),
155 signal(7)
156
158 This page is part of release 3.53 of the Linux man-pages project. A
159 description of the project, and information about reporting bugs, can
160 be found at http://www.kernel.org/doc/man-pages/.
161
162
163
164Linux 2010-09-26 SIGALTSTACK(2)