[CRIU] [PATCH 5/5] util: move network socket to free FD during restore

Adrian Reber adrian at lisas.de
Tue Aug 21 11:20:49 EDT 2012


The socket listening on the network has to be moved during the
restore so that it does not collide with FDs which are trying
to be restored.

Signed-off-by: Adrian Reber <adrian at lisas.de>
---
 image.c           |    7 +++++++
 include/crtools.h |    1 +
 util.c            |    9 +++++++++
 3 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/image.c b/image.c
index 969017a..0d84c5c 100644
--- a/image.c
+++ b/image.c
@@ -366,3 +366,10 @@ int reopen_listen_socket()
 		return open_listen_socket(network_host, network_port);
 	return 0;
 }
+
+void update_listen_socket(int fd)
+{
+	if (sockfd == -1)
+		return;
+	sockfd = fd;
+}
diff --git a/include/crtools.h b/include/crtools.h
index c092802..f056327 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -135,6 +135,7 @@ extern void close_image_dir(void);
 extern int open_listen_socket(char *host, int port);
 extern void close_listen_socket();
 extern int reopen_listen_socket();
+extern void update_listen_socket(int fd);
 
 int open_image(int type, unsigned long flags, ...);
 #define open_image_ro(type, ...) open_image(type, O_RDONLY, ##__VA_ARGS__)
diff --git a/util.c b/util.c
index 4969e8d..91795d1 100644
--- a/util.c
+++ b/util.c
@@ -83,6 +83,15 @@ int reopen_fd_as_safe(char *file, int line, int new_fd, int old_fd, bool allow_r
 	if (old_fd != new_fd) {
 
 		if (!allow_reuse_fd) {
+			/* see if we are running a network migration */
+			tmp = open_listen_socket(NULL, 0);
+			if (tmp != -1) {
+				int tmp2;
+				/* yes, network socket is open; let's move it */
+				tmp2 = fcntl(tmp, F_DUPFD, new_fd);
+				update_listen_socket(tmp2);
+				close(tmp);
+			}
 			if (fcntl(new_fd, F_GETFD) != -1 || errno != EBADF) {
 				if (new_fd < 3) {
 					/*
-- 
1.7.6.5



More information about the CRIU mailing list