xref: /openbmc/linux/samples/bpf/tracex7_user.c (revision c4f7ac64)
1 #define _GNU_SOURCE
2 
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <bpf/libbpf.h>
6 
7 int main(int argc, char **argv)
8 {
9 	struct bpf_link *link = NULL;
10 	struct bpf_program *prog;
11 	struct bpf_object *obj;
12 	char filename[256];
13 	char command[256];
14 	int ret = 0;
15 	FILE *f;
16 
17 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
18 	obj = bpf_object__open_file(filename, NULL);
19 	if (libbpf_get_error(obj)) {
20 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
21 		return 0;
22 	}
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 
30 	/* load BPF program */
31 	if (bpf_object__load(obj)) {
32 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
33 		goto cleanup;
34 	}
35 
36 	link = bpf_program__attach(prog);
37 	if (libbpf_get_error(link)) {
38 		fprintf(stderr, "ERROR: bpf_program__attach failed\n");
39 		link = NULL;
40 		goto cleanup;
41 	}
42 
43 	snprintf(command, 256, "mount %s tmpmnt/", argv[1]);
44 	f = popen(command, "r");
45 	ret = pclose(f);
46 
47 cleanup:
48 	bpf_link__destroy(link);
49 	bpf_object__close(obj);
50 	return ret ? 0 : 1;
51 }
52