1 /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
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.
10 #include <linux/bpf.h>
14 #include <linux/bpf.h>
15 #include "cgroup_helpers.h"
17 #define CGROUP_PATH "/my-cgroup"
19 int main(int argc
, char **argv
)
21 pid_t remote_pid
, local_pid
= getpid();
22 int cg2
, idx
= 0, rc
= 0;
25 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
26 if (load_bpf_file(filename
)) {
27 printf("%s", bpf_log_buf
);
31 if (setup_cgroup_environment())
34 cg2
= create_and_get_cgroup(CGROUP_PATH
);
39 if (bpf_map_update_elem(map_fd
[0], &idx
, &cg2
, BPF_ANY
)) {
40 log_err("Adding target cgroup to map");
44 if (join_cgroup(CGROUP_PATH
))
48 * The installed helper program catched the sync call, and should
49 * write it to the map.
53 bpf_map_lookup_elem(map_fd
[1], &idx
, &remote_pid
);
55 if (local_pid
!= remote_pid
) {
57 "BPF Helper didn't write correct PID to map, but: %d\n",
62 /* Verify the negative scenario; leave the cgroup */
67 bpf_map_update_elem(map_fd
[1], &idx
, &remote_pid
, BPF_ANY
);
70 bpf_map_lookup_elem(map_fd
[1], &idx
, &remote_pid
);
72 if (local_pid
== remote_pid
) {
73 fprintf(stderr
, "BPF cgroup negative test did not work\n");
83 cleanup_cgroup_environment();