2 .\" Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .TH setjmp 3 (date) "Linux man-pages (unreleased)"
8 setjmp, sigsetjmp, longjmp, siglongjmp \- performing a nonlocal goto
11 .RI ( libc ", " \-lc )
14 .B #include <setjmp.h>
16 .BI "int setjmp(jmp_buf " env );
17 .BI "int sigsetjmp(sigjmp_buf " env ", int " savesigs );
19 .BI "[[noreturn]] void longjmp(jmp_buf " env ", int " val );
20 .BI "[[noreturn]] void siglongjmp(sigjmp_buf " env ", int " val );
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
36 The functions described on this page are used for performing "nonlocal gotos":
37 transferring execution from one function to a predetermined location
41 function dynamically establishes the target to which control
42 will later be transferred, and
44 performs the transfer of execution.
48 function saves various information about the calling environment
49 (typically, the stack pointer, the instruction pointer,
50 possibly the values of other registers and the signal mask)
61 function uses the information saved in
63 to transfer control back to the point where
65 was called and to restore ("rewind") the stack to its state at the time of the
68 In addition, and depending on the implementation (see NOTES),
69 the values of some other registers and the process signal mask
70 may be restored to their state at the time of the
74 Following a successful
76 execution continues as if
78 had returned for a second time.
79 This "fake" return can be distinguished from a true
81 call because the "fake" return returns the value provided in
83 If the programmer mistakenly passes the value 0 in
85 the "fake" return will instead return 1.
86 .SS sigsetjmp() and siglongjmp()
90 also perform nonlocal gotos, but provide predictable handling of
91 the process signal mask.
97 is nonzero, the process's current signal mask is saved in
99 and will be restored if a
101 is later performed with this
107 return 0 when called directly;
108 on the "fake" return that occurs after
112 the nonzero value specified in
120 functions do not return.
122 For an explanation of the terms used in this section, see
128 Interface Attribute Value
134 T} Thread safety MT-Safe
140 T} Thread safety MT-Safe
165 POSIX does not specify whether
167 will save the signal mask
168 (to be later restored during
170 In System V it will not.
171 In 4.3BSD it will, and there
175 The behavior under Linux depends on the glibc version
176 and the setting of feature test macros.
179 follows the System V behavior by default,
180 but the BSD behavior is provided if the
182 feature test macro is explicitly defined
183 .\" so that _FAVOR_BSD is triggered
186 .BR _POSIX_C_SOURCE ,
188 .\" .BR _XOPEN_SOURCE_EXTENDED ,
195 exposes only the System V version of
197 Programs that need the BSD semantics should replace calls to
208 can be useful for dealing with errors inside deeply nested function calls
209 or to allow a signal handler to pass control to
210 a specific point in the program,
211 rather than returning to the point where the handler interrupted
214 if you want to portably save and restore signal masks, use
218 See also the discussion of program readability below.
220 The compiler may optimize variables into registers, and
222 may restore the values of other registers in addition to the
223 stack pointer and program counter.
224 Consequently, the values of automatic variables are unspecified
227 if they meet all the following criteria:
229 they are local to the function that made the corresponding
233 their values are changed between the calls to
239 they are not declared as
242 Analogous remarks apply for
245 .SS Nonlocal gotos and program readability
246 While it can be abused,
247 the traditional C "goto" statement at least has the benefit that lexical cues
248 (the goto statement and the target label)
249 allow the programmer to easily perceive the flow of control.
250 Nonlocal gotos provide no such cues: multiple
252 calls might employ the same
254 variable so that the content of the variable may change
255 over the lifetime of the application.
256 Consequently, the programmer may be forced to perform detailed
257 reading of the code to determine the dynamic target of a particular
260 (To make the programmer's life easier, each
262 call should employ a unique
266 Adding further difficulty, the
270 calls may not even be in the same source code module.
272 In summary, nonlocal gotos can make programs harder to understand
273 and maintain, and an alternative should be used if possible.
275 .SS Undefined Behavior
276 If the function which called
280 is called, the behavior is undefined.
281 Some kind of subtle or unsubtle chaos is sure to result.
283 If, in a multithreaded program, a
287 buffer that was initialized by a call to
289 in a different thread, the behavior is undefined.
291 .\" The following statement appeared in versions up to POSIX.1-2008 TC1,
292 .\" but is set to be removed in POSIX.1-2008 TC2:
294 .\" According to POSIX.1, if a
296 .\" call is performed from a nested signal handler
297 .\" (i.e., from a handler that was invoked in response to a signal that was
298 .\" generated while another signal was already in the process of being
299 .\" handled), the behavior is undefined.
301 POSIX.1-2008 Technical Corrigendum 2 adds
302 .\" http://austingroupbugs.net/view.php?id=516#c1195
306 to the list of async-signal-safe functions.
307 However, the standard recommends avoiding the use of these functions
308 from signal handlers and goes on to point out that
309 if these functions are called from a signal handler that interrupted
310 a call to a non-async-signal-safe function (or some equivalent,
311 such as the steps equivalent to
313 that occur upon a return from the initial call to
315 the behavior is undefined if the program subsequently makes a call to
316 a non-async-signal-safe function.
317 The only way of avoiding undefined behavior is to ensure one of the following:
319 After long jumping from the signal handler,
320 the program does not call any non-async-signal-safe functions
321 and does not return from the initial call to
324 Any signal whose handler performs a long jump must be blocked during
326 call to a non-async-signal-safe function and
327 no non-async-signal-safe functions are called after
328 returning from the initial call to
332 .BR signal\-safety (7)