[LTP] Do we have some simple way to get the current cgroup.procs path?

xuyang2018.jy@fujitsu.com xuyang2018.jy@fujitsu.com
Thu Jul 14 08:00:49 CEST 2022


Hi Richard, LI

I am writting a simple regression test[1]that refer to kernel selftest cgroup test_core[2]. I may name it as memcontrol05.c.

It tests cgroup migration permission check should be performed based on the credentials at the time of open instead of write.

I have used ltp cgroup framework, but ltp cgroup only use SAFE_CGROUP_PRINTF to write value.

How can get the cgroup root_dir ltp_dir path? So I can open this fd
and change/save effective uid between open and write?

[1]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1756d7994a
[2]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/cgroup/test_core.c#n684

ps: my draft code as below:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>

#include "tst_test.h"

static struct tst_cg_group *cg_child;
static uid_t nobody_uid, save_uid;

static void test_lesser_euid_open(void)
{
        cg_child = tst_cg_group_mk(tst_cg, "child");
        SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
        if (!SAFE_FORK()) {
                seteuid(nobody_uid);
                SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
                seteuid(save_uid);
                exit(0);
        }

        tst_reap_children();
        SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid());
        cg_child = tst_cg_group_rm(cg_child);
}

static void setup(void)
{
        struct passwd *pw;

        pw = SAFE_GETPWNAM("nobody");
        nobody_uid = pw->pw_uid;
        save_uid = geteuid();
}

static void cleanup(void)
{
         if (cg_child) {
                SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid());
                cg_child = tst_cg_group_rm(cg_child);
        }
}

static struct tst_test test = {
        .setup = setup,
        .cleanup = cleanup,
        .test_all = test_lesser_euid_open,
        .forks_child = 1,
        .needs_root = 1,
        .needs_cgroup_ctrls = (const char *const[]){ "memory", NULL },
};

I have think about it yesterday and read ltp cgroup framework code, but don't have good idea.

Best Regards
Yang Xu


More information about the ltp mailing list