Below is the code attached to sched_process_fork tracepoint for tracking process forks.
// fork.bpf.c
// clang -O2 -target bpf -c fork.bpf.c -o fork.bpf.o
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#define TASK_COMM_LEN 16
struct event {
__u32 pid; // tgid
char comm[TASK_COMM_LEN]; // command name
};
/* ring buffer map */
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 24); // 16MB
} events SEC(".maps");
/* Tracepoint: sched:sched_process_fork
* When a process forks, this program reserves an event from the ring buffer,
* populates pid+comm and submits it.
*/
SEC("tracepoint/sched/sched_process_fork")
int trace_sched_process_fork(struct trace_event_raw_sched_process_fork *ctx)
{
struct event *e;
u64 pid_tgid;
/* get tgid (upper 32 bits) */
pid_tgid = bpf_get_current_pid_tgid();
__u32 tgid = pid_tgid >> 32;
e = bpf_ringbuf_reserve(&events, sizeof(*e), 0);
if (!e)
return 0;
e->pid = tgid;
/* get current comm (task->comm) */
bpf_get_current_comm(&e->comm, sizeof(e->comm));
bpf_ringbuf_submit(e, 0);
return 0;
}
char LICENSE[] SEC("license") = "GPL";
I need to get the full path of the parent process. But examining the vmlinux.h file, the struct trace_event_raw_sched_process_fork does not have a filename member. Only pid and command name. How can I get the parent path?
If it is not possible from here, then which hook should be used to get the parent process path during forks? Maybe any lsm hook?