[CRIU] [RFC] sa_entry converted

Cyrill Gorcunov gorcunov at openvz.org
Tue Jul 17 10:50:31 EDT 2012


Guys, what do you thing on the patch enveloped?
I mean about the approach.

	Cyrill
-------------- next part --------------
>From e87bce36d3e6da49739728b50fbe0232e478510b Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Tue, 17 Jul 2012 18:35:32 +0400
Subject: [PATCH] protobuf: Convert sa_entry to PB format

This requires some explanation. This entry
is written to disk from inside of parasite
code where we can't use any of PB functionality.

But since PB format on disk is ABI and the sa_entry
structure is small (in term of members) and won't
be extended anytime soon -- we introduce 1:1 map
of this structure to PB ABI space via pb_sa_entry
helper structure (for this sake protobuf-abi.h
introduced). And now we can write sa_entry from
inside of the parasite code.

Other part of program (such as cr-restore.c
and cr-show.c) use regular definition from
PB generated sources.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 cr-restore.c           |   17 +++++++++++------
 cr-show.c              |   12 +++++++-----
 include/image.h        |    7 -------
 include/protobuf-abi.h |   36 ++++++++++++++++++++++++++++++++++++
 parasite.c             |   20 ++++++++++++++++++--
 protobuf/Makefile      |    1 +
 protobuf/sa.proto      |    9 +++++++++
 7 files changed, 82 insertions(+), 20 deletions(-)
 create mode 100644 include/protobuf-abi.h
 create mode 100644 protobuf/sa.proto

diff --git a/cr-restore.c b/cr-restore.c
index ce4ff47..e96f741 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -49,6 +49,9 @@
 #include "inotify.h"
 #include "pstree.h"
 
+#include "protobuf.h"
+#include "protobuf/sa.pb-c.h"
+
 static struct pstree_item *me;
 
 static int restore_task_with_children(void *);
