CONTRIBUTING.d/patches: Please provide a git-range-diff(1)
[man-pages.git] / man3 / setjmp.3
blob2319feb472c191e5708fa1a6c116c437d7410878
1 '\" t
2 .\" Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .\"
6 .TH setjmp 3 (date) "Linux man-pages (unreleased)"
7 .SH NAME
8 setjmp, sigsetjmp, longjmp, siglongjmp  \- performing a nonlocal goto
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ", " \-lc )
12 .SH SYNOPSIS
13 .nf
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 );
21 .fi
23 .RS -4
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
26 .RE
28 .BR setjmp ():
29 see NOTES.
31 .BR sigsetjmp ():
32 .nf
33     _POSIX_C_SOURCE
34 .fi
35 .SH DESCRIPTION
36 The functions described on this page are used for performing "nonlocal gotos":
37 transferring execution from one function to a predetermined location
38 in another function.
39 The
40 .BR setjmp ()
41 function dynamically establishes the target to which control
42 will later be transferred, and
43 .BR longjmp ()
44 performs the transfer of execution.
46 The
47 .BR setjmp ()
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)
51 in the buffer
52 .I env
53 for later use by
54 .BR longjmp ().
55 In this case,
56 .BR setjmp ()
57 returns 0.
59 The
60 .BR longjmp ()
61 function uses the information saved in
62 .I env
63 to transfer control back to the point where
64 .BR setjmp ()
65 was called and to restore ("rewind") the stack to its state at the time of the
66 .BR setjmp ()
67 call.
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
71 .BR setjmp ()
72 call.
74 Following a successful
75 .BR longjmp (),
76 execution continues as if
77 .BR setjmp ()
78 had returned for a second time.
79 This "fake" return can be distinguished from a true
80 .BR setjmp ()
81 call because the "fake" return returns the value provided in
82 .IR val .
83 If the programmer mistakenly passes the value 0 in
84 .IR val ,
85 the "fake" return will instead return 1.
86 .SS sigsetjmp() and siglongjmp()
87 .BR sigsetjmp ()
88 and
89 .BR siglongjmp ()
90 also perform nonlocal gotos, but provide predictable handling of
91 the process signal mask.
93 If, and only if, the
94 .I savesigs
95 argument provided to
96 .BR sigsetjmp ()
97 is nonzero, the process's current signal mask is saved in
98 .I env
99 and will be restored if a
100 .BR siglongjmp ()
101 is later performed with this
102 .IR env .
103 .SH RETURN VALUE
104 .BR setjmp ()
106 .BR sigsetjmp ()
107 return 0 when called directly;
108 on the "fake" return that occurs after
109 .BR longjmp ()
111 .BR siglongjmp (),
112 the nonzero value specified in
113 .I val
114 is returned.
117 .BR longjmp ()
119 .BR siglongjmp ()
120 functions do not return.
121 .SH ATTRIBUTES
122 For an explanation of the terms used in this section, see
123 .BR attributes (7).
125 allbox;
126 lbx lb lb
127 l l l.
128 Interface       Attribute       Value
132 .BR setjmp (),
133 .BR sigsetjmp ()
134 T}      Thread safety   MT-Safe
138 .BR longjmp (),
139 .BR siglongjmp ()
140 T}      Thread safety   MT-Safe
142 .SH STANDARDS
144 .BR setjmp ()
146 .BR longjmp ()
147 C11, POSIX.1-2008.
149 .BR sigsetjmp ()
151 .BR siglongjmp ()
152 POSIX.1-2008.
153 .SH HISTORY
155 .BR setjmp ()
157 .BR longjmp ()
158 POSIX.1-2001, C89.
160 .BR sigsetjmp ()
162 .BR siglongjmp ()
163 POSIX.1-2001.
165 POSIX does not specify whether
166 .BR setjmp ()
167 will save the signal mask
168 (to be later restored during
169 .BR longjmp ()).
170 In System V it will not.
171 In 4.3BSD it will, and there
172 is a function
173 .BR _setjmp ()
174 that will not.
175 The behavior under Linux depends on the glibc version
176 and the setting of feature test macros.
177 Before glibc 2.19,
178 .BR setjmp ()
179 follows the System V behavior by default,
180 but the BSD behavior is provided if the
181 .B _BSD_SOURCE
182 feature test macro is explicitly defined
183 .\" so that _FAVOR_BSD is triggered
184 and none of
185 .BR _POSIX_SOURCE ,
186 .BR _POSIX_C_SOURCE ,
187 .BR _XOPEN_SOURCE ,
188 .\" .BR _XOPEN_SOURCE_EXTENDED ,
189 .BR _GNU_SOURCE ,
191 .B _SVID_SOURCE
192 is defined.
193 Since glibc 2.19,
194 .I <setjmp.h>
195 exposes only the System V version of
196 .BR setjmp ().
197 Programs that need the BSD semantics should replace calls to
198 .BR setjmp ()
199 with calls to
200 .BR sigsetjmp ()
201 with a nonzero
202 .I savesigs
203 argument.
204 .SH NOTES
205 .BR setjmp ()
207 .BR longjmp ()
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
212 the main program.
213 In the latter case,
214 if you want to portably save and restore signal masks, use
215 .BR sigsetjmp ()
217 .BR siglongjmp ().
218 See also the discussion of program readability below.
219 .SH CAVEATS
220 The compiler may optimize variables into registers, and
221 .BR longjmp ()
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
225 after a call to
226 .BR longjmp ()
227 if they meet all the following criteria:
228 .IP \[bu] 3
229 they are local to the function that made the corresponding
230 .BR setjmp ()
231 call;
232 .IP \[bu]
233 their values are changed between the calls to
234 .BR setjmp ()
236 .BR longjmp ();
238 .IP \[bu]
239 they are not declared as
240 .IR volatile .
242 Analogous remarks apply for
243 .BR siglongjmp ().
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
251 .BR setjmp ()
252 calls might employ the same
253 .I jmp_buf
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
258 .BR longjmp ()
259 call.
260 (To make the programmer's life easier, each
261 .BR setjmp ()
262 call should employ a unique
263 .I jmp_buf
264 variable.)
266 Adding further difficulty, the
267 .BR setjmp ()
269 .BR longjmp ()
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
277 .BR setjmp ()
278 returns before
279 .BR longjmp ()
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
284 .BR longjmp ()
285 call employs an
286 .I env
287 buffer that was initialized by a call to
288 .BR setjmp ()
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
295 .\"     .BR longjmp ()
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
303 .BR longjmp ()
305 .BR siglongjmp ()
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
312 .BR exit (3)
313 that occur upon a return from the initial call to
314 .IR main ()),
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:
318 .IP \[bu] 3
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
322 .IR main ().
323 .IP \[bu]
324 Any signal whose handler performs a long jump must be blocked during
325 .I every
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
329 .IR main ().
330 .SH SEE ALSO
331 .BR signal (7),
332 .BR signal\-safety (7)