[CRIU] [PATCH 5/6] tests: add a test for cgroup perms/global props

Tycho Andersen tycho.andersen at canonical.com
Wed Jan 20 09:27:37 PST 2016


Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 test/zdtm.sh                        |   2 +
 test/zdtm/.gitignore                |   1 +
 test/zdtm/live/static/Makefile      |   1 +
 test/zdtm/live/static/cgroup03.c    | 171 ++++++++++++++++++++++++++++++++++++
 test/zdtm/live/static/cgroup03.desc |   1 +
 test/zdtm/live/static/cgroup03.hook |  14 +++
 6 files changed, 190 insertions(+)
 create mode 100644 test/zdtm/live/static/cgroup03.c
 create mode 100644 test/zdtm/live/static/cgroup03.desc
 create mode 100755 test/zdtm/live/static/cgroup03.hook

diff --git a/test/zdtm.sh b/test/zdtm.sh
index ce32c74..4a57cd3 100755
--- a/test/zdtm.sh
+++ b/test/zdtm.sh
@@ -208,6 +208,7 @@ generate_test_list()
 		static/cgroup00
 		static/cgroup01
 		static/cgroup02
+		static/cgroup03
 		ns/static/clean_mntns
 		static/remap_dead_pid
 		static/poll
@@ -358,6 +359,7 @@ inotify_irmap
 cgroup00
 cgroup01
 cgroup02
+cgroup03
 clean_mntns
 deleted_dev
 mntns_open
diff --git a/test/zdtm/.gitignore b/test/zdtm/.gitignore
index 5963609..3d95a2b 100644
--- a/test/zdtm/.gitignore
+++ b/test/zdtm/.gitignore
@@ -8,6 +8,7 @@
 /live/static/cgroup00
 /live/static/cgroup01
 /live/static/cgroup02
+/live/static/cgroup03
 /live/static/child_opened_proc
 /live/static/chroot
 /live/static/chroot-file
diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index 689b1e7..8ccdfce 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -214,6 +214,7 @@ TST_DIR		=				\
 		rmdir_open			\
 		cgroup01			\
 		cgroup02			\
+		cgroup03			\
 		mntns_open			\
 		mntns_link_remap		\
 		mntns_link_ghost		\
