[CRIU] [PATCH 1/4] mount: open_mountpoint returns a file descriptro

Andrey Vagin avagin at openvz.org
Tue Apr 15 01:31:39 PDT 2014


Only one function use DIR, so I don't see reason to return it

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 mount.c | 67 ++++++++++++++++++++++++++---------------------------------------
 1 file changed, 27 insertions(+), 40 deletions(-)

diff --git a/mount.c b/mount.c
index 1d0bfe9..0dcfcfd 100644
--- a/mount.c
+++ b/mount.c
@@ -40,8 +40,7 @@ static struct mount_info *mntinfo;
 static struct mount_info *mntinfo_tree;
 int mntns_root = -1;
 
-static DIR *open_mountpoint(struct mount_info *pm);
-static int close_mountpoint(DIR *dfd);
+static int open_mountpoint(struct mount_info *pm);
 
 static struct mount_info *mnt_build_tree(struct mount_info *list);
 static int validate_mounts(struct mount_info *info, bool call_plugins);
@@ -476,11 +475,10 @@ static struct mount_info *mnt_build_tree(struct mount_info *list)
  * mnt_fd is a file descriptor on the mountpoint, which is closed in an error case.
  * If mnt_fd is -1, the mountpoint will be opened by this function.
  */
-static DIR *__open_mountpoint(struct mount_info *pm, int mnt_fd)
+static int __open_mountpoint(struct mount_info *pm, int mnt_fd)
 {
 	char path[PATH_MAX + 1];
 	struct stat st;
-	DIR *fdir;
 	int ret;
 
 	if (mnt_fd == -1) {
@@ -488,7 +486,7 @@ static DIR *__open_mountpoint(struct mount_info *pm, int mnt_fd)
 		mnt_fd = openat(mntns_root, path, O_RDONLY);
 		if (mnt_fd < 0) {
 			pr_perror("Can't open %s", pm->mountpoint);
-			return NULL;
+			return -1;
 		}
 	}
 
@@ -504,28 +502,13 @@ static DIR *__open_mountpoint(struct mount_info *pm, int mnt_fd)
 		goto err;
 	}
 
-	fdir = fdopendir(mnt_fd);
-	if (fdir == NULL) {
-		pr_perror("Can't open %s", pm->mountpoint);
-		goto err;
-	}
-
-	return fdir;
+	return mnt_fd;
 err:
 	close(mnt_fd);
-	return NULL;
+	return -1;
 }
 
-static int close_mountpoint(DIR *dfd)
-{
-	if (closedir(dfd)) {
-		pr_perror("Unable to close directory");
-		return -1;
-	}
-	return 0;
-}
-
-static DIR *open_mountpoint(struct mount_info *pm)
+static int open_mountpoint(struct mount_info *pm)
 {
 	int fd = -1, ns_old = -1;
 	char mnt_path[] = "/tmp/cr-tmpfs.XXXXXX";
@@ -548,7 +531,7 @@ static DIR *open_mountpoint(struct mount_info *pm)
 	 */
 
 	if (switch_ns(root_item->pid.real, &mnt_ns_desc, &ns_old) < 0)
-		return NULL;
+		return -1;
 
 	if (mkdtemp(mnt_path) == NULL) {
 		pr_perror("Can't create a temporary directory");
@@ -576,7 +559,7 @@ out:
 	if (ns_old >= 0)
 		 restore_ns(ns_old, &mnt_ns_desc);
 	close_safe(&fd);
-	return NULL;
+	return -1;
 }
 
 /* Is it mounted w or w/o the newinstance option */
@@ -585,7 +568,7 @@ static int devpts_dump(struct mount_info *pm)
 	static const char newinstance[] = ",newinstance";
 	struct stat *host_st;
 	struct stat st;
-	DIR *fdir = NULL;
+	int fdir;
 	char *buf;
 	int len;
 
@@ -594,16 +577,16 @@ static int devpts_dump(struct mount_info *pm)
 		return -1;
 
 	fdir = open_mountpoint(pm);
-	if (fdir == NULL)
+	if (fdir < 0)
 		return -1;
 
-	if (fstat(dirfd(fdir), &st)) {
+	if (fstat(fdir, &st)) {
 		pr_perror("Unable to statfs %d:%s",
 				pm->mnt_id, pm->mountpoint);
-		close_mountpoint(fdir);
+		close(fdir);
 		return -1;
 	}
-	close_mountpoint(fdir);
+	close(fdir);
 
 	if (host_st->st_dev == st.st_dev)
 		return 0;
@@ -623,14 +606,12 @@ static int tmpfs_dump(struct mount_info *pm)
 {
 	int ret = -1;
 	char tmpfs_path[PSFDS];
-	int fd, fd_img = -1;
-	DIR *fdir = NULL;
+	int fd = -1, fd_img = -1;
 
-	fdir = open_mountpoint(pm);
-	if (fdir == NULL)
+	fd = open_mountpoint(pm);
+	if (fd < 0)
 		return -1;
 
-	fd = dirfd(fdir);
 	if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) & ~FD_CLOEXEC) == -1) {
 		pr_perror("Can not drop FD_CLOEXEC");
 		goto out;
@@ -657,7 +638,7 @@ static int tmpfs_dump(struct mount_info *pm)
 
 out:
 	close_safe(&fd_img);
-	close_mountpoint(fdir);
+	close_safe(&fd);
 	return ret;
 }
 
@@ -685,14 +666,20 @@ static int tmpfs_restore(struct mount_info *pm)
 
 static int binfmt_misc_dump(struct mount_info *pm)
 {
-	int ret = -1;
+	int fd, ret = -1;
 	struct dirent *de;
 	DIR *fdir = NULL;
 
-	fdir = open_mountpoint(pm);
-	if (fdir == NULL)
+	fd = open_mountpoint(pm);
+	if (fd < 0)
 		return -1;
 
+	fdir = fdopendir(fd);
+	if (fdir == NULL) {
+		close(fd);
+		return -1;
+	}
+
 	while ((de = readdir(fdir))) {
 		if (dir_dots(de))
 			continue;
@@ -707,7 +694,7 @@ static int binfmt_misc_dump(struct mount_info *pm)
 
 	ret = 0;
 out:
-	close_mountpoint(fdir);
+	closedir(fdir);
 	return ret;
 }
 
-- 
1.8.5.3



More information about the CRIU mailing list