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