~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/test_current_task_under_cgroup_user.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
  3  */
  4 
  5 #define _GNU_SOURCE
  6 #include <stdio.h>
  7 #include <unistd.h>
  8 #include <bpf/bpf.h>
  9 #include <bpf/libbpf.h>
 10 #include "cgroup_helpers.h"
 11 
 12 #define CGROUP_PATH             "/my-cgroup"
 13 
 14 int main(int argc, char **argv)
 15 {
 16         pid_t remote_pid, local_pid = getpid();
 17         int cg2 = -1, idx = 0, rc = 1;
 18         struct bpf_link *link = NULL;
 19         struct bpf_program *prog;
 20         struct bpf_object *obj;
 21         char filename[256];
 22         int map_fd[2];
 23 
 24         snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
 25         obj = bpf_object__open_file(filename, NULL);
 26         if (libbpf_get_error(obj)) {
 27                 fprintf(stderr, "ERROR: opening BPF object file failed\n");
 28                 return 0;
 29         }
 30 
 31         prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
 32         if (!prog) {
 33                 printf("finding a prog in obj file failed\n");
 34                 goto cleanup;
 35         }
 36 
 37         /* load BPF program */
 38         if (bpf_object__load(obj)) {
 39                 fprintf(stderr, "ERROR: loading BPF object file failed\n");
 40                 goto cleanup;
 41         }
 42 
 43         map_fd[0] = bpf_object__find_map_fd_by_name(obj, "cgroup_map");
 44         map_fd[1] = bpf_object__find_map_fd_by_name(obj, "perf_map");
 45         if (map_fd[0] < 0 || map_fd[1] < 0) {
 46                 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
 47                 goto cleanup;
 48         }
 49 
 50         link = bpf_program__attach(prog);
 51         if (libbpf_get_error(link)) {
 52                 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
 53                 link = NULL;
 54                 goto cleanup;
 55         }
 56 
 57         if (setup_cgroup_environment())
 58                 goto err;
 59 
 60         cg2 = create_and_get_cgroup(CGROUP_PATH);
 61 
 62         if (cg2 < 0)
 63                 goto err;
 64 
 65         if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
 66                 log_err("Adding target cgroup to map");
 67                 goto err;
 68         }
 69 
 70         if (join_cgroup(CGROUP_PATH))
 71                 goto err;
 72 
 73         /*
 74          * The installed helper program catched the sync call, and should
 75          * write it to the map.
 76          */
 77 
 78         sync();
 79         bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
 80 
 81         if (local_pid != remote_pid) {
 82                 fprintf(stderr,
 83                         "BPF Helper didn't write correct PID to map, but: %d\n",
 84                         remote_pid);
 85                 goto err;
 86         }
 87 
 88         /* Verify the negative scenario; leave the cgroup */
 89         if (join_cgroup("/"))
 90                 goto err;
 91 
 92         remote_pid = 0;
 93         bpf_map_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY);
 94 
 95         sync();
 96         bpf_map_lookup_elem(map_fd[1], &idx, &remote_pid);
 97 
 98         if (local_pid == remote_pid) {
 99                 fprintf(stderr, "BPF cgroup negative test did not work\n");
100                 goto err;
101         }
102 
103         rc = 0;
104 
105 err:
106         if (cg2 != -1)
107                 close(cg2);
108 
109         cleanup_cgroup_environment();
110 
111 cleanup:
112         bpf_link__destroy(link);
113         bpf_object__close(obj);
114         return rc;
115 }
116 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php