[CRIU] [PATCH 1/4] IPC: dump message queue
Kinsbursky Stanislav
skinsbursky at openvz.org
Wed Feb 8 12:30:39 EST 2012
Signed-off-by: Stanislav Kinsbursky <skinsbursky at parallels.com>
---
crtools.c | 6 ++
include/crtools.h | 5 ++
include/image.h | 14 ++++++
ipc_ns.c | 128 ++++++++++++++++++++++++++++++++++++++++++++---------
4 files changed, 131 insertions(+), 22 deletions(-)
diff --git a/crtools.c b/crtools.c
index c43eeab..fee18ef 100644
--- a/crtools.c
+++ b/crtools.c
@@ -122,6 +122,12 @@ struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX] = {
.fmt = FMT_FNAME_IPCNS_SHM,
.magic = IPCNS_SHM_MAGIC,
},
+
+ /* IPC namespace message queues */
+ [CR_FD_IPCNS_MSG] = {
+ .fmt = FMT_FNAME_IPCNS_MSG,
+ .magic = IPCNS_MSG_MAGIC,
+ },
};
static struct cr_fdset *alloc_cr_fdset(void)
diff --git a/include/crtools.h b/include/crtools.h
index cf7a46b..32c84ed 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -39,6 +39,7 @@ enum {
CR_FD_UTSNS,
CR_FD_IPCNS_VAR,
CR_FD_IPCNS_SHM,
+ CR_FD_IPCNS_MSG,
CR_FD_MAX
};
@@ -82,6 +83,7 @@ extern struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX];
#define FMT_FNAME_UTSNS "utsns-%d.img"
#define FMT_FNAME_IPCNS_VAR "ipcns-var-%d.img"
#define FMT_FNAME_IPCNS_SHM "ipcns-shm-%d.img"
+#define FMT_FNAME_IPCNS_MSG "ipcns-msg-%d.img"
extern int get_image_path(char *path, int size, const char *fmt, int pid);
@@ -114,7 +116,8 @@ struct cr_fdset {
#define CR_FD_DESC_NS (\
CR_FD_DESC_USE(CR_FD_UTSNS) |\
CR_FD_DESC_USE(CR_FD_IPCNS_VAR) |\
- CR_FD_DESC_USE(CR_FD_IPCNS_SHM) )
+ CR_FD_DESC_USE(CR_FD_IPCNS_SHM) |\
+ CR_FD_DESC_USE(CR_FD_IPCNS_MSG) )
#define CR_FD_DESC_NONE (0)
int cr_dump_tasks(pid_t pid, struct cr_options *opts);
diff --git a/include/image.h b/include/image.h
index 01a3b86..ef176f3 100644
--- a/include/image.h
+++ b/include/image.h
@@ -23,6 +23,7 @@
#define CREDS_MAGIC 0x54023547 /* Kozelsk */
#define IPCNS_VAR_MAGIC 0x53115007 /* Samara */
#define IPCNS_SHM_MAGIC 0x46283044 /* Odessa */
+#define IPCNS_MSG_MAGIC 0x55453737 /* Moscow */
#define PIPEFS_MAGIC 0x50495045
@@ -142,6 +143,19 @@ struct ipc_shm_entry {
u64 size;
} __packed;
+struct ipc_msg {
+ u64 mtype;
+ u16 size;
+ u8 pad[6];
+} __packed;
+
+struct ipc_msg_entry {
+ struct ipc_seg seg;
+ u16 qbytes;
+ u16 qnum;
+ u8 pad[4];
+} __packed;
+
#define VMA_AREA_NONE (0 << 0)
#define VMA_AREA_REGULAR (1 << 0) /* Dumpable area */
#define VMA_AREA_STACK (1 << 1)
diff --git a/ipc_ns.c b/ipc_ns.c
index 5c4d27f..c958e63 100644
--- a/ipc_ns.c
+++ b/ipc_ns.c
@@ -21,6 +21,10 @@
#define SHM_SET 15
#endif
+#ifndef MSGMAX
+#define MSGMAX 8192
+#endif
+
static void print_ipc_seg(const struct ipc_seg *seg)
{
pr_info("id: %-10d key: 0x%08x ", seg->id, seg->key);
@@ -45,6 +49,108 @@ static void fill_ipc_seg(int id, struct ipc_seg *seg, const struct ipc_perm *ipc
seg->mode = ipcp->mode;
}
+static void print_ipc_msg(int nr, const struct ipc_msg *msg)
+{
+ pr_info(" %-5d: type: %-20ld size: %-10d\n",
+ nr++, msg->mtype, msg->size);
+}
+
+static void print_ipc_msg_entry(const struct ipc_msg_entry *msg)
+{
+ print_ipc_seg(&msg->seg);
+ pr_info ("qbytes: %-10d qnum: %-10d\n", msg->qbytes, msg->qnum);
+}
+
+static int dump_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 = msgrcv(entry->seg.id, &data, sizeof(data), 0, IPC_NOWAIT);
+ if (ret < 0) {
+ pr_err("Failed to receive IPC message: %d\n", -errno);
+ return -1;
+ }
+ msg.size = ret;
+ msg.mtype = data.mtype;
+
+ print_ipc_msg(msg_nr, &msg);
+
+ ret = write_img(fd, &msg);
+ if (ret < 0) {
+ pr_err("Failed to write IPC message header\n");
+ return ret;
+ }
+ ret = write_img_buf(fd, data.mtext, round_up(msg.size, sizeof(u64)));
+ if (ret < 0) {
+ pr_err("Failed to write IPC message data\n");
+ return ret;
+ }
+ msg_nr++;
+ }
+ return 0;
+}
+
+static int dump_ipc_msg_seg(int fd, int id, const struct msqid_ds *ds)
+{
+ struct ipc_msg_entry msg;
+ int ret;
+
+ fill_ipc_seg(id, &msg.seg, &ds->msg_perm);
+ msg.qbytes = ds->msg_qbytes;
+ msg.qnum = ds->msg_qnum;
+ print_ipc_msg_entry(&msg);
+
+ ret = write_img(fd, &msg);
+ if (ret < 0) {
+ pr_err("Failed to write IPC message queue segment\n");
+ return ret;
+ }
+ return dump_ipc_msg_pages(fd, &msg);
+}
+
+static int dump_ipc_msg(int fd)
+{
+ int i, maxid;
+ struct msginfo info;
+ int err, slot;
+
+ maxid = msgctl(0, MSG_INFO, (struct msqid_ds *)&info);
+ if (maxid < 0) {
+ pr_perror("msgctl failed");
+ return maxid;
+ }
+
+ pr_info("IPC message queues: %d\n", info.msgpool);
+ for (i = 0, slot = 0; i <= maxid; i++) {
+ struct msqid_ds ds;
+ int id, ret;
+
+ id = msgctl(i, MSG_STAT, &ds);
+ if (id < 0) {
+ if (errno == EINVAL)
+ continue;
+ pr_perror("Failed to get stats for IPC message queue\n");
+ break;
+ }
+ ret = dump_ipc_msg_seg(fd, id, &ds);
+ if (!ret)
+ slot++;
+ }
+ if (slot != info.msgpool) {
+ pr_err("Failed to collect %d (only %d succeeded)\n", info.msgpool, slot);
+ return -EFAULT;
+ }
+ return info.msgpool;
+}
+
static void print_ipc_shm(const struct ipc_shm_entry *shm)
{
print_ipc_seg(&shm->seg);
@@ -72,26 +178,6 @@ static int ipc_sysctl_req(struct ipc_var_entry *e, int op)
return sysctl_op(req, op);
}
-static int dump_ipc_msg(void *data)
-{
- struct msginfo info;
- int ret;
- int fd;
-
- ret = msgctl(0, MSG_INFO, (struct msqid_ds *)&info);
- if (ret < 0) {
- pr_perror("msgctl failed");
- return ret;
- }
-
- if (ret) {
- pr_err("IPC messages migration is not supported yet\n");
- return -EINVAL;
- }
-
- return 0;
-}
-
static int dump_ipc_sem(void *data)
{
int ret;
@@ -216,7 +302,7 @@ static int dump_ipc_data(const struct cr_fdset *fdset)
ret = dump_ipc_shm(fdset->fds[CR_FD_IPCNS_SHM]);
if (ret < 0)
return ret;
- ret = dump_ipc_msg(0);
+ ret = dump_ipc_msg(fdset->fds[CR_FD_IPCNS_MSG]);
if (ret < 0)
return ret;
ret = dump_ipc_sem(0);
More information about the CRIU
mailing list