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