^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct bpf_link *link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) char command[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (!argv[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) fprintf(stderr, "ERROR: Run with the btrfs device argument!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (!prog) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) link = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (libbpf_get_error(link)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) fprintf(stderr, "ERROR: bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) snprintf(command, 256, "mount %s tmpmnt/", argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) f = popen(command, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ret = pclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) bpf_link__destroy(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return ret ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }