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