Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
0003  */
0004 
0005 #define _GNU_SOURCE
0006 #include <stdio.h>
0007 #include <unistd.h>
0008 #include <bpf/bpf.h>
0009 #include <bpf/libbpf.h>
0010 #include "cgroup_helpers.h"
0011 
0012 #define CGROUP_PATH     "/my-cgroup"
0013 
0014 int main(int argc, char **argv)
0015 {
0016     pid_t remote_pid, local_pid = getpid();
0017     struct bpf_link *link = NULL;
0018     struct bpf_program *prog;
0019     int cg2, idx = 0, rc = 1;
0020     struct bpf_object *obj;
0021     char filename[256];
0022     int map_fd[2];
0023 
0024     snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0025     obj = bpf_object__open_file(filename, NULL);
0026     if (libbpf_get_error(obj)) {
0027         fprintf(stderr, "ERROR: opening BPF object file failed\n");
0028         return 0;
0029     }
0030 
0031     prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
0032     if (!prog) {
0033         printf("finding a prog in obj file failed\n");
0034         goto cleanup;
0035     }
0036 
0037     /* load BPF program */
0038     if (bpf_object__load(obj)) {
0039         fprintf(stderr, "ERROR: loading BPF object file failed\n");
0040         goto cleanup;
0041     }
0042 
0043     map_fd[0] = bpf_object__find_map_fd_by_name(obj, "cgroup_map");
0044     map_fd[1] = bpf_object__find_map_fd_by_name(obj, "perf_map");
0045     if (map_fd[0] < 0 || map_fd[1] < 0) {
0046         fprintf(stderr, "ERROR: finding a map in obj file failed\n");
0047         goto cleanup;
0048     }
0049 
0050     link = bpf_program__attach(prog);
0051     if (libbpf_get_error(link)) {
0052         fprintf(stderr, "ERROR: bpf_program__attach failed\n");
0053         link = NULL;
0054         goto cleanup;
0055     }
0056 
0057     if (setup_cgroup_environment())
0058         goto err;
0059 
0060     cg2 = create_and_get_cgroup(CGROUP_PATH);
0061 
0062     if (cg2 < 0)
0063         goto err;
0064 
0065     if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
0066         log_err("Adding target cgroup to map");
0067         goto err;
0068     }
0069 
0070     if (join_cgroup(CGROUP_PATH))
0071         goto err;
0072 
0073     /*
0074      * The installed helper program catched the sync call, and should
0075      * write it to the map.
0076      */
0077 
0078     sync();
0079     bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
0080 
0081     if (local_pid != remote_pid) {
0082         fprintf(stderr,
0083             "BPF Helper didn't write correct PID to map, but: %d\n",
0084             remote_pid);
0085         goto err;
0086     }
0087 
0088     /* Verify the negative scenario; leave the cgroup */
0089     if (join_cgroup("/"))
0090         goto err;
0091 
0092     remote_pid = 0;
0093     bpf_map_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY);
0094 
0095     sync();
0096     bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
0097 
0098     if (local_pid == remote_pid) {
0099         fprintf(stderr, "BPF cgroup negative test did not work\n");
0100         goto err;
0101     }
0102 
0103     rc = 0;
0104 
0105 err:
0106     close(cg2);
0107     cleanup_cgroup_environment();
0108 
0109 cleanup:
0110     bpf_link__destroy(link);
0111     bpf_object__close(obj);
0112     return rc;
0113 }