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