@@ -194,7 +197,7 @@ static int prepare_sigactions(int pid)
 {
 	rt_sigaction_t act, oact;
 	int fd_sigact;
-	struct sa_entry e;
+	SaEntry *e;
 	int sig;
 	int ret = -1;
 
@@ -206,14 +209,16 @@ static int prepare_sigactions(int pid)
 		if (sig == SIGKILL || sig == SIGSTOP)
 			continue;
 
-		ret = read_img(fd_sigact, &e);
+		ret = pb_read(fd_sigact, &e, sa_entry);
 		if (ret < 0)
 			break;
 
-		ASSIGN_TYPED(act.rt_sa_handler, e.sigaction);
-		ASSIGN_TYPED(act.rt_sa_flags, e.flags);
-		ASSIGN_TYPED(act.rt_sa_restorer, e.restorer);
-		ASSIGN_TYPED(act.rt_sa_mask.sig[0], e.mask);
+		ASSIGN_TYPED(act.rt_sa_handler, e->sigaction);
+		ASSIGN_TYPED(act.rt_sa_flags, e->flags);
+		ASSIGN_TYPED(act.rt_sa_restorer, e->restorer);
+		ASSIGN_TYPED(act.rt_sa_mask.sig[0], e->mask);
+
+		sa_entry__free_unpacked(e, NULL);
 
 		if (sig == SIGCHLD) {
 			sigchld_act = act;
diff --git a/cr-show.c b/cr-show.c
index 63e1888..101f9cb 100644
--- a/cr-show.c
+++ b/cr-show.c
@@ -35,6 +35,7 @@
 #include "protobuf/pstree.pb-c.h"
 #include "protobuf/pipe.pb-c.h"
 #include "protobuf/pipe-data.pb-c.h"
+#include "protobuf/sa.pb-c.h"
 
 #define DEF_PAGES_PER_LINE	6
 
@@ -352,7 +353,7 @@ out:
 
 void show_sigacts(int fd_sigacts, struct cr_options *o)
 {
-	struct sa_entry e;
+	SaEntry *e;
 
 	pr_img_head(CR_FD_SIGACT);
 
@@ -364,10 +365,11 @@ void show_sigacts(int fd_sigacts, struct cr_options *o)
 			goto out;
 		pr_msg("sigaction: 0x%016lx mask: 0x%08lx "
 		       "flags: 0x%016lx restorer: 0x%016lx\n",
-		       (long)e.sigaction,
-		       (long)e.mask,
-		       (long)e.flags,
-		       (long)e.restorer);
+		       (long)e->sigaction,
+		       (long)e->mask,
+		       (long)e->flags,
+		       (long)e->restorer);
+		sa_entry__free_unpacked(e, NULL);
 	}
 
 out:
diff --git a/include/image.h b/include/image.h
index 852cac0..f13c839 100644
--- a/include/image.h
+++ b/include/image.h
@@ -103,13 +103,6 @@ struct page_entry {
 	u8	data[PAGE_IMAGE_SIZE];
 } __packed;
 
-struct sa_entry {
-	u64		sigaction;
-	u64		flags;
-	u64		restorer;
-	u64		mask;
-} __packed;
-
 struct itimer_entry {
 	u64		isec;
 	u64		iusec;
diff --git a/include/protobuf-abi.h b/include/protobuf-abi.h
new file mode 100644
index 0000000..eae6277
--- /dev/null
+++ b/include/protobuf-abi.h
@@ -0,0 +1,36 @@
+#ifndef PROTOBUF_ABI_H__
+#define PROTOBUF_ABI_H__
+
+#include "types.h"
+#include "compiler.h"
+#include "util.h"
+
+/*
+ * The structures which maps 1:1 to protobuf messages
+ * on ABI layer. We need them in parasite and/or restorer
+ * code where no way to use PB library.
+ */
+
+#define CR_PB_U64	1
+#define CR_PB_U32	5
+
+struct pb_sa_entry {
+	u8		__tag_pad_1;
+	u64	sigaction;
+	u8		__tag_pad_2;
+	u64	flags;
+	u8		__tag_pad_3;
+	u64	restorer;
+	u8		__tag_pad_4;
+	u64	mask;
+} __packed;
+
+#define PB_SA_ENTRY__INIT				\
+{							\
+	.__tag_pad_1	= (1 << 3) | CR_PB_U64,		\
+	.__tag_pad_2	= (2 << 3) | CR_PB_U64,		\
+	.__tag_pad_3	= (3 << 3) | CR_PB_U64,		\
+	.__tag_pad_4	= (4 << 3) | CR_PB_U64,		\
+}
+
+#endif /* PROTOBUF_ABI_H__ */
diff --git a/parasite.c b/parasite.c
index 975eea0..a8af63b 100644
--- a/parasite.c
+++ b/parasite.c
@@ -4,6 +4,8 @@
 #include "parasite.h"
 #include "util.h"
 
+#include "protobuf-abi.h"
+
 #include <string.h>
 
 /*
@@ -141,6 +143,20 @@ static int sys_write_safe(int fd, void *buf, int size)
 	return 0;
 }
 
+static int __pb_write_object_with_header(int fd, void *obj, u32 size)
+{
+	int ret = 0;
+
+	/* header */
+	ret = sys_write_safe(fd, &size, sizeof(size));
+
+	/* object */
+	if (!ret)
+		ret = sys_write_safe(fd, obj, size);
+
+	return ret;
+}
+
 /*
  * This is the main page dumping routine, it's executed
  * inside a victim process space.
@@ -265,7 +281,7 @@ static int dump_pages_fini(void)
 static int dump_sigact()
 {
 	rt_sigaction_t act;
-	struct sa_entry e;
+	struct pb_sa_entry e = PB_SA_ENTRY__INIT;
 	int fd, sig;
 	int ret;
 
@@ -288,7 +304,7 @@ static int dump_sigact()
 		ASSIGN_TYPED(e.restorer, act.rt_sa_restorer);
 		ASSIGN_TYPED(e.mask, act.rt_sa_mask.sig[0]);
 
-		ret = sys_write_safe(fd, &e, sizeof(e));
+		ret = __pb_write_object_with_header(fd, &e, sizeof(e));
 		if (ret)
 			goto err_close;
 	}
diff --git a/protobuf/Makefile b/protobuf/Makefile
index 641155a..5c1636b 100644
--- a/protobuf/Makefile
+++ b/protobuf/Makefile
@@ -44,6 +44,7 @@ PROTO_FILES	+= ipc-desc.proto
 PROTO_FILES	+= ipc-shm.proto
 PROTO_FILES	+= ipc-msg.proto
 PROTO_FILES	+= ipc-sem.proto
+PROTO_FILES	+= sa.proto
 
 HDRS	:= $(patsubst %.proto,%.pb-c.h,$(PROTO_FILES))
 SRCS	:= $(patsubst %.proto,%.pb-c.c,$(PROTO_FILES))
diff --git a/protobuf/sa.proto b/protobuf/sa.proto
new file mode 100644
index 0000000..8af8be1
--- /dev/null
+++ b/protobuf/sa.proto
@@ -0,0 +1,9 @@
+/*
+ * Don't forget to update protobuf-abi.h if change the message
+ */
+message sa_entry {
+	required fixed64	sigaction	= 1;
+	required fixed64	flags		= 2;
+	required fixed64	restorer	= 3;
+	required fixed64	mask		= 4;
+}
-- 
1.7.7.6



More information about the CRIU mailing list