Skip to content

Instantly share code, notes, and snippets.

@yani-
Created July 4, 2018 14:12
Show Gist options
  • Save yani-/b849cc0bedfd63d423f035bd84dd227f to your computer and use it in GitHub Desktop.
Save yani-/b849cc0bedfd63d423f035bd84dd227f to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
extern char **environ;
static void
die(const char *s)
{
perror(s);
exit(EXIT_FAILURE);
}
static void
printsigstack(const char *prefix)
{
stack_t ss;
if (sigaltstack(NULL, &ss) < 0)
die("sigaltstack get");
printf("%s - ss_sp=%p ss_size=%lu ss_flags=0x%x\n", prefix, ss.ss_sp,
(unsigned long)ss.ss_size, (unsigned int)ss.ss_flags);
}
static void
setstack(void *arg)
{
stack_t ss;
ss.ss_sp = malloc(SIGSTKSZ);
if (ss.ss_sp == NULL)
die("malloc");
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) < 0)
die("sigaltstack set");
}
static void *
thread(void *arg)
{
char *argv[] = { "./test-sigs", "no-more", 0 };
printsigstack("thread - before setstack");
setstack(arg);
printsigstack("thread - after setstack + before execve");
execve(argv[0], &argv[0], environ);
die("exec failed");
return NULL;
}
int
main(int argc, char** argv)
{
int i, j;
int alloc_size = 32 * 1024;
pthread_t tid;
char buf[1024];
void *arg;
printf("argc: %d, %s\n", argc, argv[0]);
printsigstack("main");
if (argc != 1)
return 0;
j = pthread_create(&tid, NULL, thread, NULL);
if (j != 0) {
errno = i;
die("pthread_create");
}
j = pthread_join(tid, NULL);
if (j != 0) {
errno = j;
die("pthread_join");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment