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