[CRIU] [PATCH 1/5] service-fd: close a source file descriptor in install_service_fd()

Andrei Vagin avagin at virtuozzo.com
Thu Sep 13 02:41:11 MSK 2018


Currently, install_service_fd() always creates a new file descriptor,
but it is going to be reworked to reuse a file descriptor when it is
possible.

Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
 criu/action-scripts.c | 2 +-
 criu/cgroup.c         | 1 -
 criu/cr-dump.c        | 2 --
 criu/cr-restore.c     | 1 -
 criu/fdstore.c        | 1 -
 criu/files.c          | 5 +----
 criu/image.c          | 3 ++-
 criu/log.c            | 1 -
 criu/mount.c          | 1 -
 criu/namespaces.c     | 2 --
 criu/net.c            | 1 -
 criu/util.c           | 6 +++---
 12 files changed, 7 insertions(+), 19 deletions(-)

diff --git a/criu/action-scripts.c b/criu/action-scripts.c
index d382547cd..2803ec0f7 100644
--- a/criu/action-scripts.c
+++ b/criu/action-scripts.c
@@ -164,7 +164,7 @@ int add_rpc_notify(int sk)
 	BUG_ON(scripts_mode == SCRIPTS_SHELL);
 	scripts_mode = SCRIPTS_RPC;
 
-	if (install_service_fd(RPC_SK_OFF, sk) < 0)
+	if (install_service_fd(RPC_SK_OFF, dup(sk)) < 0)
 		return -1;
 
 	return 0;
diff --git a/criu/cgroup.c b/criu/cgroup.c
index 868196a99..26f159c74 100644
--- a/criu/cgroup.c
+++ b/criu/cgroup.c
@@ -1671,7 +1671,6 @@ static int prepare_cgroup_sfd(CgroupEntry *ce)
 	}
 
 	ret = install_service_fd(CGROUP_YARD, i);
-	close(i);
 	if (ret < 0)
 		goto err;
 
diff --git a/criu/cr-dump.c b/criu/cr-dump.c
index cbc72f016..fe7a3046d 100644
--- a/criu/cr-dump.c
+++ b/criu/cr-dump.c
@@ -1298,8 +1298,6 @@ static int dump_one_task(struct pstree_item *item, InventoryEntry *parent_ie)
 
 		if (install_service_fd(CR_PROC_FD_OFF, pfd) < 0)
 			goto err_cure_imgset;
-
-		close(pfd);
 	}
 
 	ret = parasite_fixup_vdso(parasite_ctl, pid, &vmas);
diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 45ed9c63a..2014d87f5 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -2052,7 +2052,6 @@ static int restore_root_task(struct pstree_item *init)
 	}
 
 	ret = install_service_fd(CR_PROC_FD_OFF, fd);
-	close(fd);
 	if (ret < 0)
 		return -1;
 
diff --git a/criu/fdstore.c b/criu/fdstore.c
index 88a5a9a15..a4583fdf4 100644
--- a/criu/fdstore.c
+++ b/criu/fdstore.c
@@ -78,7 +78,6 @@ int fdstore_init(void)
 	}
 
 	ret = install_service_fd(FDSTORE_SK_OFF, sk);
-	close(sk);
 	if (ret < 0)
 		return -1;
 
diff --git a/criu/files.c b/criu/files.c
index e59388f28..81ee7004f 100644
--- a/criu/files.c
+++ b/criu/files.c
@@ -1764,11 +1764,8 @@ int open_transport_socket(void)
 		goto out;
 	}
 
-	if (install_service_fd(TRANSPORT_FD_OFF, sock) < 0) {
-		close(sock);
+	if (install_service_fd(TRANSPORT_FD_OFF, sock) < 0)
 		goto out;
-	}
-	close(sock);
 	ret = 0;
 out:
 	return ret;
diff --git a/criu/image.c b/criu/image.c
index c28bfabee..170ebf4dc 100644
--- a/criu/image.c
+++ b/criu/image.c
@@ -520,7 +520,8 @@ int open_image_dir(char *dir)
 	}
 
 	ret = install_service_fd(IMG_FD_OFF, fd);
-	close(fd);
+	if (ret < 0)
+		return -1;
 	fd = ret;
 
 	if (opts.remote) {
diff --git a/criu/log.c b/criu/log.c
index 9e7703542..2f5b28a62 100644
--- a/criu/log.c
+++ b/criu/log.c
@@ -183,7 +183,6 @@ int log_init(const char *output)
 	}
 
 	fd = install_service_fd(LOG_FD_OFF, new_logfd);
-	close(new_logfd);
 	if (fd < 0)
 		goto err;
 
diff --git a/criu/mount.c b/criu/mount.c
index bdc39b530..218facb76 100644
--- a/criu/mount.c
+++ b/criu/mount.c
@@ -3398,7 +3398,6 @@ static int mntns_set_root_fd(pid_t pid, int fd)
 	ret = install_service_fd(ROOT_FD_OFF, fd);
 	if (ret >= 0)
 		mntns_root_pid = pid;
-	close(fd);
 
 	return ret;
 }
diff --git a/criu/namespaces.c b/criu/namespaces.c
index e8908e8ff..24cd61e2b 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -1427,11 +1427,9 @@ static int start_usernsd(void)
 	if (install_service_fd(USERNSD_SK, sk[0]) < 0) {
 		kill(usernsd_pid, SIGKILL);
 		waitpid(usernsd_pid, NULL, 0);
-		close(sk[0]);
 		return -1;
 	}
 
-	close(sk[0]);
 	return 0;
 }
 
diff --git a/criu/net.c b/criu/net.c
index 0431b62f9..40bf47b32 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -2549,7 +2549,6 @@ int netns_keep_nsfd(void)
 		pr_err("Can't install ns net reference\n");
 	else
 		pr_info("Saved netns fd for links restore\n");
-	close(ns_fd);
 
 	return ret >= 0 ? 0 : -1;
 }
diff --git a/criu/util.c b/criu/util.c
index 22d0be42f..bc932923c 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -314,7 +314,6 @@ static inline int set_proc_pid_fd(int pid, int fd)
 
 	open_proc_pid = pid;
 	ret = install_service_fd(PROC_PID_FD_OFF, fd);
-	close(fd);
 
 	return ret;
 }
@@ -348,7 +347,7 @@ void close_proc()
 
 int set_proc_fd(int fd)
 {
-	if (install_service_fd(PROC_FD_OFF, fd) < 0)
+	if (install_service_fd(PROC_FD_OFF, dup(fd)) < 0)
 		return -1;
 	return 0;
 }
@@ -365,7 +364,6 @@ static int open_proc_sfd(char *path)
 	}
 
 	ret = install_service_fd(PROC_FD_OFF, fd);
-	close(fd);
 	if (ret < 0)
 		return -1;
 
@@ -502,10 +500,12 @@ int install_service_fd(enum sfd_type type, int fd)
 
 	if (dup3(fd, sfd, O_CLOEXEC) != sfd) {
 		pr_perror("Dup %d -> %d failed", fd, sfd);
+		close(fd);
 		return -1;
 	}
 
 	set_bit(type, sfd_map);
+	close(fd);
 	return sfd;
 }
 
-- 
2.14.3



More information about the CRIU mailing list