1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Bytedance */
3 
4 #include <sys/syscall.h>
5 #include <test_progs.h>
6 #include <cgroup_helpers.h>
7 #include "test_task_under_cgroup.skel.h"
8 
9 #define FOO	"/foo"
10 
test_task_under_cgroup(void)11 void test_task_under_cgroup(void)
12 {
13 	struct test_task_under_cgroup *skel;
14 	int ret, foo;
15 	pid_t pid;
16 
17 	foo = test__join_cgroup(FOO);
18 	if (!ASSERT_OK(foo < 0, "cgroup_join_foo"))
19 		return;
20 
21 	skel = test_task_under_cgroup__open();
22 	if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open"))
23 		goto cleanup;
24 
25 	skel->rodata->local_pid = getpid();
26 	skel->bss->remote_pid = getpid();
27 	skel->rodata->cgid = get_cgroup_id(FOO);
28 
29 	ret = test_task_under_cgroup__load(skel);
30 	if (!ASSERT_OK(ret, "test_task_under_cgroup__load"))
31 		goto cleanup;
32 
33 	ret = test_task_under_cgroup__attach(skel);
34 	if (!ASSERT_OK(ret, "test_task_under_cgroup__attach"))
35 		goto cleanup;
36 
37 	pid = fork();
38 	if (pid == 0)
39 		exit(0);
40 
41 	ret = (pid == -1);
42 	if (ASSERT_OK(ret, "fork process"))
43 		wait(NULL);
44 
45 	test_task_under_cgroup__detach(skel);
46 
47 	ASSERT_NEQ(skel->bss->remote_pid, skel->rodata->local_pid,
48 		   "test task_under_cgroup");
49 
50 cleanup:
51 	test_task_under_cgroup__destroy(skel);
52 	close(foo);
53 }
54