sigaltstack(2)
π sigaltstack β bestie, let's talk about your signal stack π
okay so real talk, this is a page from the System Calls Manual, and the function we're stanning today is sigaltstack(2). she lives in the Standard C library (libc, -lc). come sit with me. π«Ά
β¨ NAME β¨
sigaltstack β set and/or get signal stack context π
π LIBRARY π
Standard C library (libc, -lc). she's built in, you don't gotta install anything, slay.
πΈ SYNOPSIS πΈ
#include <signal.h>
int sigaltstack(const stack_t *_Nullable restrict ss,
stack_t *_Nullable restrict old_ss);
Feature Test Macro Requirements for glibc (go read feature_test_macros(7) if you're nosy, which, same):
sigaltstack():
_XOPEN_SOURCE >= 500
|| /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
|| /* glibc <= 2.19: */ _BSD_SOURCE
iykyk. those macros are your β¨entry ticketβ¨ to even see the function.
π DESCRIPTION π
So what does sigaltstack() actually DO? She lets a thread define a brand new alternate signal stack, and/or retrieve the state of one you already got going. And why do you want a whole second stack? Because an alternate signal stack gets used during the execution of a signal handler β but only if the establishment of that handler (hey, sigaction(2)) specifically asked for it. no request, no alternate stack, periodt.
The normal sequence of events for using an alternate signal stack goes like this:
- Allocate an area of memory to be used for the alternate signal stack.
- Use
sigaltstack()to inform the system of the existence and location of the alternate signal stack. - When establishing a signal handler using
sigaction(2), inform the system that the signal handler should be executed on the alternate signal stack by specifying theSA_ONSTACKflag.
the ss argument specifies a new alternate signal stack. the old_ss argument retrieves info about the currently established one. only need one of those jobs done? pass NULL for the other one, easy. π
The stack_t type that types these arguments is defined like this, and yes this is verbatim, do not touch:
typedef struct {
void *ss_sp; /* Base address of stack */
int ss_flags; /* Flags */
size_t ss_size; /* Number of bytes in stack */
} stack_t;
π To establish a new alternate signal stack π
set the fields like so:
ss.ss_flags
This field contains either 0, or this one flag right here:
SS_AUTODISARM(since Linux 4.7) β clear the alternate signal stack settings on entry to the signal handler. when the signal handler returns, the previous alternate signal stack settings are restored. mind. blown. π€―
fr fr though, this flag was added so it's safe to switch away from the signal handler with swapcontext(3). without it, a subsequently handled signal will corrupt the state of the switched-away signal handler π. and heads up: on kernels where this flag isn't supported, sigaltstack() fails with the error EINVAL when you supply it. no cap.
ss.ss_sp
This field specifies the starting address of the stack. when a signal handler is invoked on the alternate stack, the kernel automatically aligns the address you gave in ss.ss_sp to a suitable address boundary for the underlying hardware architecture. she's got good manners like that. β¨
ss.ss_size
This field specifies the size of the stack. the constant SIGSTKSZ is defined to be large enough to cover the usual size requirements for an alternate signal stack, and the constant MINSIGSTKSZ defines the minimum size required to execute a signal handler. (MINSIGSTKSZ = bare minimum, SIGSTKSZ = comfortable. don't be cheap bestie.)
π To disable an existing stack π
specify ss.ss_flags as SS_DISABLE. in this case the kernel ignores any other flags in ss.ss_flags and the remaining fields in ss. just straight up ignores them. iconic.
π¦ If old_ss is not NULL π¦
Then it's used to return information about the alternate signal stack which was in effect prior to the call to sigaltstack(). The old_ss.ss_sp and old_ss.ss_size fields return the starting address and size of that stack. And old_ss.ss_flags may return either of these values:
SS_ONSTACKβ the thread is currently executing on the alternate signal stack. β οΈ note that it is not possible to change the alternate signal stack if the thread is currently executing on it. you are stuck there, it's giving trapped.SS_DISABLEβ the alternate signal stack is currently disabled.
alternatively, this value is returned if the thread is currently executing on an alternate signal stack that was established using the SS_AUTODISARM flag. in this case, it is safe to switch away from the signal handler with swapcontext(3). it's also possible to set up a different alternative signal stack using a further call to sigaltstack(). so like, you have options. π
SS_AUTODISARMβ the alternate signal stack has been marked to be autodisarmed, as described above. (yes girl, the flag shows up in the readback too.)
And one last trick: by specifying ss as NULL and old_ss as a non-NULL value, you can obtain the current settings for the alternate signal stack without changing them. read-only era. π
π₯ RETURN VALUE π₯
sigaltstack() returns 0 on success, or -1 on failure with errno set to indicate the error. classic.
π ERRORS π
EFAULTβ Eitherssorold_ssis notNULLand points to an area outside of the process's address space. you pointed somewhere illegal bestie, the kernel is not having it.EINVALβssis notNULLand thess_flagsfield contains an invalid flag. invalid, as in not a real flag, we don't do that here.ENOMEMβ The specified size of the new alternate signal stackss.ss_sizewas less thanMINSIGSTKSZ. too small. the stack said "I cannot fit, I'm sorry. π"EPERMβ An attempt was made to change the alternate signal stack while it was active (i.e., the thread was already executing on the current alternate signal stack). yeah you can't redecorate while you're standing inside the room.
π ATTRIBUTES π
for an explanation of the terms used in this section, see attributes(7). (no tables here, so here's the row all cute-like:)
sigaltstack()β Thread safety:MT-Safeβ β she's safe across threads, we love a multithread-friendly queen.
β STANDARDS β
POSIX.1-2024.
SS_AUTODISARM is a Linux extension. (so it's Linux-only, don't go expecting it on your friend's BSD, mwah.)
π§ HISTORY π§
POSIX.1-2001, SUSv2, SVr4. she's been around, she's a legacy icon.
π History π
4.2BSD had a sigstack() system call. it used a slightly different struct, and had the major disadvantage that the caller had to know the direction of stack growth. π© imagine having to know that. couldn't be sigaltstack().
π BUGS π
In Linux 2.2 and earlier, the only flag that could be specified in ss.sa_flags was SS_DISABLE. in the lead up to the release of the Linux 2.4 kernel, a change was made to allow sigaltstack() to allow ss.ss_flags==SS_ONSTACK with the same meaning as ss.ss_flags==0 (i.e., the inclusion of SS_ONSTACK in ss.ss_flags is a no-op). on other implementations, and according to POSIX.1, SS_ONSTACK appears only as a reported flag in old_ss.ss_flags.
β οΈ On Linux, there is no need ever to specifySS_ONSTACKinss.ss_flags, and indeed doing so should be avoided on portability grounds: various other systems give an error ifSS_ONSTACKis specified inss.ss_flags. so like, just don't. π
π EXAMPLES π
this code segment demonstrates the use of sigaltstack() (and sigaction(2)) to install an alternate signal stack that's employed by a handler for the SIGSEGV signal. copy-paste responsibly, bestie:
stack_t ss;
ss.ss_sp = malloc(SIGSTKSZ);
if (ss.ss_sp == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) == -1) {
perror("sigaltstack");
exit(EXIT_FAILURE);
}
sa.sa_flags = SA_ONSTACK;
sa.sa_handler = handler(); /* Address of a signal handler */
sigemptyset(&sa.sa_mask);
if (sigaction(SIGSEGV, &sa, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
π NOTES π
The most common usage of an alternate signal stack is to handle the SIGSEGV signal that is generated if the space available for the standard stack is exhausted: in this case, a signal handler for SIGSEGV cannot be invoked on the standard stack; if we wish to handle it, we must use an alternate signal stack. that's literally the whole point. main character energy for the second stack. π
Establishing an alternate signal stack is useful if a thread expects that it may exhaust its standard stack. this may occur, for example, because the stack grows so large that it encounters the upwardly growing heap, or it reaches a limit established by a call to setrlimit(RLIMIT_STACK, &rlim). if the standard stack is exhausted, the kernel sends the thread a SIGSEGV signal. in these circumstances the only way to catch this signal is on an alternate signal stack. no alternate stack, no catch. simple as.
On most hardware architectures supported by Linux, stacks grow downward. sigaltstack() automatically takes account of the direction of stack growth. so you don't gotta think about it, she handles it. π¦
Functions called from a signal handler executing on an alternate signal stack will also use the alternate signal stack. (this also applies to any handlers invoked for other signals while the thread is executing on the alternate signal stack.) unlike the standard stack, the system does not automatically extend the alternate signal stack. exceeding the allocated size of the alternate signal stack will lead to unpredictable results. π so size it right, okay? okay.
- A successful call to
execve(2)removes any existing alternate signal stack. - A child process created via
fork(2)inherits a copy of its parent's alternate signal stack settings. - The same is also true for a child process created using
clone(2), unless the clone flags includeCLONE_VMand do not includeCLONE_VFORK, in which case any alternate signal stack that was established in the parent is disabled in the child process.
sigaltstack() supersedes the older sigstack() call. for backward compatibility, glibc also provides sigstack(). all new applications should be written using sigaltstack(). new era, new call, we don't do the old one anymore. π
π History π
4.2BSD had a sigstack() system call. it used a slightly different struct, and had the major disadvantage that the caller had to know the direction of stack growth. (yes, this story shows up twice on this page. the manpage said it twice, so I said it twice. respectfully. π)
π SEE ALSO π
execve(2), setrlimit(2), sigaction(2), siglongjmp(3), sigsetjmp(3), signal(7)
and that's the whole page bestie!! π this one's straight from Linux man-pages 6.20, dated 2026-02-08 β shoutout to the man-pages girlies for keeping the docs immaculate, they ate and left no crumbs. now go give your SIGSEGV handler somewhere cute to land. π¦β¨π§
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2001-2017, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms