[CRIU] [PATCH 2/5] image: don't use global variables for saving service fds

Andrey Vagin avagin at openvz.org
Sun Jan 6 05:48:13 EST 2013


It's preparation for cloning service descriptors

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 image.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/image.c b/image.c
index f1f93a0..79e392b 100644
--- a/image.c
+++ b/image.c
@@ -227,10 +227,11 @@ struct cr_fdset *cr_glob_fdset_open(int mode)
 	return cr_fdset_open(-1 /* ignored */, _CR_FD_GLOB_FROM, _CR_FD_GLOB_TO, mode);
 }
 
-static int image_dir_fd = -1;
+static bool image_dir_inited = false;
 
 int open_image(int type, unsigned long flags, ...)
 {
+	int dfd = get_service_fd(IMG_FD_OFF);
 	char path[PATH_MAX];
 	va_list args;
 	int ret;
@@ -240,14 +241,14 @@ int open_image(int type, unsigned long flags, ...)
 	va_end(args);
 
 	if (flags & O_EXCL) {
-		ret = unlinkat(image_dir_fd, path, 0);
+		ret = unlinkat(dfd, path, 0);
 		if (ret && errno != ENOENT) {
 			pr_perror("Unable to unlink %s", path);
 			goto err;
 		}
 	}
 
-	ret = openat(image_dir_fd, path, flags, CR_FD_PERM);
+	ret = openat(dfd, path, flags, CR_FD_PERM);
 	if (ret < 0) {
 		pr_perror("Unable to open %s", path);
 		goto err;
@@ -279,12 +280,7 @@ err:
 int open_image_dir(void)
 {
 	int fd;
-
-	image_dir_fd = get_service_fd(IMG_FD_OFF);
-	if (image_dir_fd < 0) {
-		pr_perror("Can't get image fd");
-		return -1;
-	}
+	int dfd = get_service_fd(IMG_FD_OFF);
 
 	fd = open(".", O_RDONLY);
 	if (fd < 0) {
@@ -292,13 +288,19 @@ int open_image_dir(void)
 		return -1;
 	}
 
-	pr_info("Image dir fd is %d\n", image_dir_fd);
+	pr_info("Image dir fd is %d\n", dfd);
 
-	return reopen_fd_as(image_dir_fd, fd);
+	image_dir_inited = true;
+
+	return reopen_fd_as(dfd, fd);
 }
 
 void close_image_dir(void)
 {
-	close(image_dir_fd);
-	image_dir_fd = -1;
+	int dfd = get_service_fd(IMG_FD_OFF);
+
+	if (!image_dir_inited)
+		return;
+	close_safe(&dfd);
+	image_dir_inited = false;
 }
-- 
1.7.11.7



More information about the CRIU mailing list