A signal stack is a special area of memory to be used as the execution
stack during signal handlers. It should be fairly large, to avoid any
danger that it will overflow in turn; the macro SIGSTKSZ
is
defined to a canonical size for signal stacks. You can use
malloc
to allocate the space for the stack. Then call
sigaltstack
or sigstack
to tell the system to use that
space for the signal stack.
You don't need to write signal handlers differently in order to use a signal stack. Switching from one stack to the other happens automatically. (Some non-GNU debuggers on some machines may get confused if you examine a stack trace while a handler that uses the signal stack is running.)
There are two interfaces for telling the system to use a separate signal
stack. sigstack
is the older interface, which comes from 4.2
BSD. sigaltstack
is the newer interface, and comes from 4.4
BSD. The sigaltstack
interface has the advantage that it does
not require your program to know which direction the stack grows, which
depends on the specific machine and operating system.
void *ss_sp
size_t ss_size
SIGSTKSZ
MINSIGSTKSZ
SIGSTKSZ
for ss_size
is
sufficient. But if you know how much stack space your program's signal
handlers will need, you may want to use a different size. In this case,
you should allocate MINSIGSTKSZ
additional bytes for the signal
stack and increase ss_size
accordingly.
int ss_flags
SS_DISABLE
SS_ONSTACK
sigaltstack
function specifies an alternate stack for use
during signal handling. When a signal is received by the process and
its action indicates that the signal stack is used, the system arranges
a switch to the currently installed signal stack while the handler for
that signal is executed.
If oldstack is not a null pointer, information about the currently installed signal stack is returned in the location it points to. If stack is not a null pointer, then this is installed as the new stack for use by signal handlers.
The return value is 0
on success and -1
on failure. If
sigaltstack
fails, it sets errno
to one of these values:
EINVAL
ENOMEM
MINSIGSTKSZ
.
Here is the older sigstack
interface. You should use
sigaltstack
instead on systems that have it.
void *ss_sp
int ss_onstack
sigstack
function specifies an alternate stack for use during
signal handling. When a signal is received by the process and its
action indicates that the signal stack is used, the system arranges a
switch to the currently installed signal stack while the handler for
that signal is executed.
If oldstack is not a null pointer, information about the currently installed signal stack is returned in the location it points to. If stack is not a null pointer, then this is installed as the new stack for use by signal handlers.
The return value is 0
on success and -1
on failure.
Go to the first, previous, next, last section, table of contents.