diff --git a/test/zdtm/live/static/cgroup03.c b/test/zdtm/live/static/cgroup03.c
new file mode 100644
index 0000000..c6c4938
--- /dev/null
+++ b/test/zdtm/live/static/cgroup03.c
@@ -0,0 +1,171 @@
+#include <unistd.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include "zdtmtst.h"
+
+const char *test_doc	= "Check that global cgroup settings (+perms) are restored";
+const char *test_author	= "Tycho Andersen <tycho.andersen at canonical.com>";
+
+char *dirname;
+TEST_OPTION(dirname, string, "cgroup directory name", 1);
+static const char *cgname = "zdtmtst";
+
+int mount_and_add(const char *controller, const char *path)
+{
+	char aux[1024], paux[1024], subdir[1024];
+	int cgfd, l;
+
+	if (mkdir(dirname, 0700) < 0 && errno != EEXIST) {
+		pr_perror("Can't make dir");
+		return -1;
+	}
+
+	sprintf(subdir, "%s/%s", dirname, controller);
+	if (mkdir(subdir, 0700) < 0) {
+		pr_perror("Can't make dir");
+		return -1;
+	}
+
+	sprintf(aux, "none,name=%s", controller);
+	if (mount("none", subdir, "cgroup", 0, aux)) {
+		pr_perror("Can't mount cgroups");
+		goto err_rd;
+	}
+
+	sprintf(paux, "%s/%s", subdir, path);
+	mkdir(paux, 0600);
+
+	l = sprintf(aux, "%d", getpid());
+	sprintf(paux, "%s/%s/tasks", subdir, path);
+
+	cgfd = open(paux, O_WRONLY);
+	if (cgfd < 0) {
+		pr_perror("Can't open tasks");
+		goto err_rs;
+	}
+
+	l = write(cgfd, aux, l);
+	close(cgfd);
+
+	if (l < 0) {
+		pr_perror("Can't move self to subcg");
+		goto err_rs;
+	}
+
+	return 0;
+err_rs:
+	umount(dirname);
+err_rd:
+	rmdir(dirname);
+	return -1;
+}
+
+int chownmod(char *path, int flags)
+{
+	int fd, ret = -1;
+
+	fd = open(path, flags);
+	if (fd < 0) {
+		pr_perror("can't open %s", path);
+		return -1;
+	}
+
+	if (fchown(fd, 1000, 1000) < 0) {
+		pr_perror("can't chown %s", path);
+		goto out;
+	}
+
+	if (fchmod(fd, 0777) < 0) {
+		pr_perror("can't chmod %s", path);
+		goto out;
+	}
+
+	ret = 0;
+out:
+	close(fd);
+	return ret;
+}
+
+int checkperms(char *path)
+{
+	struct stat sb;
+
+	if (stat(path, &sb) < 0) {
+		pr_perror("can't stat %s", path);
+		return -1;
+	}
+
+	if ((sb.st_mode & 0777) != 0777) {
+		fail("mode for %s doesn't match (%o)\n", path, sb.st_mode);
+		return -1;
+	}
+
+	if (sb.st_uid != 1000) {
+		fail("uid for %s doesn't match (%d)\n", path, sb.st_uid);
+		return -1;
+	}
+
+	if (sb.st_gid != 1000) {
+		fail("gid for %s doesn't match (%d)\n", path, sb.st_gid);
+		return -1;
+	}
+
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	int ret = -1;
+	char path[PATH_MAX];
+
+	test_init(argc, argv);
+
+	if (mount_and_add(cgname, "test") < 0)
+		return -1;
+
+	sprintf(path, "%s/%s/test", dirname, cgname);
+	if (chownmod(path, O_DIRECTORY) < 0)
+		goto out_umount;
+
+	sprintf(path, "%s/%s/test/notify_on_release", dirname, cgname);
+	if (chownmod(path, O_RDWR) < 0)
+		goto out_umount;
+
+
+	sprintf(path, "%s/%s/test/cgroup.procs", dirname, cgname);
+	if (chownmod(path, O_RDWR) < 0)
+		goto out_umount;
+
+	test_daemon();
+	test_waitsig();
+
+	sprintf(path, "%s/%s/test", dirname, cgname);
+	if (checkperms(path) < 0)
+		goto out_umount;
+
+	sprintf(path, "%s/%s/test/notify_on_release", dirname, cgname);
+	if (checkperms(path) < 0)
+		goto out_umount;
+
+	sprintf(path, "%s/%s/test/cgroup.procs", dirname, cgname);
+	if (checkperms(path) < 0)
+		goto out_umount;
+
+	pass();
+	ret = 0;
+
+out_umount:
+	sprintf(path, "%s/%s/test", dirname, cgname);
+	rmdir(path);
+	sprintf(path, "%s/%s", dirname, cgname);
+	umount(path);
+	rmdir(path);
+	rmdir(dirname);
+	return ret;
+}
diff --git a/test/zdtm/live/static/cgroup03.desc b/test/zdtm/live/static/cgroup03.desc
new file mode 100644
index 0000000..3c6c4a7
--- /dev/null
+++ b/test/zdtm/live/static/cgroup03.desc
@@ -0,0 +1 @@
+{'flavor': 'h', 'flags': 'suid', 'opts': '--manage-cgroups'}
diff --git a/test/zdtm/live/static/cgroup03.hook b/test/zdtm/live/static/cgroup03.hook
new file mode 100755
index 0000000..6f11022
--- /dev/null
+++ b/test/zdtm/live/static/cgroup03.hook
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+[ "$1" == "--clean" -o "$1" == "--pre-restore" ] || exit 0
+
+tname=$(mktemp -d cgclean.XXXXXX)
+mount -t cgroup none $tname -o "none,name=zdtmtst"
+
+echo "Cleaning $tname"
+set +e
+rmdir "$tname/test"
+set -e
+
+umount "$tname"
+rmdir "$tname"
-- 
2.5.0



More information about the CRIU mailing list