[CRIU] [crtools-bot] fdset: Make fdset open helpers to work in all-or-nothing manner v2

Cyrill Gorcunov gorcunov at openvz.org
Tue Jan 31 05:32:58 EST 2012


The commit is pushed to "master" and will appear on git://github.com/cyrillos/crtools.git
------>
commit 55e35580244f07efd0f716c487fe8f392581ac43
Author: Cyrill Gorcunov <gorcunov at openvz.org>
Date:   Tue Jan 31 14:32:58 2012 +0400

    fdset: Make fdset open helpers to work in all-or-nothing manner v2
    
    When cr_fdset_open or prep_cr_fdset_for_restore is called
    the caller expects either all operations are successfull
    or there is some error happened and NULL returned, but
    instead at moment partially filled fdset might be returned.
    
    Fix it -- we either should get all or nothing.
    
    Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 crtools.c |   69 +++++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 40 insertions(+), 29 deletions(-)

diff --git a/crtools.c b/crtools.c
index 926d26b..9ead604 100644
--- a/crtools.c
+++ b/crtools.c
@@ -123,6 +123,33 @@ static struct cr_fdset *alloc_cr_fdset(void)
 	return cr_fdset;
 }
 
+static void __close_cr_fdset(struct cr_fdset *fdset)
+{
+	unsigned int i;
+
+	if (!fdset)
+		return;
+
+	for (i = 0; i < CR_FD_MAX; i++) {
+		if (fdset->fds[i] == -1)
+			continue;
+
+		pr_debug("Closed %d/%d\n", i, fdset->fds[i]);
+
+		close(fdset->fds[i]);
+		fdset->fds[i] = -1;
+	}
+}
+
+void close_cr_fdset(struct cr_fdset **cr_fdset)
+{
+	if (cr_fdset && *cr_fdset) {
+		__close_cr_fdset(*cr_fdset);
+		xfree(*cr_fdset);
+		*cr_fdset = NULL;
+	}
+}
+
 struct cr_fdset *cr_fdset_open(int pid, unsigned long use_mask, struct cr_fdset *cr_fdset)
 {
 	unsigned int i;
@@ -157,15 +184,19 @@ struct cr_fdset *cr_fdset_open(int pid, unsigned long use_mask, struct cr_fdset
 			pr_perror("Unable to open %s\n", path);
 			goto err;
 		}
+		cr_fdset->fds[i] = ret;
 
 		pr_debug("Opened %s with %d\n", path, ret);
 		if (write_img(ret, &fdset_template[i].magic))
 			goto err;
-
-		cr_fdset->fds[i] = ret;
 	}
-err:
+
 	return cr_fdset;
+
+err:
+	__close_cr_fdset(cr_fdset);
+	xfree(cr_fdset);
+	return NULL;
 }
 
 struct cr_fdset *prep_cr_fdset_for_restore(int pid, unsigned long use_mask)
@@ -194,43 +225,23 @@ struct cr_fdset *prep_cr_fdset_for_restore(int pid, unsigned long use_mask)
 			pr_perror("Unable to open %s\n", path);
 			goto err;
 		}
+		cr_fdset->fds[i] = ret;
 
 		pr_debug("Opened %s with %d\n", path, ret);
 		if (read_img(ret, &magic) < 0)
 			goto err;
 		if (magic != fdset_template[i].magic) {
-			close(ret);
 			pr_err("Magic doesn't match for %s\n", path);
 			goto err;
 		}
-
-		cr_fdset->fds[i] = ret;
 	}
-err:
-	return cr_fdset;
-}
-
-void close_cr_fdset(struct cr_fdset **cr_fdset)
-{
-	struct cr_fdset *fdset;
-	unsigned int i;
 
-	if (!cr_fdset || !*cr_fdset)
-		return;
-
-	fdset = *cr_fdset;
-
-	for (i = 0; i < CR_FD_MAX; i++) {
-		if (fdset->fds[i] == -1)
-			continue;
-
-		pr_debug("Closed %d/%d\n", i, fdset->fds[i]);
-		close(fdset->fds[i]);
-		fdset->fds[i] = -1;
-	}
+	return cr_fdset;
 
-	xfree(fdset);
-	*cr_fdset = NULL;
+err:
+	__close_cr_fdset(cr_fdset);
+	xfree(cr_fdset);
+	return NULL;
 }
 
 int main(int argc, char *argv[])


More information about the CRIU mailing list