1f4fba2d6SSong Liu // SPDX-License-Identifier: GPL-2.0-only
2f4fba2d6SSong Liu /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3f4fba2d6SSong Liu
4f4fba2d6SSong Liu #include <errno.h>
5f4fba2d6SSong Liu #include <fcntl.h>
6f4fba2d6SSong Liu #include <sys/stat.h>
7f4fba2d6SSong Liu #include <string.h>
8f4fba2d6SSong Liu #include <unistd.h>
9f4fba2d6SSong Liu #include <sys/socket.h>
10f4fba2d6SSong Liu #include <test_progs.h>
11*bacdf5a0SSong Liu #include "cgroup_helpers.h"
12f4fba2d6SSong Liu
13f4fba2d6SSong Liu #include "read_cgroupfs_xattr.skel.h"
14f4fba2d6SSong Liu #include "cgroup_read_xattr.skel.h"
15f4fba2d6SSong Liu
16*bacdf5a0SSong Liu #define CGROUP_FS_PARENT "foo/"
17f4fba2d6SSong Liu #define CGROUP_FS_CHILD CGROUP_FS_PARENT "bar/"
18*bacdf5a0SSong Liu #define TMP_FILE "/tmp/selftests_cgroup_xattr"
19f4fba2d6SSong Liu
20f4fba2d6SSong Liu static const char xattr_value_a[] = "bpf_selftest_value_a";
21f4fba2d6SSong Liu static const char xattr_value_b[] = "bpf_selftest_value_b";
22f4fba2d6SSong Liu static const char xattr_name[] = "user.bpf_test";
23f4fba2d6SSong Liu
test_read_cgroup_xattr(void)24f4fba2d6SSong Liu static void test_read_cgroup_xattr(void)
25f4fba2d6SSong Liu {
26*bacdf5a0SSong Liu int tmp_fd, parent_cgroup_fd = -1, child_cgroup_fd = -1;
27f4fba2d6SSong Liu struct read_cgroupfs_xattr *skel = NULL;
28f4fba2d6SSong Liu
29*bacdf5a0SSong Liu parent_cgroup_fd = test__join_cgroup(CGROUP_FS_PARENT);
30*bacdf5a0SSong Liu if (!ASSERT_OK_FD(parent_cgroup_fd, "create parent cgroup"))
31f4fba2d6SSong Liu return;
32*bacdf5a0SSong Liu if (!ASSERT_OK(set_cgroup_xattr(CGROUP_FS_PARENT, xattr_name, xattr_value_a),
33*bacdf5a0SSong Liu "set parent xattr"))
34*bacdf5a0SSong Liu goto out;
35*bacdf5a0SSong Liu
36*bacdf5a0SSong Liu child_cgroup_fd = test__join_cgroup(CGROUP_FS_CHILD);
37*bacdf5a0SSong Liu if (!ASSERT_OK_FD(child_cgroup_fd, "create child cgroup"))
38*bacdf5a0SSong Liu goto out;
39*bacdf5a0SSong Liu if (!ASSERT_OK(set_cgroup_xattr(CGROUP_FS_CHILD, xattr_name, xattr_value_b),
40*bacdf5a0SSong Liu "set child xattr"))
41f4fba2d6SSong Liu goto out;
42f4fba2d6SSong Liu
43f4fba2d6SSong Liu skel = read_cgroupfs_xattr__open_and_load();
44f4fba2d6SSong Liu if (!ASSERT_OK_PTR(skel, "read_cgroupfs_xattr__open_and_load"))
45f4fba2d6SSong Liu goto out;
46f4fba2d6SSong Liu
47*bacdf5a0SSong Liu skel->bss->target_pid = gettid();
48f4fba2d6SSong Liu
49f4fba2d6SSong Liu if (!ASSERT_OK(read_cgroupfs_xattr__attach(skel), "read_cgroupfs_xattr__attach"))
50f4fba2d6SSong Liu goto out;
51f4fba2d6SSong Liu
52*bacdf5a0SSong Liu tmp_fd = open(TMP_FILE, O_RDONLY | O_CREAT);
53*bacdf5a0SSong Liu ASSERT_OK_FD(tmp_fd, "open tmp file");
54*bacdf5a0SSong Liu close(tmp_fd);
55f4fba2d6SSong Liu
56f4fba2d6SSong Liu ASSERT_TRUE(skel->bss->found_value_a, "found_value_a");
57f4fba2d6SSong Liu ASSERT_TRUE(skel->bss->found_value_b, "found_value_b");
58f4fba2d6SSong Liu
59f4fba2d6SSong Liu out:
60*bacdf5a0SSong Liu close(child_cgroup_fd);
61*bacdf5a0SSong Liu close(parent_cgroup_fd);
62f4fba2d6SSong Liu read_cgroupfs_xattr__destroy(skel);
63*bacdf5a0SSong Liu unlink(TMP_FILE);
64f4fba2d6SSong Liu }
65f4fba2d6SSong Liu
test_cgroup_xattr(void)66f4fba2d6SSong Liu void test_cgroup_xattr(void)
67f4fba2d6SSong Liu {
68f4fba2d6SSong Liu RUN_TESTS(cgroup_read_xattr);
69f4fba2d6SSong Liu
70f4fba2d6SSong Liu if (test__start_subtest("read_cgroupfs_xattr"))
71f4fba2d6SSong Liu test_read_cgroup_xattr();
72f4fba2d6SSong Liu }
73