xref: /openbsd/lib/libc/sys/sigaltstack.2 (revision 133306f0)
1.\"	$OpenBSD: sigaltstack.2,v 1.10 2000/01/22 12:05:49 aaron Exp $
2.\"	$NetBSD: sigaltstack.2,v 1.3 1995/02/27 10:41:52 cgd Exp $
3.\"
4.\" Copyright (c) 1983, 1991, 1992, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)sigaltstack.2	8.1 (Berkeley) 6/4/93
36.\"
37.Dd June 4, 1993
38.Dt SIGALTSTACK 2
39.Os
40.Sh NAME
41.Nm sigaltstack
42.Nd set and/or get signal stack context
43.Sh SYNOPSIS
44.Fd #include <sys/types.h>
45.Fd #include <signal.h>
46.Bd -literal
47
48struct sigaltstack {
49	void	*ss_sp;
50	int	ss_size;
51	int	ss_flags;
52};
53.Ed
54.Ft int
55.Fn sigaltstack "const struct sigaltstack *ss" "struct sigaltstack *oss"
56.Sh DESCRIPTION
57.Fn sigaltstack
58allows users to define an alternate stack on which signals
59are to be processed.
60If
61.Fa ss
62is non-zero,
63it specifies a pointer to and the size of a
64.Em "signal stack"
65on which to deliver signals,
66and tells the system if the process is currently executing
67on that stack.
68When a signal's action indicates its handler
69should execute on the signal stack (specified with a
70.Xr sigaction 2
71call), the system checks to see
72if the process is currently executing on that stack.
73If the process is not currently executing on the signal stack,
74the system arranges a switch to the signal stack for the
75duration of the signal handler's execution.
76.Pp
77If
78.Dv SS_DISABLE
79is set in
80.Fa ss_flags ,
81.Fa ss_sp
82and
83.Fa ss_size
84are ignored and the signal stack will be disabled.
85Trying to disable an active stack will cause
86.Nm
87to return \-1 with
88.Va errno
89set to
90.Er EINVAL .
91A disabled stack will cause all signals to be
92taken on the regular user stack.
93If the stack is later re-enabled then all signals that were specified
94to be processed on an alternate stack will resume doing so.
95.Pp
96If
97.Fa oss
98is non-zero, the current signal stack state is returned.
99The
100.Fa ss_flags
101field will contain the value
102.Dv SS_ONSTACK
103if the process is currently on a signal stack and
104.Dv SS_DISABLE
105if the signal stack is currently disabled.
106.Sh NOTES
107The value
108.Dv SIGSTKSZ
109is defined to be the number of bytes/chars that would be used to cover
110the usual case when allocating an alternate stack area.
111The following code fragment is typically used to allocate an alternate stack.
112.Bd -literal -offset indent
113if ((sigstk.ss_sp = malloc(SIGSTKSZ)) == NULL)
114	/* error return */
115sigstk.ss_size = SIGSTKSZ;
116sigstk.ss_flags = 0;
117if (sigaltstack(&sigstk,0) < 0)
118	perror("sigaltstack");
119.Ed
120.Pp
121An alternative approach is provided for programs with signal handlers
122that require a specific amount of stack space other than the default size.
123The value
124.Dv MINSIGSTKSZ
125is defined to be the number of bytes/chars that is required by
126the operating system to implement the alternate stack feature.
127In computing an alternate stack size,
128programs should add
129.Dv MINSIGSTKSZ
130to their stack requirements to allow for the operating system overhead.
131.Pp
132Signal stacks are automatically adjusted for the direction of stack
133growth and alignment requirements.
134Signal stacks may or may not be protected by the hardware and
135are not ``grown'' automatically as is done for the normal stack.
136If the stack overflows and this space is not protected
137unpredictable results may occur.
138.Sh RETURN VALUES
139Upon successful completion, a value of 0 is returned.
140Otherwise, a value of \-1 is returned and
141.Va errno
142is set to indicate the error.
143.Sh ERRORS
144.Fn sigaltstack
145will fail and the signal stack context will remain unchanged
146if one of the following occurs.
147.Bl -tag -width [ENOMEM]
148.It Bq Er EFAULT
149Either
150.Fa ss
151or
152.Fa oss
153points to memory that is not a valid part of the process
154address space.
155.It Bq Er EINVAL
156An attempt was made to disable an active stack.
157.It Bq Er ENOMEM
158Size of alternate stack area is less than or equal to
159.Dv MINSIGSTKSZ .
160.El
161.Sh SEE ALSO
162.Xr sigaction 2 ,
163.Xr setjmp 3
164.Sh HISTORY
165The predecessor to
166.Nm sigaltstack ,
167the
168.Fn sigstack
169system call, appeared in
170.Bx 4.2 .
171