[CRIU] [PATCH 1/4] IPC: dump semaphores set

Kinsbursky Stanislav skinsbursky at openvz.org
Thu Feb 9 10:57:49 EST 2012



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

---
 crtools.c         |    6 +++
 include/crtools.h |    3 +
 include/image.h   |    7 +++
 ipc_ns.c          |  114 +++++++++++++++++++++++++++++++++++++++++++++--------
 4 files changed, 112 insertions(+), 18 deletions(-)

diff --git a/crtools.c b/crtools.c
index fee18ef..3c6380c 100644
--- a/crtools.c
+++ b/crtools.c
@@ -128,6 +128,12 @@ struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX] = {
 		.fmt	= FMT_FNAME_IPCNS_MSG,
 		.magic	= IPCNS_MSG_MAGIC,
 	},
+
+	/* IPC namespace message queues */
+	[CR_FD_IPCNS_SEM] = {
+		.fmt	= FMT_FNAME_IPCNS_SEM,
+		.magic	= IPCNS_SEM_MAGIC,
+	},
 };
 
 static struct cr_fdset *alloc_cr_fdset(void)
diff --git a/include/crtools.h b/include/crtools.h
index 231ea7a..08cedae 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -40,6 +40,7 @@ enum {
 	CR_FD_IPCNS_VAR,
 	CR_FD_IPCNS_SHM,
 	CR_FD_IPCNS_MSG,
+	CR_FD_IPCNS_SEM,
 
 	CR_FD_MAX
 };
@@ -84,6 +85,7 @@ extern struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX];
 #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"
+#define FMT_FNAME_IPCNS_SEM	"ipcns-sem-%d.img"
 
 extern int get_image_path(char *path, int size, const char *fmt, int pid);
 
@@ -117,6 +119,7 @@ struct cr_fdset {
 	CR_FD_DESC_USE(CR_FD_UTSNS)		|\
 	CR_FD_DESC_USE(CR_FD_IPCNS_VAR)		|\
 	CR_FD_DESC_USE(CR_FD_IPCNS_MSG)		|\
+	CR_FD_DESC_USE(CR_FD_IPCNS_SEM)		|\
 	CR_FD_DESC_USE(CR_FD_IPCNS_SHM)		)
 #define CR_FD_DESC_NONE			(0)
 
diff --git a/include/image.h b/include/image.h
index ef176f3..87f721e 100644
--- a/include/image.h
+++ b/include/image.h
@@ -24,6 +24,7 @@
 #define IPCNS_VAR_MAGIC	0x53115007 /* Samara */
 #define IPCNS_SHM_MAGIC	0x46283044 /* Odessa */
 #define IPCNS_MSG_MAGIC	0x55453737 /* Moscow */
+#define IPCNS_SEM_MAGIC	0x59573019 /* St. Petersburg */
 
 #define PIPEFS_MAGIC	0x50495045
 
@@ -156,6 +157,12 @@ struct ipc_msg_entry {
 	u8	pad[4];
 } __packed;
 
+struct ipc_sem_entry {
+	struct ipc_seg seg;
+	u16	nsems;
+	u8	pad[6];
+} __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 eaaf14e..d6b575f 100644
--- a/ipc_ns.c
+++ b/ipc_ns.c
@@ -53,6 +53,101 @@ static void fill_ipc_seg(int id, struct ipc_seg *seg, const struct ipc_perm *ipc
 	seg->mode = ipcp->mode;
 }
 
+static void print_ipc_sem(int nr, u16 *values)
+{
+	while(nr--)
+		pr_info("  %-5d", values[nr]);
+	pr_info("\n");
+}
+
+static void print_ipc_sem_entry(const struct ipc_sem_entry *sem)
+{
+	print_ipc_seg(&sem->seg);
+	pr_info ("nsems: %-10d\n", sem->nsems);
+}
+
+static int dump_ipc_sem_set(int fd, const struct ipc_sem_entry *entry)
+{
+	int ret, size;
+	u16 *values;
+
+	size = sizeof(u16) * entry->nsems;
+	values = xmalloc(size);
+	if (values == NULL) {
+		pr_err("Failed to allocate memory for semaphore set values\n");
+		ret = -ENOMEM;
+		goto out;
+	}
+	ret = semctl(entry->seg.id, 0, GETALL, values);
+	if (ret < 0) {
+		pr_perror("Failed to get semaphore set values");
+		ret = -errno;
+		goto out;
+	}
+	print_ipc_sem(entry->nsems, values);
+
+	ret = write_img_buf(fd, values, round_up(size, sizeof(u64)));
+	if (ret < 0) {
+		pr_err("Failed to write IPC message data\n");
+		goto out;
+	}
+out:
+	xfree(values);
+	return ret;
+}
+
+static int dump_ipc_sem_seg(int fd, int id, const struct semid_ds *ds)
+{
+	struct ipc_sem_entry sem;
+	int ret;
+
+	fill_ipc_seg(id, &sem.seg, &ds->sem_perm);
+	sem.nsems = ds->sem_nsems;
+	print_ipc_sem_entry(&sem);
+
+	ret = write_img(fd, &sem);
+	if (ret < 0) {
+		pr_err("Failed to write IPC semaphores set\n");
+		return ret;
+	}
+	return dump_ipc_sem_set(fd, &sem);
+}
+
+static int dump_ipc_sem(int fd)
+{
+	int i, maxid;
+	struct seminfo info;
+	int err, slot;
+
+	maxid = semctl(0, 0, SEM_INFO, &info);
+	if (maxid < 0) {
+		pr_perror("semctl failed");
+		return -errno;
+	}
+
+	pr_info("IPC semaphore sets: %d\n", info.semusz);
+	for (i = 0, slot = 0; i <= maxid; i++) {
+		struct semid_ds ds;
+		int id, ret;
+
+		id = semctl(i, 0, SEM_STAT, &ds);
+		if (id < 0) {
+			if (errno == EINVAL)
+				continue;
+			pr_perror("Failed to get stats for IPC semaphore set");
+			break;
+		}
+		ret = dump_ipc_sem_seg(fd, id, &ds);
+		if (!ret)
+			slot++;
+	}
+	if (slot != info.semusz) {
+		pr_err("Failed to collect %d (only %d succeeded)\n", info.semusz, slot);
+		return -EFAULT;
+	}
+	return info.semusz;
+}
+
 static void print_ipc_msg(int nr, const struct ipc_msg *msg)
 {
 	pr_info("  %-5d: type: %-20ld size: %-10d\n",
@@ -182,23 +277,6 @@ static int ipc_sysctl_req(struct ipc_var_entry *e, int op)
 	return sysctl_op(req, op);
 }
 
-static int dump_ipc_sem(void *data)
-{
-	int ret;
-	struct seminfo info;
-
-	ret = semctl(0, 0, SEM_INFO, &info);
-	if (ret < 0)
-		pr_perror("semctl failed");
-
-	if (ret) {
-		pr_err("IPC semaphores migration is not supported yet\n");
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
 /*
  * TODO: Function below should be later improved to locate and dump only dirty
  * pages via updated sys_mincore().
@@ -317,7 +395,7 @@ static int dump_ipc_data(const struct cr_fdset *fdset)
 	ret = dump_ipc_msg(fdset->fds[CR_FD_IPCNS_MSG]);
 	if (ret < 0)
 		return ret;
-	ret = dump_ipc_sem(0);
+	ret = dump_ipc_sem(fdset->fds[CR_FD_IPCNS_SEM]);
 	if (ret < 0)
 		return ret;
 	return 0;



More information about the CRIU mailing list