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

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

Version: ~ [ linux-5.16-rc3 ] ~ [ linux-5.15.5 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.82 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.162 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.218 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.256 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.291 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.293 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ 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.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /* Copyright (c) 2016 Facebook
  3  */
  4 #include <linux/unistd.h>
  5 #include <linux/bpf.h>
  6 
  7 #include <stdio.h>
  8 #include <stdint.h>
  9 #include <unistd.h>
 10 #include <string.h>
 11 #include <errno.h>
 12 #include <fcntl.h>
 13 
 14 #include <bpf/bpf.h>
 15 
 16 static void usage(void)
 17 {
 18         printf("Usage: test_cgrp2_array_pin [...]\n");
 19         printf("       -F <file>   File to pin an BPF cgroup array\n");
 20         printf("       -U <file>   Update an already pinned BPF cgroup array\n");
 21         printf("       -v <value>  Full path of the cgroup2\n");
 22         printf("       -h          Display this help\n");
 23 }
 24 
 25 int main(int argc, char **argv)
 26 {
 27         const char *pinned_file = NULL, *cg2 = NULL;
 28         int create_array = 1;
 29         int array_key = 0;
 30         int array_fd = -1;
 31         int cg2_fd = -1;
 32         int ret = -1;
 33         int opt;
 34 
 35         while ((opt = getopt(argc, argv, "F:U:v:")) != -1) {
 36                 switch (opt) {
 37                 /* General args */
 38                 case 'F':
 39                         pinned_file = optarg;
 40                         break;
 41                 case 'U':
 42                         pinned_file = optarg;
 43                         create_array = 0;
 44                         break;
 45                 case 'v':
 46                         cg2 = optarg;
 47                         break;
 48                 default:
 49                         usage();
 50                         goto out;
 51                 }
 52         }
 53 
 54         if (!cg2 || !pinned_file) {
 55                 usage();
 56                 goto out;
 57         }
 58 
 59         cg2_fd = open(cg2, O_RDONLY);
 60         if (cg2_fd < 0) {
 61                 fprintf(stderr, "open(%s,...): %s(%d)\n",
 62                         cg2, strerror(errno), errno);
 63                 goto out;
 64         }
 65 
 66         if (create_array) {
 67                 array_fd = bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,
 68                                           sizeof(uint32_t), sizeof(uint32_t),
 69                                           1, 0);
 70                 if (array_fd < 0) {
 71                         fprintf(stderr,
 72                                 "bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,...): %s(%d)\n",
 73                                 strerror(errno), errno);
 74                         goto out;
 75                 }
 76         } else {
 77                 array_fd = bpf_obj_get(pinned_file);
 78                 if (array_fd < 0) {
 79                         fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
 80                                 pinned_file, strerror(errno), errno);
 81                         goto out;
 82                 }
 83         }
 84 
 85         ret = bpf_map_update_elem(array_fd, &array_key, &cg2_fd, 0);
 86         if (ret) {
 87                 perror("bpf_map_update_elem");
 88                 goto out;
 89         }
 90 
 91         if (create_array) {
 92                 ret = bpf_obj_pin(array_fd, pinned_file);
 93                 if (ret) {
 94                         fprintf(stderr, "bpf_obj_pin(..., %s): %s(%d)\n",
 95                                 pinned_file, strerror(errno), errno);
 96                         goto out;
 97                 }
 98         }
 99 
100 out:
101         if (array_fd != -1)
102                 close(array_fd);
103         if (cg2_fd != -1)
104                 close(cg2_fd);
105         return ret;
106 }
107 

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

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | 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.

osdn.jp