1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
Feng Zhou 49e0263ab4 selftests/bpf: Add testcase for bpf_task_under_cgroup
test_progs:
Tests new kfunc bpf_task_under_cgroup().

The bpf program saves the new task's pid within a given cgroup to
the remote_pid, which is convenient for the user-mode program to
verify the test correctness.

The user-mode program creates its own mount namespace, and mounts the
cgroupsv2 hierarchy in there, call the fork syscall, then check if
remote_pid and local_pid are unequal.

Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/r/20230506031545.35991-3-zhoufeng.zf@bytedance.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-05-06 13:56:38 -07:00

53 lines
1.1 KiB
C

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Bytedance */
#include <sys/syscall.h>
#include <test_progs.h>
#include <cgroup_helpers.h>
#include "test_task_under_cgroup.skel.h"
#define FOO "/foo"
void test_task_under_cgroup(void)
{
struct test_task_under_cgroup *skel;
int ret, foo;
pid_t pid;
foo = test__join_cgroup(FOO);
if (!ASSERT_OK(foo < 0, "cgroup_join_foo"))
return;
skel = test_task_under_cgroup__open();
if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open"))
goto cleanup;
skel->rodata->local_pid = getpid();
skel->bss->remote_pid = getpid();
skel->rodata->cgid = get_cgroup_id(FOO);
ret = test_task_under_cgroup__load(skel);
if (!ASSERT_OK(ret, "test_task_under_cgroup__load"))
goto cleanup;
ret = test_task_under_cgroup__attach(skel);
if (!ASSERT_OK(ret, "test_task_under_cgroup__attach"))
goto cleanup;
pid = fork();
if (pid == 0)
exit(0);
ret = (pid == -1);
if (ASSERT_OK(ret, "fork process"))
wait(NULL);
test_task_under_cgroup__detach(skel);
ASSERT_NEQ(skel->bss->remote_pid, skel->rodata->local_pid,
"test task_under_cgroup");
cleanup:
test_task_under_cgroup__destroy(skel);
close(foo);
}