summaryrefslogtreecommitdiffstats
path: root/test/unit/fork.c
blob: afe2214139259b9b6015ea98f67b5480e534aad2 (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
#include "test/jemalloc_test.h"

#ifndef _WIN32
#include <sys/wait.h>
#endif

TEST_BEGIN(test_fork) {
#ifndef _WIN32
	void *p;
	pid_t pid;

	/* Set up a manually managed arena for test. */
	unsigned arena_ind;
	size_t sz = sizeof(unsigned);
	assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
	    0, "Unexpected mallctl() failure");

	/* Migrate to the new arena. */
	unsigned old_arena_ind;
	sz = sizeof(old_arena_ind);
	assert_d_eq(mallctl("thread.arena", (void *)&old_arena_ind, &sz,
	    (void *)&arena_ind, sizeof(arena_ind)), 0,
	    "Unexpected mallctl() failure");

	p = malloc(1);
	assert_ptr_not_null(p, "Unexpected malloc() failure");

	pid = fork();

	free(p);

	p = malloc(64);
	assert_ptr_not_null(p, "Unexpected malloc() failure");
	free(p);

	if (pid == -1) {
		/* Error. */
		test_fail("Unexpected fork() failure");
	} else if (pid == 0) {
		/* Child. */
		_exit(0);
	} else {
		int status;

		/* Parent. */
		while (true) {
			if (waitpid(pid, &status, 0) == -1) {
				test_fail("Unexpected waitpid() failure");
			}
			if (WIFSIGNALED(status)) {
				test_fail("Unexpected child termination due to "
				    "signal %d", WTERMSIG(status));
				break;
			}
			if (WIFEXITED(status)) {
				if (WEXITSTATUS(status) != 0) {
					test_fail(
					    "Unexpected child exit value %d",
					    WEXITSTATUS(status));
				}
				break;
			}
		}
	}
#else
	test_skip("fork(2) is irrelevant to Windows");
#endif
}
TEST_END

int
main(void) {
	return test(
	    test_fork);
}