Debian Codegraph @

upstream/debian-unstable/man3/setjmp.3

raw
1 '\" t
2 .\" Copyright 2016, Michael Kerrisk <[email protected]>
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .\"
7 .TH setjmp 3 2026-02-08 "Linux man-pages 6.18"
8 .SH NAME
9 setjmp, sigsetjmp, longjmp, siglongjmp \- performing a nonlocal goto
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .B #include <setjmp.h>
16 .P
17 .BI "int setjmp(jmp_buf " env );
18 .BI "int sigsetjmp(sigjmp_buf " env ", int " savesigs );
19 .P
20 .BI "[[noreturn]] void longjmp(jmp_buf " env ", int " val );
21 .BI "[[noreturn]] void siglongjmp(sigjmp_buf " env ", int " val );
22 .fi
23 .P
24 .RS -4
25 Feature Test Macro Requirements for glibc (see
26 .BR feature_test_macros (7)):
27 .RE
28 .P
29 .BR setjmp ():
30 see HISTORY.
31 .P
32 .BR sigsetjmp ():
33 .nf
34 _POSIX_C_SOURCE
35 .fi
36 .SH DESCRIPTION
37 The functions described on this page are used for performing "nonlocal gotos":
38 transferring execution from one function to a predetermined location
39 in another function.
40 The
41 .BR setjmp ()
42 function dynamically establishes the target to which control
43 will later be transferred, and
44 .BR longjmp ()
45 performs the transfer of execution.
46 .P
47 The
48 .BR setjmp ()
49 function saves various information about the calling environment
50 (typically, the stack pointer, the instruction pointer,
51 possibly the values of other registers and the signal mask)
52 in the buffer
53 .I env
54 for later use by
55 .BR longjmp ().
56 In this case,
57 .BR setjmp ()
58 returns 0.
59 .P
60 The
61 .BR longjmp ()
62 function uses the information saved in
63 .I env
64 to transfer control back to the point where
65 .BR setjmp ()
66 was called and to restore ("rewind") the stack to its state at the time of the
67 .BR setjmp ()
68 call.
69 In addition, and depending on the implementation (see NOTES and HISTORY),
70 the values of some other registers and the process signal mask
71 may be restored to their state at the time of the
72 .BR setjmp ()
73 call.
74 .P
75 Following a successful
76 .BR longjmp (),
77 execution continues as if
78 .BR setjmp ()
79 had returned for a second time.
80 This "fake" return can be distinguished from a true
81 .BR setjmp ()
82 call because the "fake" return returns the value provided in
83 .IR val .
84 If the programmer mistakenly passes the value 0 in
85 .IR val ,
86 the "fake" return will instead return 1.
87 .SS sigsetjmp() and siglongjmp()
88 .BR sigsetjmp ()
89 and
90 .BR siglongjmp ()
91 also perform nonlocal gotos, but provide predictable handling of
92 the process signal mask.
93 .P
94 If, and only if, the
95 .I savesigs
96 argument provided to
97 .BR sigsetjmp ()
98 is nonzero, the process's current signal mask is saved in
99 .I env
100 and will be restored if a
101 .BR siglongjmp ()
102 is later performed with this
103 .IR env .
104 .SH RETURN VALUE
105 .BR setjmp ()
106 and
107 .BR sigsetjmp ()
108 return 0 when called directly;
109 on the "fake" return that occurs after
110 .BR longjmp ()
111 or
112 .BR siglongjmp (),
113 the nonzero value specified in
114 .I val
115 is returned.
116 .P
117 The
118 .BR longjmp ()
119 or
120 .BR siglongjmp ()
121 functions do not return.
122 .SH ATTRIBUTES
123 For an explanation of the terms used in this section, see
124 .BR attributes (7).
125 .TS
126 allbox;
127 lbx lb lb
128 l l l.
129 Interface Attribute Value
130 T{
131 .na
132 .nh
133 .BR setjmp (),
134 .BR sigsetjmp ()
135 T} Thread safety MT-Safe
136 T{
137 .na
138 .nh
139 .BR longjmp (),
140 .BR siglongjmp ()
141 T} Thread safety MT-Safe
142 .TE
143 .SH STANDARDS
144 .TP
145 .BR setjmp ()
146 .TQ
147 .BR longjmp ()
148 C11, POSIX.1-2008.
149 .TP
150 .BR sigsetjmp ()
151 .TQ
152 .BR siglongjmp ()
153 POSIX.1-2008.
154 .SH HISTORY
155 .TP
156 .BR setjmp ()
157 .TQ
158 .BR longjmp ()
159 POSIX.1-2001, C89.
160 .TP
161 .BR sigsetjmp ()
162 .TQ
163 .BR siglongjmp ()
164 POSIX.1-2001.
165 .P
166 POSIX does not specify whether
167 .BR setjmp ()
168 will save the signal mask
169 (to be later restored during
170 .BR longjmp ()).
171 In System V it will not.
172 In 4.3BSD it will, and there
173 is a function
174 .BR _setjmp ()
175 that will not.
176 The behavior under Linux depends on the glibc version
177 and the setting of feature test macros.
178 Before glibc 2.19,
179 .BR setjmp ()
180 follows the System V behavior by default,
181 but the BSD behavior is provided if the
182 .B _BSD_SOURCE
183 feature test macro is explicitly defined
184 .\" so that _FAVOR_BSD is triggered
185 and none of
186 .BR _POSIX_SOURCE ,
187 .BR _POSIX_C_SOURCE ,
188 .BR _XOPEN_SOURCE ,
189 .\" .BR _XOPEN_SOURCE_EXTENDED ,
190 .BR _GNU_SOURCE ,
191 or
192 .B _SVID_SOURCE
193 is defined.
194 Since glibc 2.19,
195 .I <setjmp.h>
196 exposes only the System V version of
197 .BR setjmp ().
198 Programs that need the BSD semantics should replace calls to
199 .BR setjmp ()
200 with calls to
201 .BR sigsetjmp ()
202 with a nonzero
203 .I savesigs
204 argument.
205 .SH NOTES
206 .BR setjmp ()
207 and
208 .BR longjmp ()
209 can be useful for dealing with errors inside deeply nested function calls
210 or to allow a signal handler to pass control to
211 a specific point in the program,
212 rather than returning to the point where the handler interrupted
213 the main program.
214 In the latter case,
215 if you want to portably save and restore signal masks, use
216 .BR sigsetjmp ()
217 and
218 .BR siglongjmp ().
219 See also the discussion of program readability below.
220 .SH CAVEATS
221 The compiler may optimize variables into registers, and
222 .BR longjmp ()
223 may restore the values of other registers in addition to the
224 stack pointer and program counter.
225 Consequently, the values of automatic variables are unspecified
226 after a call to
227 .BR longjmp ()
228 if they meet all the following criteria:
229 .IP \[bu] 3
230 they are local to the function that made the corresponding
231 .BR setjmp ()
232 call;
233 .IP \[bu]
234 their values are changed between the calls to
235 .BR setjmp ()
236 and
237 .BR longjmp ();
238 and
239 .IP \[bu]
240 they are not declared as
241 .IR volatile .
242 .P
243 Analogous remarks apply for
244 .BR siglongjmp ().
245 .\"
246 .SS Nonlocal gotos and program readability
247 While it can be abused,
248 the traditional C "goto" statement at least has the benefit that lexical cues
249 (the goto statement and the target label)
250 allow the programmer to easily perceive the flow of control.
251 Nonlocal gotos provide no such cues: multiple
252 .BR setjmp ()
253 calls might employ the same
254 .I jmp_buf
255 variable so that the content of the variable may change
256 over the lifetime of the application.
257 Consequently, the programmer may be forced to perform detailed
258 reading of the code to determine the dynamic target of a particular
259 .BR longjmp ()
260 call.
261 (To make the programmer's life easier, each
262 .BR setjmp ()
263 call should employ a unique
264 .I jmp_buf
265 variable.)
266 .P
267 Adding further difficulty, the
268 .BR setjmp ()
269 and
270 .BR longjmp ()
271 calls may not even be in the same source code module.
272 .P
273 In summary, nonlocal gotos can make programs harder to understand
274 and maintain, and an alternative should be used if possible.
275 .\"
276 .SS Undefined behavior
277 If the function which called
278 .BR setjmp ()
279 returns before
280 .BR longjmp ()
281 is called, the behavior is undefined.
282 Some kind of subtle or unsubtle chaos is sure to result.
283 .P
284 If, in a multithreaded program, a
285 .BR longjmp ()
286 call employs an
287 .I env
288 buffer that was initialized by a call to
289 .BR setjmp ()
290 in a different thread, the behavior is undefined.
291 .\"
292 .\" The following statement appeared in versions up to POSIX.1-2008 TC1,
293 .\" but is set to be removed in POSIX.1-2008 TC2:
294 .\"
295 .\" According to POSIX.1, if a
296 .\" .BR longjmp ()
297 .\" call is performed from a nested signal handler
298 .\" (i.e., from a handler that was invoked in response to a signal that was
299 .\" generated while another signal was already in the process of being
300 .\" handled), the behavior is undefined.
301 .P
302 POSIX.1-2008 Technical Corrigendum 2 adds
303 .\" http://austingroupbugs.net/view.php?id=516#c1195
304 .BR longjmp ()
305 and
306 .BR siglongjmp ()
307 to the list of async-signal-safe functions.
308 However, the standard recommends avoiding the use of these functions
309 from signal handlers and goes on to point out that
310 if these functions are called from a signal handler that interrupted
311 a call to a non-async-signal-safe function (or some equivalent,
312 such as the steps equivalent to
313 .BR exit (3)
314 that occur upon a return from the initial call to
315 .IR main ()),
316 the behavior is undefined if the program subsequently makes a call to
317 a non-async-signal-safe function.
318 The only way of avoiding undefined behavior is to ensure one of the following:
319 .IP \[bu] 3
320 After long jumping from the signal handler,
321 the program does not call any non-async-signal-safe functions
322 and does not return from the initial call to
323 .IR main ().
324 .IP \[bu]
325 Any signal whose handler performs a long jump must be blocked during
326 .I every
327 call to a non-async-signal-safe function and
328 no non-async-signal-safe functions are called after
329 returning from the initial call to
330 .IR main ().
331 .SH SEE ALSO
332 .BR signal (7),
333 .BR signal\-safety (7)