1sigaltstack(2) System Calls sigaltstack(2)
2
3
4
6 sigaltstack - set or get signal alternate stack context
7
9 #include <signal.h>
10
11 int sigaltstack(const stack_t *restrict ss, stack_t *restrict oss);
12
13
15 The sigaltstack() function allows a thread to define and examine the
16 state of an alternate stack area on which signals are processed. If ss
17 is non-zero, it specifies a pointer to and the size of a stack area on
18 which to deliver signals, and informs the system whether the thread is
19 currently executing on that stack. When a signal's action indicates
20 its handler should execute on the alternate signal stack (specified
21 with a sigaction(2) call), the system checks whether the thread chosen
22 to execute the signal handler is currently executing on that stack. If
23 the thread is not currently executing on the signal stack, the system
24 arranges a switch to the alternate signal stack for the duration of the
25 signal handler's execution.
26
27
28 The stack_t structure includes the following members:
29
30 int *ss_sp
31 long ss_size
32 int ss_flags
33
34
35
36 If ss is not NULL, it points to a structure specifying the alternate
37 signal stack that will take effect upon successful return from sigalt‐
38 stack(). The ss_sp and ss_size members specify the new base and size of
39 the stack, which is automatically adjusted for direction of growth and
40 alignment. The ss_flags member specifies the new stack state and may
41 be set to the following:
42
43 SS_DISABLE The stack is to be disabled and ss_sp and ss_size are
44 ignored. If SS_DISABLE is not set, the stack will be
45 enabled.
46
47
48
49 If oss is not NULL, it points to a structure specifying the alternate
50 signal stack that was in effect prior to the call to sigaltstack(). The
51 ss_sp and ss_size members specify the base and size of that stack. The
52 ss_flags member specifies the stack's state, and may contain the fol‐
53 lowing values:
54
55 SS_ONSTACK The thread is currently executing on the alternate signal
56 stack. Attempts to modify the alternate signal stack
57 while the thread is executing on it will fail.
58
59
60 SS_DISABLE The alternate signal stack is currently disabled.
61
62
64 Upon successful completion, 0 is return. Otherwise, −1 is returned and
65 errno is set to indicate the error.
66
68 The sigaltstack() function will fail if:
69
70 EFAULT The ss or oss argument points to an illegal address.
71
72
73 EINVAL The ss argument is not a null pointer, and the ss_flags mem‐
74 ber pointed to by ss contains flags other than SS_DISABLE.
75
76
77 ENOMEM The size of the alternate stack area is less than MIN‐
78 SIGSTKSZ.
79
80
81 EPERM An attempt was made to modify an active stack.
82
83
85 See attributes(5) for descriptions of the following attributes:
86
87
88
89
90 ┌─────────────────────────────┬─────────────────────────────┐
91 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
92 ├─────────────────────────────┼─────────────────────────────┤
93 │Interface Stability │Standard │
94 ├─────────────────────────────┼─────────────────────────────┤
95 │MT-Level │Async-Signal-Safe │
96 └─────────────────────────────┴─────────────────────────────┘
97
99 getcontext(2), mmap(2), sigaction(2), ucontext.h(3HEAD), attributes(5),
100 standards(5)
101
103 The value SIGSTKSZ is defined to be the number of bytes that would be
104 used to cover the usual case when allocating an alternate stack area.
105 The value MINSIGSTKSZ is defined to be the minimum stack size for a
106 signal handler. In computing an alternate stack size, a program
107 should add that amount to its stack requirements to allow for the oper‐
108 ating system overhead.
109
110
111 The following code fragment is typically used to allocate an alternate
112 stack with an adjacent red zone (an unmapped page) to guard against
113 stack overflow, as with default stacks:
114
115 #include <signal.h>
116 #include <sys/mman.h>
117
118 stack_t sigstk;
119 sigstk.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
120 MAP_PRIVATE | MAP_ANON, -1, 0);
121 if (sigstk.ss_sp == MAP_FAILED)
122 /* error return */;
123 sigstk.ss_size = SIGSTKSZ;
124 sigstk.ss_flags = 0;
125 if (sigaltstack(&sigstk, NULL) < 0)
126 perror("sigaltstack");
127
128
129
130
131SunOS 5.11 1 Nov 2003 sigaltstack(2)