2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-30 22:05:36 +00:00

zdtm: Sanitize cgroup00 test

Prepare it for multiple tasks by moving cg creation and
checking code into helpers.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
This commit is contained in:
Pavel Emelyanov
2014-08-08 17:58:55 +04:00
parent f5e92084ee
commit 5a44f7f43c

View File

@@ -12,13 +12,73 @@ const char *test_author = "Pavel Emelianov <xemul@parallels.com>";
char *dirname;
TEST_OPTION(dirname, string, "cgroup directory name", 1);
static const char *cgname = "zdtmtst";
static const char *subname = "subcg";
#define SUBNAME "subcg"
static int cg_move(char *name)
{
int cgfd, l;
char paux[256];
sprintf(paux, "%s/%s", dirname, name);
mkdir(paux, 0600);
sprintf(paux, "%s/%s/tasks", dirname, name);
cgfd = open(paux, O_WRONLY);
if (cgfd < 0) {
err("Can't open tasks");
return -1;
}
l = write(cgfd, "0", 2);
close(cgfd);
if (l < 0) {
err("Can't move self to subcg");
return -1;
}
return 0;
}
static int cg_check(char *name)
{
int found = 0;
FILE *cgf;
char paux[256], aux[128];
cgf = fopen("/proc/self/cgroup", "r");
if (cgf == NULL)
return -1;
sprintf(aux, "name=%s:/%s\n", cgname, name);
while (fgets(paux, sizeof(paux), cgf)) {
char *s;
s = strchr(paux, ':') + 1;
test_msg("CMP [%s] vs [%s]\n", s, aux);
if (!strcmp(s, aux)) {
found = 1;
break;
}
}
fclose(cgf);
return found ? 0 : -1;
}
static void cg_cleanup(void)
{
char paux[256];
sprintf(paux, "%s/%s", dirname, SUBNAME);
rmdir(paux);
}
int main(int argc, char **argv)
{
int cgfd, l, ret = 1;
char aux[32], paux[1024];
FILE *cgf;
char aux[64];
test_init(argc, argv);
@@ -33,62 +93,24 @@ int main(int argc, char **argv)
goto out_rd;
}
sprintf(paux, "%s/%s", dirname, subname);
mkdir(paux, 0600);
l = sprintf(aux, "%d", getpid());
sprintf(paux, "%s/%s/tasks", dirname, subname);
cgfd = open(paux, O_WRONLY);
if (cgfd < 0) {
err("Can't open tasks");
if (cg_move(SUBNAME))
goto out_rs;
}
l = write(cgfd, aux, l);
close(cgfd);
if (l < 0) {
err("Can't move self to subcg");
goto out_rs;
}
close(cgfd);
test_daemon();
test_waitsig();
cgf = fopen("/proc/self/cgroup", "r");
if (cgf == NULL) {
fail("No cgroups file");
if (cg_check(SUBNAME)) {
fail("Top level task cg changed");
goto out_rs;
}
sprintf(aux, "name=%s:/%s\n", cgname, subname);
while (fgets(paux, sizeof(paux), cgf)) {
char *s;
s = strchr(paux, ':') + 1;
test_msg("CMP [%s] vs [%s]\n", s, aux);
if (!strcmp(s, aux)) {
ret = 0;
break;
}
}
fclose(cgf);
if (!ret)
pass();
else
fail("Task is not in subgroups");
pass();
out_rs:
sprintf(paux, "%s/%s", dirname, subname);
rmdir(paux);
cg_cleanup();
umount(dirname);
out_rd:
rmdir(dirname);
out:
return ret;
return 0;
}