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