]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/sigaltstack.2
sched_setattr.2: tfix
[thirdparty/man-pages.git] / man2 / sigaltstack.2
1 '\" t
2 .\" Copyright (c) 2001, 2017 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" aeb, various minor fixes
27 .TH SIGALTSTACK 2 2017-11-08 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 sigaltstack \- set and/or get signal stack context
30 .SH SYNOPSIS
31 .B #include <signal.h>
32 .PP
33 .BI "int sigaltstack(const stack_t *" ss ", stack_t *" old_ss );
34 .PP
35 .in -4n
36 Feature Test Macro Requirements for glibc (see
37 .BR feature_test_macros (7)):
38 .in
39 .PP
40 .BR sigaltstack ():
41 .ad l
42 .RS 4
43 .PD 0
44 _XOPEN_SOURCE\ >=\ 500
45 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
46 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
47 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
48 .PD
49 .RE
50 .ad
51 .SH DESCRIPTION
52 .BR sigaltstack ()
53 allows a process to define a new alternate
54 signal stack and/or retrieve the state of an existing
55 alternate signal stack.
56 An alternate signal stack is used during the
57 execution of a signal handler if the establishment of that handler (see
58 .BR sigaction (2))
59 requested it.
60 .PP
61 The normal sequence of events for using an alternate signal stack
62 is the following:
63 .TP 3
64 1.
65 Allocate an area of memory to be used for the alternate
66 signal stack.
67 .TP
68 2.
69 Use
70 .BR sigaltstack ()
71 to inform the system of the existence and
72 location of the alternate signal stack.
73 .TP
74 3.
75 When establishing a signal handler using
76 .BR sigaction (2),
77 inform the system that the signal handler should be executed
78 on the alternate signal stack by
79 specifying the \fBSA_ONSTACK\fP flag.
80 .PP
81 The \fIss\fP argument is used to specify a new
82 alternate signal stack, while the \fIold_ss\fP argument
83 is used to retrieve information about the currently
84 established signal stack.
85 If we are interested in performing just one
86 of these tasks, then the other argument can be specified as NULL.
87 .PP
88 The
89 .I stack_t
90 type used to type the arguments of this function is defined as follows:
91 .PP
92 .in +4n
93 .EX
94 typedef struct {
95 void *ss_sp; /* Base address of stack */
96 int ss_flags; /* Flags */
97 size_t ss_size; /* Number of bytes in stack */
98 } stack_t;
99 .EE
100 .in
101 .PP
102 To establish a new alternate signal stack,
103 the fields of this structure are set as follows:
104 .TP
105 .I ss.ss_flags
106 This field contains either 0, or the following flag:
107 .RS
108 .TP
109 .BR SS_AUTODISARM " (since Linux 4.7)"
110 .\" commit 2a74213838104a41588d86fd5e8d344972891ace
111 .\" See tools/testing/selftests/sigaltstack/sas.c in kernel sources
112 Clear the alternate signal stack settings on entry to the signal handler.
113 When the signal handler returns,
114 the previous alternate signal stack settings are restored.
115 .IP
116 This flag was added in order make it safe
117 to switch away from the signal handler with
118 .BR swapcontext (3).
119 Without this flag, a subsequently handled signal will corrupt
120 the state of the switched-away signal handler.
121 On kernels where this flag is not supported,
122 .BR sigaltstack ()
123 fails with the error
124 .BR EINVAL
125 when this flag is supplied.
126 .RE
127 .TP
128 .I ss.ss_sp
129 This field specifies the starting address of the stack.
130 When a signal handler is invoked on the alternate stack,
131 the kernel automatically aligns the address given in \fIss.ss_sp\fP
132 to a suitable address boundary for the underlying hardware architecture.
133 .TP
134 .I ss.ss_size
135 This field specifies the size of the stack.
136 The constant \fBSIGSTKSZ\fP is defined to be large enough
137 to cover the usual size requirements for an alternate signal stack,
138 and the constant \fBMINSIGSTKSZ\fP defines the minimum
139 size required to execute a signal handler.
140 .PP
141 To disable an existing stack, specify \fIss.ss_flags\fP
142 as \fBSS_DISABLE\fP.
143 In this case, the kernel ignores any other flags in
144 .IR ss.ss_flags
145 and the remaining fields
146 in \fIss\fP.
147 .PP
148 If \fIold_ss\fP is not NULL, then it is used to return information about
149 the alternate signal stack which was in effect prior to the
150 call to
151 .BR sigaltstack ().
152 The \fIold_ss.ss_sp\fP and \fIold_ss.ss_size\fP fields return the starting
153 address and size of that stack.
154 The \fIold_ss.ss_flags\fP may return either of the following values:
155 .TP
156 .B SS_ONSTACK
157 The process is currently executing on the alternate signal stack.
158 (Note that it is not possible
159 to change the alternate signal stack if the process is
160 currently executing on it.)
161 .TP
162 .B SS_DISABLE
163 The alternate signal stack is currently disabled.
164 .IP
165 Alternatively, this value is returned if the process is currently
166 executing on an alternate signal stack that was established using the
167 .B SS_AUTODISARM
168 flag.
169 In this case, it is safe to switch away from the signal handler with
170 .BR swapcontext (3).
171 It is also possible to set up a different alternative signal stack
172 using a further call to
173 .BR sigaltstack ().
174 .\" FIXME Was it intended that one can set up a different alternative
175 .\" signal stack in this scenario? (In passing, if one does this, the
176 .\" sigaltstack(NULL, &old_ss) now returns old_ss.ss_flags==SS_AUTODISARM
177 .\" rather than old_ss.ss_flags==SS_DISABLE. The API design here seems
178 .\" confusing...
179 .TP
180 .B SS_AUTODISARM
181 The alternate signal stack has been marked to be autodisarmed
182 as described above.
183 .PP
184 By specifying
185 .I ss
186 as NULL, and
187 .I old_ss
188 as a non-NULL value, one can obtain the current settings for
189 the alternate signal stack without changing them.
190 .SH RETURN VALUE
191 .BR sigaltstack ()
192 returns 0 on success, or \-1 on failure with
193 \fIerrno\fP set to indicate the error.
194 .SH ERRORS
195 .TP
196 .B EFAULT
197 Either \fIss\fP or \fIold_ss\fP is not NULL and points to an area
198 outside of the process's address space.
199 .TP
200 .B EINVAL
201 \fIss\fP is not NULL and the \fIss_flags\fP field contains
202 an invalid flag.
203 .TP
204 .B ENOMEM
205 The specified size of the new alternate signal stack
206 .I ss.ss_size
207 was less than
208 .BR MINSIGSTKSZ .
209 .TP
210 .B EPERM
211 An attempt was made to change the alternate signal stack while
212 it was active (i.e., the process was already executing
213 on the current alternate signal stack).
214 .SH ATTRIBUTES
215 For an explanation of the terms used in this section, see
216 .BR attributes (7).
217 .TS
218 allbox;
219 lb lb lb
220 l l l.
221 Interface Attribute Value
222 T{
223 .BR sigaltstack ()
224 T} Thread safety MT-Safe
225 .TE
226 .SH CONFORMING TO
227 POSIX.1-2001, POSIX.1-2008, SUSv2, SVr4.
228 .PP
229 The
230 .B SS_AUTODISARM
231 flag is a Linux extension.
232 .SH NOTES
233 The most common usage of an alternate signal stack is to handle the
234 .B SIGSEGV
235 signal that is generated if the space available for the
236 normal process stack is exhausted: in this case, a signal handler for
237 .B SIGSEGV
238 cannot be invoked on the process stack; if we wish to handle it,
239 we must use an alternate signal stack.
240 .PP
241 Establishing an alternate signal stack is useful if a process
242 expects that it may exhaust its standard stack.
243 This may occur, for example, because the stack grows so large
244 that it encounters the upwardly growing heap, or it reaches a
245 limit established by a call to \fBsetrlimit(RLIMIT_STACK, &rlim)\fP.
246 If the standard stack is exhausted, the kernel sends
247 the process a \fBSIGSEGV\fP signal.
248 In these circumstances the only way to catch this signal is
249 on an alternate signal stack.
250 .PP
251 On most hardware architectures supported by Linux, stacks grow
252 downward.
253 .BR sigaltstack ()
254 automatically takes account
255 of the direction of stack growth.
256 .PP
257 Functions called from a signal handler executing on an alternate
258 signal stack will also use the alternate signal stack.
259 (This also applies to any handlers invoked for other signals while
260 the process is executing on the alternate signal stack.)
261 Unlike the standard stack, the system does not
262 automatically extend the alternate signal stack.
263 Exceeding the allocated size of the alternate signal stack will
264 lead to unpredictable results.
265 .PP
266 A successful call to
267 .BR execve (2)
268 removes any existing alternate
269 signal stack.
270 A child process created via
271 .BR fork (2)
272 inherits a copy of its parent's alternate signal stack settings.
273 .PP
274 .BR sigaltstack ()
275 supersedes the older
276 .BR sigstack ()
277 call.
278 For backward compatibility, glibc also provides
279 .BR sigstack ().
280 All new applications should be written using
281 .BR sigaltstack ().
282 .SS History
283 4.2BSD had a
284 .BR sigstack ()
285 system call.
286 It used a slightly
287 different struct, and had the major disadvantage that the caller
288 had to know the direction of stack growth.
289 .SH EXAMPLE
290 The following code segment demonstrates the use of
291 .BR sigaltstack ()
292 (and
293 .BR sigaction (2))
294 to install an alternate signal stack that is employed by a handler
295 for the
296 .BR SIGSEGV
297 signal:
298 .PP
299 .in +4n
300 .EX
301 stack_t ss;
302
303 ss.ss_sp = malloc(SIGSTKSZ);
304 if (ss.ss_sp == NULL) {
305 perror("malloc");
306 exit(EXIT_FAILURE);
307 }
308
309 ss.ss_size = SIGSTKSZ;
310 ss.ss_flags = 0;
311 if (sigaltstack(&ss, NULL) == \-1) {
312 perror("sigaltstack");
313 exit(EXIT_FAILURE);
314 }
315
316 sa.sa_flags = SA_ONSTACK;
317 sa.sa_handler = handler(); /* Address of a signal handler */
318 sigemptyset(&sa.sa_mask);
319 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
320 perror("sigaction");
321 exit(EXIT_FAILURE);
322 }
323 .EE
324 .in
325 .SH BUGS
326 In Linux 2.2 and earlier, the only flag that could be specified
327 in
328 .I ss.sa_flags
329 was
330 .BR SS_DISABLE .
331 In the lead up to the release of the Linux 2.4 kernel,
332 .\" Linux 2.3.40
333 .\" After quite a bit of web and mail archive searching,
334 .\" I could not find the patch on any mailing list, and I
335 .\" could find no place where the rationale for this change
336 .\" explained -- mtk
337 a change was made to allow
338 .BR sigaltstack ()
339 to allow
340 .I ss.ss_flags==SS_ONSTACK
341 with the same meaning as
342 .IR "ss.ss_flags==0"
343 (i.e., the inclusion of
344 .B SS_ONSTACK
345 in
346 .I ss.ss_flags
347 is a no-op).
348 On other implementations, and according to POSIX.1,
349 .B SS_ONSTACK
350 appears only as a reported flag in
351 .IR old_ss.ss_flags .
352 On Linux, there is no need ever to specify
353 .B SS_ONSTACK
354 in
355 .IR ss.ss_flags ,
356 and indeed doing so should be avoided on portability grounds:
357 various other systems
358 .\" See the source code of Illumos and FreeBSD, for example.
359 give an error if
360 .B SS_ONSTACK
361 is specified in
362 .IR ss.ss_flags .
363 .SH SEE ALSO
364 .BR execve (2),
365 .BR setrlimit (2),
366 .BR sigaction (2),
367 .BR siglongjmp (3),
368 .BR sigsetjmp (3),
369 .BR signal (7)