The Pedigree Project 0.1
child-wait-contract-test/main.c
1#define _GNU_SOURCE
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5
6#include "contract.h"
7
8int main(int argc, char** argv) {
9 setvbuf(stdout, NULL, _IONBF, 0);
10 if (argc > 2)
11 return 2;
12 cw_page = (size_t)sysconf(_SC_PAGESIZE);
13 if (!cw_page || cw_page > 65536)
14 return 2;
15 const struct {
16 const char* name;
17 int (*run)(void);
18 } families[] = {{"selectors", cw_selectors}, {"events", cw_events},
19 {"lifetime", cw_lifetime}, {"copyout", cw_copyout},
20 {"errors", cw_errors}, {"interruption", cw_interruption}};
21 int selected = 0;
22 for (size_t i = 0; i < sizeof(families) / sizeof(families[0]); ++i) {
23 if (argc == 2 && strcmp(argv[1], "all") && strcmp(argv[1], families[i].name))
24 continue;
25 ++selected;
26 printf("CHILD-WAIT-CONTRACT: BEGIN %s\n", families[i].name);
27 pid_t pid = fork();
28 if (!pid) {
29 alarm(40);
30 int result = families[i].run();
31 fflush(stderr);
32 _exit(result ? 1 : 0);
33 }
34 int status = pid < 0 ? -1 : cw_reap(pid, 45000);
35 printf("CHILD-WAIT-CONTRACT: %s %s status=%d\n", status ? "FAIL" : "PASS", families[i].name,
36 status);
37 if (status)
38 return 1;
39 }
40 if (!selected)
41 return 2;
42 puts("CHILD-WAIT-CONTRACT: END PASS");
43 return 0;
44}