SIGALTSTACK(2)
β¨ sigaltstack β set and/or get signal stack context β¨
okay bestie, welcome to the sigaltstack(2) page β it lives in the System Calls Manual (section 2 of the Unix manuals, iykyk) π
and it's brought to you by the Standard C Library (libc, -lc). let's get into it π«Ά
πΈ SYNOPSIS πΈ
#include <signal.h>
typedef struct {
char *ss_sp;
size_t ss_size;
int ss_flags;
} stack_t;
int
sigaltstack(const stack_t * restrict ss, stack_t * restrict oss);
π DESCRIPTION π
so the sigaltstack() system call is literally how you define an alternate stack β a whole other stack where signals get processed for the current thread. main character energy fr π
if ss is non-zero, it's giving a pointer to a signal stack plus the size of it, and that's the stack signals get delivered on. when a signal's action says its handler should run on the signal stack (you set that up with a sigaction(2) system call), the system goes okay lemme check β is this thread already chilling on that stack? if the thread is not currently executing on the signal stack, the system arranges a lil switch over to the signal stack for however long the signal handler runs. periodt.
π« an active stack cannot be modified. no cap.
if SS_DISABLE is set in ss_flags, then ss_sp and ss_size get straight-up ignored and the signal stack will be disabled. a disabled stack means all signals get taken on the regular user stack instead. and if you later re-enable the stack? all the signals that were specified to be processed on an alternate stack just resume doing that, slay π
if oss is non-zero, the current signal stack state gets returned to you. the ss_flags field will contain SS_ONSTACK if the thread is currently on a signal stack, and SS_DISABLE if the signal stack is currently disabled. iconic.
π§ NOTES π§
SIGSTKSZ is defined to be the number of bytes/chars that would be used to cover the usual case when you're allocating an alternate stack area. the following code fragment is typically what you'd use to allocate an alternate stack:
if ((sigstk.ss_sp = malloc(SIGSTKSZ)) == NULL)
/* error return */
sigstk.ss_size = SIGSTKSZ;
sigstk.ss_flags = 0;
if (sigaltstack(&sigstk, NULL) < 0)
perror("sigaltstack");
there's also an alternative approach for programs whose signal handlers need a specific amount of stack space, not just the default size. MINSIGSTKSZ is defined to be the number of bytes/chars that the operating system requires to implement the alternate stack feature. so when you're computing an alternate stack size, bestie, add MINSIGSTKSZ to your stack requirements to allow for the operating system overhead π
signal stacks are automatically adjusted for the direction of stack growth and alignment requirements β cute, right? but heads up: signal stacks may or may not be protected by the hardware, and they are not "grown" automatically the way the normal stack is. so if the stack overflows and that space isn't protectedβ¦
π unpredictable results may occur. we do not love that.
β RETURN VALUES β
the sigaltstack() function returns the value 0 if it's successful π otherwise the value -1 is returned and the global variable errno is set to indicate the error.
π₯ ERRORS π₯
the sigaltstack() system call will fail and the signal stack context will remain unchanged if one of the following occurs:
[EFAULT]β eitherssorosspoints to memory that is not a valid part of the process address space.[EPERM]β an attempt was made to modify an active stack.[EINVAL]β thess_flagsfield was invalid.[ENOMEM]β size of alternate stack area is less than or equal toMINSIGSTKSZ.
π SEE ALSO π
π HISTORY π
the predecessor to sigaltstack(), the sigstack() system call, appeared in 4.2BSD. ancestral icon behavior, honestly.
and that's the whole page, bestie π¦ this one's from Debian, dated May 6, 2010 β so yeah, it's been serving signal-stack realness for a minute π go forth and stack responsibly π«Άβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1992, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms