1.\" $NetBSD: setjmp.3,v 1.18 2021/05/24 23:59:59 riastradh 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.\" @(#)setjmp.3 8.1 (Berkeley) 6/4/93 35.\" 36.Dd June 1, 2008 37.Dt SETJMP 3 38.Os 39.Sh NAME 40.Nm sigsetjmp , 41.Nm siglongjmp , 42.Nm setjmp , 43.Nm longjmp , 44.Nm _setjmp , 45.Nm _longjmp , 46.Nm longjmperror 47.Nd non-local jumps 48.Sh LIBRARY 49.Lb libc 50.Sh SYNOPSIS 51.In setjmp.h 52.Ft int 53.Fn sigsetjmp "sigjmp_buf env" "int savemask" 54.Ft void 55.Fn siglongjmp "sigjmp_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 int 61.Fn _setjmp "jmp_buf env" 62.Ft void 63.Fn _longjmp "jmp_buf env" "int val" 64.Ft void 65.Fn longjmperror void 66.Sh DESCRIPTION 67The 68.Fn sigsetjmp , 69.Fn setjmp , 70and 71.Fn _setjmp 72functions save their calling environment in 73.Fa env . 74Each of these functions returns 0. 75.Pp 76The corresponding 77.Fn longjmp 78functions restore the environment saved by the most recent 79invocation of the respective 80.Fn setjmp 81function. 82They then return so that program execution continues as if the corresponding 83invocation of the 84.Fn setjmp 85call had just returned the value specified by 86.Fa val , 87instead of 0. 88The 89.Fn longjmp 90functions cannot cause 91.Fn setjmp 92to return 0; if 93.Fa val 94is 0, 95.Fn setjmp 96returns 1 instead. 97.Pp 98Pairs of calls may be intermixed, i.e., both 99.Fn sigsetjmp 100and 101.Fn siglongjmp 102as well as 103.Fn setjmp 104and 105.Fn longjmp 106combinations may be used in the same program. 107However, individual calls may not, e.g., the 108.Fa env 109argument to 110.Fn setjmp 111may not be passed to 112.Fn siglongjmp . 113.Pp 114The 115.Fn longjmp 116routines may not be called after the routine which called the 117.Fn setjmp 118routines returns. 119.Pp 120All accessible objects have values as of the time 121.Fn longjmp 122routine was called, except that the values of objects of automatic storage 123invocation duration that do not have the 124.Li volatile 125type and have been changed between the 126.Fn setjmp 127invocation and 128.Fn longjmp 129call are indeterminate. 130.Pp 131The 132.Fn setjmp Ns / Ns Fn longjmp 133function pairs save and restore the signal mask while 134.Fn _setjmp Ns / Ns Fn _longjmp 135function pairs save and restore only the register set and the stack. 136(See 137.Fn sigprocmask 2 . ) 138.Pp 139The 140.Fn sigsetjmp Ns / Ns Fn siglongjmp 141function pairs save and restore the signal mask if the argument 142.Fa savemask 143is non-zero. 144Otherwise, only the register set and the stack are saved. 145.Pp 146In other words, 147.Fn setjmp Ns / Ns Fn longjmp 148are functionally equivalent to 149.Fn sigsetjmp Ns / Ns Fn siglongjmp 150when 151.Fn sigsetjmp 152is called with a non-zero 153.Fa savemask 154argument. 155Conversely, 156.Fn _setjmp Ns / Ns Fn _longjmp 157are functionally equivalent to 158.Fn sigsetjmp Ns / Ns Fn siglongjmp 159when 160.Fn sigsetjmp 161is called with a zero-value 162.Fa savemask . 163.Pp 164The 165.Fn sigsetjmp Ns / Ns Fn siglongjmp 166interfaces are preferred for maximum portability. 167.Sh ERRORS 168If the contents of the 169.Fa env 170are corrupted or correspond to an environment that has already returned, 171the 172.Fn longjmp 173routine calls the routine 174.Xr longjmperror 3 . 175If 176.Fn longjmperror 177returns, the program is aborted (see 178.Xr abort 3 ) . 179The default version of 180.Fn longjmperror 181prints the message 182.Dq Li longjmp botch 183to standard error and returns. 184User programs wishing to exit more gracefully should write their own 185versions of 186.Fn longjmperror . 187.Sh SEE ALSO 188.Xr sigaction 2 , 189.Xr sigaltstack 2 , 190.Xr sigprocmask 2 , 191.Xr pthread_sigmask 3 , 192.Xr signal 3 193.Sh STANDARDS 194The 195.Fn setjmp 196and 197.Fn longjmp 198functions conform to 199.St -ansiC . 200The 201.Fn sigsetjmp 202and 203.Fn siglongjmp 204functions conform to 205.St -p1003.1-90 . 206.Sh CAVEATS 207Historically, on 208.At V , 209the 210.Fn setjmp Ns / Ns Fn longjmp 211functions have been equivalent to the 212.Bx 213.Fn _setjmp Ns / Ns Fn _longjmp 214functions and do not restore the signal mask. 215Because of this discrepancy, the 216.Fn sigsetjmp Ns / Ns Fn siglongjmp 217interfaces should be used if portability is desired. 218.Pp 219Use of 220.Fn longjmp 221or 222.Fn siglongjmp 223from inside a signal handler is not as easy as it might seem. 224Generally speaking, all possible code paths between the 225.Fn setjmp 226and 227.Fn longjmp 228must be signal race safe. 229Furthermore, the code paths must not do resource management 230(such as 231.Xr open 2 232or 233.Xr close 2 ) 234without blocking the signal in question, or resources might 235be mismanaged. 236Obviously this makes 237.Fn longjmp 238much less useful than previously thought. 239