NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ATTRIBUTES |STANDARDS |HISTORY |NOTES |CAVEATS |SEE ALSO |COLOPHON | |
setjmp(3) Library Functions Manualsetjmp(3)setjmp, sigsetjmp, longjmp, siglongjmp - performing a nonlocal goto
Standard C library (libc,-lc)
#include <setjmp.h>int setjmp(jmp_bufenv);int sigsetjmp(sigjmp_bufenv, intsavesigs);[[noreturn]] void longjmp(jmp_bufenv, intval);[[noreturn]] void siglongjmp(sigjmp_bufenv, intval); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):setjmp(): see HISTORY.sigsetjmp(): _POSIX_C_SOURCE
The functions described on this page are used for performing "nonlocal gotos": transferring execution from one function to a predetermined location in another function. Thesetjmp() function dynamically establishes the target to which control will later be transferred, andlongjmp() performs the transfer of execution. Thesetjmp() 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 bufferenv for later use bylongjmp(). In this case,setjmp() returns 0. Thelongjmp() function uses the information saved inenv to transfer control back to the point wheresetjmp() was called and to restore ("rewind") the stack to its state at the time of thesetjmp() call. In addition, and depending on the implementation (see NOTES and HISTORY), the values of some other registers and the process signal mask may be restored to their state at the time of thesetjmp() call. Following a successfullongjmp(), execution continues as ifsetjmp() had returned for a second time. This "fake" return can be distinguished from a truesetjmp() call because the "fake" return returns the value provided inval. If the programmer mistakenly passes the value 0 inval, the "fake" return will instead return 1.sigsetjmp() and siglongjmp()sigsetjmp() andsiglongjmp() also perform nonlocal gotos, but provide predictable handling of the process signal mask. If, and only if, thesavesigs argument provided tosigsetjmp() is nonzero, the process's current signal mask is saved inenv and will be restored if asiglongjmp() is later performed with thisenv.setjmp() andsigsetjmp() return 0 when called directly; on the "fake" return that occurs afterlongjmp() orsiglongjmp(), the nonzero value specified inval is returned. Thelongjmp() orsiglongjmp() functions do not return.
For an explanation of the terms used in this section, seeattributes(7). ┌──────────────────────────────────────┬───────────────┬─────────┐ │Interface│Attribute│Value│ ├──────────────────────────────────────┼───────────────┼─────────┤ │setjmp(),sigsetjmp() │ Thread safety │ MT-Safe │ ├──────────────────────────────────────┼───────────────┼─────────┤ │longjmp(),siglongjmp() │ Thread safety │ MT-Safe │ └──────────────────────────────────────┴───────────────┴─────────┘
setjmp()longjmp() C11, POSIX.1-2008.sigsetjmp()siglongjmp() POSIX.1-2008.
setjmp()longjmp() POSIX.1-2001, C89.sigsetjmp()siglongjmp() POSIX.1-2001. POSIX does not specify whethersetjmp() will save the signal mask (to be later restored duringlongjmp()). In System V it will not. In 4.3BSD it will, and there is a function_setjmp() that will not. The behavior under Linux depends on the glibc version and the setting of feature test macros. Before glibc 2.19,setjmp() follows the System V behavior by default, but the BSD behavior is provided if the_BSD_SOURCEfeature test macro is explicitly defined and none of_POSIX_SOURCE,_POSIX_C_SOURCE,_XOPEN_SOURCE,_GNU_SOURCE, or_SVID_SOURCEis defined. Since glibc 2.19,<setjmp.h> exposes only the System V version ofsetjmp(). Programs that need the BSD semantics should replace calls tosetjmp() with calls tosigsetjmp() with a nonzerosavesigs argument.
setjmp() andlongjmp() 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, usesigsetjmp() andsiglongjmp(). See also the discussion of program readability below.
The compiler may optimize variables into registers, andlongjmp() 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 tolongjmp() if they meet all the following criteria: • they are local to the function that made the correspondingsetjmp() call; • their values are changed between the calls tosetjmp() andlongjmp(); and • they are not declared asvolatile. Analogous remarks apply forsiglongjmp().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: multiplesetjmp() calls might employ the samejmp_buf 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 particularlongjmp() call. (To make the programmer's life easier, eachsetjmp() call should employ a uniquejmp_buf variable.) Adding further difficulty, thesetjmp() andlongjmp() 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.Undefined behavior If the function which calledsetjmp() returns beforelongjmp() is called, the behavior is undefined. Some kind of subtle or unsubtle chaos is sure to result. If, in a multithreaded program, alongjmp() call employs anenv buffer that was initialized by a call tosetjmp() in a different thread, the behavior is undefined. POSIX.1-2008 Technical Corrigendum 2 addslongjmp() andsiglongjmp() 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 toexit(3) that occur upon a return from the initial call tomain()), 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 tomain(). • Any signal whose handler performs a long jump must be blocked duringevery call to a non-async-signal-safe function and no non-async-signal-safe functions are called after returning from the initial call tomain().
signal(7),signal-safety(7)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17setjmp(3)Pages that refer to this page:sigaltstack(2), abort(3), alloca(3), atexit(3), exit(3), ftw(3), getcontext(3), libexpect(3), makecontext(3), pthread_cleanup_push(3), sleep(3), signal(7), signal-safety(7), ld.so(8)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |