summaryrefslogtreecommitdiff
path: root/regress/sys/kern/stackjmp/stackjmp.c
blob: aa73c2d97a07116ae7ff2baf7bf06e0ba2af3a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*	$OpenBSD: stackjmp.c,v 1.1 2012/06/23 05:54:49 matthew Exp $	*/
#include <assert.h>
#include <setjmp.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

static jmp_buf jb;
static char buf[SIGSTKSZ];
static volatile int handled;

static int
isaltstack()
{
	stack_t os;
	assert(sigaltstack(NULL, &os) == 0);
	return (os.ss_flags & SS_ONSTACK) != 0;
}

static void
inthandler(int signo)
{
	assert(isaltstack());
	handled = 1;
	siglongjmp(jb, 1);
}

int
main()
{
	struct sigaction sa;
	stack_t stack;

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = inthandler;
	sa.sa_flags = SA_ONSTACK;
	assert(sigaction(SIGINT, &sa, NULL) == 0);

	memset(&stack, 0, sizeof(stack));
	stack.ss_sp = buf;
	stack.ss_size = sizeof(buf);
	stack.ss_flags = 0;
	assert(sigaltstack(&stack, NULL) == 0);

	assert(!isaltstack());
	sigsetjmp(jb, 1);
	assert(!isaltstack());
	if (!handled) {
		kill(getpid(), SIGINT);
		assert(0); /* Shouldn't reach here. */
	}

	return (0);
}