1SETJMP(3)                  Linux Programmer's Manual                 SETJMP(3)
2
3
4

NAME

6       setjmp, sigsetjmp, longjmp, siglongjmp  - performing a nonlocal goto
7

SYNOPSIS

9       #include <setjmp.h>
10
11       int setjmp(jmp_buf env);
12       int sigsetjmp(sigjmp_buf env, int savesigs);
13
14       noreturn void longjmp(jmp_buf env, int val);
15       noreturn 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():
22           _POSIX_C_SOURCE
23

DESCRIPTION

25       The  functions described on this page are used for performing "nonlocal
26       gotos": transferring execution from one function to a predetermined lo‐
27       cation  in  another function.  The setjmp() function dynamically estab‐
28       lishes the target to which  control  will  later  be  transferred,  and
29       longjmp() performs the transfer of execution.
30
31       The setjmp() function saves various information about the calling envi‐
32       ronment (typically, the stack pointer, the instruction pointer,  possi‐
33       bly  the  values  of other registers and the signal mask) in the buffer
34       env for later use by longjmp().  In this case, setjmp() returns 0.
35
36       The longjmp() function uses the information saved in  env  to  transfer
37       control  back  to  the  point  where setjmp() was called and to restore
38       ("rewind") the stack to its state at the time of the setjmp() call.  In
39       addition,  and  depending on the implementation (see NOTES), the values
40       of some other registers and the process signal mask may be restored  to
41       their state at the time of the setjmp() call.
42
43       Following  a  successful  longjmp(), execution continues as if setjmp()
44       had returned for a second time.  This  "fake"  return  can  be  distin‐
45       guished from a true setjmp() call because the "fake" return returns the
46       value provided in val.  If the programmer mistakenly passes the value 0
47       in val, the "fake" return will instead return 1.
48
49   sigsetjmp() and siglongjmp()
50       sigsetjmp()  and  siglongjmp() also perform nonlocal gotos, but provide
51       predictable handling of the process signal mask.
52
53       If, and only if, the savesigs argument provided to sigsetjmp() is  non‐
54       zero, the process's current signal mask is saved in env and will be re‐
55       stored if a siglongjmp() is later performed with this env.
56

RETURN VALUE

58       setjmp() and sigsetjmp() return 0 when called directly; on  the  "fake"
59       return  that  occurs after longjmp() or siglongjmp(), the nonzero value
60       specified in val is returned.
61
62       The longjmp() or siglongjmp() functions do not return.
63

ATTRIBUTES

65       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
66       tributes(7).
67
68       ┌────────────────────────────────────────────┬───────────────┬─────────┐
69Interface                                   Attribute     Value   
70       ├────────────────────────────────────────────┼───────────────┼─────────┤
71setjmp(), sigsetjmp()                       │ Thread safety │ MT-Safe │
72       ├────────────────────────────────────────────┼───────────────┼─────────┤
73longjmp(), siglongjmp()                     │ Thread safety │ MT-Safe │
74       └────────────────────────────────────────────┴───────────────┴─────────┘
75

CONFORMING TO

77       setjmp(), longjmp(): POSIX.1-2001, POSIX.1-2008, C89, C99.
78
79       sigsetjmp(), siglongjmp(): POSIX.1-2001, POSIX.1-2008.
80

NOTES

