1.\" $OpenBSD: setjmp.3,v 1.22 2007/05/31 19:19:28 jmc Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" the American National Standards Committee X3, on Information 8.\" Processing Systems. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.Dd $Mdocdate: May 31 2007 $ 35.Dt SETJMP 3 36.Os 37.Sh NAME 38.Nm sigsetjmp , 39.Nm siglongjmp , 40.Nm setjmp , 41.Nm longjmp , 42.Nm _setjmp , 43.Nm _longjmp , 44.Nm longjmperror 45.Nd non-local jumps 46.Sh SYNOPSIS 47.Fd #include <setjmp.h> 48.Ft int 49.Fn sigsetjmp "sigjmp_buf env" "int savemask" 50.Ft void 51.Fn siglongjmp "sigjmp_buf env" "int val" 52.Ft int 53.Fn setjmp "jmp_buf env" 54.Ft void 55.Fn longjmp "jmp_buf env" "int val" 56.Ft int 57.Fn _setjmp "jmp_buf env" 58.Ft void 59.Fn _longjmp "jmp_buf env" "int val" 60.Ft void 61.Fn longjmperror void 62.Sh DESCRIPTION 63The 64.Fn sigsetjmp , 65.Fn setjmp , 66and 67.Fn _setjmp 68functions save their calling environment in 69.Fa env . 70Each of these functions returns 0. 71.Pp 72The corresponding 73.Fn longjmp 74functions restore the environment saved by the most recent 75invocation of the respective 76.Fn setjmp 77function. 78They then return so that program execution continues as if the corresponding 79invocation of the 80.Fn setjmp 81call had just returned the value specified by 82.Fa val , 83instead of 0. 84The value specified by 85.Fa val 86must be non-zero; a 0 value is treated as 1 to allow the programmer 87to differentiate between a direct invocation of 88.Fn setjmp 89and a return via 90.Fn longjmp . 91.Pp 92Pairs of calls may be intermixed; i.e., both 93.Fn sigsetjmp 94and 95.Fn siglongjmp 96as well as 97.Fn setjmp 98and 99.Fn longjmp 100combinations may be used in the same program. 101However, individual calls may not \(em e.g., the 102.Fa env 103argument to 104.Fn setjmp 105may not be passed to 106.Fn siglongjmp . 107.Pp 108The 109.Fn longjmp 110routines may not be called after the routine which called the 111.Fn setjmp 112routines returns. 113.Pp 114All accessible objects have values as of the time the 115.Fn longjmp 116routine was called, except that the values of objects of automatic storage 117invocation duration that do not have the 118.Li volatile 119type and have been changed between the 120.Fn setjmp 121invocation and 122.Fn longjmp 123call are indeterminate. 124.Pp 125The 126.Fn setjmp Ns / Ns Fn longjmp 127function pairs save and restore the signal mask while the 128.Fn _setjmp Ns / Ns Fn _longjmp 129function pairs save and restore only the register set and the stack (see 130.Xr sigmask 3 ) . 131.Pp 132The 133.Fn sigsetjmp Ns / Ns Fn siglongjmp 134function pairs save and restore the signal mask if the argument 135.Fa savemask 136is non-zero. 137Otherwise, only the register set and the stack are saved. 138.Pp 139In other words, 140.Fn setjmp Ns / Ns Fn longjmp 141are functionally equivalent to 142.Fn sigsetjmp Ns / Ns Fn siglongjmp 143when 144.Fn sigsetjmp 145is called with a non-zero 146.Fa savemask 147argument. 148Conversely, 149.Fn _setjmp Ns / Ns Fn _longjmp 150are functionally equivalent to 151.Fn sigsetjmp Ns / Ns Fn siglongjmp 152when 153.Fn sigsetjmp 154is called with a zero-value 155.Fa savemask . 156.Pp 157The 158.Fn sigsetjmp Ns / Ns Fn siglongjmp 159interfaces are preferred for maximum portability. 160.Sh ERRORS 161If the contents of the 162.Fa env 163are corrupted or correspond to an environment that has already returned, 164the 165.Fn longjmp 166routine calls the routine 167.Xr longjmperror 3 . 168If 169.Fn longjmperror 170returns, the program is aborted (see 171.Xr abort 3 ) . 172The default version of 173.Fn longjmperror 174prints the message 175.Dq Li longjmp botch 176to standard error and returns. 177User programs wishing to exit more gracefully should write their own 178versions of 179.Fn longjmperror . 180.Sh SEE ALSO 181.Xr sigaction 2 , 182.Xr sigaltstack 2 , 183.Xr signal 3 184.Sh STANDARDS 185The 186.Fn setjmp 187and 188.Fn longjmp 189functions conform to 190.St -ansiC . 191The 192.Fn sigsetjmp 193and 194.Fn siglongjmp 195functions conform to 196.St -p1003.1-90 . 197.Sh CAVEATS 198Historically, on 199.At V , 200the 201.Fn setjmp Ns / Ns Fn longjmp 202functions have been equivalent to the 203.Bx 204.Fn _setjmp Ns / Ns Fn _longjmp 205functions and do not restore the signal mask. 206Because of this discrepancy, the 207.Fn sigsetjmp Ns / Ns Fn siglongjmp 208interfaces should be used if portability is desired. 209.Pp 210Use of 211.Fn longjmp 212or 213.Fn siglongjmp 214from inside a signal handler is not as easy as it might seem. 215Generally speaking, all possible code paths between the 216.Fn setjmp 217and 218.Fn longjmp 219must be signal race safe, as discussed in 220.Xr signal 3 . 221Furthermore, the code paths must not do resource management 222(such as 223.Xr open 2 224or 225.Xr close 2 ) 226without blocking the signal in question, or resources might 227be mismanaged. 228Obviously this makes 229.Fn longjmp 230much less useful than previously thought. 231