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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/module_attach.c
Extension
.c
Size
5052 bytes
Lines
195
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) 2020 Facebook */

#include <test_progs.h>
#include <stdbool.h>
#include "test_module_attach.skel.h"
#include "testing_helpers.h"

static const char * const read_tests[] = {
	"handle_raw_tp",
	"handle_tp_btf",
	"handle_fentry",
	"handle_fentry_explicit",
	"handle_fmod_ret",
};

static const char * const detach_tests[] = {
	"handle_fentry",
	"handle_fexit",
	"kprobe_multi",
};

static const int READ_SZ = 456;
static const int WRITE_SZ = 457;

static int trigger_module_test_writable(int *val)
{
	int fd, err;
	char buf[65];
	ssize_t rd;

	fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
	err = -errno;
	if (!ASSERT_GE(fd, 0, "testmode_file_open"))
		return err;

	rd = read(fd, buf, sizeof(buf) - 1);
	err = -errno;
	if (!ASSERT_GT(rd, 0, "testmod_file_rd_val")) {
		close(fd);
		return err;
	}

	buf[rd] = '\0';
	*val = strtol(buf, NULL, 0);
	close(fd);

	return 0;
}

static void test_module_attach_prog(const char *prog_name, int sz,
				    const char *attach_target, int ret)
{
	struct test_module_attach *skel;
	struct bpf_program *prog;
	int err;

	skel = test_module_attach__open();
	if (!ASSERT_OK_PTR(skel, "module_attach open"))
		return;

	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
	if (!ASSERT_OK_PTR(prog, "module_attach find_program"))
		goto cleanup;
	bpf_program__set_autoload(prog, true);

	if (attach_target) {
		err = bpf_program__set_attach_target(prog, 0, attach_target);
		if (!ASSERT_OK(err, attach_target))
			goto cleanup;
	}

	err = test_module_attach__load(skel);
	if (!ASSERT_OK(err, "module_attach load"))
		goto cleanup;

	err = test_module_attach__attach(skel);
	if (!ASSERT_OK(err, "module_attach attach"))
		goto cleanup;

	if (sz) {
		/* trigger both read and write though each test uses only one */
		ASSERT_OK(trigger_module_test_read(sz), "trigger_read");
		ASSERT_OK(trigger_module_test_write(sz), "trigger_write");

		ASSERT_EQ(skel->bss->sz, sz, prog_name);
	}

	if (ret)
		ASSERT_EQ(skel->bss->retval, ret, "ret");

Annotation

Implementation Notes