blob: 46c815ef1d6cbe3a82499c4435df9ff23e1db164 [file] [log] [blame]
Jason Evans174c0c32016-04-25 23:14:40 -07001#include "test/jemalloc_test.h"
2
Jason Evans1eb46ab2016-05-03 17:18:34 -07003#ifndef _WIN32
Jason Evans174c0c32016-04-25 23:14:40 -07004#include <sys/wait.h>
Jason Evans1eb46ab2016-05-03 17:18:34 -07005#endif
Jason Evans174c0c32016-04-25 23:14:40 -07006
7TEST_BEGIN(test_fork)
8{
Jason Evans1eb46ab2016-05-03 17:18:34 -07009#ifndef _WIN32
Jason Evans174c0c32016-04-25 23:14:40 -070010 void *p;
11 pid_t pid;
12
13 p = malloc(1);
14 assert_ptr_not_null(p, "Unexpected malloc() failure");
15
16 pid = fork();
Jason Evans108c4a12016-04-26 10:47:22 -070017
18 free(p);
19
20 p = malloc(64);
21 assert_ptr_not_null(p, "Unexpected malloc() failure");
22 free(p);
23
Jason Evans174c0c32016-04-25 23:14:40 -070024 if (pid == -1) {
25 /* Error. */
26 test_fail("Unexpected fork() failure");
27 } else if (pid == 0) {
28 /* Child. */
29 exit(0);
30 } else {
31 int status;
32
33 /* Parent. */
Jason Evans108c4a12016-04-26 10:47:22 -070034 while (true) {
Jason Evans174c0c32016-04-25 23:14:40 -070035 if (waitpid(pid, &status, 0) == -1)
36 test_fail("Unexpected waitpid() failure");
Jason Evans108c4a12016-04-26 10:47:22 -070037 if (WIFSIGNALED(status)) {
38 test_fail("Unexpected child termination due to "
39 "signal %d", WTERMSIG(status));
40 break;
41 }
42 if (WIFEXITED(status)) {
43 if (WEXITSTATUS(status) != 0) {
44 test_fail(
45 "Unexpected child exit value %d",
46 WEXITSTATUS(status));
47 }
48 break;
49 }
50 }
Jason Evans174c0c32016-04-25 23:14:40 -070051 }
Jason Evans1eb46ab2016-05-03 17:18:34 -070052#else
53 test_skip("fork(2) is irrelevant to Windows");
54#endif
Jason Evans174c0c32016-04-25 23:14:40 -070055}
56TEST_END
57
58int
59main(void)
60{
61
62 return (test(
63 test_fork));
64}