[CRIU] [PATCH 2/5] sk-queue: add an ability to dump addresses for messages

Andrey Vagin avagin at openvz.org
Mon Jun 13 16:35:32 PDT 2016


From: Andrew Vagin <avagin at virtuozzo.com>

Save an address in a protobuf message.

Signed-off-by: Andrew Vagin <avagin at virtuozzo.com>
---
 criu/include/sk-queue.h |  2 +-
 criu/sk-queue.c         | 30 ++++++++++++++++++++++++++++--
 criu/sk-unix.c          |  2 +-
 images/sk-packet.proto  |  1 +
 4 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/criu/include/sk-queue.h b/criu/include/sk-queue.h
index e0a47af..295a227 100644
--- a/criu/include/sk-queue.h
+++ b/criu/include/sk-queue.h
@@ -2,7 +2,7 @@
 #define __CR_SK_QUEUE_H__
 
 extern struct collect_image_info sk_queues_cinfo;
-extern int dump_sk_queue(int sock_fd, int sock_id);
+extern int dump_sk_queue(int sock_fd, int sock_id, bool dump_addr);
 extern int restore_sk_queue(int fd, unsigned int peer_id);
 
 #endif /* __CR_SK_QUEUE_H__ */
diff --git a/criu/sk-queue.c b/criu/sk-queue.c
index 50854d7..b6cc454 100644
--- a/criu/sk-queue.c
+++ b/criu/sk-queue.c
@@ -9,6 +9,7 @@
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/sendfile.h>
+#include <linux/socket.h>
 
 #include "asm/types.h"
 #include "list.h"
@@ -59,7 +60,7 @@ struct collect_image_info sk_queues_cinfo = {
 	.collect = collect_one_packet,
 };
 
-int dump_sk_queue(int sock_fd, int sock_id)
+int dump_sk_queue(int sock_fd, int sock_id, bool dump_addr)
 {
 	SkPacketEntry pe = SK_PACKET_ENTRY__INIT;
 	int ret, size, orig_peek_off;
@@ -109,6 +110,7 @@ int dump_sk_queue(int sock_fd, int sock_id)
 	pe.id_for = sock_id;
 
 	while (1) {
+		unsigned char addr[_K_SS_MAXSIZE];
 		struct iovec iov = {
 			.iov_base	= data,
 			.iov_len	= size,
@@ -118,6 +120,11 @@ int dump_sk_queue(int sock_fd, int sock_id)
 			.msg_iovlen	= 1,
 		};
 
+		if (dump_addr) {
+			msg.msg_name	= addr;
+			msg.msg_namelen	= _K_SS_MAXSIZE;
+		}
+
 		ret = pe.length = recvmsg(sock_fd, &msg, MSG_DONTWAIT | MSG_PEEK);
 		if (!ret)
 			/*
@@ -141,6 +148,12 @@ int dump_sk_queue(int sock_fd, int sock_id)
 			goto err_set_sock;
 		}
 
+		if (msg.msg_namelen) {
+			pe.has_addr = true;
+			pe.addr.data = addr;
+			pe.addr.len = msg.msg_namelen;
+		}
+
 		ret = pb_write_one(img_from_set(glob_imgset, CR_FD_SK_QUEUES), &pe, PB_SK_QUEUES);
 		if (ret < 0) {
 			ret = -EIO;
@@ -186,6 +199,13 @@ int restore_sk_queue(int fd, unsigned int peer_id)
 	list_for_each_entry_safe(pkt, tmp, &packets_list, list) {
 		SkPacketEntry *entry = pkt->entry;
 		char *buf;
+		struct iovec iov = {
+			.iov_len	= entry->length,
+		};
+		struct msghdr msg = {
+			.msg_iov	= &iov,
+			.msg_iovlen	= 1,
+		};
 
 		if (entry->id_for != peer_id)
 			continue;
@@ -204,6 +224,12 @@ int restore_sk_queue(int fd, unsigned int peer_id)
 		buf = xmalloc(entry->length);
 		if (buf ==NULL)
 			goto err;
+		iov.iov_base = buf;
+
+		if (entry->has_addr) {
+			msg.msg_name = entry->addr.data;
+			msg.msg_namelen = entry->addr.len;
+		}
 
 		if (lseek(img_raw_fd(img), pkt->img_off, SEEK_SET) == -1) {
 			pr_perror("lseek() failed");
@@ -215,7 +241,7 @@ int restore_sk_queue(int fd, unsigned int peer_id)
 			goto err;
 		}
 
-		ret = write(fd, buf, entry->length);
+		ret = sendmsg(fd, &msg, 0);
 		xfree(buf);
 		if (ret < 0) {
 			pr_perror("Failed to send packet");
diff --git a/criu/sk-unix.c b/criu/sk-unix.c
index c6cf672..b4dda33 100644
--- a/criu/sk-unix.c
+++ b/criu/sk-unix.c
@@ -444,7 +444,7 @@ dump:
 	 */
 	if (sk->rqlen != 0 && !(sk->type == SOCK_STREAM &&
 				sk->state == TCP_LISTEN))
-		if (dump_sk_queue(lfd, id))
+		if (dump_sk_queue(lfd, id, false))
 			goto err;
 
 	pr_info("Dumping unix socket at %d\n", p->fd);
diff --git a/images/sk-packet.proto b/images/sk-packet.proto
index 10ef5c9..ce8dcda 100644
--- a/images/sk-packet.proto
+++ b/images/sk-packet.proto
@@ -1,4 +1,5 @@
 message sk_packet_entry {
 	required uint32		id_for		= 1;
 	required uint32		length		= 2;
+	optional bytes		addr		= 3;
 }
-- 
2.7.4



More information about the CRIU mailing list