1 /* Copyright (c) 2016 Facebook
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 #include <linux/unistd.h>
19 static void usage(void)
21 printf("Usage: test_cgrp2_array_pin [...]\n");
22 printf(" -F <file> File to pin an BPF cgroup array\n");
23 printf(" -U <file> Update an already pinned BPF cgroup array\n");
24 printf(" -v <value> Full path of the cgroup2\n");
25 printf(" -h Display this help\n");
28 int main(int argc
, char **argv
)
30 const char *pinned_file
= NULL
, *cg2
= NULL
;
38 while ((opt
= getopt(argc
, argv
, "F:U:v:")) != -1) {
57 if (!cg2
|| !pinned_file
) {
62 cg2_fd
= open(cg2
, O_RDONLY
);
64 fprintf(stderr
, "open(%s,...): %s(%d)\n",
65 cg2
, strerror(errno
), errno
);
70 array_fd
= bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY
,
71 sizeof(uint32_t), sizeof(uint32_t),
75 "bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,...): %s(%d)\n",
76 strerror(errno
), errno
);
80 array_fd
= bpf_obj_get(pinned_file
);
82 fprintf(stderr
, "bpf_obj_get(%s): %s(%d)\n",
83 pinned_file
, strerror(errno
), errno
);
88 ret
= bpf_map_update_elem(array_fd
, &array_key
, &cg2_fd
, 0);
90 perror("bpf_map_update_elem");
95 ret
= bpf_obj_pin(array_fd
, pinned_file
);
97 fprintf(stderr
, "bpf_obj_pin(..., %s): %s(%d)\n",
98 pinned_file
, strerror(errno
), errno
);