setjmp (3)
Leading comments
Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com> %%%LICENSE_START(GPLv2+_DOC_FULL) This is free documentation; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GNU General Public License's references to "object code" and "executables" are to be interpreted as the output of any document formatting or typesetting system,...
NAME
setjmp, sigsetjmp - save stack context for nonlocal gotoSYNOPSIS
#include <setjmp.h>
int setjmp(jmp_buf env); int sigsetjmp(sigjmp_buf env, int savesigs);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
setjmp():
see NOTES.
sigsetjmp():
_POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_C_SOURCE
DESCRIPTION
setjmp() and longjmp(3) are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. setjmp() saves the stack context/environment in env for later use by longjmp(3). The stack context will be invalidated if the function which called setjmp() returns. sigsetjmp() is similar to setjmp(). If, and only if, savesigs is nonzero, the process's current signal mask is saved in env and will be restored if a siglongjmp(3) is later performed with this env.RETURN VALUE
setjmp() and sigsetjmp() return 0 if returning directly, and nonzero when returning from longjmp(3) or siglongjmp(3) using the saved context.ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).Interface | Attribute | Value |
setjmp(), sigsetjmp() | Thread safety | MT-Safe |
CONFORMING TO
setjmp(): POSIX.1-2001, POSIX.1-2008, C89, C99.sigsetjmp(): POSIX.1-2001, POSIX.1-2008.
NOTES
POSIX does not specify whether setjmp() will save the signal mask (to be later restored during longjmp(3)). In System V it will not. In 4.3BSD it will, and there is a function _setjmp that will not. On Linux with glibc versions before 2.19, setjmp() follows the System V behavior by default, but the BSD behavior is provided if the _BSD_SOURCE feature test macro is defined and none of _POSIX_SOURCE, _POSIX_C_SOURCE, _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED, _GNU_SOURCE, or _SVID_SOURCE is defined. Since glibc 2.19, <setjmp.h> exposes only the System V version of setjmp(). Programs that need the BSD semantics should replace calls to setjmp() with calls to sigsetjmp() with a nonzero savesigs argument.If you want to portably save and restore signal masks, use sigsetjmp() and siglongjmp(3). setjmp() and sigsetjmp() make programs hard to understand and maintain. If possible, an alternative should be used.