]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/setjmp.3
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man3 / setjmp.3
1 .\" Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" SPDX-License-Identifier: GPL-2.0-or-later
4 .\"
5 .TH setjmp 3 (date) "Linux man-pages (unreleased)"
6 .SH NAME
7 setjmp, sigsetjmp, longjmp, siglongjmp \- performing a nonlocal goto
8 .SH LIBRARY
9 Standard C library
10 .RI ( libc ", " \-lc )
11 .SH SYNOPSIS
12 .nf
13 .B #include <setjmp.h>
14 .PP
15 .BI "int setjmp(jmp_buf " env );
16 .BI "int sigsetjmp(sigjmp_buf " env ", int " savesigs );
17 .PP
18 .BI "noreturn void longjmp(jmp_buf " env ", int " val );
19 .BI "noreturn void siglongjmp(sigjmp_buf " env ", int " val );
20 .fi
21 .PP
22 .RS -4
23 Feature Test Macro Requirements for glibc (see
24 .BR feature_test_macros (7)):
25 .RE
26 .PP
27 .BR setjmp ():
28 see NOTES.
29 .PP
30 .BR sigsetjmp ():
31 .nf
32 _POSIX_C_SOURCE
33 .fi
34 .SH DESCRIPTION
35 The functions described on this page are used for performing "nonlocal gotos":
36 transferring execution from one function to a predetermined location
37 in another function.
38 The
39 .BR setjmp ()
40 function dynamically establishes the target to which control
41 will later be transferred, and
42 .BR longjmp ()
43 performs the transfer of execution.
44 .PP
45 The
46 .BR setjmp ()
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)
50 in the buffer
51 .I env
52 for later use by
53 .BR longjmp ().
54 In this case,
55 .BR setjmp ()
56 returns 0.
57 .PP
58 The
59 .BR longjmp ()
60 function uses the information saved in
61 .I env
62 to transfer control back to the point where
63 .BR setjmp ()
64 was called and to restore ("rewind") the stack to its state at the time of the
65 .BR setjmp ()
66 call.
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
70 .BR setjmp ()
71 call.
72 .PP
73 Following a successful
74 .BR longjmp (),
75 execution continues as if
76 .BR setjmp ()
77 had returned for a second time.
78 This "fake" return can be distinguished from a true
79 .BR setjmp ()
80 call because the "fake" return returns the value provided in
81 .IR val .
82 If the programmer mistakenly passes the value 0 in
83 .IR val ,
84 the "fake" return will instead return 1.
85 .SS sigsetjmp() and siglongjmp()
86 .BR sigsetjmp ()
87 and
88 .BR siglongjmp ()
89 also perform nonlocal gotos, but provide predictable handling of
90 the process signal mask.
91 .PP
92 If, and only if, the
93 .I savesigs
94 argument provided to
95 .BR sigsetjmp ()
96 is nonzero, the process's current signal mask is saved in
97 .I env
98 and will be restored if a
99 .BR siglongjmp ()
100 is later performed with this
101 .IR env .
102 .SH RETURN VALUE
103 .BR setjmp ()
104 and
105 .BR sigsetjmp ()
106 return 0 when called directly;
107 on the "fake" return that occurs after
108 .BR longjmp ()
109 or
110 .BR siglongjmp (),
111 the nonzero value specified in
112 .I val
113 is returned.
114 .PP
115 The
116 .BR longjmp ()
117 or
118 .BR siglongjmp ()
119 functions do not return.
120 .SH ATTRIBUTES
121 For an explanation of the terms used in this section, see
122 .BR attributes (7).
123 .ad l
124 .nh
125 .TS
126 allbox;
127 lbx lb lb
128 l l l.
129 Interface Attribute Value
130 T{
131 .BR setjmp (),
132 .BR sigsetjmp ()
133 T} Thread safety MT-Safe
134 T{
135 .BR longjmp (),
136 .BR siglongjmp ()
137 T} Thread safety MT-Safe
138 .TE
139 .hy
140 .ad
141 .sp 1
142 .SH STANDARDS
143 .BR setjmp (),
144 .BR longjmp ():
145 POSIX.1-2001, POSIX.1-2008, C89, C99.
146 .PP
147 .BR sigsetjmp (),
148 .BR siglongjmp ():
149 POSIX.1-2001, POSIX.1-2008.
150 .SH NOTES
151 POSIX does not specify whether
152 .BR setjmp ()
153 will save the signal mask
154 (to be later restored during
155 .BR longjmp ()).
156 In System V it will not.
157 In 4.3BSD it will, and there
158 is a function
159 .BR _setjmp ()
160 that will not.
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,
164 .BR setjmp ()
165 follows the System V behavior by default,
166 but the BSD behavior is provided if the
167 .B _BSD_SOURCE
168 feature test macro is explicitly defined
169 .\" so that _FAVOR_BSD is triggered
170 and none of
171 .BR _POSIX_SOURCE ,
172 .BR _POSIX_C_SOURCE ,
173 .BR _XOPEN_SOURCE ,
174 .\" .BR _XOPEN_SOURCE_EXTENDED ,
175 .BR _GNU_SOURCE ,
176 or
177 .B _SVID_SOURCE
178 is defined.
179 Since glibc 2.19,
180 .I <setjmp.h>
181 exposes only the System V version of
182 .BR setjmp ().
183 Programs that need the BSD semantics should replace calls to
184 .BR setjmp ()
185 with calls to
186 .BR sigsetjmp ()
187 with a nonzero
188 .I savesigs
189 argument.
190 .PP
191 .BR setjmp ()
192 and
193 .BR longjmp ()
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
198 the main program.
199 In the latter case,
200 if you want to portably save and restore signal masks, use
201 .BR sigsetjmp ()
202 and
203 .BR siglongjmp ().
204 See also the discussion of program readability below.
205 .PP
206 The compiler may optimize variables into registers, and
207 .BR longjmp ()
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
211 after a call to
212 .BR longjmp ()
213 if they meet all the following criteria:
214 .IP \(bu 3
215 they are local to the function that made the corresponding
216 .BR setjmp ()
217 call;
218 .IP \(bu
219 their values are changed between the calls to
220 .BR setjmp ()
221 and
222 .BR longjmp ();
223 and
224 .IP \(bu
225 they are not declared as
226 .IR volatile .
227 .PP
228 Analogous remarks apply for
229 .BR siglongjmp ().
230 .\"
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
237 .BR setjmp ()
238 calls might employ the same
239 .I jmp_buf
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
244 .BR longjmp ()
245 call.
246 (To make the programmer's life easier, each
247 .BR setjmp ()
248 call should employ a unique
249 .I jmp_buf
250 variable.)
251 .PP
252 Adding further difficulty, the
253 .BR setjmp ()
254 and
255 .BR longjmp ()
256 calls may not even be in the same source code module.
257 .PP
258 In summary, nonlocal gotos can make programs harder to understand
259 and maintain, and an alternative should be used if possible.
260 .\"
261 .SS Caveats
262 If the function which called
263 .BR setjmp ()
264 returns before
265 .BR longjmp ()
266 is called, the behavior is undefined.
267 Some kind of subtle or unsubtle chaos is sure to result.
268 .PP
269 If, in a multithreaded program, a
270 .BR longjmp ()
271 call employs an
272 .I env
273 buffer that was initialized by a call to
274 .BR setjmp ()
275 in a different thread, the behavior is undefined.
276 .\"
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:
279 .\"
280 .\" According to POSIX.1, if a
281 .\" .BR longjmp ()
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.
286 .PP
287 POSIX.1-2008 Technical Corrigendum 2 adds
288 .\" http://austingroupbugs.net/view.php?id=516#c1195
289 .BR longjmp ()
290 and
291 .BR siglongjmp ()
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
298 .BR exit (3)
299 that occur upon a return from the initial call to
300 .IR main ()),
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:
304 .IP \(bu 3
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
308 .IR main ().
309 .IP \(bu
310 Any signal whose handler performs a long jump must be blocked during
311 .I every
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
315 .IR main ().
316 .SH SEE ALSO
317 .BR signal (7),
318 .BR signal\-safety (7)