|
7 | 7 | #include <string.h>
|
8 | 8 | #include <unistd.h>
|
9 | 9 | #include <sys/socket.h>
|
10 |
| -#include <sys/xattr.h> |
11 |
| - |
12 | 10 | #include <test_progs.h>
|
| 11 | +#include "cgroup_helpers.h" |
13 | 12 |
|
14 | 13 | #include "read_cgroupfs_xattr.skel.h"
|
15 | 14 | #include "cgroup_read_xattr.skel.h"
|
16 | 15 |
|
17 |
| -#define CGROUP_FS_ROOT "/sys/fs/cgroup/" |
18 |
| -#define CGROUP_FS_PARENT CGROUP_FS_ROOT "foo/" |
| 16 | +#define CGROUP_FS_PARENT "foo/" |
19 | 17 | #define CGROUP_FS_CHILD CGROUP_FS_PARENT "bar/"
|
20 |
| - |
21 |
| -static int move_pid_to_cgroup(const char *cgroup_folder, pid_t pid) |
22 |
| -{ |
23 |
| - char filename[128]; |
24 |
| - char pid_str[64]; |
25 |
| - int procs_fd; |
26 |
| - int ret; |
27 |
| - |
28 |
| - snprintf(filename, sizeof(filename), "%scgroup.procs", cgroup_folder); |
29 |
| - snprintf(pid_str, sizeof(pid_str), "%d", pid); |
30 |
| - |
31 |
| - procs_fd = open(filename, O_WRONLY | O_APPEND); |
32 |
| - if (!ASSERT_OK_FD(procs_fd, "open")) |
33 |
| - return -1; |
34 |
| - |
35 |
| - ret = write(procs_fd, pid_str, strlen(pid_str)); |
36 |
| - close(procs_fd); |
37 |
| - if (!ASSERT_GT(ret, 0, "write cgroup.procs")) |
38 |
| - return -1; |
39 |
| - return 0; |
40 |
| -} |
41 |
| - |
42 |
| -static void reset_cgroups_and_lo(void) |
43 |
| -{ |
44 |
| - rmdir(CGROUP_FS_CHILD); |
45 |
| - rmdir(CGROUP_FS_PARENT); |
46 |
| - system("ip addr del 1.1.1.1/32 dev lo"); |
47 |
| - system("ip link set dev lo down"); |
48 |
| -} |
| 18 | +#define TMP_FILE "/tmp/selftests_cgroup_xattr" |
49 | 19 |
|
50 | 20 | static const char xattr_value_a[] = "bpf_selftest_value_a";
|
51 | 21 | static const char xattr_value_b[] = "bpf_selftest_value_b";
|
52 | 22 | static const char xattr_name[] = "user.bpf_test";
|
53 | 23 |
|
54 |
| -static int setup_cgroups_and_lo(void) |
55 |
| -{ |
56 |
| - int err; |
57 |
| - |
58 |
| - err = mkdir(CGROUP_FS_PARENT, 0755); |
59 |
| - if (!ASSERT_OK(err, "mkdir 1")) |
60 |
| - goto error; |
61 |
| - err = mkdir(CGROUP_FS_CHILD, 0755); |
62 |
| - if (!ASSERT_OK(err, "mkdir 2")) |
63 |
| - goto error; |
64 |
| - |
65 |
| - err = setxattr(CGROUP_FS_PARENT, xattr_name, xattr_value_a, |
66 |
| - strlen(xattr_value_a) + 1, 0); |
67 |
| - if (!ASSERT_OK(err, "setxattr 1")) |
68 |
| - goto error; |
69 |
| - |
70 |
| - err = setxattr(CGROUP_FS_CHILD, xattr_name, xattr_value_b, |
71 |
| - strlen(xattr_value_b) + 1, 0); |
72 |
| - if (!ASSERT_OK(err, "setxattr 2")) |
73 |
| - goto error; |
74 |
| - |
75 |
| - err = system("ip link set dev lo up"); |
76 |
| - if (!ASSERT_OK(err, "lo up")) |
77 |
| - goto error; |
78 |
| - |
79 |
| - err = system("ip addr add 1.1.1.1 dev lo"); |
80 |
| - if (!ASSERT_OK(err, "lo addr v4")) |
81 |
| - goto error; |
82 |
| - |
83 |
| - err = write_sysctl("/proc/sys/net/ipv4/ping_group_range", "0 0"); |
84 |
| - if (!ASSERT_OK(err, "write_sysctl")) |
85 |
| - goto error; |
86 |
| - |
87 |
| - return 0; |
88 |
| -error: |
89 |
| - reset_cgroups_and_lo(); |
90 |
| - return err; |
91 |
| -} |
92 |
| - |
93 | 24 | static void test_read_cgroup_xattr(void)
|
94 | 25 | {
|
95 |
| - struct sockaddr_in sa4 = { |
96 |
| - .sin_family = AF_INET, |
97 |
| - .sin_addr.s_addr = htonl(INADDR_LOOPBACK), |
98 |
| - }; |
| 26 | + int tmp_fd, parent_cgroup_fd = -1, child_cgroup_fd = -1; |
99 | 27 | struct read_cgroupfs_xattr *skel = NULL;
|
100 |
| - pid_t pid = gettid(); |
101 |
| - int sock_fd = -1; |
102 |
| - int connect_fd = -1; |
103 | 28 |
|
104 |
| - if (!ASSERT_OK(setup_cgroups_and_lo(), "setup_cgroups_and_lo")) |
| 29 | + parent_cgroup_fd = test__join_cgroup(CGROUP_FS_PARENT); |
| 30 | + if (!ASSERT_OK_FD(parent_cgroup_fd, "create parent cgroup")) |
105 | 31 | return;
|
106 |
| - if (!ASSERT_OK(move_pid_to_cgroup(CGROUP_FS_CHILD, pid), |
107 |
| - "move_pid_to_cgroup")) |
| 32 | + if (!ASSERT_OK(set_cgroup_xattr(CGROUP_FS_PARENT, xattr_name, xattr_value_a), |
| 33 | + "set parent xattr")) |
| 34 | + goto out; |
| 35 | + |
| 36 | + child_cgroup_fd = test__join_cgroup(CGROUP_FS_CHILD); |
| 37 | + if (!ASSERT_OK_FD(child_cgroup_fd, "create child cgroup")) |
| 38 | + goto out; |
| 39 | + if (!ASSERT_OK(set_cgroup_xattr(CGROUP_FS_CHILD, xattr_name, xattr_value_b), |
| 40 | + "set child xattr")) |
108 | 41 | goto out;
|
109 | 42 |
|
110 | 43 | skel = read_cgroupfs_xattr__open_and_load();
|
111 | 44 | if (!ASSERT_OK_PTR(skel, "read_cgroupfs_xattr__open_and_load"))
|
112 | 45 | goto out;
|
113 | 46 |
|
114 |
| - skel->bss->target_pid = pid; |
| 47 | + skel->bss->target_pid = gettid(); |
115 | 48 |
|
116 | 49 | if (!ASSERT_OK(read_cgroupfs_xattr__attach(skel), "read_cgroupfs_xattr__attach"))
|
117 | 50 | goto out;
|
118 | 51 |
|
119 |
| - sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); |
120 |
| - if (!ASSERT_OK_FD(sock_fd, "sock create")) |
121 |
| - goto out; |
122 |
| - |
123 |
| - connect_fd = connect(sock_fd, &sa4, sizeof(sa4)); |
124 |
| - if (!ASSERT_OK_FD(connect_fd, "connect 1")) |
125 |
| - goto out; |
126 |
| - close(connect_fd); |
| 52 | + tmp_fd = open(TMP_FILE, O_RDONLY | O_CREAT); |
| 53 | + ASSERT_OK_FD(tmp_fd, "open tmp file"); |
| 54 | + close(tmp_fd); |
127 | 55 |
|
128 | 56 | ASSERT_TRUE(skel->bss->found_value_a, "found_value_a");
|
129 | 57 | ASSERT_TRUE(skel->bss->found_value_b, "found_value_b");
|
130 | 58 |
|
131 | 59 | out:
|
132 |
| - close(connect_fd); |
133 |
| - close(sock_fd); |
| 60 | + close(child_cgroup_fd); |
| 61 | + close(parent_cgroup_fd); |
134 | 62 | read_cgroupfs_xattr__destroy(skel);
|
135 |
| - move_pid_to_cgroup(CGROUP_FS_ROOT, pid); |
136 |
| - reset_cgroups_and_lo(); |
| 63 | + unlink(TMP_FILE); |
137 | 64 | }
|
138 | 65 |
|
139 | 66 | void test_cgroup_xattr(void)
|
|
0 commit comments