SIGALTSTACK(3POSIX)
π SIGALTSTACK π β the POSIX Programmer's Manual page, but make it make sense
π PROLOG
okay so real talk before we even start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for details on how Linux actually does it), or the interface might not even be implemented on Linux at all. π Just so we're clear, bestie.
β¨ NAME β¨
sigaltstack β set and get signal alternate stack context
π«Ά SYNOPSIS π«Ά
#include <signal.h>
int sigaltstack(const stack_t *restrict ss, stack_t *restrict oss);
π DESCRIPTION π
So the sigaltstack() function lets a process define and examine the state of an alternate stack for signal handlers β for the current thread, specifically. And any signal that's been explicitly declared to execute on the alternate stack? Yeah, it gets delivered on the alternate stack. no cap. π₯
If ss is not a null pointer, it points to a stack_t structure that specifies the alternate signal stack that shall take effect upon return from sigaltstack(). The ss_flags member specifies the new stack state:
- If
ss_flagsis set toSS_DISABLE, the stack is disabled andss_spandss_sizeare straight up ignored. - Otherwise the stack shall be enabled, and the
ss_spandss_sizemembers specify the new address and size of the stack.
The range of addresses starting at ss_sp up to but not including ss_sp+ss_size is available to the implementation for use as the stack. And listen β this function makes no assumptions regarding which end is the stack base and in which direction the stack grows as items are pushed. It's very demure, very unopinionated. π
If oss is not a null pointer, then upon successful completion it shall point to a stack_t structure that specifies the alternate signal stack that was in effect prior to the call to sigaltstack(). The ss_sp and ss_size members specify the address and size of that stack. The ss_flags member specifies the stack's state, and may contain one of these iconic values:
SS_ONSTACKβ The process is currently executing on the alternate signal stack. Attempts to modify the alternate signal stack while the process is executing on it fail. This flag shall not be modified by processes. hands off bestie πSS_DISABLEβ The alternate signal stack is currently disabled.
The value SIGSTKSZ is a system default specifying the number of bytes that would be used to cover the usual case when you're manually allocating an alternate stack area. The value MINSIGSTKSZ is defined to be the minimum stack size for a signal handler. So when you're computing an alternate stack size, a program should add that amount to its stack requirements to allow for the system implementation overhead. (Budget for it, babe. β¨) The constants SS_ONSTACK, SS_DISABLE, SIGSTKSZ, and MINSIGSTKSZ are all defined in <signal.h>.
After a successful call to one of the exec functions, there are no alternate signal stacks in the new process image. fresh start energy, honestly.
In some implementations, a signal (whether or not indicated to execute on the alternate stack) shall always execute on the alternate stack if it is delivered while another signal is being caught using the alternate stack.
β οΈ Use of this function by library threads that are not bound to kernel-scheduled entities results in undefined behavior. Do not do this. I'm begging. π
π― RETURN VALUE π―
Upon successful completion, sigaltstack() shall return 0. Otherwise it shall return -1 and set errno to indicate the error. Simple as. π
π¨ ERRORS π¨
The sigaltstack() function shall fail if:
EINVALβ Thessargument is not a null pointer, and thess_flagsmember pointed to bysscontains flags other thanSS_DISABLE.ENOMEMβ The size of the alternate stack area is less thanMINSIGSTKSZ.EPERMβ An attempt was made to modify an active stack.
The following sections are informative. (iykyk π)
πΈ EXAMPLES πΈ
Allocating Memory for an Alternate Stack
The following example illustrates a method for allocating memory for an alternate stack. Copy it, it's cute:
#include <signal.h>
...
if ((sigstk.ss_sp = malloc(SIGSTKSZ)) == NULL)
/* Error return. */
sigstk.ss_size = SIGSTKSZ;
sigstk.ss_flags = 0;
if (sigaltstack(&sigstk,(stack_t *)0) < 0)
perror("sigaltstack");
π§ APPLICATION USAGE π§
On some implementations, stack space is automatically extended as needed. But on those implementations, automatic extension is typically not available for an alternate stack. And if the stack overflows? bestie. the behavior is undefined. π
π RATIONALE π
None. (Not me leaving it blank β that's literally what the page says. iconic minimalism. π )
π¦ FUTURE DIRECTIONS π¦
None.
π SEE ALSO π
- Section 2.4, Signal Concepts
execsigaction()sigsetjmp()- The Base Definitions volume of POSIX.1β2017,
<signal.h>
β COPYRIGHT β
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
okay that's the whole page, you ate that π«Ά this was brought to you by IEEE/The Open Group, dated 2017, and it's SIGALTSTACK(3POSIX) signing off. go handle your signals, queen πβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms