2025-06-22 23:38:54 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <test_progs.h>
|
2025-06-27 12:12:21 -07:00
|
|
|
#include "cgroup_helpers.h"
|
2025-06-22 23:38:54 -07:00
|
|
|
|
|
|
|
#include "read_cgroupfs_xattr.skel.h"
|
|
|
|
#include "cgroup_read_xattr.skel.h"
|
|
|
|
|
2025-06-27 12:12:21 -07:00
|
|
|
#define CGROUP_FS_PARENT "foo/"
|
2025-06-22 23:38:54 -07:00
|
|
|
#define CGROUP_FS_CHILD CGROUP_FS_PARENT "bar/"
|
2025-06-27 12:12:21 -07:00
|
|
|
#define TMP_FILE "/tmp/selftests_cgroup_xattr"
|
2025-06-22 23:38:54 -07:00
|
|
|
|
|
|
|
static const char xattr_value_a[] = "bpf_selftest_value_a";
|
|
|
|
static const char xattr_value_b[] = "bpf_selftest_value_b";
|
|
|
|
static const char xattr_name[] = "user.bpf_test";
|
|
|
|
|
|
|
|
static void test_read_cgroup_xattr(void)
|
|
|
|
{
|
2025-06-27 12:12:21 -07:00
|
|
|
int tmp_fd, parent_cgroup_fd = -1, child_cgroup_fd = -1;
|
2025-06-22 23:38:54 -07:00
|
|
|
struct read_cgroupfs_xattr *skel = NULL;
|
|
|
|
|
2025-06-27 12:12:21 -07:00
|
|
|
parent_cgroup_fd = test__join_cgroup(CGROUP_FS_PARENT);
|
|
|
|
if (!ASSERT_OK_FD(parent_cgroup_fd, "create parent cgroup"))
|
2025-06-22 23:38:54 -07:00
|
|
|
return;
|
2025-06-27 12:12:21 -07:00
|
|
|
if (!ASSERT_OK(set_cgroup_xattr(CGROUP_FS_PARENT, xattr_name, xattr_value_a),
|
|
|
|
"set parent xattr"))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
child_cgroup_fd = test__join_cgroup(CGROUP_FS_CHILD);
|
|
|
|
if (!ASSERT_OK_FD(child_cgroup_fd, "create child cgroup"))
|
|
|
|
goto out;
|
|
|
|
if (!ASSERT_OK(set_cgroup_xattr(CGROUP_FS_CHILD, xattr_name, xattr_value_b),
|
|
|
|
"set child xattr"))
|
2025-06-22 23:38:54 -07:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
skel = read_cgroupfs_xattr__open_and_load();
|
|
|
|
if (!ASSERT_OK_PTR(skel, "read_cgroupfs_xattr__open_and_load"))
|
|
|
|
goto out;
|
|
|
|
|
2025-06-27 12:12:21 -07:00
|
|
|
skel->bss->target_pid = gettid();
|
2025-06-22 23:38:54 -07:00
|
|
|
|
|
|
|
if (!ASSERT_OK(read_cgroupfs_xattr__attach(skel), "read_cgroupfs_xattr__attach"))
|
|
|
|
goto out;
|
|
|
|
|
2025-06-27 12:12:21 -07:00
|
|
|
tmp_fd = open(TMP_FILE, O_RDONLY | O_CREAT);
|
|
|
|
ASSERT_OK_FD(tmp_fd, "open tmp file");
|
|
|
|
close(tmp_fd);
|
2025-06-22 23:38:54 -07:00
|
|
|
|
|
|
|
ASSERT_TRUE(skel->bss->found_value_a, "found_value_a");
|
|
|
|
ASSERT_TRUE(skel->bss->found_value_b, "found_value_b");
|
|
|
|
|
|
|
|
out:
|
2025-06-27 12:12:21 -07:00
|
|
|
close(child_cgroup_fd);
|
|
|
|
close(parent_cgroup_fd);
|
2025-06-22 23:38:54 -07:00
|
|
|
read_cgroupfs_xattr__destroy(skel);
|
2025-06-27 12:12:21 -07:00
|
|
|
unlink(TMP_FILE);
|
2025-06-22 23:38:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_cgroup_xattr(void)
|
|
|
|
{
|
|
|
|
RUN_TESTS(cgroup_read_xattr);
|
|
|
|
|
|
|
|
if (test__start_subtest("read_cgroupfs_xattr"))
|
|
|
|
test_read_cgroup_xattr();
|
|
|
|
}
|