1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
4 #include <linux/unistd.h>
16 static void usage(void)
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");
25 int main(int argc, char **argv)
27 const char *pinned_file = NULL, *cg2 = NULL;
35 while ((opt = getopt(argc, argv, "F:U:v:")) != -1) {
54 if (!cg2 || !pinned_file) {
59 cg2_fd = open(cg2, O_RDONLY);
61 fprintf(stderr, "open(%s,...): %s(%d)\n",
62 cg2, strerror(errno), errno);
67 array_fd = bpf_map_create(BPF_MAP_TYPE_CGROUP_ARRAY, NULL,
68 sizeof(uint32_t), sizeof(uint32_t),
72 "bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,...): %s(%d)\n",
73 strerror(errno), errno);
77 array_fd = bpf_obj_get(pinned_file);
79 fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
80 pinned_file, strerror(errno), errno);
85 ret = bpf_map_update_elem(array_fd, &array_key, &cg2_fd, 0);
87 perror("bpf_map_update_elem");
92 ret = bpf_obj_pin(array_fd, pinned_file);
94 fprintf(stderr, "bpf_obj_pin(..., %s): %s(%d)\n",
95 pinned_file, strerror(errno), errno);