summaryrefslogtreecommitdiff
path: root/test/setjmp/tst-vfork-longjmp.c
blob: 644379245541ee09008c2caa1d886a68a192f0b9 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* make sure we can vfork/exec across setjmp/longjmp's
 * and make sure signal block masks don't get corrupted
 * in the process.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>

int verbose = 0;

int execute_child(const char *prog)
{
	int status;
	pid_t child;
	child = vfork();
	if (child == 0) {
		execlp(prog, prog, NULL);
		perror("Could not execute specified prog");
		_exit(1);
	} else if (child == 1)
		return 1;
	wait(&status);
	return WEXITSTATUS(status);
}

sigset_t orig_mask;

int check_sig_mask(void)
{
	int status;
	pid_t child;

	child = vfork();
	if (child == 0) {
		int ret;
		sigset_t child_mask;
		memset(&child_mask, 0x00, sizeof(child_mask));
		ret = sigprocmask(SIG_BLOCK, NULL, &child_mask);
		if (ret != 0) {
			perror("could not get child sig block mask");
			_exit(1);
		}
		ret = memcmp(&orig_mask, &child_mask, sizeof(orig_mask));
		if (verbose) {
			printf("sigmsk: %08lx%08lx ", child_mask.__val[1], child_mask.__val[0]);
			printf("sigmsk: %08lx%08lx ", orig_mask.__val[1], orig_mask.__val[0]);
			printf("%i\n", ret);
		}
		_exit(ret);
	} else if (child == 1)
		return 1;
	wait(&status);
	return WEXITSTATUS(status);
}

int main(int argc, char *argv[])
{
	const char *prog;
	jmp_buf env;
	sigjmp_buf sigenv;
	int cnt, max, ret;

	memset(&orig_mask, 0x00, sizeof(orig_mask));
	ret = sigprocmask(SIG_BLOCK, NULL, &orig_mask);
	if (ret != 0) {
		perror("could not get orig sig block mask");
		return 1;
	}

	prog = (argc > 1 ? argv[1] : "true");
	ret = 0;
	verbose = 0;
	max = 10;

	/* test vfork() instead of sigsetjmp/siglongjmp */
	cnt = 0;
	sigsetjmp(sigenv, 1);
	++cnt;
	if (verbose)
		printf("sigsetjmp loop %i\n", cnt);
	ret |= check_sig_mask();
	ret |= execute_child(prog);
	if (cnt < max)
		siglongjmp(sigenv, 0);

	/* test vfork() inside of setjmp/longjmp */
	cnt = 0;
	setjmp(env);
	++cnt;
	if (verbose)
		printf("setjmp loop %i\n", cnt);
	ret |= check_sig_mask();
	ret |= execute_child(prog);
	if (cnt < max)
		longjmp(env, 0);

	return ret;
}