[CRIU] [PATCH] cgroup: don't use FILE* for cg property restore

Tycho Andersen tycho.andersen at canonical.com
Mon Feb 29 14:32:03 PST 2016


Some libcs buffer writes to FILE*, which means that we error on fclose
instead of write, which makes it hard to figure out what property actually
failed writing.

Also shorten the error path a bit. Hopefully this patch will help with
debugging #118

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 criu/cgroup.c | 39 ++++++++++++++-------------------------
 1 file changed, 14 insertions(+), 25 deletions(-)

diff --git a/criu/cgroup.c b/criu/cgroup.c
index 7742fda..0d61204 100644
--- a/criu/cgroup.c
+++ b/criu/cgroup.c
@@ -1138,8 +1138,7 @@ static int restore_perms(int fd, const char *path, CgroupPerms *perms)
 static int restore_cgroup_prop(const CgroupPropEntry * cg_prop_entry_p,
 			       char *path, int off)
 {
-	FILE *f;
-	int cg, fd;
+	int cg, fd, len, ret = -1;;
 	CgroupPerms *perms = cg_prop_entry_p->perms;
 
 	if (!cg_prop_entry_p->value) {
@@ -1155,42 +1154,32 @@ static int restore_cgroup_prop(const CgroupPropEntry * cg_prop_entry_p,
 	pr_info("Restoring cgroup property value [%s] to [%s]\n", cg_prop_entry_p->value, path);
 
 	cg = get_service_fd(CGROUP_YARD);
-	f = fopenat(cg, path, "w+");
-	if (!f) {
-		pr_perror("Failed opening %s for writing", path);
-		return -1;
-	}
-
-	fd = fileno(f);
+	fd = openat(cg, path, O_WRONLY);
 	if (fd < 0) {
-		fclose(f);
 		pr_err("bad file stream?");
 		return -1;
 	}
 
-	if (restore_perms(fd, path, perms) < 0) {
-		fclose(f);
-		return -1;
-	}
+	if (restore_perms(fd, path, perms) < 0)
+		goto out;
 
 	/* skip these two since restoring their values doesn't make sense */
-	if (!strcmp(cg_prop_entry_p->name, "cgroup.procs") || !strcmp(cg_prop_entry_p->name, "tasks")) {
-		fclose(f);
-		return 0;
-	}
+	if (!strcmp(cg_prop_entry_p->name, "cgroup.procs") || !strcmp(cg_prop_entry_p->name, "tasks"))
+		goto out;
 
-	if (fprintf(f, "%s", cg_prop_entry_p->value) < 0) {
-		fclose(f);
+	len = strlen(cg_prop_entry_p->value);
+	if (write(fd, cg_prop_entry_p->value, len) != len) {
 		pr_err("Failed writing %s to %s\n", cg_prop_entry_p->value, path);
-		return -1;
+		goto out;
 	}
 
-	if (fclose(f) != 0) {
+out:
+	if (close(fd) != 0)
 		pr_perror("Failed closing %s", path);
-		return -1;
-	}
+	else
+		ret = 0;
 
-	return 0;
+	return ret;
 }
 
 static CgroupPropEntry *freezer_state_entry;
-- 
2.6.4



More information about the CRIU mailing list