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