[CRIU] [PATCH 2/5] image: add functions to close and re-open the network listen socket

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


During the restore of the file descriptors all open network
sockets have to be closed. The network hostname and port
are stored during open_listen_socket() and after close_listen_socket()
they can be re-used by calling reopen_listen_socket().

Signed-off-by: Adrian Reber <adrian at lisas.de>
---
 image.c           |   28 ++++++++++++++++++++++++++++
 include/crtools.h |    2 ++
 2 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/image.c b/image.c
index 2115412..969017a 100644
--- a/image.c
+++ b/image.c
@@ -218,6 +218,8 @@ struct cr_fdset *cr_glob_fdset_open(int mode)
 
 static int image_dir_fd = -1;
 static int sockfd = -1;
+static char network_host[255] = "";
+static int network_port = -1;
 
 int open_image(int type, unsigned long flags, ...)
 {
@@ -309,6 +311,15 @@ int open_listen_socket(char *host, int port)
 	struct hostent *server;
 	struct sockaddr_in serveraddr;
 
+	if (sockfd != -1)
+		return sockfd;
+
+	/* if host is not specified return early */
+	if (host == NULL)
+		return -1;
+
+	pr_info("Listening on %s:%d\n", host, port);
+
 	sockfd = socket(AF_INET, SOCK_STREAM, 0);
 	if (sockfd < 0) {
 		pr_perror("ERROR opening socket");
@@ -336,5 +347,22 @@ int open_listen_socket(char *host, int port)
 		return -1;
 	}
 
+	strncpy(network_host, host, 255);
+	network_port = port;
 	return sockfd;
 }
+
+void close_listen_socket()
+{
+	if (sockfd == -1)
+		return;
+	close_safe(&sockfd);
+	sockfd = -1;
+}
+
+int reopen_listen_socket()
+{
+	if (network_port != -1)
+		return open_listen_socket(network_host, network_port);
+	return 0;
+}
diff --git a/include/crtools.h b/include/crtools.h
index c2ccbe0..c092802 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -133,6 +133,8 @@ extern int open_image_dir(void);
 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();
 
 int open_image(int type, unsigned long flags, ...);
 #define open_image_ro(type, ...) open_image(type, O_RDONLY, ##__VA_ARGS__)
-- 
1.7.6.5



More information about the CRIU mailing list