man-pages/man3/setjmp.3.html
2021-03-31 01:06:50 +01:00

487 lines
10 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SETJMP</TITLE>
</HEAD><BODY>
<H1>SETJMP</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-03-13<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
setjmp, sigsetjmp, longjmp, siglongjmp - performing a nonlocal goto
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/setjmp.h">setjmp.h</A>&gt;</B>
<B>int setjmp(jmp_buf </B><I>env</I><B>);</B>
<B>int sigsetjmp(sigjmp_buf </B><I>env</I><B>, int </B><I>savesigs</I><B>);</B>
<B>void longjmp(jmp_buf </B><I>env</I><B>, int </B><I>val</I><B>);</B>
<B>void siglongjmp(sigjmp_buf </B><I>env</I><B>, int </B><I>val</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>setjmp</B>():
see NOTES.
<P>
<B>sigsetjmp</B>():
_POSIX_C_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The functions described on this page are used for performing &quot;nonlocal gotos&quot;:
transferring execution from one function to a predetermined location
in another function.
The
<B>setjmp</B>()
function dynamically establishes the target to which control
will later be transferred, and
<B>longjmp</B>()
performs the transfer of execution.
<P>
The
<B>setjmp</B>()
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
<I>env</I>
for later use by
<B>longjmp</B>().
In this case,
<B>setjmp</B>()
returns 0.
<P>
The
<B>longjmp</B>()
function uses the information saved in
<I>env</I>
to transfer control back to the point where
<B>setjmp</B>()
was called and to restore (&quot;rewind&quot;) the stack to its state at the time of the
<B>setjmp</B>()
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
<B>setjmp</B>()
call.
<P>
Following a successful
<B>longjmp</B>(),
execution continues as if
<B>setjmp</B>()
had returned for a second time.
This &quot;fake&quot; return can be distinguished from a true
<B>setjmp</B>()
call because the &quot;fake&quot; return returns the value provided in
<I>val</I>.
If the programmer mistakenly passes the value 0 in
<I>val</I>,
the &quot;fake&quot; return will instead return 1.
<P>
<A NAME="lbAE">&nbsp;</A>
<H3>sigsetjmp() and siglongjmp()</H3>
<B>sigsetjmp</B>()
and
<B>siglongjmp</B>()
also perform nonlocal gotos, but provide predictable handling of
the process signal mask.
<P>
If, and only if, the
<I>savesigs</I>
argument provided to
<B>sigsetjmp</B>()
is nonzero, the process's current signal mask is saved in
<I>env</I>
and will be restored if a
<B>siglongjmp</B>()
is later performed with this
<I>env</I>.
<A NAME="lbAF">&nbsp;</A>
<H2>RETURN VALUE</H2>
<B>setjmp</B>()
and
<B>sigsetjmp</B>()
return 0 when called directly;
on the &quot;fake&quot; return that occurs after
<B>longjmp</B>()
or
<B>siglongjmp</B>(),
the nonzero value specified in
<I>val</I>
is returned.
<P>
The
<B>longjmp</B>()
or
<B>siglongjmp</B>()
functions do not return.
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>setjmp</B>(),
<B>sigsetjmp</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
<TR VALIGN=top><TD>
<B>longjmp</B>(),
<B>siglongjmp</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>setjmp</B>(),
<B>longjmp</B>():
POSIX.1-2001, POSIX.1-2008, C89, C99.
<P>
<B>sigsetjmp</B>(),
<B>siglongjmp</B>():
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
POSIX does not specify whether
<B>setjmp</B>()
will save the signal mask
(to be later restored during
<B>longjmp</B>()).
In System V it will not.
In 4.3BSD it will, and there
is a function
<B>_setjmp</B>()
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,
<B>setjmp</B>()
follows the System V behavior by default,
but the BSD behavior is provided if the
<B>_BSD_SOURCE</B>
feature test macro is explicitly defined
and none of
<B>_POSIX_SOURCE</B>,
<B>_POSIX_C_SOURCE</B>,
<B>_XOPEN_SOURCE</B>,
<B>_GNU_SOURCE</B>,
or
<B>_SVID_SOURCE</B>
is defined.
Since glibc 2.19,
<I>&lt;<A HREF="file:///usr/include/setjmp.h">setjmp.h</A>&gt;</I>
exposes only the System V version of
<B>setjmp</B>().
Programs that need the BSD semantics should replace calls to
<B>setjmp</B>()
with calls to
<B>sigsetjmp</B>()
with a nonzero
<I>savesigs</I>
argument.
<P>
<B>setjmp</B>()
and
<B>longjmp</B>()
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
<B>sigsetjmp</B>()
and
<B>siglongjmp</B>().
See also the discussion of program readability below.
<P>
The compiler may optimize variables into registers, and
<B>longjmp</B>()
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
<B>longjmp</B>()
if they meet all the following criteria:
<DL COMPACT>
<DT id="1">&bull;<DD>
they are local to the function that made the corresponding
<B>setjmp</B>()
call;
<DT id="2">&bull;<DD>
their values are changed between the calls to
<B>setjmp</B>()
and
<B>longjmp</B>();
and
<DT id="3">&bull;<DD>
they are not declared as
<I>volatile</I>.
</DL>
<P>
Analogous remarks apply for
<B>siglongjmp</B>().
<A NAME="lbAJ">&nbsp;</A>
<H3>Nonlocal gotos and program readability</H3>
While it can be abused,
the traditional C &quot;goto&quot; 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
<B>setjmp</B>()
calls might employ the same
<I>jmp_buf</I>
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
<B>longjmp</B>()
call.
(To make the programmer's life easier, each
<B>setjmp</B>()
call should employ a unique
<I>jmp_buf</I>
variable.)
<P>
Adding further difficulty, the
<B>setjmp</B>()
and
<B>longjmp</B>()
calls may not even be in the same source code module.
<P>
In summary, nonlocal gotos can make programs harder to understand
and maintain, and an alternative should be used if possible.
<A NAME="lbAK">&nbsp;</A>
<H3>Caveats</H3>
If the function which called
<B>setjmp</B>()
returns before
<B>longjmp</B>()
is called, the behavior is undefined.
Some kind of subtle or unsubtle chaos is sure to result.
<P>
If, in a multithreaded program, a
<B>longjmp</B>()
call employs an
<I>env</I>
buffer that was initialized by a call to
<B>setjmp</B>()
in a different thread, the behavior is undefined.
<P>
POSIX.1-2008 Technical Corrigendum 2 adds
<B>longjmp</B>()
and
<B>siglongjmp</B>()
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
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
that occur upon a return from the initial call to
<I>main</I>()),
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:
<DL COMPACT>
<DT id="4">*<DD>
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
<I>main</I>().
<DT id="5">*<DD>
Any signal whose handler performs a long jump must be blocked during
<I>every</I>
call to a non-async-signal-safe function and
no non-async-signal-safe functions are called after
returning from the initial call to
<I>main</I>().
</DL>
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+signal-safety">signal-safety</A></B>(7)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="9"><A HREF="#lbAE">sigsetjmp() and siglongjmp()</A><DD>
</DL>
<DT id="10"><A HREF="#lbAF">RETURN VALUE</A><DD>
<DT id="11"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAI">NOTES</A><DD>
<DL>
<DT id="14"><A HREF="#lbAJ">Nonlocal gotos and program readability</A><DD>
<DT id="15"><A HREF="#lbAK">Caveats</A><DD>
</DL>
<DT id="16"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="17"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:56 GMT, March 31, 2021
</BODY>
</HTML>