Back to home page

OSCL-LXR

 
 

    


0001 #define _GNU_SOURCE
0002 
0003 #include <stdio.h>
0004 #include <unistd.h>
0005 #include <bpf/libbpf.h>
0006 
0007 int main(int argc, char **argv)
0008 {
0009     struct bpf_link *link = NULL;
0010     struct bpf_program *prog;
0011     struct bpf_object *obj;
0012     char filename[256];
0013     char command[256];
0014     int ret = 0;
0015     FILE *f;
0016 
0017     if (!argv[1]) {
0018         fprintf(stderr, "ERROR: Run with the btrfs device argument!\n");
0019         return 0;
0020     }
0021 
0022     snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0023     obj = bpf_object__open_file(filename, NULL);
0024     if (libbpf_get_error(obj)) {
0025         fprintf(stderr, "ERROR: opening BPF object file failed\n");
0026         return 0;
0027     }
0028 
0029     prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
0030     if (!prog) {
0031         fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
0032         goto cleanup;
0033     }
0034 
0035     /* load BPF program */
0036     if (bpf_object__load(obj)) {
0037         fprintf(stderr, "ERROR: loading BPF object file failed\n");
0038         goto cleanup;
0039     }
0040 
0041     link = bpf_program__attach(prog);
0042     if (libbpf_get_error(link)) {
0043         fprintf(stderr, "ERROR: bpf_program__attach failed\n");
0044         link = NULL;
0045         goto cleanup;
0046     }
0047 
0048     snprintf(command, 256, "mount %s tmpmnt/", argv[1]);
0049     f = popen(command, "r");
0050     ret = pclose(f);
0051 
0052 cleanup:
0053     bpf_link__destroy(link);
0054     bpf_object__close(obj);
0055     return ret ? 0 : 1;
0056 }