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