1
2
3#define _GNU_SOURCE
4#include <sched.h>
5#include <test_progs.h>
6#include <time.h>
7#include <sys/mman.h>
8#include <sys/syscall.h>
9#include "fexit_sleep.lskel.h"
10
11static int do_sleep(void *skel)
12{
13 struct fexit_sleep *fexit_skel = skel;
14 struct timespec ts1 = { .tv_nsec = 1 };
15 struct timespec ts2 = { .tv_sec = 10 };
16
17 fexit_skel->bss->pid = getpid();
18 (void)syscall(__NR_nanosleep, &ts1, NULL);
19 (void)syscall(__NR_nanosleep, &ts2, NULL);
20 return 0;
21}
22
23#define STACK_SIZE (1024 * 1024)
24static char child_stack[STACK_SIZE];
25
26void test_fexit_sleep(void)
27{
28 struct fexit_sleep *fexit_skel = NULL;
29 int wstatus, duration = 0;
30 pid_t cpid;
31 int err, fexit_cnt;
32
33 fexit_skel = fexit_sleep__open_and_load();
34 if (CHECK(!fexit_skel, "fexit_skel_load", "fexit skeleton failed\n"))
35 goto cleanup;
36
37 err = fexit_sleep__attach(fexit_skel);
38 if (CHECK(err, "fexit_attach", "fexit attach failed: %d\n", err))
39 goto cleanup;
40
41 cpid = clone(do_sleep, child_stack + STACK_SIZE, CLONE_FILES | SIGCHLD, fexit_skel);
42 if (CHECK(cpid == -1, "clone", "%s\n", strerror(errno)))
43 goto cleanup;
44
45
46 while (READ_ONCE(fexit_skel->bss->fentry_cnt) != 2);
47 fexit_cnt = READ_ONCE(fexit_skel->bss->fexit_cnt);
48 if (CHECK(fexit_cnt != 1, "fexit_cnt", "%d", fexit_cnt))
49 goto cleanup;
50
51
52
53
54
55
56
57
58
59
60
61 close(fexit_skel->progs.nanosleep_fentry.prog_fd);
62 close(fexit_skel->progs.nanosleep_fexit.prog_fd);
63 fexit_sleep__detach(fexit_skel);
64
65
66 kill(cpid, 9);
67
68 if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", "%s\n", strerror(errno)))
69 goto cleanup;
70 if (CHECK(WEXITSTATUS(wstatus) != 0, "exitstatus", "failed"))
71 goto cleanup;
72
73
74
75
76 fexit_cnt = READ_ONCE(fexit_skel->bss->fexit_cnt);
77 if (CHECK(fexit_cnt != 1, "fexit_cnt", "%d", fexit_cnt))
78 goto cleanup;
79
80cleanup:
81 fexit_sleep__destroy(fexit_skel);
82}
83