nightly: remove unused BINARCHIVE
[unleashed.git] / share / man / man3c / setjmp.3c
blobf5223bac486246f1bf08319b51633329a2a1fa7f
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright (c) 1997, The Open Group. All Rights Reserved.
45 .\" Copyright (c) 2002, Sun Microsystems, Inc.  All Rights Reserved.
46 .\"
47 .TH SETJMP 3C "Aug 14, 2002"
48 .SH NAME
49 setjmp, sigsetjmp, longjmp, siglongjmp \- non-local goto
50 .SH SYNOPSIS
51 .LP
52 .nf
53 #include <setjmp.h>
55 \fBint\fR \fBsetjmp\fR(\fBjmp_buf\fR \fIenv\fR);
56 .fi
58 .LP
59 .nf
60 \fBint\fR \fBsigsetjmp\fR(\fBsigjmp_buf\fR \fIenv\fR, \fBint\fR \fIsavemask\fR);
61 .fi
63 .LP
64 .nf
65 \fBvoid\fR \fBlongjmp\fR(\fBjmp_buf\fR \fIenv\fR, \fBint\fR \fIval\fR);
66 .fi
68 .LP
69 .nf
70 \fBvoid\fR \fBsiglongjmp\fR(\fBsigjmp_buf\fR \fIenv\fR, \fBint\fR \fIval\fR);
71 .fi
73 .SH DESCRIPTION
74 .sp
75 .LP
76 These functions are useful for dealing with errors  and interrupts encountered
77 in a low-level subroutine of a program.
78 .sp
79 .LP
80 The \fBsetjmp()\fR function saves its stack environment in \fIenv\fR for later
81 use by \fBlongjmp()\fR.
82 .sp
83 .LP
84 The \fBsigsetjmp()\fR function saves the calling process's registers and stack
85 environment (see \fBsigaltstack\fR(2)) in  \fIenv\fR for later use by
86 \fBsiglongjmp()\fR. If  \fIsavemask\fR is non-zero, the calling process's
87 signal mask (see \fBsigprocmask\fR(2)) and scheduling parameters (see
88 \fBpriocntl\fR(2)) are also saved.
89 .sp
90 .LP
91 The \fBlongjmp()\fR function restores the environment saved by the last call of
92 \fBsetjmp()\fR with the corresponding \fIenv\fR argument. After \fBlongjmp()\fR
93 completes, program execution continues as if the corresponding call to
94 \fBsetjmp()\fR had just returned the value \fIval\fR. The caller of
95 \fBsetjmp()\fR must not have returned in the interim.  The \fBlongjmp()\fR
96 function cannot cause \fBsetjmp()\fR to return the value 0.  If \fBlongjmp()\fR
97 is invoked with a second argument of 0, \fBsetjmp()\fR will return 1. At the
98 time of the second return from \fBsetjmp()\fR, all external and static
99 variables have values as of the time \fBlongjmp()\fR is called (see
100 \fBEXAMPLES\fR).
103 The \fBsiglongjmp()\fR function restores the environment saved by the last call
104 of \fBsigsetjmp()\fR with the corresponding \fIenv\fR argument. After
105 \fBsiglongjmp()\fR completes, program execution continues as if the
106 corresponding call to \fBsigsetjmp()\fR had just returned the value \fIval\fR.
107 The \fBsiglongjmp()\fR function cannot cause \fBsigsetjmp()\fR to return the
108 value 0.  If \fBsiglongjmp()\fR is invoked with a second argument of 0,
109 \fBsigsetjmp()\fR will return 1. At the time of the second return from
110 \fBsigsetjmp()\fR, all external and static variables have values as of the time
111 \fBsiglongjmp()\fR was called.
114 If a signal-catching function interrupts \fBsleep\fR(3C) and calls
115 \fBsiglongjmp()\fR to restore an environment saved prior to the \fBsleep()\fR
116 call, the action associated with \fBSIGALRM\fR and time it is scheduled to be
117 generated are unspecified. It is also unspecified whether the \fBSIGALRM\fR
118 signal is blocked, unless the process's signal mask is restored as part of the
119 environment.
122 The  \fBsiglongjmp()\fR function restores the saved signal mask if and only if
123 the  \fIenv\fR argument was initialized by a call to the \fBsigsetjmp()\fR
124 function with a non-zero  \fIsavemask\fR argument.
127 The values of register and automatic variables are undefined. Register or
128 automatic variables whose value must be relied upon must be declared as
129 volatile.
130 .SH RETURN VALUES
133 If the return is from a direct invocation, \fBsetjmp()\fR and \fBsigsetjmp()\fR
134 return \fB0\fR. If the return is from a call to \fBlongjmp()\fR, \fBsetjmp()\fR
135 returns a non-zero value. If the return is from a call to \fBsiglongjmp()\fR,
136 \fBsigsetjmp()\fR returns a non-zero value.
139 After \fBlongjmp()\fR is completed, program execution continues as if the
140 corresponding invocation of \fBsetjmp()\fR had just returned the value
141 specified by \fIval\fR. The \fBlongjmp()\fR function cannot cause
142 \fBsetjmp()\fR to return 0; if \fIval\fR is 0, \fBsetjmp()\fR returns 1.
145 After \fBsiglongjmp()\fR is completed, program execution continues as if the
146 corresponding invocation of \fBsigsetjmp()\fR had just returned the value
147 specified by \fIval\fR. The \fBsiglongjmp()\fR function cannot cause
148 \fBsigsetjmp()\fR to return 0; if \fIval\fR is 0, \fBsigsetjmp()\fR returns 1.
149 .SH EXAMPLES
151 \fBExample 1 \fRExample of \fBsetjmp()\fR and \fBlongjmp()\fR functions.
154 The following example uses both \fBsetjmp()\fR and \fBlongjmp()\fR to return
155 the flow of control to the appropriate instruction block:
158 .in +2
160 #include <stdio.h>
161 #include <setjmp.h>
162 #include <signal.h>
163 #include <unistd.h>
164 jmp_buf env; static void signal_handler();
166 main(\|)  {
167         int returned_from_longjump, processing = 1;
168         unsigned int time_interval = 4;
169         if ((returned_from_longjump = setjmp(env)) != 0)
170             switch (returned_from_longjump)     {
171               case SIGINT:
172                 printf("longjumped from interrupt %d\en",SIGINT);
173                 break;
174               case SIGALRM:
175                 printf("longjumped from alarm %d\en",SIGALRM);
176                 break;
177             }
178         (void) signal(SIGINT, signal_handler);
179         (void) signal(SIGALRM, signal_handler);
180         alarm(time_interval);
181         while (processing)        {
182           printf(" waiting for you to INTERRUPT (cntrl-C) ...\en");
183           sleep(1);
184         }                /* end while forever loop */
187 static void signal_handler(sig)
188 int sig; {
189         switch (sig)     {
190           case SIGINT:  ...       /* process for interrupt */
191                         longjmp(env,sig);
192                                   /* break never reached */
193           case SIGALRM: ...       /* process for alarm */
194                         longjmp(env,sig);
195                                /* break never reached */
196           default:      exit(sig);
197         }
200 .in -2
204 When this example is compiled and executed, and the user sends an interrupt
205 signal, the output will be:
208 .in +2
210 longjumped from interrupt
212 .in -2
217 Additionally, every 4 seconds the alarm will expire, signalling this process,
218 and the output will be:
221 .in +2
223 longjumped from alarm
225 .in -2
228 .SH ATTRIBUTES
231 See \fBattributes\fR(5) for descriptions of the following attributes:
236 box;
237 c | c
238 l | l .
239 ATTRIBUTE TYPE  ATTRIBUTE VALUE
241 Interface Stability     Standard
243 MT-Level        Unsafe
246 .SH SEE ALSO
249 \fBgetcontext\fR(2), \fBpriocntl\fR(2), \fBsigaction\fR(2),
250 \fBsigaltstack\fR(2), \fBsigprocmask\fR(2), \fBsignal\fR(3C),
251 \fBattributes\fR(5), \fBstandards\fR(5)
252 .SH WARNINGS
255 If \fBlongjmp()\fR or \fBsiglongjmp()\fR are called even though \fIenv\fR was
256 never primed by a call to \fBsetjmp()\fR or \fBsigsetjmp()\fR, or when the last
257 such call was in a function that has since returned, the results are undefined.