1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2022 Google */
3
4#include "bpf_iter.h"
5#include <bpf/bpf_helpers.h>
6#include <bpf/bpf_tracing.h>
7
8char _license[] SEC("license") = "GPL";
9int terminate_early = 0;
10u64 terminal_cgroup = 0;
11
12static inline u64 cgroup_id(struct cgroup *cgrp)
13{
14	return cgrp->kn->id;
15}
16
17SEC("iter/cgroup")
18int cgroup_id_printer(struct bpf_iter__cgroup *ctx)
19{
20	struct seq_file *seq = ctx->meta->seq;
21	struct cgroup *cgrp = ctx->cgroup;
22
23	/* epilogue */
24	if (cgrp == NULL) {
25		BPF_SEQ_PRINTF(seq, "epilogue\n");
26		return 0;
27	}
28
29	/* prologue */
30	if (ctx->meta->seq_num == 0)
31		BPF_SEQ_PRINTF(seq, "prologue\n");
32
33	BPF_SEQ_PRINTF(seq, "%8llu\n", cgroup_id(cgrp));
34
35	if (terminal_cgroup == cgroup_id(cgrp))
36		return 1;
37
38	return terminate_early ? 1 : 0;
39}
40