1 .\" Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .TH setjmp 3 (date) "Linux man-pages (unreleased)"
7 setjmp, sigsetjmp, longjmp, siglongjmp \- performing a nonlocal goto
10 .RI ( libc ", " \-lc )
13 .B #include <setjmp.h>
15 .BI "int setjmp(jmp_buf " env );
16 .BI "int sigsetjmp(sigjmp_buf " env ", int " savesigs );
18 .BI "noreturn void longjmp(jmp_buf " env ", int " val );
19 .BI "noreturn void siglongjmp(sigjmp_buf " env ", int " val );
23 Feature Test Macro Requirements for glibc (see
24 .BR feature_test_macros (7)):
35 The functions described on this page are used for performing "nonlocal gotos":
36 transferring execution from one function to a predetermined location
40 function dynamically establishes the target to which control
41 will later be transferred, and
43 performs the transfer of execution.
47 function saves various information about the calling environment
48 (typically, the stack pointer, the instruction pointer,
49 possibly the values of other registers and the signal mask)
60 function uses the information saved in
62 to transfer control back to the point where
64 was called and to restore ("rewind") the stack to its state at the time of the
67 In addition, and depending on the implementation (see NOTES),
68 the values of some other registers and the process signal mask
69 may be restored to their state at the time of the
73 Following a successful
75 execution continues as if
77 had returned for a second time.
78 This "fake" return can be distinguished from a true
80 call because the "fake" return returns the value provided in
82 If the programmer mistakenly passes the value 0 in
84 the "fake" return will instead return 1.
85 .SS sigsetjmp() and siglongjmp()
89 also perform nonlocal gotos, but provide predictable handling of
90 the process signal mask.
96 is nonzero, the process's current signal mask is saved in
98 and will be restored if a
100 is later performed with this
106 return 0 when called directly;
107 on the "fake" return that occurs after
111 the nonzero value specified in
119 functions do not return.
121 For an explanation of the terms used in this section, see
129 Interface Attribute Value
133 T} Thread safety MT-Safe
137 T} Thread safety MT-Safe
145 POSIX.1-2001, POSIX.1-2008, C89, C99.
149 POSIX.1-2001, POSIX.1-2008.
151 POSIX does not specify whether
153 will save the signal mask
154 (to be later restored during
156 In System V it will not.
157 In 4.3BSD it will, and there
161 The behavior under Linux depends on the glibc version
162 and the setting of feature test macros.
163 On Linux with glibc versions before 2.19,
165 follows the System V behavior by default,
166 but the BSD behavior is provided if the
168 feature test macro is explicitly defined
169 .\" so that _FAVOR_BSD is triggered
172 .BR _POSIX_C_SOURCE ,
174 .\" .BR _XOPEN_SOURCE_EXTENDED ,
181 exposes only the System V version of
183 Programs that need the BSD semantics should replace calls to
194 can be useful for dealing with errors inside deeply nested function calls
195 or to allow a signal handler to pass control to
196 a specific point in the program,
197 rather than returning to the point where the handler interrupted
200 if you want to portably save and restore signal masks, use
204 See also the discussion of program readability below.
206 The compiler may optimize variables into registers, and
208 may restore the values of other registers in addition to the
209 stack pointer and program counter.
210 Consequently, the values of automatic variables are unspecified
213 if they meet all the following criteria:
215 they are local to the function that made the corresponding
219 their values are changed between the calls to
225 they are not declared as
228 Analogous remarks apply for
231 .SS Nonlocal gotos and program readability
232 While it can be abused,
233 the traditional C "goto" statement at least has the benefit that lexical cues
234 (the goto statement and the target label)
235 allow the programmer to easily perceive the flow of control.
236 Nonlocal gotos provide no such cues: multiple
238 calls might employ the same
240 variable so that the content of the variable may change
241 over the lifetime of the application.
242 Consequently, the programmer may be forced to perform detailed
243 reading of the code to determine the dynamic target of a particular
246 (To make the programmer's life easier, each
248 call should employ a unique
252 Adding further difficulty, the
256 calls may not even be in the same source code module.
258 In summary, nonlocal gotos can make programs harder to understand
259 and maintain, and an alternative should be used if possible.
262 If the function which called
266 is called, the behavior is undefined.
267 Some kind of subtle or unsubtle chaos is sure to result.
269 If, in a multithreaded program, a
273 buffer that was initialized by a call to
275 in a different thread, the behavior is undefined.
277 .\" The following statement appeared in versions up to POSIX.1-2008 TC1,
278 .\" but is set to be removed in POSIX.1-2008 TC2:
280 .\" According to POSIX.1, if a
282 .\" call is performed from a nested signal handler
283 .\" (i.e., from a handler that was invoked in response to a signal that was
284 .\" generated while another signal was already in the process of being
285 .\" handled), the behavior is undefined.
287 POSIX.1-2008 Technical Corrigendum 2 adds
288 .\" http://austingroupbugs.net/view.php?id=516#c1195
292 to the list of async-signal-safe functions.
293 However, the standard recommends avoiding the use of these functions
294 from signal handlers and goes on to point out that
295 if these functions are called from a signal handler that interrupted
296 a call to a non-async-signal-safe function (or some equivalent,
297 such as the steps equivalent to
299 that occur upon a return from the initial call to
301 the behavior is undefined if the program subsequently makes a call to
302 a non-async-signal-safe function.
303 The only way of avoiding undefined behavior is to ensure one of the following:
305 After long jumping from the signal handler,
306 the program does not call any non-async-signal-safe functions
307 and does not return from the initial call to
310 Any signal whose handler performs a long jump must be blocked during
312 call to a non-async-signal-safe function and
313 no non-async-signal-safe functions are called after
314 returning from the initial call to
318 .BR signal\-safety (7)