1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Carlos Neira cneirabustos@gmail.com */
3 #include <test_progs.h>
7 #include <sys/syscall.h>
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
;
29 obj
= bpf_object__open_file(file
, NULL
);
30 if (CHECK(IS_ERR(obj
), "obj_open", "err %ld\n", PTR_ERR(obj
)))
33 err
= bpf_object__load(obj
);
34 if (CHECK(err
, "obj_load", "err %d errno %d\n", err
, errno
))
37 bss_map
= bpf_object__find_map_by_name(obj
, "test_ns_.bss");
38 if (CHECK(!bss_map
, "find_bss_map", "failed\n"))
41 prog
= bpf_object__find_program_by_title(obj
, probe_name
);
42 if (CHECK(!prog
, "find_prog", "prog '%s' not found\n",
46 memset(&bss
, 0, sizeof(bss
));
47 pid_t tid
= syscall(SYS_gettid
);
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");
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
))
65 link
= bpf_program__attach_raw_tracepoint(prog
, "sys_enter");
66 if (CHECK(IS_ERR(link
), "attach_raw_tp", "err %ld\n",
72 /* trigger some syscalls */
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
))
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
))
83 bpf_link__destroy(link
);
84 bpf_object__close(obj
);