82       POSIX  does  not specify whether setjmp() will save the signal mask (to
83       be later restored during longjmp()).  In System  V  it  will  not.   In
84       4.3BSD  it  will, and there is a function _setjmp() that will not.  The
85       behavior under Linux depends on the glibc version and  the  setting  of
86       feature  test  macros.   On  Linux  with  glibc  versions  before 2.19,
87       setjmp() follows the System V behavior by default, but the BSD behavior
88       is provided if the _BSD_SOURCE feature test macro is explicitly defined
89       and none of _POSIX_SOURCE, _POSIX_C_SOURCE, _XOPEN_SOURCE, _GNU_SOURCE,
90       or  _SVID_SOURCE is defined.  Since glibc 2.19, <setjmp.h> exposes only
91       the System V version of setjmp().  Programs that need the BSD semantics
92       should  replace calls to setjmp() with calls to sigsetjmp() with a non‐
93       zero savesigs argument.
94
95       setjmp() and longjmp() can be useful for  dealing  with  errors  inside
96       deeply  nested function calls or to allow a signal handler to pass con‐
97       trol to a specific point in the program, rather than returning  to  the
98       point  where  the  handler interrupted the main program.  In the latter
99       case, if you want to  portably  save  and  restore  signal  masks,  use
100       sigsetjmp() and siglongjmp().  See also the discussion of program read‐
101       ability below.
102
103       The compiler may optimize variables into registers, and  longjmp()  may
104       restore  the values of other registers in addition to the stack pointer
105       and program counter.  Consequently, the values of  automatic  variables
106       are  unspecified after a call to longjmp() if they meet all the follow‐
107       ing criteria:
108
109       •  they are local to the function that made the corresponding  setjmp()
110          call;
111
112       •  their   values  are  changed  between  the  calls  to  setjmp()  and
113          longjmp(); and
114
115       •  they are not declared as volatile.
116
117       Analogous remarks apply for siglongjmp().
118
119   Nonlocal gotos and program readability
120       While it can be abused, the traditional C "goto" statement at least has
121       the benefit that lexical cues (the goto statement and the target label)
122       allow the programmer to easily perceive the flow of control.   Nonlocal
123       gotos  provide  no  such cues: multiple setjmp() calls might employ the
124       same jmp_buf variable so that the content of the  variable  may  change
125       over the lifetime of the application.  Consequently, the programmer may
126       be forced to perform detailed reading of the code to determine the  dy‐
127       namic target of a particular longjmp() call.  (To make the programmer's
128       life easier, each setjmp() call should employ a  unique  jmp_buf  vari‐
129       able.)
130
131       Adding  further  difficulty,  the  setjmp() and longjmp() calls may not
132       even be in the same source code module.
133
134       In summary, nonlocal gotos can make programs harder to  understand  and
135       maintain, and an alternative should be used if possible.
136
137   Caveats
138       If  the  function  which  called  setjmp()  returns before longjmp() is
139       called, the behavior is undefined.  Some kind  of  subtle  or  unsubtle
140       chaos is sure to result.
141
142       If,  in a multithreaded program, a longjmp() call employs an env buffer
143       that was initialized by a call to setjmp() in a different  thread,  the
144       behavior is undefined.
145
146       POSIX.1-2008 Technical Corrigendum 2 adds longjmp() and siglongjmp() to
147       the list of async-signal-safe functions.  However, the standard  recom‐
148       mends avoiding the use of these functions from signal handlers and goes
149       on to point out that if these functions are called from a  signal  han‐
150       dler  that  interrupted  a call to a non-async-signal-safe function (or
151       some equivalent, such as the steps equivalent to exit(3) that occur up‐
152       on a return from the initial call to main()), the behavior is undefined
153       if the program subsequently makes a  call  to  a  non-async-signal-safe
154       function.  The only way of avoiding undefined behavior is to ensure one
155       of the following:
156
157       *  After long jumping from the signal handler,  the  program  does  not
158          call  any  non-async-signal-safe  functions and does not return from
159          the initial call to main().
160
161       *  Any signal whose handler performs a long jump must be blocked during
162          every call to a non-async-signal-safe function and no non-async-sig‐
163          nal-safe functions are called after returning from the initial  call
164          to main().
165

SEE ALSO

167       signal(7), signal-safety(7)
168

COLOPHON

170       This  page  is  part of release 5.12 of the Linux man-pages project.  A
171       description of the project, information about reporting bugs,  and  the
172       latest     version     of     this    page,    can    be    found    at
173       https://www.kernel.org/doc/man-pages/.
174
175
176
177                                  2021-03-22                         SETJMP(3)
Impressum