]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/sigaltstack.2
Reordered sections to be more consistent, in some cases renaming
[thirdparty/man-pages.git] / man2 / sigaltstack.2
CommitLineData
fea681da 1'\" t
305a0578 2.\" Copyright (c) 2001, Michael Kerrisk (mtk-manpages@gmx.net)
fea681da
MK
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.
17.\"
18.\" Formatted or processed versions of this manual, if unaccompanied by
19.\" the source, must acknowledge the copyright and authors of this work.
20.\"
21.\" aeb, various minor fixes
22.TH SIGALTSTACK 2 2001-09-27 "Linux 2.4" "Linux Programmer's Manual"
23.SH NAME
e9496f74 24sigaltstack \- set and/or get signal stack context
fea681da
MK
25.SH SYNOPSIS
26.B #include <signal.h>
27.sp
28.BI "int sigaltstack(const stack_t *" ss ", stack_t *" oss );
29.SH DESCRIPTION
60a90ecd
MK
30.BR sigaltstack ()
31allows a process to define a new alternate
fea681da 32signal stack and/or retrieve the state of an existing
c13182ef
MK
33alternate signal stack.
34An alternate signal stack is used during the
fea681da
MK
35execution of a signal handler if the establishment of that handler (see
36.BR sigaction (2))
37requested it.
38
39The normal sequence of events for using an alternate signal stack
40is the following:
41.TP
421.
43Allocate an area of memory to be used for the alternate
44signal stack.
45.TP
462.
60a90ecd
MK
47Use
48.BR sigaltstack ()
49to inform the system of the existence and
fea681da
MK
50location of the alternate signal stack.
51.TP
523.
60a90ecd
MK
53When establishing a signal handler using
54.BR sigaction (2),
fea681da
MK
55inform the system that the signal handler should be executed
56on the alternate signal stack by
57specifying the \fBSA_ONSTACK\fP flag.
58.P
59The \fIss\fP argument is used to specify a new
60alternate signal stack, while the \fIoss\fP argument
61is used to retrieve information about the currently
62established signal stack.
63If we are interested in performing just one
64of these tasks then the other argument can be specified as NULL.
65Each of these arguments is a structure of the following type:
66.sp
67.RS
68.nf
69typedef struct {
70 void *ss_sp; /* Base address of stack */
71 int ss_flags; /* Flags */
72 size_t ss_size; /* Number of bytes in stack */
73} stack_t;
74.fi
75.RE
76
77To establish a new alternate signal stack,
75cad981 78\fIss.ss_flags\fP is set to zero, and \fIss.ss_sp\fP and
fea681da
MK
79\fIss.ss_size\fP specify the starting address and size of
80the stack.
81The constant \fBSIGSTKSZ\fP is defined to be large enough
82to cover the usual size requirements for an alternate signal stack,
83and the constant \fBMINSIGSTKSZ\fP defines the minimum
84size required to execute a signal handler.
85
c13182ef
MK
86When a signal handler is invoked on the alternate stack,
87the kernel automatically aligns the address given in \fIss.ss_sp\fP
cd31a0d6 88to a suitable address boundary for the underlying hardware architecture.
75cad981 89
fea681da 90To disable an existing stack, specify \fIss.ss_flags\fP
c13182ef
MK
91as \fBSS_DISABLE\fP.
92In this case, the remaining fields
fea681da
MK
93in \fIss\fP are ignored.
94
95If \fIoss\fP is not NULL, then it is used to return information about
96the alternate signal stack which was in effect prior to the
60a90ecd
MK
97call to
98.BR sigaltstack ().
fea681da
MK
99The \fIoss.ss_sp\fP and \fIoss.ss_size\fP fields return the starting
100address and size of that stack.
101The \fIoss.ss_flags\fP may return either of the following values:
fea681da
MK
102.TP
103.B SS_ONSTACK
c13182ef
MK
104The process is currently executing on the alternate signal stack.
105(Note that it is not possible
fea681da
MK
106to change the alternate signal stack if the process is
107currently executing on it.)
108.TP
109.B SS_DISABLE
110The alternate signal stack is currently disabled.
fea681da 111.SH "RETURN VALUE"
60a90ecd
MK
112.BR sigaltstack ()
113returns 0 on success, or \-1 on failure with
fea681da 114\fIerrno\fP set to indicate the error.
fea681da
MK
115.SH ERRORS
116.TP
117.B EFAULT
118Either \fIss\fP or \fIoss\fP is not NULL and points to an area
119outside of the process's address space.
120.TP
121.B EINVAL
122\fIss\fP is not NULL and the \fPss_flags\fP field contains
123a non-zero value other than SS_DISABLE.
124.TP
125.B ENOMEM
126The specified size of the new alternate signal stack
127(\fIss.ss_size\fP) was less than \fBMINSTKSZ\fP.
128.TP
129.B EPERM
130An attempt was made to change the alternate signal stack while
131it was active (i.e., the process was already executing
132on the current alternate signal stack).
889829be 133.SH EXAMPLE
60a90ecd
MK
134The following code segment demonstrates the use of
135.BR sigaltstack ():
fea681da
MK
136
137.RS
138.nf
139stack_t ss;
140
141ss.ss_sp = malloc(SIGSTKSZ);
142if (ss.ss_sp == NULL)
143 /* Handle error */;
144ss.ss_size = SIGSTKSZ;
145ss.ss_flags = 0;
2bc2f479 146if (sigaltstack(&ss, NULL) == \-1)
fea681da
MK
147 /* Handle error */;
148.fi
149.RE
889829be
MK
150.SH NOTES
151The most common usage of an alternate signal stack is to handle the
152.B SIGSEGV
153signal that is generated if the space available for the
154normal process stack is exhausted: in this case, a signal handler for
155.B SIGSEGV
156cannot be invoked on the process stack; if we wish to handle it,
157we must use an alternate signal stack.
fea681da
MK
158.P
159Establishing an alternate signal stack is useful if a process
160expects that it may exhaust its standard stack.
161This may occur, for example, because the stack grows so large
162that it encounters the upwardly growing heap, or it reaches a
163limit established by a call to \fBsetrlimit(RLIMIT_STACK, &rlim)\fP.
164If the standard stack is exhausted, the kernel sends
165the process a \fBSIGSEGV\fP signal.
166In these circumstances the only way to catch this signal is
167on an alternate signal stack.
168.P
169On most hardware architectures supported by Linux, stacks grow
0bfa087b 170downwards.
60a90ecd
MK
171.BR sigaltstack ()
172automatically takes account
fea681da
MK
173of the direction of stack growth.
174.P
175Functions called from a signal handler executing on an alternate
176signal stack will also use the alternate signal stack.
177(This also applies to any handlers invoked for other signals while
178the process is executing on the alternate signal stack.)
179Unlike the standard stack, the system does not
180automatically extend the alternate signal stack.
181Exceeding the allocated size of the alternate signal stack will
182lead to unpredictable results.
183.P
60a90ecd
MK
184A successful call to
185.BR execve (2)
186removes any existing alternate
fea681da
MK
187signal stack.
188.P
60a90ecd
MK
189.BR sigaltstack ()
190supersedes the older
191.BR sigstack ()
192call.
193For backwards compatibility, glibc also provides
194.BR sigstack ().
195All new applications should be written using
196.BR sigaltstack ().
889829be 197.SS History
d9c1ae64
MK
1984.2BSD had a
199.BR sigstack ()
200system call.
c13182ef 201It used a slightly
75cad981 202different struct, and had the major disadvantage that the caller
fea681da 203had to know the direction of stack growth.
fea681da 204.SH "CONFORMING TO"
97c1eac8 205SUSv2, SVr4, POSIX.1-2001.
fea681da
MK
206.SH "SEE ALSO"
207.BR execve (2),
208.BR setrlimit (2),
209.BR sigaction (2),
210.BR siglongjmp (3),
211.BR sigsetjmp (3),
212.BR signal (7)