Section: Linux Programmer's Manual (3)
Return to Main Contents
setjmp, sigsetjmp, longjmp, siglongjmp - performing a nonlocal goto
int setjmp(jmp_buf env);
int sigsetjmp(sigjmp_buf env, int savesigs);
void longjmp(jmp_buf env, int val);
void siglongjmp(sigjmp_buf env, int val);
Feature Test Macro Requirements for glibc (see
The functions described on this page are used for performing "nonlocal gotos":
transferring execution from one function to a predetermined location
in another function.
function dynamically establishes the target to which control
will later be transferred, and
performs the transfer of execution.
function saves various information about the calling environment
(typically, the stack pointer, the instruction pointer,
possibly the values of other registers and the signal mask)
in the buffer
for later use by
In this case,
function uses the information saved in
to transfer control back to the point where
was called and to restore ("rewind") the stack to its state at the time of the
In addition, and depending on the implementation (see NOTES),
the values of some other registers and the process signal mask
may be restored to their state at the time of the
Following a successful
execution continues as if
had returned for a second time.
This "fake" return can be distinguished from a true
call because the "fake" return returns the value provided in
If the programmer mistakenly passes the value 0 in
the "fake" return will instead return 1.
sigsetjmp() and siglongjmp()
also perform nonlocal gotos, but provide predictable handling of
the process signal mask.
If, and only if, the
argument provided to
is nonzero, the process's current signal mask is saved in
and will be restored if a
is later performed with this
return 0 when called directly;
on the "fake" return that occurs after
the nonzero value specified in
functions do not return.
For an explanation of the terms used in this section, see
POSIX.1-2001, POSIX.1-2008, C89, C99.
POSIX does not specify whether
will save the signal mask
(to be later restored during
In System V it will not.
In 4.3BSD it will, and there
is a function
that will not.
The behavior under Linux depends on the glibc version
and the setting of feature test macros.
On Linux with glibc versions before 2.19,
follows the System V behavior by default,
but the BSD behavior is provided if the
feature test macro is explicitly defined
and none of
Since glibc 2.19,
exposes only the System V version of
Programs that need the BSD semantics should replace calls to
with calls to
with a nonzero
can be useful for dealing with errors inside deeply nested function calls
or to allow a signal handler to pass control to
a specific point in the program,
rather than returning to the point where the handler interrupted
the main program.
In the latter case,
if you want to portably save and restore signal masks, use
See also the discussion of program readability below.
The compiler may optimize variables into registers, and
may restore the values of other registers in addition to the
stack pointer and program counter.
Consequently, the values of automatic variables are unspecified
after a call to
if they meet all the following criteria:
they are local to the function that made the corresponding
their values are changed between the calls to
they are not declared as
Analogous remarks apply for
Nonlocal gotos and program readability
While it can be abused,
the traditional C "goto" statement at least has the benefit that lexical cues
(the goto statement and the target label)
allow the programmer to easily perceive the flow of control.
Nonlocal gotos provide no such cues: multiple
calls might employ the same
variable so that the content of the variable may change
over the lifetime of the application.
Consequently, the programmer may be forced to perform detailed
reading of the code to determine the dynamic target of a particular
(To make the programmer's life easier, each
call should employ a unique
Adding further difficulty, the
calls may not even be in the same source code module.
In summary, nonlocal gotos can make programs harder to understand
and maintain, and an alternative should be used if possible.
If the function which called
is called, the behavior is undefined.
Some kind of subtle or unsubtle chaos is sure to result.
If, in a multithreaded program, a
call employs an
buffer that was initialized by a call to
in a different thread, the behavior is undefined.
POSIX.1-2008 Technical Corrigendum 2 adds
to the list of async-signal-safe functions.
However, the standard recommends avoiding the use of these functions
from signal handlers and goes on to point out that
if these functions are called from a signal handler that interrupted
a call to a non-async-signal-safe function (or some equivalent,
such as the steps equivalent to
that occur upon a return from the initial call to
the behavior is undefined if the program subsequently makes a call to
a non-async-signal-safe function.
The only way of avoiding undefined behavior is to ensure one of the following:
After long jumping from the signal handler,
the program does not call any non-async-signal-safe functions
and does not return from the initial call to
Any signal whose handler performs a long jump must be blocked during
call to a non-async-signal-safe function and
no non-async-signal-safe functions are called after
returning from the initial call to
This page is part of release 4.15 of the Linux
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
- sigsetjmp() and siglongjmp()
- RETURN VALUE
- CONFORMING TO
- Nonlocal gotos and program readability
- SEE ALSO
This document was created by
using the manual pages.
Time: 02:14:06 GMT, September 23, 2018