tools/testing/selftests/bpf/prog_tests/custom_sec_handlers.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/custom_sec_handlers.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/custom_sec_handlers.c
Extension
.c
Size
5673 bytes
Lines
177
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Facebook */

#include <test_progs.h>
#include "test_custom_sec_handlers.skel.h"

#define COOKIE_ABC1 1
#define COOKIE_ABC2 2
#define COOKIE_CUSTOM 3
#define COOKIE_FALLBACK 4
#define COOKIE_KPROBE 5

static int custom_setup_prog(struct bpf_program *prog, long cookie)
{
	if (cookie == COOKIE_ABC1)
		bpf_program__set_autoload(prog, false);

	return 0;
}

static int custom_prepare_load_prog(struct bpf_program *prog,
				    struct bpf_prog_load_opts *opts, long cookie)
{
	if (cookie == COOKIE_FALLBACK)
		opts->prog_flags |= BPF_F_SLEEPABLE;
	else if (cookie == COOKIE_ABC1)
		ASSERT_FALSE(true, "unexpected preload for abc");

	return 0;
}

static int custom_attach_prog(const struct bpf_program *prog, long cookie,
			      struct bpf_link **link)
{
	switch (cookie) {
	case COOKIE_ABC2:
		*link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
		return libbpf_get_error(*link);
	case COOKIE_CUSTOM:
		*link = bpf_program__attach_tracepoint(prog, "syscalls", "sys_enter_nanosleep");
		return libbpf_get_error(*link);
	case COOKIE_KPROBE:
	case COOKIE_FALLBACK:
		/* no auto-attach for SEC("xyz") and SEC("kprobe") */
		*link = NULL;
		return 0;
	default:
		ASSERT_FALSE(true, "unexpected cookie");
		return -EINVAL;
	}
}

static int abc1_id;
static int abc2_id;
static int custom_id;
static int fallback_id;
static int kprobe_id;

__attribute__((constructor))
static void register_sec_handlers(void)
{
	LIBBPF_OPTS(libbpf_prog_handler_opts, abc1_opts,
		.cookie = COOKIE_ABC1,
		.prog_setup_fn = custom_setup_prog,
		.prog_prepare_load_fn = custom_prepare_load_prog,
		.prog_attach_fn = NULL,
	);
	LIBBPF_OPTS(libbpf_prog_handler_opts, abc2_opts,
		.cookie = COOKIE_ABC2,
		.prog_setup_fn = custom_setup_prog,
		.prog_prepare_load_fn = custom_prepare_load_prog,
		.prog_attach_fn = custom_attach_prog,
	);
	LIBBPF_OPTS(libbpf_prog_handler_opts, custom_opts,
		.cookie = COOKIE_CUSTOM,
		.prog_setup_fn = NULL,
		.prog_prepare_load_fn = NULL,
		.prog_attach_fn = custom_attach_prog,
	);

	abc1_id = libbpf_register_prog_handler("abc", BPF_PROG_TYPE_RAW_TRACEPOINT, 0, &abc1_opts);
	abc2_id = libbpf_register_prog_handler("abc/", BPF_PROG_TYPE_RAW_TRACEPOINT, 0, &abc2_opts);
	custom_id = libbpf_register_prog_handler("custom+", BPF_PROG_TYPE_TRACEPOINT, 0, &custom_opts);
}

__attribute__((destructor))
static void unregister_sec_handlers(void)
{
	libbpf_unregister_prog_handler(abc1_id);
	libbpf_unregister_prog_handler(abc2_id);

Annotation

Implementation Notes