Skip to content

Instantly share code, notes, and snippets.

@jedisct1
Last active April 25, 2019 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedisct1/8220833d3ec09020389447a93ac905ba to your computer and use it in GitHub Desktop.
Save jedisct1/8220833d3ec09020389447a93ac905ba to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void
handler(int sig)
{
stack_t another_stack;
(void) sig;
another_stack.ss_sp = malloc(SIGSTKSZ);
another_stack.ss_size = SIGSTKSZ;
another_stack.ss_flags = 0;
if (sigaltstack(&another_stack, NULL) != 0) {
perror("sigaltstack in handler"); /* fails with EPERM as expected */
_exit(1);
}
}
int
main(void)
{
struct sigaction sa;
stack_t signal_stack;
signal_stack.ss_sp = malloc(SIGSTKSZ);
signal_stack.ss_size = SIGSTKSZ;
signal_stack.ss_flags = 0;
if (sigaltstack(&signal_stack, NULL) != 0) {
perror("sigaltstack");
}
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK;
sigaction(SIGALRM, &sa, NULL);
alarm(1);
sleep(2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment