[CRIU] [PATCH 2/3] zdtm: Sanitize cgroup00 test
Pavel Emelyanov
xemul at parallels.com
Fri Aug 8 06:58:55 PDT 2014
Prepare it for multiple tasks by moving cg creation and
checking code into helpers.
Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
test/zdtm/live/static/cgroup00.c | 102 ++++++++++++++++++++++++---------------
1 file changed, 62 insertions(+), 40 deletions(-)
diff --git a/test/zdtm/live/static/cgroup00.c b/test/zdtm/live/static/cgroup00.c
index e9669af..ca5e3fe 100644
--- a/test/zdtm/live/static/cgroup00.c
+++ b/test/zdtm/live/static/cgroup00.c
@@ -12,83 +12,105 @@ const char *test_author = "Pavel Emelianov <xemul at 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"
-int main(int argc, char **argv)
+static int cg_move(char *name)
{
- int cgfd, l, ret = 1;
- char aux[32], paux[1024];
- FILE *cgf;
-
- test_init(argc, argv);
-
- if (mkdir(dirname, 0700) < 0) {
- err("Can't make dir");
- goto out;
- }
-
- sprintf(aux, "none,name=%s", cgname);
- if (mount("none", dirname, "cgroup", 0, aux)) {
- err("Can't mount cgroups");
- goto out_rd;
- }
+ int cgfd, l;
+ char paux[256];
- sprintf(paux, "%s/%s", dirname, subname);
+ sprintf(paux, "%s/%s", dirname, name);
mkdir(paux, 0600);
- l = sprintf(aux, "%d", getpid());
- sprintf(paux, "%s/%s/tasks", dirname, subname);
+ sprintf(paux, "%s/%s/tasks", dirname, name);
cgfd = open(paux, O_WRONLY);
if (cgfd < 0) {
err("Can't open tasks");
- goto out_rs;
+ return -1;
}
- l = write(cgfd, aux, l);
+ l = write(cgfd, "0", 2);
close(cgfd);
if (l < 0) {
err("Can't move self to subcg");
- goto out_rs;
+ return -1;
}
- close(cgfd);
+ return 0;
+}
- test_daemon();
- test_waitsig();
+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) {
- fail("No cgroups file");
- goto out_rs;
- }
+ if (cgf == NULL)
+ return -1;
- sprintf(aux, "name=%s:/%s\n", cgname, subname);
+ 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)) {
- ret = 0;
+ found = 1;
break;
}
}
fclose(cgf);
- if (!ret)
- pass();
- else
- fail("Task is not in subgroups");
+ return found ? 0 : -1;
+}
-out_rs:
- sprintf(paux, "%s/%s", dirname, subname);
+static void cg_cleanup(void)
+{
+ char paux[256];
+
+ sprintf(paux, "%s/%s", dirname, SUBNAME);
rmdir(paux);
+}
+
+int main(int argc, char **argv)
+{
+ char aux[64];
+
+ test_init(argc, argv);
+
+ if (mkdir(dirname, 0700) < 0) {
+ err("Can't make dir");
+ goto out;
+ }
+
+ sprintf(aux, "none,name=%s", cgname);
+ if (mount("none", dirname, "cgroup", 0, aux)) {
+ err("Can't mount cgroups");
+ goto out_rd;
+ }
+
+ if (cg_move(SUBNAME))
+ goto out_rs;
+
+ test_daemon();
+ test_waitsig();
+
+ if (cg_check(SUBNAME)) {
+ fail("Top level task cg changed");
+ goto out_rs;
+ }
+
+ pass();
+
+out_rs:
+ cg_cleanup();
umount(dirname);
out_rd:
rmdir(dirname);
out:
- return ret;
+ return 0;
}
--
1.8.4.2
More information about the CRIU
mailing list