blob: 65b5fb51c1dbc5b34dcf4dc01e6d6e16eb43b265 [file] [log] [blame]
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -07001/* 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 Dhillon9e6e60e2016-08-12 08:57:04 -070014#include <linux/bpf.h>
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080015#include "cgroup_helpers.h"
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070016
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080017#define CGROUP_PATH "/my-cgroup"
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070018
19int main(int argc, char **argv)
20{
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070021 pid_t remote_pid, local_pid = getpid();
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080022 int cg2, idx = 0, rc = 0;
23 char filename[256];
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070024
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 Dhillon1a922fe2016-12-02 02:42:18 -080031 if (setup_cgroup_environment())
32 goto err;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070033
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080034 cg2 = create_and_get_cgroup(CGROUP_PATH);
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070035
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080036 if (!cg2)
37 goto err;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070038
Joe Stringerd40fc182016-12-14 14:43:38 -080039 if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070040 log_err("Adding target cgroup to map");
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080041 goto err;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070042 }
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080043
44 if (join_cgroup(CGROUP_PATH))
45 goto err;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070046
47 /*
48 * The installed helper program catched the sync call, and should
49 * write it to the map.
50 */
51
52 sync();
Joe Stringerd40fc182016-12-14 14:43:38 -080053 bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070054
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 Dhillon1a922fe2016-12-02 02:42:18 -080059 goto err;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070060 }
61
62 /* Verify the negative scenario; leave the cgroup */
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080063 if (join_cgroup("/"))
64 goto err;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070065
66 remote_pid = 0;
Joe Stringerd40fc182016-12-14 14:43:38 -080067 bpf_map_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY);
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070068
69 sync();
Joe Stringerd40fc182016-12-14 14:43:38 -080070 bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070071
72 if (local_pid == remote_pid) {
73 fprintf(stderr, "BPF cgroup negative test did not work\n");
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080074 goto err;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070075 }
76
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080077 goto out;
78err:
79 rc = 1;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070080
Sargun Dhillon1a922fe2016-12-02 02:42:18 -080081out:
82 close(cg2);
83 cleanup_cgroup_environment();
84 return rc;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070085}