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