2016-04-26 14:14:40 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2016-05-04 08:18:34 +08:00
|
|
|
#ifndef _WIN32
|
2016-04-26 14:14:40 +08:00
|
|
|
#include <sys/wait.h>
|
2016-05-04 08:18:34 +08:00
|
|
|
#endif
|
2016-04-26 14:14:40 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_fork) {
|
2016-05-04 08:18:34 +08:00
|
|
|
#ifndef _WIN32
|
2016-04-26 14:14:40 +08:00
|
|
|
void *p;
|
|
|
|
pid_t pid;
|
|
|
|
|
2017-03-09 05:00:42 +08:00
|
|
|
/* 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");
|
|
|
|
|
2016-04-26 14:14:40 +08:00
|
|
|
p = malloc(1);
|
|
|
|
assert_ptr_not_null(p, "Unexpected malloc() failure");
|
|
|
|
|
|
|
|
pid = fork();
|
2016-04-27 01:47:22 +08:00
|
|
|
|
|
|
|
free(p);
|
|
|
|
|
|
|
|
p = malloc(64);
|
|
|
|
assert_ptr_not_null(p, "Unexpected malloc() failure");
|
|
|
|
free(p);
|
|
|
|
|
2016-04-26 14:14:40 +08:00
|
|
|
if (pid == -1) {
|
|
|
|
/* Error. */
|
|
|
|
test_fail("Unexpected fork() failure");
|
|
|
|
} else if (pid == 0) {
|
|
|
|
/* Child. */
|
2016-11-03 09:05:19 +08:00
|
|
|
_exit(0);
|
2016-04-26 14:14:40 +08:00
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Parent. */
|
2016-04-27 01:47:22 +08:00
|
|
|
while (true) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (waitpid(pid, &status, 0) == -1) {
|
2016-04-26 14:14:40 +08:00
|
|
|
test_fail("Unexpected waitpid() failure");
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-04-27 01:47:22 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 14:14:40 +08:00
|
|
|
}
|
2016-05-04 08:18:34 +08:00
|
|
|
#else
|
|
|
|
test_skip("fork(2) is irrelevant to Windows");
|
|
|
|
#endif
|
2016-04-26 14:14:40 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
|
|
|
test_fork);
|
2016-04-26 14:14:40 +08:00
|
|
|
}
|