xref: /linux/tools/testing/selftests/bpf/progs/bpf_iter_task_file.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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 int last_tgid = 0;
11 int unique_tgid_count = 0;
12 
13 SEC("iter/task_file")
dump_task_file(struct bpf_iter__task_file * ctx)14 int dump_task_file(struct bpf_iter__task_file *ctx)
15 {
16 	struct seq_file *seq = ctx->meta->seq;
17 	struct task_struct *task = ctx->task;
18 	struct file *file = ctx->file;
19 	__u32 fd = ctx->fd;
20 
21 	if (task == (void *)0 || file == (void *)0)
22 		return 0;
23 
24 	if (ctx->meta->seq_num == 0) {
25 		count = 0;
26 		BPF_SEQ_PRINTF(seq, "    tgid      gid       fd      file\n");
27 	}
28 
29 	if (tgid == task->tgid && task->tgid != task->pid)
30 		count++;
31 
32 	if (last_tgid != task->tgid) {
33 		last_tgid = task->tgid;
34 		unique_tgid_count++;
35 	}
36 
37 	BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd,
38 		       (long)file->f_op);
39 	return 0;
40 }
41