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

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

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ 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) 2017 Facebook
  3  */
  4 #include <stdio.h>
  5 #include <unistd.h>
  6 #include <fcntl.h>
  7 #include <stdlib.h>
  8 #include <signal.h>
  9 #include <linux/bpf.h>
 10 #include <string.h>
 11 #include <linux/perf_event.h>
 12 #include <errno.h>
 13 #include <assert.h>
 14 #include <stdbool.h>
 15 #include <sys/resource.h>
 16 #include <bpf/bpf.h>
 17 #include "bpf_load.h"
 18 
 19 /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
 20  * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
 21  */
 22 
 23 static void usage(const char *cmd)
 24 {
 25         printf("USAGE: %s [-i num_progs] [-h]\n", cmd);
 26         printf("       -i num_progs      # number of progs of the test\n");
 27         printf("       -h                # help\n");
 28 }
 29 
 30 static void verify_map(int map_id)
 31 {
 32         __u32 key = 0;
 33         __u32 val;
 34 
 35         if (bpf_map_lookup_elem(map_id, &key, &val) != 0) {
 36                 fprintf(stderr, "map_lookup failed: %s\n", strerror(errno));
 37                 return;
 38         }
 39         if (val == 0) {
 40                 fprintf(stderr, "failed: map #%d returns value 0\n", map_id);
 41                 return;
 42         }
 43         val = 0;
 44         if (bpf_map_update_elem(map_id, &key, &val, BPF_ANY) != 0) {
 45                 fprintf(stderr, "map_update failed: %s\n", strerror(errno));
 46                 return;
 47         }
 48 }
 49 
 50 static int test(char *filename, int num_progs)
 51 {
 52         int i, fd, map0_fds[num_progs], map1_fds[num_progs];
 53 
 54         for (i = 0; i < num_progs; i++) {
 55                 if (load_bpf_file(filename)) {
 56                         fprintf(stderr, "%s", bpf_log_buf);
 57                         return 1;
 58                 }
 59                 printf("prog #%d: map ids %d %d\n", i, map_fd[0], map_fd[1]);
 60                 map0_fds[i] = map_fd[0];
 61                 map1_fds[i] = map_fd[1];
 62         }
 63 
 64         /* current load_bpf_file has perf_event_open default pid = -1
 65          * and cpu = 0, which permits attached bpf execution on
 66          * all cpus for all pid's. bpf program execution ignores
 67          * cpu affinity.
 68          */
 69         /* trigger some "open" operations */
 70         fd = open(filename, O_RDONLY);
 71         if (fd < 0) {
 72                 fprintf(stderr, "open failed: %s\n", strerror(errno));
 73                 return 1;
 74         }
 75         close(fd);
 76 
 77         /* verify the map */
 78         for (i = 0; i < num_progs; i++) {
 79                 verify_map(map0_fds[i]);
 80                 verify_map(map1_fds[i]);
 81         }
 82 
 83         return 0;
 84 }
 85 
 86 int main(int argc, char **argv)
 87 {
 88         struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
 89         int opt, num_progs = 1;
 90         char filename[256];
 91 
 92         while ((opt = getopt(argc, argv, "i:h")) != -1) {
 93                 switch (opt) {
 94                 case 'i':
 95                         num_progs = atoi(optarg);
 96                         break;
 97                 case 'h':
 98                 default:
 99                         usage(argv[0]);
100                         return 0;
101                 }
102         }
103 
104         setrlimit(RLIMIT_MEMLOCK, &r);
105         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
106 
107         return test(filename, num_progs);
108 }
109 

~ [ 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