OSDN Git Service

(split) LDP: Update original to LDP v3.65
[linuxjm/LDP_man-pages.git] / original / man2 / sigaltstack.2
1 '\" t
2 .\" Copyright (c) 2001, Michael Kerrisk (mtk.manpages@gmail.com)
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" aeb, various minor fixes
27 .TH SIGALTSTACK 2 2010-09-26 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 sigaltstack \- set and/or get signal stack context
30 .SH SYNOPSIS
31 .B #include <signal.h>
32 .sp
33 .BI "int sigaltstack(const stack_t *" ss ", stack_t *" oss );
34 .sp
35 .in -4n
36 Feature Test Macro Requirements for glibc (see
37 .BR feature_test_macros (7)):
38 .in
39 .sp
40 .BR sigaltstack ():
41 .ad l
42 .RS 4
43 .PD 0
44 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
45 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
46 .br
47 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
48 .PD
49 .RE
50 .ad
51 .SH DESCRIPTION
52 .BR sigaltstack ()
53 allows a process to define a new alternate
54 signal stack and/or retrieve the state of an existing
55 alternate signal stack.
56 An alternate signal stack is used during the
57 execution of a signal handler if the establishment of that handler (see
58 .BR sigaction (2))
59 requested it.
60
61 The normal sequence of events for using an alternate signal stack
62 is the following:
63 .TP 3
64 1.
65 Allocate an area of memory to be used for the alternate
66 signal stack.
67 .TP
68 2.
69 Use
70 .BR sigaltstack ()
71 to inform the system of the existence and
72 location of the alternate signal stack.
73 .TP
74 3.
75 When establishing a signal handler using
76 .BR sigaction (2),
77 inform the system that the signal handler should be executed
78 on the alternate signal stack by
79 specifying the \fBSA_ONSTACK\fP flag.
80 .P
81 The \fIss\fP argument is used to specify a new
82 alternate signal stack, while the \fIoss\fP argument
83 is used to retrieve information about the currently
84 established signal stack.
85 If we are interested in performing just one
86 of these tasks, then the other argument can be specified as NULL.
87 Each of these arguments is a structure of the following type:
88 .sp
89 .in +4n
90 .nf
91 typedef struct {
92     void  *ss_sp;     /* Base address of stack */
93     int    ss_flags;  /* Flags */
94     size_t ss_size;   /* Number of bytes in stack */
95 } stack_t;
96 .fi
97 .in
98
99 To establish a new alternate signal stack,
100 \fIss.ss_flags\fP is set to zero, and \fIss.ss_sp\fP and
101 \fIss.ss_size\fP specify the starting address and size of
102 the stack.
103 The constant \fBSIGSTKSZ\fP is defined to be large enough
104 to cover the usual size requirements for an alternate signal stack,
105 and the constant \fBMINSIGSTKSZ\fP defines the minimum
106 size required to execute a signal handler.
107
108 When a signal handler is invoked on the alternate stack,
109 the kernel automatically aligns the address given in \fIss.ss_sp\fP
110 to a suitable address boundary for the underlying hardware architecture.
111
112 To disable an existing stack, specify \fIss.ss_flags\fP
113 as \fBSS_DISABLE\fP.
114 In this case, the remaining fields
115 in \fIss\fP are ignored.
116
117 If \fIoss\fP is not NULL, then it is used to return information about
118 the alternate signal stack which was in effect prior to the
119 call to
120 .BR sigaltstack ().
121 The \fIoss.ss_sp\fP and \fIoss.ss_size\fP fields return the starting
122 address and size of that stack.
123 The \fIoss.ss_flags\fP may return either of the following values:
124 .TP
125 .B SS_ONSTACK
126 The process is currently executing on the alternate signal stack.
127 (Note that it is not possible
128 to change the alternate signal stack if the process is
129 currently executing on it.)
130 .TP
131 .B SS_DISABLE
132 The alternate signal stack is currently disabled.
133 .SH RETURN VALUE
134 .BR sigaltstack ()
135 returns 0 on success, or \-1 on failure with
136 \fIerrno\fP set to indicate the error.
137 .SH ERRORS
138 .TP
139 .B EFAULT
140 Either \fIss\fP or \fIoss\fP is not NULL and points to an area
141 outside of the process's address space.
142 .TP
143 .B EINVAL
144 \fIss\fP is not NULL and the \fIss_flags\fP field contains
145 a nonzero value other than
146 .BR SS_DISABLE .
147 .TP
148 .B ENOMEM
149 The specified size of the new alternate signal stack
150 (\fIss.ss_size\fP) was less than \fBMINSTKSZ\fP.
151 .TP
152 .B EPERM
153 An attempt was made to change the alternate signal stack while
154 it was active (i.e., the process was already executing
155 on the current alternate signal stack).
156 .SH CONFORMING TO
157 SUSv2, SVr4, POSIX.1-2001.
158 .SH NOTES
159 The most common usage of an alternate signal stack is to handle the
160 .B SIGSEGV
161 signal that is generated if the space available for the
162 normal process stack is exhausted: in this case, a signal handler for
163 .B SIGSEGV
164 cannot be invoked on the process stack; if we wish to handle it,
165 we must use an alternate signal stack.
166 .P
167 Establishing an alternate signal stack is useful if a process
168 expects that it may exhaust its standard stack.
169 This may occur, for example, because the stack grows so large
170 that it encounters the upwardly growing heap, or it reaches a
171 limit established by a call to \fBsetrlimit(RLIMIT_STACK, &rlim)\fP.
172 If the standard stack is exhausted, the kernel sends
173 the process a \fBSIGSEGV\fP signal.
174 In these circumstances the only way to catch this signal is
175 on an alternate signal stack.
176 .P
177 On most hardware architectures supported by Linux, stacks grow
178 downward.
179 .BR sigaltstack ()
180 automatically takes account
181 of the direction of stack growth.
182 .P
183 Functions called from a signal handler executing on an alternate
184 signal stack will also use the alternate signal stack.
185 (This also applies to any handlers invoked for other signals while
186 the process is executing on the alternate signal stack.)
187 Unlike the standard stack, the system does not
188 automatically extend the alternate signal stack.
189 Exceeding the allocated size of the alternate signal stack will
190 lead to unpredictable results.
191 .P
192 A successful call to
193 .BR execve (2)
194 removes any existing alternate
195 signal stack.
196 A child process created via
197 .BR fork (2)
198 inherits a copy of its parent's alternate signal stack settings.
199 .P
200 .BR sigaltstack ()
201 supersedes the older
202 .BR sigstack ()
203 call.
204 For backward compatibility, glibc also provides
205 .BR sigstack ().
206 All new applications should be written using
207 .BR sigaltstack ().
208 .SS History
209 4.2BSD had a
210 .BR sigstack ()
211 system call.
212 It used a slightly
213 different struct, and had the major disadvantage that the caller
214 had to know the direction of stack growth.
215 .SH EXAMPLE
216 The following code segment demonstrates the use of
217 .BR sigaltstack ():
218
219 .in +4n
220 .nf
221 stack_t ss;
222
223 ss.ss_sp = malloc(SIGSTKSZ);
224 if (ss.ss_sp == NULL)
225     /* Handle error */;
226 ss.ss_size = SIGSTKSZ;
227 ss.ss_flags = 0;
228 if (sigaltstack(&ss, NULL) == \-1)
229     /* Handle error */;
230 .fi
231 .in
232 .SH SEE ALSO
233 .BR execve (2),
234 .BR setrlimit (2),
235 .BR sigaction (2),
236 .BR siglongjmp (3),
237 .BR sigsetjmp (3),
238 .BR signal (7)
239 .SH COLOPHON
240 This page is part of release 3.65 of the Linux
241 .I man-pages
242 project.
243 A description of the project,
244 and information about reporting bugs,
245 can be found at
246 \%http://www.kernel.org/doc/man\-pages/.