[CRIU] [PATCH 4/8] files: Split fs restore into prepare and restore

Pavel Emelyanov xemul at parallels.com
Thu Jul 3 08:07:44 PDT 2014


The prepare one will become more complicated soon.

Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 cr-restore.c       |  6 ++++-
 files.c            | 67 +++++++++++++++++++++++++++++++++++-------------------
 include/files.h    |  3 ++-
 include/rst_info.h |  5 ++++
 4 files changed, 55 insertions(+), 26 deletions(-)

diff --git a/cr-restore.c b/cr-restore.c
index 92a617a..556a35e 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -195,6 +195,10 @@ static int root_prepare_shared(void)
 		ret = prepare_fd_pid(pi);
 		if (ret < 0)
 			break;
+
+		ret = prepare_fs_pid(pi);
+		if (ret < 0)
+			break;
 	}
 
 	if (ret < 0)
@@ -2679,7 +2683,7 @@ static int sigreturn_restore(pid_t pid, CoreEntry *core)
 	 * attempts to open files by paths above (e.g. /proc).
 	 */
 
-	if (restore_fs(pid))
+	if (restore_fs(current))
 		goto err;
 
 	close_image_dir();
diff --git a/files.c b/files.c
index 2ccc5d1..2326b36 100644
--- a/files.c
+++ b/files.c
@@ -1018,33 +1018,26 @@ static int fchroot(int fd)
 	return chroot(fd_path);
 }
 
-int restore_fs(int pid)
+int restore_fs(struct pstree_item *me)
 {
-	int ifd, dd_root, dd_cwd, ret, err = -1;
-	FsEntry *fe;
-
-	ifd = open_image(CR_FD_FS, O_RSTR, pid);
-	if (ifd < 0)
-		goto out;
-
-	if (pb_read_one(ifd, &fe, PB_FS) < 0)
-		goto out_i;
+	int dd_root, dd_cwd, ret, err = -1;
+	struct rst_info *ri = me->rst;
 
 	/*
 	 * First -- open both descriptors. We will not
 	 * be able to open the cwd one after we chroot.
 	 */
 
-	dd_root = open_reg_by_id(fe->root_id);
+	dd_root = open_reg_by_id(ri->root_id);
 	if (dd_root < 0) {
-		pr_err("Can't open root %#x\n", fe->root_id);
-		goto err;
+		pr_err("Can't open root %#x\n", ri->root_id);
+		goto out;
 	}
 
-	dd_cwd = open_reg_by_id(fe->cwd_id);
+	dd_cwd = open_reg_by_id(ri->cwd_id);
 	if (dd_cwd < 0) {
-		pr_err("Can't open cwd %#x\n", fe->cwd_id);
-		goto err;
+		pr_err("Can't open cwd %#x\n", ri->cwd_id);
+		goto out;
 	}
 
 	/*
@@ -1057,28 +1050,54 @@ int restore_fs(int pid)
 	close(dd_root);
 	if (ret < 0) {
 		pr_perror("Can't change root");
-		goto err;
+		goto out;
 	}
 
 	ret = fchdir(dd_cwd);
 	close(dd_cwd);
 	if (ret < 0) {
 		pr_perror("Can't change cwd");
-		goto err;
+		goto out;
 	}
 
-	if (fe->has_umask) {
-		pr_info("Restoring umask to %o\n", fe->umask);
-		umask(fe->umask);
+	if (ri->has_umask) {
+		pr_info("Restoring umask to %o\n", ri->umask);
+		umask(ri->umask);
 	}
 
 	err = 0;
-err:
+out:
+	return err;
+}
+
+int prepare_fs_pid(struct pstree_item *item)
+{
+	pid_t pid = item->pid.virt;
+	struct rst_info *ri = item->rst;
+	int ifd;
+	FsEntry *fe;
+
+	ifd = open_image(CR_FD_FS, O_RSTR, pid);
+	if (ifd < 0)
+		goto out;
+
+	if (pb_read_one(ifd, &fe, PB_FS) < 0)
+		goto out_i;
+
+	close(ifd);
+
+	ri->cwd_id = fe->cwd_id;
+	ri->root_id = fe->root_id;
+	ri->has_umask = fe->has_umask;
+	ri->umask = fe->umask;
+
 	fs_entry__free_unpacked(fe, NULL);
+	return 0;
+
 out_i:
-	close_safe(&ifd);
+	close(ifd);
 out:
-	return err;
+	return -1;
 }
 
 int shared_fdt_prepare(struct pstree_item *item)
diff --git a/include/files.h b/include/files.h
index e63a998..950b341 100644
--- a/include/files.h
+++ b/include/files.h
@@ -148,7 +148,8 @@ extern int prepare_fd_pid(struct pstree_item *me);
 extern int prepare_ctl_tty(int pid, struct rst_info *rst_info, u32 ctl_tty_id);
 extern int prepare_shared_fdinfo(void);
 extern int get_filemap_fd(struct vma_area *);
-extern int restore_fs(int pid);
+extern int restore_fs(struct pstree_item *);
+extern int prepare_fs_pid(struct pstree_item *);
 extern int set_fd_flags(int fd, int flags);
 
 extern int close_old_fds(struct pstree_item *me);
diff --git a/include/rst_info.h b/include/rst_info.h
index d4df529..a8e0d38 100644
--- a/include/rst_info.h
+++ b/include/rst_info.h
@@ -49,6 +49,11 @@ struct rst_info {
 		struct pstree_item	*pgrp_leader;
 		futex_t			pgrp_set;
 	};
+
+	u32			cwd_id;
+	u32			root_id;
+	bool			has_umask;
+	u32			umask;
 };
 
 #endif /* __CR_RST_INFO_H__ */
-- 
1.8.3.1




More information about the CRIU mailing list