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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/test_local_storage.c
Extension
.c
Size
4827 bytes
Lines
173
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

struct storage {
	void *inode;
	unsigned int value;
};

/* Fork and exec the provided rm binary and return the exit code of the
 * forked process and its pid.
 */
static int run_self_unlink(struct local_storage *skel, const char *rm_path)
{
	int child_pid, child_status, ret;
	int null_fd;

	child_pid = fork();
	if (child_pid == 0) {
		null_fd = open("/dev/null", O_WRONLY);
		dup2(null_fd, STDOUT_FILENO);
		dup2(null_fd, STDERR_FILENO);
		close(null_fd);

		skel->bss->monitored_pid = getpid();
		/* Use the copied /usr/bin/rm to delete itself
		 * /tmp/copy_of_rm /tmp/copy_of_rm.
		 */
		ret = execlp(rm_path, rm_path, rm_path, NULL);
		if (ret)
			exit(errno);
	} else if (child_pid > 0) {
		waitpid(child_pid, &child_status, 0);
		ASSERT_EQ(skel->data->task_storage_result, 0, "task_storage_result");
		return WEXITSTATUS(child_status);
	}

	return -EINVAL;
}

static bool check_syscall_operations(int map_fd, int obj_fd)
{
	struct storage val = { .value = TEST_STORAGE_VALUE },
		       lookup_val = { .value = 0 };
	int err;

	/* Looking up an existing element should fail initially */
	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
	if (!ASSERT_EQ(err, -ENOENT, "bpf_map_lookup_elem"))
		return false;

	/* Create a new element */
	err = bpf_map_update_elem(map_fd, &obj_fd, &val, BPF_NOEXIST);
	if (!ASSERT_OK(err, "bpf_map_update_elem"))
		return false;

	/* Lookup the newly created element */
	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
	if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
		return false;

	/* Check the value of the newly created element */
	if (!ASSERT_EQ(lookup_val.value, val.value, "bpf_map_lookup_elem"))
		return false;

	err = bpf_map_delete_elem(map_fd, &obj_fd);
	if (!ASSERT_OK(err, "bpf_map_delete_elem()"))
		return false;

	/* The lookup should fail, now that the element has been deleted */
	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
	if (!ASSERT_EQ(err, -ENOENT, "bpf_map_lookup_elem"))
		return false;

	return true;
}

void test_test_local_storage(void)
{
	char tmp_dir_path[] = "/tmp/local_storageXXXXXX";
	int err, serv_sk = -1, task_fd = -1, rm_fd = -1;
	struct local_storage *skel = NULL;
	char tmp_exec_path[64];
	char cmd[256];

	skel = local_storage__open_and_load();
	if (!ASSERT_OK_PTR(skel, "skel_load"))
		goto close_prog;

	err = local_storage__attach(skel);
	if (!ASSERT_OK(err, "attach"))
		goto close_prog;

	task_fd = sys_pidfd_open(getpid(), 0);

Annotation

Implementation Notes