WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / ns_current_pid_tgid.c
blobe74dc501b27f1961c686635fdb4f98ed382dc79e
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Carlos Neira cneirabustos@gmail.com */
3 #include <test_progs.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <sys/syscall.h>
9 struct bss {
10 __u64 dev;
11 __u64 ino;
12 __u64 pid_tgid;
13 __u64 user_pid_tgid;
16 void test_ns_current_pid_tgid(void)
18 const char *probe_name = "raw_tracepoint/sys_enter";
19 const char *file = "test_ns_current_pid_tgid.o";
20 int err, key = 0, duration = 0;
21 struct bpf_link *link = NULL;
22 struct bpf_program *prog;
23 struct bpf_map *bss_map;
24 struct bpf_object *obj;
25 struct bss bss;
26 struct stat st;
27 __u64 id;
29 obj = bpf_object__open_file(file, NULL);
30 if (CHECK(IS_ERR(obj), "obj_open", "err %ld\n", PTR_ERR(obj)))
31 return;
33 err = bpf_object__load(obj);
34 if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno))
35 goto cleanup;
37 bss_map = bpf_object__find_map_by_name(obj, "test_ns_.bss");
38 if (CHECK(!bss_map, "find_bss_map", "failed\n"))
39 goto cleanup;
41 prog = bpf_object__find_program_by_title(obj, probe_name);
42 if (CHECK(!prog, "find_prog", "prog '%s' not found\n",
43 probe_name))
44 goto cleanup;
46 memset(&bss, 0, sizeof(bss));
47 pid_t tid = syscall(SYS_gettid);
48 pid_t pid = getpid();
50 id = (__u64) tid << 32 | pid;
51 bss.user_pid_tgid = id;
53 if (CHECK_FAIL(stat("/proc/self/ns/pid", &st))) {
54 perror("Failed to stat /proc/self/ns/pid");
55 goto cleanup;
58 bss.dev = st.st_dev;
59 bss.ino = st.st_ino;
61 err = bpf_map_update_elem(bpf_map__fd(bss_map), &key, &bss, 0);
62 if (CHECK(err, "setting_bss", "failed to set bss : %d\n", err))
63 goto cleanup;
65 link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
66 if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n",
67 PTR_ERR(link))) {
68 link = NULL;
69 goto cleanup;
72 /* trigger some syscalls */
73 usleep(1);
75 err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &key, &bss);
76 if (CHECK(err, "set_bss", "failed to get bss : %d\n", err))
77 goto cleanup;
79 if (CHECK(id != bss.pid_tgid, "Compare user pid/tgid vs. bpf pid/tgid",
80 "User pid/tgid %llu BPF pid/tgid %llu\n", id, bss.pid_tgid))
81 goto cleanup;
82 cleanup:
83 bpf_link__destroy(link);
84 bpf_object__close(obj);