9330 stack overflow when creating a deeply nested dataset
[unleashed.git] / usr / src / man / man3c / makecontext.3c
blob8b0d36a7c761288c525c3196c7f183a412c8a3e0
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) 1992, X/Open Company Limited.  All Rights Reserved.
45 .\" Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved.
46 .\"
47 .TH MAKECONTEXT 3C "Mar 8, 2004"
48 .SH NAME
49 makecontext, swapcontext \- manipulate user contexts
50 .SH SYNOPSIS
51 .LP
52 .nf
53 #include <ucontext.h>
55 \fBvoid\fR \fBmakecontext\fR(\fBucontext_t *\fR\fIucp\fR, \fBvoid (*\fR\fIfunc\fR)(), \fBint\fR \fIargc\fR...);
56 .fi
58 .LP
59 .nf
60 \fBint\fR \fBswapcontext\fR(\fBucontext_t *restrict\fR \fIoucp\fR,
61      \fBconst ucontext_t *restrict\fR \fIucp\fR);
62 .fi
64 .SH DESCRIPTION
65 .sp
66 .LP
67 The \fBmakecontext()\fR function modifies the context specified by \fIucp\fR,
68 which has been initialized using \fBgetcontext\fR(2). When this context is
69 resumed using \fBswapcontext()\fR or \fBsetcontext\fR(2), execution continues
70 by calling the function \fIfunc\fR, passing it the arguments that follow
71 \fIargc\fR in the \fBmakecontext()\fR call. The value of \fIargc\fR must match
72 the number of pointer-sized integer arguments passed to \fIfunc\fR, otherwise
73 the behavior is undefined.
74 .sp
75 .LP
76 Before a call is made to \fBmakecontext()\fR, the context being modified should
77 have a stack allocated for it. The stack is assigned to the context by
78 initializing the \fBuc_stack\fR member.
79 .sp
80 .LP
81 The \fBuc_link\fR member is used to determine the context that will be resumed
82 when the context being modified by \fBmakecontext()\fR returns.  The
83 \fBuc_link\fR member should be initialized prior to the call to
84 \fBmakecontext()\fR. If the \fBuc_link\fR member is initialized to \fINULL\fR,
85 the thread executing \fIfunc\fR will exit when \fIfunc\fR returns. See
86 \fBpthread_exit\fR(3C).
87 .sp
88 .LP
89 The \fBswapcontext()\fR function saves the current context in the context
90 structure pointed to by \fIoucp\fR and sets the context to the context
91 structure pointed to by \fIucp\fR.
92 .sp
93 .LP
94 If the \fIucp\fR or \fIoucp\fR argument points to an invalid address, the
95 behavior is undefined and \fBerrno\fR may be set to \fBEFAULT\fR.
96 .SH RETURN VALUES
97 .sp
98 .LP
99 On successful completion, \fBswapcontext()\fR returns \fB0\fR. Otherwise,
100 \fB\(mi1\fR is returned and \fBerrno\fR is set to indicate the error.
101 .SH ERRORS
104 The \fBswapcontext()\fR function will fail if:
106 .ne 2
108 \fB\fBENOMEM\fR\fR
110 .RS 10n
111 The \fIucp\fR argument does not have enough stack left to complete the
112 operation.
117 The \fBswapcontext()\fR function may fail if:
119 .ne 2
121 \fB\fBEFAULT\fR\fR
123 .RS 10n
124 The \fIucp\fR or \fIoucp\fR argument points to an invalid address.
127 .SH EXAMPLES
129 \fBExample 1 \fRAlternate execution context on a stack whose memory was
130 allocated using \fBmmap()\fR.
132 .in +2
134 #include <stdio.h>
135 #include <ucontext.h>
136 #include <sys/mman.h>
138 void
139 assign(long a, int *b)
141         *b = (int)a;
145 main(int argc, char **argv)
147         ucontext_t uc, back;
148         size_t sz = 0x10000;
149         int value = 0;
151         getcontext(&uc);
153         uc.uc_stack.ss_sp = mmap(0, sz,
154             PROT_READ | PROT_WRITE | PROT_EXEC,
155             MAP_PRIVATE | MAP_ANON, -1, 0);
156         uc.uc_stack.ss_size = sz;
157         uc.uc_stack.ss_flags = 0;
159         uc.uc_link = &back;
161         makecontext(&uc, assign, 2, 100L, &value);
162         swapcontext(&back, &uc);
164         printf("done %d\en", value);
166         return (0);
169 .in -2
171 .SH USAGE
174 These functions are useful for implementing user-level context switching
175 between multiple threads of control within a process (co-processing). More
176 effective multiple threads of control can be obtained by using native support
177 for multithreading. See \fBthreads\fR(5).
178 .SH ATTRIBUTES
181 See \fBattributes\fR(5) for descriptions of the following attributes:
186 box;
187 c | c
188 l | l .
189 ATTRIBUTE TYPE  ATTRIBUTE VALUE
191 Interface Stability     Standard
193 MT-Level        MT-Safe
196 .SH SEE ALSO
199 \fBmmap\fR(2), \fBgetcontext\fR(2), \fBsigaction\fR(2), \fBsigprocmask\fR(2),
200 \fBpthread_exit\fR(3C), \fBucontext.h\fR(3HEAD), \fBattributes\fR(5),
201 \fBstandards\fR(5), \fBthreads\fR(5)
202 .SH NOTES
205 The semantics of the \fBuc_stack\fR member of the \fBucontext_t\fR structure
206 have changed as they apply to inputs to \fBmakecontext()\fR. Prior to Solaris
207 10, the \fBss_sp\fR member of the \fBuc_stack\fR structure represented the high
208 memory address of the area reserved for the stack. The \fBss_sp\fR member now
209 represents the base (low memory address), in keeping with other uses of
210 \fBss_sp\fR.
213 This change in the meaning of \fBss_sp\fR is now the default behavior. The
214 \fB-D__MAKECONTEXT_V2_SOURCE\fR compilation flag used in Solaris 9 update
215 releases to access this behavior is obsolete.
218 Binary compatibility has been preserved with releases prior to Solaris 10.
219 Before recompiling, applications that use \fBmakecontext()\fR must be updated
220 to reflect this behavior change. The example below demonstates a typical change
221 that must be applied:
223 .in +2
225 --- example1_s9.c       Thu Oct  3 11:58:17 2002
226 +++ example1.c  Thu Jun 27 13:28:16 2002
227 @@ -27,12 +27,9 @@
228         uc.uc_stack.ss_sp = mmap(0, sz,
229             PROT_READ | PROT_WRITE | PROT_EXEC,
230             MAP_PRIVATE | MAP_ANON, -1, 0);
231 -       uc.uc_stack.ss_sp = (char *)uc.uc_stack.ss_sp + sz - 8;
232         uc.uc_stack.ss_size = sz;
233         uc.uc_stack.ss_flags = 0;
235         uc.uc_link = &back
237         makecontext(&uc, assign, 2, 100L, &value);
239 .in -2