sigaltstack(2) System Calls Manual sigaltstack(2) sigaltstack - LIBRARY Standard C library (libc, -lc) #include int sigaltstack(const stack_t *_Nullable restrict ss, stack_t *_Nullable restrict old_ss); glibc (. feature_test_macros(7)): sigaltstack(): _XOPEN_SOURCE >= 500 || /* glibc 2.12: */ _POSIX_C_SOURCE >= 200809L || /* glibc <= 2.19: */ _BSD_SOURCE sigaltstack() allows a thread to define a new alternate signal stack and/or retrieve the state of an existing alternate signal stack. An alternate signal stack is used during the execution of a signal handler if the establishment of that handler (see sigaction(2)) requested it. : 1. , . 2. sigaltstack() . 3. sigaction(2) ( SA_ONSTACK) , . ss , old_ss . - , NULL. stack_t, , : typedef struct { void *ss_sp; /* */ int ss_flags; /* */ size_t ss_size; /* */ } stack_t; : ss.ss_flags 0 : SS_AUTODISARM ( Linux 4.7) . , . swapcontext(3). , (switched-away). sigaltstack() EINVAL, . ss.ss_sp . , ss.ss_sp, , . ss.ss_size . SIGSTKSZ, MINSIGSTKSZ. , ss.ss_flags SS_DISABLE. ss.ss_flags ss. old_ss NULL, , sigaltstack(). old_ss.ss_sp old_ss.ss_size . old_ss.ss_flags : 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.) SS_DISABLE . 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 is also possible to set up a different alternative signal stack using a further call to sigaltstack(). SS_AUTODISARM (autodisarmed), . ss NULL, old_ss -- NULL, . sigaltstack() 0. -1, errno . EFAULT ss old_ss NULL . EINVAL ss NULL ss_flags . ENOMEM ss.ss_size MINSIGSTKSZ. 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). attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |sigaltstack() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ POSIX.1-2008. SS_AUTODISARM is a Linux extension. POSIX.1-2001, SUSv2, SVr4. 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. 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. , Linux, . sigaltstack() . 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. 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 include CLONE_VM and do not include CLONE_VFORK, in which case any alternate signal stack that was established in the parent is disabled in the child process. sigaltstack() sigstack(). glibc sigstack(). sigaltstack(). sigstack() 4.2BSD. , , . Linux 2.2 ss.sa_flags SS_DISABLE. Linux 2.4 sigaltstack() ss.ss_flags==SS_ONSTACK ss.ss_flags==0 (. ., SS_ONSTACK ss.ss_flags ). POSIX.1 SS_ONSTACK old_ss.ss_flags . Linux ss.ss_flags, , , ss.ss_flags SS_ONSTACK. sigaltstack() ( sigaction(2)) , SIGSEGV: 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); } execve(2), setrlimit(2), sigaction(2), siglongjmp(3), sigsetjmp(3), signal(7) Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , <>. Linux man-pages 6.8 2 2024 . sigaltstack(2)