1SETJMP(3) Linux Programmer's Manual SETJMP(3)
2
3
4
6 setjmp, sigsetjmp, longjmp, siglongjmp - performing a nonlocal goto
7
9 #include <setjmp.h>
10
11 int setjmp(jmp_buf env);
12 int sigsetjmp(sigjmp_buf env, int savesigs);
13
14 void longjmp(jmp_buf env, int val);
15 void siglongjmp(sigjmp_buf env, int val);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 setjmp(): see NOTES.
20
21 sigsetjmp(): _POSIX_C_SOURCE
22
24 The functions described on this page are used for performing "nonlocal
25 gotos": transferring execution from one function to a predetermined
26 location in another function. The setjmp() function dynamically estab‐
27 lishes the target to which control will later be transferred, and
28 longjmp() performs the transfer of execution.
29
30 The setjmp() function saves various information about the calling envi‐
31 ronment (typically, the stack pointer, the instruction pointer, possi‐
32 bly the values of other registers and the signal mask) in the buffer
33 env for later use by longjmp(). In this case, setjmp() returns 0.
34
35 The longjmp() function uses the information saved in env to transfer
36 control back to the point where setjmp() was called and to restore
37 ("rewind") the stack to its state at the time of the setjmp() call. In
38 addition, and depending on the implementation (see NOTES), the values
39 of some other registers and the process signal mask may be restored to
40 their state at the time of the setjmp() call.
41
42 Following a successful longjmp(), execution continues as if setjmp()
43 had returned for a second time. This "fake" return can be distin‐
44 guished from a true setjmp() call because the "fake" return returns the
45 value provided in val. If the programmer mistakenly passes the value 0
46 in val, the "fake" return will instead return 1.
47
48 sigsetjmp() and siglongjmp()
49 sigsetjmp() and siglongjmp() also perform nonlocal gotos, but provide
50 predictable handling of the process signal mask.
51
52 If, and only if, the savesigs argument provided to sigsetjmp() is
53 nonzero, the process's current signal mask is saved in env and will be
54 restored if a siglongjmp() is later performed with this env.
55
57 setjmp() and sigsetjmp() return 0 when called directly; on the "fake"
58 return that occurs after longjmp() or siglongjmp(), the nonzero value
59 specified in val is returned.
60
61 The longjmp() or siglongjmp() functions do not return.
62
64 For an explanation of the terms used in this section, see
65 attributes(7).
66
67 ┌────────────────────────┬───────────────┬─────────┐
68 │Interface │ Attribute │ Value │
69 ├────────────────────────┼───────────────┼─────────┤
70 │setjmp(), sigsetjmp() │ Thread safety │ MT-Safe │
71 ├────────────────────────┼───────────────┼─────────┤
72 │longjmp(), siglongjmp() │ Thread safety │ MT-Safe │
73 └────────────────────────┴───────────────┴─────────┘
75 setjmp(), longjmp(): POSIX.1-2001, POSIX.1-2008, C89, C99.
76
77 sigsetjmp(), siglongjmp(): POSIX.1-2001, POSIX.1-2008.
78
80 POSIX does not specify whether setjmp() will save the signal mask (to
81 be later restored during longjmp()). In System V it will not. In
82 4.3BSD it will, and there is a function _setjmp() that will not. The
83 behavior under Linux depends on the glibc version and the setting of
84 feature test macros. On Linux with glibc versions before 2.19,
85 setjmp() follows the System V behavior by default, but the BSD behavior
86 is provided if the _BSD_SOURCE feature test macro is explicitly defined
87 and none of _POSIX_SOURCE, _POSIX_C_SOURCE, _XOPEN_SOURCE, _GNU_SOURCE,
88 or _SVID_SOURCE is defined. Since glibc 2.19, <setjmp.h> exposes only
89 the System V version of setjmp(). Programs that need the BSD semantics
90 should replace calls to setjmp() with calls to sigsetjmp() with a
91 nonzero savesigs argument.
92
93 setjmp() and longjmp() can be useful for dealing with errors inside
94 deeply nested function calls or to allow a signal handler to pass con‐
95 trol to a specific point in the program, rather than returning to the
96 point where the handler interrupted the main program. In the latter
97 case, if you want to portably save and restore signal masks, use
98 sigsetjmp() and siglongjmp(). See also the discussion of program read‐
99 ability below.
100
101 The compiler may optimize variables into registers, and longjmp() may
102 restore the values of other registers in addition to the stack pointer
103 and program counter. Consequently, the values of automatic variables
104 are unspecified after a call to longjmp() if they meet all the follow‐
105 ing criteria:
106
107 · they are local to the function that made the corresponding setjmp()
108 call;
109
110 · their values are changed between the calls to setjmp() and
111 longjmp(); and
112
113 · they are not declared as volatile.
114
115 Analogous remarks apply for siglongjmp().
116
117 Nonlocal gotos and program readability
118 While it can be abused, the traditional C "goto" statement at least has
119 the benefit that lexical cues (the goto statement and the target label)
120 allow the programmer to easily perceive the flow of control. Nonlocal
121 gotos provide no such cues: multiple setjmp() calls might employ the
122 same jmp_buf variable so that the content of the variable may change
123 over the lifetime of the application. Consequently, the programmer may
124 be forced to perform detailed reading of the code to determine the
125 dynamic target of a particular longjmp() call. (To make the program‐
126 mer's life easier, each setjmp() call should employ a unique jmp_buf
127 variable.)
128
129 Adding further difficulty, the setjmp() and longjmp() calls may not
130 even be in the same source code module.
131
132 In summary, nonlocal gotos can make programs harder to understand and
133 maintain, and an alternative should be used if possible.
134
135 Caveats
136 If the function which called setjmp() returns before longjmp() is
137 called, the behavior is undefined. Some kind of subtle or unsubtle
138 chaos is sure to result.
139
140 If, in a multithreaded program, a longjmp() call employs an env buffer
141 that was initialized by a call to setjmp() in a different thread, the
142 behavior is undefined.
143
144 POSIX.1-2008 Technical Corrigendum 2 adds longjmp() and siglongjmp() to
145 the list of async-signal-safe functions. However, the standard recom‐
146 mends avoiding the use of these functions from signal handlers and goes
147 on to point out that if these functions are called from a signal han‐
148 dler that interrupted a call to a non-async-signal-safe function (or
149 some equivalent, such as the steps equivalent to exit(3) that occur
150 upon a return from the initial call to main()), the behavior is unde‐
151 fined if the program subsequently makes a call to a non-async-signal-
152 safe function. The only way of avoiding undefined behavior is to
153 ensure one of the following:
154
155 * After long jumping from the signal handler, the program does not
156 call any non-async-signal-safe functions and does not return from
157 the initial call to main().
158
159 * Any signal whose handler performs a long jump must be blocked during
160 every call to a non-async-signal-safe function and no non-async-sig‐
161 nal-safe functions are called after returning from the initial call
162 to main().
163
165 signal(7), signal-safety(7)
166
168 This page is part of release 4.16 of the Linux man-pages project. A
169 description of the project, information about reporting bugs, and the
170 latest version of this page, can be found at
171 https://www.kernel.org/doc/man-pages/.
172
173
174
175 2017-03-13 SETJMP(3)