[CRIU] [PATCH 2/3] Add restoring of unnamed unix sockets.

Artem Kuzmitskiy artem.kuzmitskiy at gmail.com
Wed Jul 29 04:05:51 PDT 2015


Added functionality for restoring unnamed unix sockets
using already implemented feature - inherit fd and using same command line
option.
Usage example:
criu restore -d -D images -o restore.log --pidfile restore.pid -v4 \
     -x --inherit-fd fd[3]:socket:[9677263]

Signed-off-by: Artem Kuzmitskiy <artem.kuzmitskiy at lge.com>
---
 include/files.h |  2 +-
 include/image.h |  1 +
 sk-unix.c       | 32 ++++++++++++++++++++++++++++----
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/include/files.h b/include/files.h
index db7e108..25d69ad 100644
--- a/include/files.h
+++ b/include/files.h
@@ -174,7 +174,7 @@ extern int inherit_fd_add(int fd, char *key);
 extern void inherit_fd_log(void);
 extern int inherit_fd_resolve_clash(int fd);
 extern int inherit_fd_fini(void);
-
+extern bool inherit_fd_lookup_desc(struct file_desc *);
 extern bool inherited_fd(struct file_desc *, int *fdp);

 #endif /* __CR_FILES_H__ */
diff --git a/include/image.h b/include/image.h
index c13ead0..305febf 100644
--- a/include/image.h
+++ b/include/image.h
@@ -41,6 +41,7 @@
 #define USK_EXTERN	(1 << 0)
 #define USK_SERVICE	(1 << 1)
 #define USK_CALLBACK	(1 << 2)
+#define USK_INHERIT	(1 << 3)

 /*
  * VMA_AREA status:
diff --git a/sk-unix.c b/sk-unix.c
index ed8f319..a9c8052 100644
--- a/sk-unix.c
+++ b/sk-unix.c
@@ -153,7 +153,6 @@ static bool unix_sk_exception_lookup_id(ino_t ino)
 	return ret;
 }

-
 static int write_unix_entry(struct unix_sk_desc *sk)
 {
 	int ret;
@@ -738,16 +737,19 @@ static int post_open_unix_sk(struct file_desc *d, int fd)
 	if (ui->ue->uflags & USK_CALLBACK)
 		return 0;

-	pr_info("\tConnect %#x to %#x\n", ui->ue->ino, peer->ue->ino);
-
 	/* Skip external sockets */
 	if (!list_empty(&peer->d.fd_info_head))
 		futex_wait_while(&peer->prepared, 0);

+	if (ui->ue->uflags & USK_INHERIT)
+		return 0;
+
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_UNIX;
 	memcpy(&addr.sun_path, peer->name, peer->ue->name.len);

+	pr_info("\tConnect %#x to %#x\n", ui->ue->ino, peer->ue->ino);
+
 	if (connect(fd, (struct sockaddr *)&addr,
 				sizeof(addr.sun_family) +
 				peer->ue->name.len) < 0) {
@@ -1028,7 +1030,13 @@ static int open_unix_sk(struct file_desc *d)
 	struct unix_sk_info *ui;

 	ui = container_of(d, struct unix_sk_info, d);
-	if (ui->flags & USK_PAIR_MASTER)
+
+	int unixsk_fd = -1;
+
+	if (inherited_fd(d, &unixsk_fd)) {
+		ui->ue->uflags |= USK_INHERIT;
+		return unixsk_fd;
+	} else if (ui->flags & USK_PAIR_MASTER)
 		return open_unixsk_pair_master(ui);
 	else if (ui->flags & USK_PAIR_SLAVE)
 		return open_unixsk_pair_slave(ui);
@@ -1036,11 +1044,27 @@ static int open_unix_sk(struct file_desc *d)
 		return open_unixsk_standalone(ui);
 }

+static char *socket_d_name(struct file_desc *d, char *buf, size_t s)
+{
+	struct unix_sk_info *ui;
+
+	ui = container_of(d, struct unix_sk_info, d);
+
+	if (snprintf(buf, s, "socket:[%d]", ui->ue->ino) >= s) {
+		pr_err("Not enough room for unixsk %d identifier string\n",
+				ui->ue->ino);
+		return NULL;
+	}
+
+	return buf;
+}
+
 static struct file_desc_ops unix_desc_ops = {
 	.type = FD_TYPES__UNIXSK,
 	.open = open_unix_sk,
 	.post_open = post_open_unix_sk,
 	.want_transport = unixsk_should_open_transport,
+	.name = socket_d_name,
 };

 static int collect_one_unixsk(void *o, ProtobufCMessage *base)
--
2.4.6



More information about the CRIU mailing list