OSDN Git Service

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