summaryrefslogtreecommitdiff
path: root/setjmp.c
blob: b740be5bd3b0366d090d6dfee96fc3795470b3ca (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
55
56
57
58
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>

#define TEST2(c, l, ...) ((c) ? 1 : \
(err++,printf(__FILE__":"#l": "#c" failed: " __VA_ARGS__),putchar('\n'),0))
#define TEST1(c, l, ...) TEST2(c, l, __VA_ARGS__)
#define TEST(c, ...) TEST1(c, __LINE__, __VA_ARGS__)

#define TESTE(c) TEST(c, "errno = %s", strerror(errno))

int test_setjmp(void)
{
	volatile int err = 0;
	volatile int x = 0, r;
	jmp_buf jb;
	sigjmp_buf sjb;
	volatile sigset_t oldset;
	sigset_t set;

	if (!setjmp(jb)) {
		x = 1;
		longjmp(jb, 1);
	}
	TEST(x==1, "setjmp/longjmp seems to have been bypassed");

	x = 0;
	r = setjmp(jb);
	if (!x) {
		x = 1;
		longjmp(jb, 0);
	}
	TEST(r==1, "longjmp(jb, 0) caused setjmp to return %d", r);

	sigemptyset(&set);
	sigaddset(&set, SIGUSR1);
	sigprocmask(SIG_UNBLOCK, &set, &set);
	oldset = set;

	/* Improve the chances of catching failure of sigsetjmp to
	 * properly save the signal mask in the sigjmb_buf. */
	memset(&sjb, -1, sizeof sjb);

	if (!sigsetjmp(sjb, 1)) {
		sigemptyset(&set);
		sigaddset(&set, SIGUSR1);
		sigprocmask(SIG_BLOCK, &set, 0);
		siglongjmp(sjb, 1);
	}
	set = oldset;
	sigprocmask(SIG_SETMASK, &set, &set);
	TEST(sigismember(&set, SIGUSR1)==0, "siglongjmp failed to restore mask");

	return err;
}