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