[CRIU] [PATCH 3/4] IPC: restore message queue

Kinsbursky Stanislav skinsbursky at openvz.org
Wed Feb 8 12:30:53 EST 2012



Signed-off-by: Stanislav Kinsbursky <skinsbursky at parallels.com>

---
 ipc_ns.c |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 117 insertions(+), 0 deletions(-)

diff --git a/ipc_ns.c b/ipc_ns.c
index 00043ad..be3faaf 100644
--- a/ipc_ns.c
+++ b/ipc_ns.c
@@ -25,6 +25,10 @@
 #define MSGMAX			8192
 #endif
 
+#ifndef MSG_SET
+#define MSG_SET			13
+#endif
+
 static void print_ipc_seg(const struct ipc_seg *seg)
 {
 	pr_info("id: %-10d key: 0x%08x ", seg->id, seg->key);
@@ -413,6 +417,116 @@ void show_ipc_var(int fd)
 	pr_img_tail(CR_FD_IPCNS);
 }
 
+static int prepare_ipc_msg_pages(int fd, const struct ipc_msg_entry *entry)
+{
+	int msg_nr = 0;
+
+	while (msg_nr < entry->qnum) {
+		struct msgbuf {
+			long mtype;
+			char mtext[MSGMAX];
+		} data;
+		struct ipc_msg msg;
+		int ret;
+
+		ret = read_img(fd, &msg);
+		if (ret <= 0)
+			return -EIO;
+
+		print_ipc_msg(msg_nr, &msg);
+
+		if (msg.size > MSGMAX) {
+			pr_err("Unsupported message size: %d (MAX: %d)\n",
+						msg.size, MSGMAX);
+			return ret;
+		}
+
+		ret = read_img_buf(fd, data.mtext, round_up(msg.size, sizeof(u64)));
+		if (ret < 0) {
+			pr_err("Failed to read IPC message data\n");
+			return ret;
+		}
+
+		data.mtype = msg.mtype;
+		ret = msgsnd(entry->seg.id, &data, msg.size, IPC_NOWAIT);
+		if (ret < 0) {
+			pr_err("Failed to send IPC message: %d\n", -errno);
+			return -1;
+		}
+		msg_nr++;
+	}
+	return 0;
+}
+
+static int prepare_ipc_msg_seg(int fd, const struct ipc_msg_entry *entry)
+{
+	int ret, id;
+	struct msqid_ds ds;
+
+	id = msgget(entry->seg.id,
+		     entry->seg.mode | IPC_CREAT | IPC_EXCL | IPC_PRESET);
+	if (id == -1) {
+		pr_err("Failed to create shm segment\n");
+		return -errno;
+	}
+
+	if (id != entry->seg.id) {
+		pr_err("Failed to preset id (%d instead of %d)\n",
+							id, entry->seg.id);
+		return -EFAULT;
+	}
+
+	ret = msgctl(id, MSG_STAT, &ds);
+	if (ret < 0) {
+		pr_err("Failed to stat msg segment\n");
+		return -errno;
+	}
+
+	ds.msg_perm.KEY = entry->seg.key;
+	ret = msgctl(id, MSG_SET, &ds);
+	if (ret < 0) {
+		pr_err("Failed to update msg key\n");
+		return -errno;
+	}
+	ret = prepare_ipc_msg_pages(fd, entry);
+	if (ret < 0) {
+		pr_err("Failed to update msg pages\n");
+		return -errno;
+	}
+	return 0;
+}
+
+static int prepare_ipc_msg(int pid)
+{
+	int fd;
+
+	pr_info("Restoring IPC message queues\n");
+	fd = open_image_ro(CR_FD_IPCNS_MSG, pid);
+	if (fd < 0)
+		return -1;
+
+	while (1) {
+		int ret, id;
+		struct ipc_msg_entry entry;
+		struct msqid_ds ds;
+
+		ret = read_img_eof(fd, &entry);
+		if (ret < 0)
+			return -EIO;
+		if (ret == 0)
+			break;
+
+		print_ipc_msg_entry(&entry);
+
+		ret = prepare_ipc_msg_seg(fd, &entry);
+		if (ret < 0) {
+			pr_err("Failed to prepare msg segment\n");
+			return ret;
+		}
+	}
+	return 0;
+}
+
 static int prepare_ipc_shm_pages(int fd, const struct ipc_shm_entry *shm)
 {
 	int ret;
@@ -533,5 +647,8 @@ int prepare_ipc_ns(int pid)
 	ret = prepare_ipc_shm(pid);
 	if (ret < 0)
 		return ret;
+	ret = prepare_ipc_msg(pid);
+	if (ret < 0)
+		return ret;
 	return 0;
 }



More information about the CRIU mailing list