setjmp, sigsetjmp, longjmp, siglongjmp - performing a nonlocal goto
#include <setjmp.h>
int setjmp(jmp_buf env);
int sigsetjmp(sigjmp_buf env, int savesigs);
[[noreturn]] void longjmp(jmp_buf env, int val);
[[noreturn]] void siglongjmp(sigjmp_buf env, int val);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
The functions described on this page are used for performing "nonlocal gotos": transferring execution from one function to a predetermined location in another function. The setjmp() function dynamically establishes the target to which control will later be transferred, and longjmp() performs the transfer of execution.
The setjmp() 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 env
for later use by
longjmp(). In this case, setjmp()
returns 0.
The longjmp() function uses the information saved in
env
to transfer control back to the point where
setjmp() was called and to restore ("rewind") the stack
to its state at the time of the setjmp() call. 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 setjmp() call.
Following a successful longjmp(), execution
continues as if setjmp() had returned for a second
time. This "fake" return can be distinguished from a true
setjmp() call because the "fake" return returns the
value provided in val
. If the programmer mistakenly passes the
value 0 in val
, 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 savesigs
argument provided to
sigsetjmp() is nonzero, the process's current signal
mask is saved in env
and will be restored if a
siglongjmp() is later performed with this
env
.
setjmp() and sigsetjmp() return 0
when called directly; on the "fake" return that occurs after
longjmp() or siglongjmp(), the nonzero
value specified in val
is returned.
The longjmp() or siglongjmp() functions do not return.
signal(7), signal-safety(7)