Blame view
samples/bpf/test_current_task_under_cgroup_user.c
1.53 KB
25763b3c8
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
9e6e60ecb
|
2 |
/* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me> |
9e6e60ecb
|
3 4 5 6 7 8 |
*/ #define _GNU_SOURCE #include <stdio.h> #include <linux/bpf.h> #include <unistd.h> |
2bf3e2ef4
|
9 |
#include <bpf/bpf.h> |
9e6e60ecb
|
10 |
#include "bpf_load.h" |
1a922fee6
|
11 |
#include "cgroup_helpers.h" |
9e6e60ecb
|
12 |
|
1a922fee6
|
13 |
#define CGROUP_PATH "/my-cgroup" |
9e6e60ecb
|
14 15 16 |
int main(int argc, char **argv) { |
9e6e60ecb
|
17 |
pid_t remote_pid, local_pid = getpid(); |
1a922fee6
|
18 19 |
int cg2, idx = 0, rc = 0; char filename[256]; |
9e6e60ecb
|
20 21 22 23 24 25 |
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); if (load_bpf_file(filename)) { printf("%s", bpf_log_buf); return 1; } |
1a922fee6
|
26 27 |
if (setup_cgroup_environment()) goto err; |
9e6e60ecb
|
28 |
|
1a922fee6
|
29 |
cg2 = create_and_get_cgroup(CGROUP_PATH); |
9e6e60ecb
|
30 |
|
a8911d6d5
|
31 |
if (cg2 < 0) |
1a922fee6
|
32 |
goto err; |
9e6e60ecb
|
33 |
|
d40fc181e
|
34 |
if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) { |
9e6e60ecb
|
35 |
log_err("Adding target cgroup to map"); |
1a922fee6
|
36 |
goto err; |
9e6e60ecb
|
37 |
} |
1a922fee6
|
38 39 |
if (join_cgroup(CGROUP_PATH)) goto err; |
9e6e60ecb
|
40 41 42 43 44 45 |
/* * The installed helper program catched the sync call, and should * write it to the map. */ sync(); |
d40fc181e
|
46 |
bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid); |
9e6e60ecb
|
47 48 49 50 51 52 |
if (local_pid != remote_pid) { fprintf(stderr, "BPF Helper didn't write correct PID to map, but: %d ", remote_pid); |
1a922fee6
|
53 |
goto err; |
9e6e60ecb
|
54 55 56 |
} /* Verify the negative scenario; leave the cgroup */ |
1a922fee6
|
57 58 |
if (join_cgroup("/")) goto err; |
9e6e60ecb
|
59 60 |
remote_pid = 0; |
d40fc181e
|
61 |
bpf_map_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY); |
9e6e60ecb
|
62 63 |
sync(); |
d40fc181e
|
64 |
bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid); |
9e6e60ecb
|
65 66 67 68 |
if (local_pid == remote_pid) { fprintf(stderr, "BPF cgroup negative test did not work "); |
1a922fee6
|
69 |
goto err; |
9e6e60ecb
|
70 |
} |
1a922fee6
|
71 72 73 |
goto out; err: rc = 1; |
9e6e60ecb
|
74 |
|
1a922fee6
|
75 76 77 78 |
out: close(cg2); cleanup_cgroup_environment(); return rc; |
9e6e60ecb
|
79 |
} |