tracex7_user.c 1.2 KB

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