]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - samples/bpf/test_current_task_under_cgroup_user.c
Merge tag 'gfs2-4.20.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2...
[mirror_ubuntu-eoan-kernel.git] / samples / bpf / test_current_task_under_cgroup_user.c
CommitLineData
9e6e60ec
SD
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>
2bf3e2ef 12#include <bpf/bpf.h>
9e6e60ec 13#include "bpf_load.h"
1a922fee 14#include "cgroup_helpers.h"
9e6e60ec 15
1a922fee 16#define CGROUP_PATH "/my-cgroup"
9e6e60ec
SD
17
18int main(int argc, char **argv)
19{
9e6e60ec 20 pid_t remote_pid, local_pid = getpid();
1a922fee
SD
21 int cg2, idx = 0, rc = 0;
22 char filename[256];
9e6e60ec
SD
23
24 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
25 if (load_bpf_file(filename)) {
26 printf("%s", bpf_log_buf);
27 return 1;
28 }
29
1a922fee
SD
30 if (setup_cgroup_environment())
31 goto err;
9e6e60ec 32
1a922fee 33 cg2 = create_and_get_cgroup(CGROUP_PATH);
9e6e60ec 34
1a922fee
SD
35 if (!cg2)
36 goto err;
9e6e60ec 37
d40fc181 38 if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
9e6e60ec 39 log_err("Adding target cgroup to map");
1a922fee 40 goto err;
9e6e60ec
SD
41 }
42
1a922fee
SD
43 if (join_cgroup(CGROUP_PATH))
44 goto err;
45
9e6e60ec
SD
46 /*
47 * The installed helper program catched the sync call, and should
48 * write it to the map.
49 */
50
51 sync();
d40fc181 52 bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
9e6e60ec
SD
53
54 if (local_pid != remote_pid) {
55 fprintf(stderr,
56 "BPF Helper didn't write correct PID to map, but: %d\n",
57 remote_pid);
1a922fee 58 goto err;
9e6e60ec
SD
59 }
60
61 /* Verify the negative scenario; leave the cgroup */
1a922fee
SD
62 if (join_cgroup("/"))
63 goto err;
9e6e60ec
SD
64
65 remote_pid = 0;
d40fc181 66 bpf_map_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY);
9e6e60ec
SD
67
68 sync();
d40fc181 69 bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
9e6e60ec
SD
70
71 if (local_pid == remote_pid) {
72 fprintf(stderr, "BPF cgroup negative test did not work\n");
1a922fee 73 goto err;
9e6e60ec
SD
74 }
75
1a922fee
SD
76 goto out;
77err:
78 rc = 1;
9e6e60ec 79
1a922fee
SD
80out:
81 close(cg2);
82 cleanup_cgroup_environment();
83 return rc;
9e6e60ec 84}