xref: /linux/tools/testing/selftests/bpf/progs/bpf_iter_task_file.c (revision ec8a42e7343234802b9054874fe01810880289ce)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include "bpf_iter.h"
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 
7 char _license[] SEC("license") = "GPL";
8 
9 int count = 0;
10 int tgid = 0;
11 
12 SEC("iter/task_file")
13 int dump_task_file(struct bpf_iter__task_file *ctx)
14 {
15 	struct seq_file *seq = ctx->meta->seq;
16 	struct task_struct *task = ctx->task;
17 	__u32 fd = ctx->fd;
18 	struct file *file = ctx->file;
19 
20 	if (task == (void *)0 || file == (void *)0)
21 		return 0;
22 
23 	if (ctx->meta->seq_num == 0) {
24 		count = 0;
25 		BPF_SEQ_PRINTF(seq, "    tgid      gid       fd      file\n");
26 	}
27 
28 	if (tgid == task->tgid && task->tgid != task->pid)
29 		count++;
30 
31 	BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd,
32 		       (long)file->f_op);
33 	return 0;
34 }
35