[CRIU] [PATCH 1/2] Re-create cgroups if necessary

Tycho Andersen tycho.andersen at canonical.com
Mon Jun 23 19:26:18 PDT 2014


On Mon, Jun 23, 2014 at 12:00:13PM -0700, Saied Kazemi wrote:
> >  if (strlen(path) < PATH_MAX)
> 
> Shouldn't the comparison inmkdirpat() be >= instead of less than?

Yes, of course. I added the error checking after the rest of the
patch, sorry about that. Below is the updated version.

Tycho

If a task had cgroups, these should be restored. (Also any properties of
cgroups, but this commit doesn't do that yet.)

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 cgroup.c       |  6 +++++-
 include/util.h |  5 +++++
 util.c         | 26 ++++++++++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/cgroup.c b/cgroup.c
index 2d9ebad..8b2deb2 100644
--- a/cgroup.c
+++ b/cgroup.c
@@ -258,9 +258,13 @@ static int move_in_cgroup(CgSetEntry *se)
 		int fd, err;
 		ControllerEntry *ce = se->ctls[i];
 
+		sprintf(aux, "%s/%s", ce->name, ce->path);
+		if (mkdirpat(cg, aux) < 0)
+			return -1;
+
 		sprintf(aux, "%s/%s/tasks", ce->name, ce->path);
 		pr_debug("  `-> %s\n", aux);
-		err = fd = openat(cg, aux, O_WRONLY);
+		err = fd = openat(cg, aux, O_WRONLY | O_CREAT, 0700);
 		if (fd >= 0) {
 			/*
 			 * Writing zero into this file moves current
diff --git a/include/util.h b/include/util.h
index 22a0f3d..e2ec22e 100644
--- a/include/util.h
+++ b/include/util.h
@@ -303,4 +303,9 @@ static inline bool strstartswith(char *str, char *sub)
 	}
 }
 
+/*
+ * mkdir -p for a file descriptor
+ */
+int mkdirpat(int fd, const char* path);
+
 #endif /* __CR_UTIL_H__ */
diff --git a/util.c b/util.c
index 975af30..cd8102e 100644
--- a/util.c
+++ b/util.c
@@ -677,3 +677,29 @@ struct vma_area *alloc_vma_area(void)
 
 	return p;
 }
+
+int mkdirpat(int fd, const char* path)
+{
+	char* tok;
+	char made_path[PATH_MAX] = "", tok_path[PATH_MAX];
+
+	if (strlen(path) >= PATH_MAX)
+	{
+		pr_err("path %s is longer than PATH_MAX", path);
+		return -1;
+	}
+
+	strncpy(tok_path, path, PATH_MAX);
+
+	for(tok = strtok(tok_path, "/"); tok; tok = strtok(NULL, "/")) {
+		strcat(made_path, tok);
+		strcat(made_path, "/");
+		if (mkdirat(fd, made_path, 0755) < 0 && errno != EEXIST) {
+			pr_perror("couldn't mkdirpat directory\n");
+			return -1;
+		}
+	}
+
+	return 0;
+
+}
-- 
1.9.1



More information about the CRIU mailing list