1.\" $NetBSD: sigaltstack.2,v 1.3 1995/02/27 10:41:52 cgd 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.Bd -literal 46struct sigaltstack { 47 char *ss_sp; 48 int ss_size; 49 int ss_flags; 50}; 51.Ed 52.Ft int 53.Fn sigaltstack "const struct sigaltstack *ss" "struct sigaltstack *oss" 54.Sh DESCRIPTION 55.Fn Sigaltstack 56allows users to define an alternate stack on which signals 57are to be processed. 58If 59.Fa ss 60is non-zero, 61it specifies a pointer to and the size of a 62.Em "signal stack" 63on which to deliver signals, 64and tells the system if the process is currently executing 65on that stack. 66When a signal's action indicates its handler 67should execute on the signal stack (specified with a 68.Xr sigaction 2 69call), the system checks to see 70if the process is currently executing on that stack. 71If the process is not currently executing on the signal stack, 72the system arranges a switch to the signal stack for the 73duration of the signal handler's execution. 74.Pp 75If 76.Dv SA_DISABLE 77is set in 78.Fa ss_flags , 79.Fa ss_sp 80and 81.Fa ss_size 82are ignored and the signal stack will be disabled. 83Trying to disable an active stack will cause 84.Nm 85to return -1 with 86.Va errno 87set to 88.Dv EINVAL . 89A disabled stack will cause all signals to be 90taken on the regular user stack. 91If the stack is later re-enabled then all signals that were specified 92to be processed on an alternate stack will resume doing so. 93.Pp 94If 95.Fa oss 96is non-zero, the current signal stack state is returned. 97The 98.Fa ss_flags 99field will contain the value 100.Dv SA_ONSTACK 101if the process is currently on a signal stack and 102.Dv SA_DISABLE 103if the signal stack is currently disabled. 104.Sh NOTES 105The value 106.Dv SIGSTKSZ 107is defined to be the number of bytes/chars that would be used to cover 108the usual case when allocating an alternate stack area. 109The following code fragment is typically used to allocate an alternate stack. 110.Bd -literal -offset indent 111if ((sigstk.ss_sp = malloc(SIGSTKSZ)) == NULL) 112 /* error return */ 113sigstk.ss_size = SIGSTKSZ; 114sigstk.ss_flags = 0; 115if (sigaltstack(&sigstk,0) < 0) 116 perror("sigaltstack"); 117.Ed 118An alternative approach is provided for programs with signal handlers 119that require a specific amount of stack space other than the default size. 120The value 121.Dv MINSIGSTKSZ 122is defined to be the number of bytes/chars that is required by 123the operating system to implement the alternate stack feature. 124In computing an alternate stack size, 125programs should add 126.Dv MINSIGSTKSZ 127to their stack requirements to allow for the operating system overhead. 128.Pp 129Signal stacks are automatically adjusted for the direction of stack 130growth and alignment requirements. 131Signal stacks may or may not be protected by the hardware and 132are not ``grown'' automatically as is done for the normal stack. 133If the stack overflows and this space is not protected 134unpredictable results may occur. 135.Sh RETURN VALUES 136Upon successful completion, a value of 0 is returned. 137Otherwise, a value of -1 is returned and 138.Va errno 139is set to indicate the error. 140.Sh ERRORS 141.Fn Sigstack 142will fail and the signal stack context will remain unchanged 143if one of the following occurs. 144.Bl -tag -width [ENOMEM] 145.It Bq Er EFAULT 146Either 147.Fa ss 148or 149.Fa oss 150points to memory that is not a valid part of the process 151address space. 152.It Bq Er EINVAL 153An attempt was made to disable an active stack. 154.It Bq Er ENOMEM 155Size of alternate stack area is less than or equal to 156.Dv MINSIGSTKSZ . 157.El 158.Sh SEE ALSO 159.Xr sigaction 2 , 160.Xr setjmp 3 161.Sh HISTORY 162The predecessor to 163.Nm sigaltstack , 164the 165.Fn sigstack 166system call, appeared in 167.Bx 4.2 . 168