The Pedigree Project 0.1
fanotify-handle-contract-test/main.c
1#define _GNU_SOURCE
2#include <signal.h>
3#include <string.h>
4#include <unistd.h>
5
6#include "contract.h"
7
8size_t fh_page;
9static const char *persist_base, *persist_token;
10static int persist_write;
11
12static int run(const char* name, int (*test)(void)) {
13 printf("FANOTIFY-HANDLE-CONTRACT: BEGIN %s\n", name);
14 fflush(stdout);
15 pid_t child = fork();
16 if (!child) {
17 alarm(40);
18 int result = test();
19 fflush(stdout);
20 fflush(stderr);
21 _exit(result ? 1 : 0);
22 }
23 int result = child > 0 ? fh_reap(child, 45000) : -1;
24 printf("FANOTIFY-HANDLE-CONTRACT: %s %s status=%d\n", result ? "FAIL" : "PASS", name, result);
25 fflush(stdout);
26 return result;
27}
28static int persistence(void) {
29 return fh_persistence(persist_base, persist_token, persist_write);
30}
31int main(int argc, char** argv) {
32 fh_page = (size_t)sysconf(_SC_PAGESIZE);
33 if (fh_page < 512 || fh_page > 65536 || (fh_page & (fh_page - 1)))
34 return 2;
35 signal(SIGPIPE, SIG_IGN);
36 if (argc > 1 && !strcmp(argv[1], "fanotify-exec"))
37 return fh_exec(argc, argv);
38 if (argc == 4 && (!strcmp(argv[1], "persist-write") || !strcmp(argv[1], "persist-read"))) {
39 persist_base = argv[2];
40 persist_token = argv[3];
41 persist_write = !strcmp(argv[1], "persist-write");
42 if (run(argv[1], persistence))
43 return 1;
44 printf("FANOTIFY-HANDLE-PERSIST: END %s PASS token=%s\n", persist_write ? "WRITE" : "READ",
45 persist_token);
46 return 0;
47 }
48 if (argc > 2)
49 return 2;
50 static const struct {
51 const char* name;
52 int (*test)(void);
53 } suites[] = {{"handles", fh_handles}, {"admission", fh_admission}, {"events", fh_events},
54 {"queue", fh_queue}, {"lifetime", fh_lifetime}, {"waits", fh_waits}};
55 int found = 0;
56 for (size_t n = 0; n < sizeof(suites) / sizeof(suites[0]); ++n) {
57 if (argc == 2 && strcmp(argv[1], "all") && strcmp(argv[1], suites[n].name))
58 continue;
59 found = 1;
60 if (run(suites[n].name, suites[n].test))
61 return 1;
62 }
63 if (!found)
64 return 2;
65 puts("FANOTIFY-HANDLE-CONTRACT: END PASS");
66 return 0;
67}