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