1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #include <errno.h>
4 #include <linux/limits.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 #include "kselftest.h"
13 #include "../pidfd/pidfd.h"
14 #include "cgroup_util.h"
15
16 /*
17 * Kill the given cgroup and wait for the inotify signal.
18 * If there are no events in 10 seconds, treat this as an error.
19 * Then check that the cgroup is in the desired state.
20 */
cg_kill_wait(const char * cgroup)21 static int cg_kill_wait(const char *cgroup)
22 {
23 int fd, ret = -1;
24
25 fd = cg_prepare_for_wait(cgroup);
26 if (fd < 0)
27 return fd;
28
29 ret = cg_write(cgroup, "cgroup.kill", "1");
30 if (ret)
31 goto out;
32
33 ret = cg_wait_for(fd);
34 if (ret)
35 goto out;
36
37 out:
38 close(fd);
39 return ret;
40 }
41
42 /*
43 * A simple process running in a sleep loop until being
44 * re-parented.
45 */
child_fn(const char * cgroup,void * arg)46 static int child_fn(const char *cgroup, void *arg)
47 {
48 int ppid = getppid();
49
50 while (getppid() == ppid)
51 usleep(1000);
52
53 return getppid() == ppid;
54 }
55
test_cgkill_simple(const char * root)56 static int test_cgkill_simple(const char *root)
57 {
58 pid_t pids[100];
59 int ret = KSFT_FAIL;
60 char *cgroup = NULL;
61 int i;
62
63 cgroup = cg_name(root, "cg_test_simple");
64 if (!cgroup)
65 goto cleanup;
66
67 if (cg_create(cgroup))
68 goto cleanup;
69
70 for (i = 0; i < 100; i++)
71 pids[i] = cg_run_nowait(cgroup, child_fn, NULL);
72
73 if (cg_wait_for_proc_count(cgroup, 100))
74 goto cleanup;
75
76 if (cg_read_strcmp(cgroup, "cgroup.events", "populated 1\n"))
77 goto cleanup;
78
79 if (cg_kill_wait(cgroup))
80 goto cleanup;
81
82 ret = KSFT_PASS;
83
84 cleanup:
85 for (i = 0; i < 100; i++)
86 wait_for_pid(pids[i]);
87
88 if (ret == KSFT_PASS &&
89 cg_read_strcmp_wait(cgroup, "cgroup.events", "populated 0\n"))
90 ret = KSFT_FAIL;
91
92 if (cgroup)
93 cg_destroy(cgroup);
94 free(cgroup);
95 return ret;
96 }
97
98 /*
99 * The test creates the following hierarchy:
100 * A
101 * / / \ \
102 * B E I K
103 * /\ |
104 * C D F
105 * |
106 * G
107 * |
108 * H
109 *
110 * with a process in C, H and 3 processes in K.
111 * Then it tries to kill the whole tree.
112 */
test_cgkill_tree(const char * root)113 static int test_cgkill_tree(const char *root)
114 {
115 pid_t pids[5];
116 char *cgroup[10] = {0};
117 int ret = KSFT_FAIL;
118 int i;
119
120 cgroup[0] = cg_name(root, "cg_test_tree_A");
121 if (!cgroup[0])
122 goto cleanup;
123
124 cgroup[1] = cg_name(cgroup[0], "B");
125 if (!cgroup[1])
126 goto cleanup;
127
128 cgroup[2] = cg_name(cgroup[1], "C");
129 if (!cgroup[2])
130 goto cleanup;
131
132 cgroup[3] = cg_name(cgroup[1], "D");
133 if (!cgroup[3])
134 goto cleanup;
135
136 cgroup[4] = cg_name(cgroup[0], "E");
137 if (!cgroup[4])
138 goto cleanup;
139
140 cgroup[5] = cg_name(cgroup[4], "F");
141 if (!cgroup[5])
142 goto cleanup;
143
144 cgroup[6] = cg_name(cgroup[5], "G");
145 if (!cgroup[6])
146 goto cleanup;
147
148 cgroup[7] = cg_name(cgroup[6], "H");
149 if (!cgroup[7])
150 goto cleanup;
151
152 cgroup[8] = cg_name(cgroup[0], "I");
153 if (!cgroup[8])
154 goto cleanup;
155
156 cgroup[9] = cg_name(cgroup[0], "K");
157 if (!cgroup[9])
158 goto cleanup;
159
160 for (i = 0; i < 10; i++)
161 if (cg_create(cgroup[i]))
162 goto cleanup;
163
164 pids[0] = cg_run_nowait(cgroup[2], child_fn, NULL);
165 pids[1] = cg_run_nowait(cgroup[7], child_fn, NULL);
166 pids[2] = cg_run_nowait(cgroup[9], child_fn, NULL);
167 pids[3] = cg_run_nowait(cgroup[9], child_fn, NULL);
168 pids[4] = cg_run_nowait(cgroup[9], child_fn, NULL);
169
170 /*
171 * Wait until all child processes will enter
172 * corresponding cgroups.
173 */
174
175 if (cg_wait_for_proc_count(cgroup[2], 1) ||
176 cg_wait_for_proc_count(cgroup[7], 1) ||
177 cg_wait_for_proc_count(cgroup[9], 3))
178 goto cleanup;
179
180 /*
181 * Kill A and check that we get an empty notification.
182 */
183 if (cg_kill_wait(cgroup[0]))
184 goto cleanup;
185
186 ret = KSFT_PASS;
187
188 cleanup:
189 for (i = 0; i < 5; i++)
190 wait_for_pid(pids[i]);
191
192 if (ret == KSFT_PASS &&
193 cg_read_strcmp_wait(cgroup[0], "cgroup.events",
194 "populated 0\n"))
195 ret = KSFT_FAIL;
196
197 for (i = 9; i >= 0 && cgroup[i]; i--) {
198 cg_destroy(cgroup[i]);
199 free(cgroup[i]);
200 }
201
202 return ret;
203 }
204
forkbomb_fn(const char * cgroup,void * arg)205 static int forkbomb_fn(const char *cgroup, void *arg)
206 {
207 int ppid;
208
209 fork();
210 fork();
211
212 ppid = getppid();
213
214 while (getppid() == ppid)
215 usleep(1000);
216
217 return getppid() == ppid;
218 }
219
220 /*
221 * The test runs a fork bomb in a cgroup and tries to kill it.
222 */
test_cgkill_forkbomb(const char * root)223 static int test_cgkill_forkbomb(const char *root)
224 {
225 int ret = KSFT_FAIL;
226 char *cgroup = NULL;
227 pid_t pid = -ESRCH;
228
229 cgroup = cg_name(root, "cg_forkbomb_test");
230 if (!cgroup)
231 goto cleanup;
232
233 if (cg_create(cgroup))
234 goto cleanup;
235
236 pid = cg_run_nowait(cgroup, forkbomb_fn, NULL);
237 if (pid < 0)
238 goto cleanup;
239
240 usleep(100000);
241
242 if (cg_kill_wait(cgroup))
243 goto cleanup;
244
245 if (cg_wait_for_proc_count(cgroup, 0))
246 goto cleanup;
247
248 ret = KSFT_PASS;
249
250 cleanup:
251 if (pid > 0)
252 wait_for_pid(pid);
253
254 if (ret == KSFT_PASS &&
255 cg_read_strcmp_wait(cgroup, "cgroup.events", "populated 0\n"))
256 ret = KSFT_FAIL;
257
258 if (cgroup)
259 cg_destroy(cgroup);
260 free(cgroup);
261 return ret;
262 }
263
264 #define T(x) { x, #x }
265 struct cgkill_test {
266 int (*fn)(const char *root);
267 const char *name;
268 } tests[] = {
269 T(test_cgkill_simple),
270 T(test_cgkill_tree),
271 T(test_cgkill_forkbomb),
272 };
273 #undef T
274
main(int argc,char * argv[])275 int main(int argc, char *argv[])
276 {
277 char root[PATH_MAX];
278 int i;
279
280 ksft_print_header();
281 ksft_set_plan(ARRAY_SIZE(tests));
282 if (cg_find_unified_root(root, sizeof(root), NULL))
283 ksft_exit_skip("cgroup v2 isn't mounted\n");
284 for (i = 0; i < ARRAY_SIZE(tests); i++) {
285 switch (tests[i].fn(root)) {
286 case KSFT_PASS:
287 ksft_test_result_pass("%s\n", tests[i].name);
288 break;
289 case KSFT_SKIP:
290 ksft_test_result_skip("%s\n", tests[i].name);
291 break;
292 default:
293 ksft_test_result_fail("%s\n", tests[i].name);
294 break;
295 }
296 }
297
298 ksft_finished();
299 }
300