Blame view
samples/bpf/test_current_task_under_cgroup_user.c
1.7 KB
9e6e60ecb
|
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
|
14 |
#include <linux/bpf.h> |
1a922fee6
|
15 |
#include "cgroup_helpers.h" |
9e6e60ecb
|
16 |
|
1a922fee6
|
17 |
#define CGROUP_PATH "/my-cgroup" |
9e6e60ecb
|
18 19 20 |
int main(int argc, char **argv) { |
9e6e60ecb
|
21 |
pid_t remote_pid, local_pid = getpid(); |
1a922fee6
|
22 23 |
int cg2, idx = 0, rc = 0; char filename[256]; |
9e6e60ecb
|
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
|
30 31 |
if (setup_cgroup_environment()) goto err; |
9e6e60ecb
|
32 |
|
1a922fee6
|
33 |
cg2 = create_and_get_cgroup(CGROUP_PATH); |
9e6e60ecb
|
34 |
|
1a922fee6
|
35 36 |
if (!cg2) goto err; |
9e6e60ecb
|
37 |
|
d40fc181e
|
38 |
if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) { |
9e6e60ecb
|
39 |
log_err("Adding target cgroup to map"); |
1a922fee6
|
40 |
goto err; |
9e6e60ecb
|
41 |
} |
1a922fee6
|
42 43 |
if (join_cgroup(CGROUP_PATH)) goto err; |
9e6e60ecb
|
44 45 46 47 48 49 |
/* * The installed helper program catched the sync call, and should * write it to the map. */ sync(); |
d40fc181e
|
50 |
bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid); |
9e6e60ecb
|
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
|
57 |
goto err; |
9e6e60ecb
|
58 59 60 |
} /* Verify the negative scenario; leave the cgroup */ |
1a922fee6
|
61 62 |
if (join_cgroup("/")) goto err; |
9e6e60ecb
|
63 64 |
remote_pid = 0; |
d40fc181e
|
65 |
bpf_map_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY); |
9e6e60ecb
|
66 67 |
sync(); |
d40fc181e
|
68 |
bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid); |
9e6e60ecb
|
69 70 71 72 |
if (local_pid == remote_pid) { fprintf(stderr, "BPF cgroup negative test did not work "); |
1a922fee6
|
73 |
goto err; |
9e6e60ecb
|
74 |
} |
1a922fee6
|
75 76 77 |
goto out; err: rc = 1; |
9e6e60ecb
|
78 |
|
1a922fee6
|
79 80 81 82 |
out: close(cg2); cleanup_cgroup_environment(); return rc; |
9e6e60ecb
|
83 |
} |