[CRIU] [PATCH 3/4] compel: simplify usage wrt ids

Kir Kolyshkin kir at openvz.org
Tue Dec 6 15:54:28 PST 2016


Currently, some compel internals are exposed to user API
(both C and CLI), making its usage more complicated than
it can be.

In particular, compel user have to specify a number of parameters
(names for various data) on the command line, and when in C code
assign a struc piegen_opt_t fields using the same names, without
using those identifiers anywhere else in the code.

It makes sense to hide this complexity from a user, which is what
this commit does.

First, remove the ability to specify individual names for data,
instead introducing a prefix that is prepended to all the names.
Second, generate a function %PREFIX%_setup_c_header() which does
all the needed assignments.
Third, convert users (criu/pie and compel test) to the new API.

Signed-off-by: Kir Kolyshkin <kir at openvz.org>
---
 compel/include/piegen.h     |  5 +----
 compel/src/lib/handle-elf.c | 34 +++++++++++++++++++++++++++++-----
 compel/src/main.c           | 22 +++-------------------
 compel/test/infect/spy.c    | 16 +---------------
 criu/include/restorer.h     |  5 ++---
 criu/parasite-syscall.c     | 33 ++-------------------------------
 criu/pie/Makefile           |  5 +----
 scripts/gen-offsets.sh      |  4 ++--
 8 files changed, 41 insertions(+), 83 deletions(-)

diff --git a/compel/include/piegen.h b/compel/include/piegen.h
index d240cd6..3dd62e6 100644
--- a/compel/include/piegen.h
+++ b/compel/include/piegen.h
@@ -12,10 +12,7 @@ typedef struct {
 	char		*input_filename;
 	char		*output_filename;
 	char		*uapi_dir;
-	char		*stream_name;
-	char		*prefix_name;
-	char		*var_name;
-	char		*nrgotpcrel_name;
+	char		*prefix;
 	FILE		*fout;
 } piegen_opt_t;
 
diff --git a/compel/src/lib/handle-elf.c b/compel/src/lib/handle-elf.c
index 477358a..d53e74a 100644
--- a/compel/src/lib/handle-elf.c
+++ b/compel/src/lib/handle-elf.c
@@ -257,15 +257,15 @@ int __handle_elf(void *mem, size_t size)
 					sh_src = sec_hdrs[sym->st_shndx];
 					ptr_func_exit(sh_src);
 				}
-				pr_out("#define %s%s 0x%lx\n",
-				       opts.prefix_name, name,
+				pr_out("#define %s_sym%s 0x%lx\n",
+				       opts.prefix, name,
 				       (unsigned long)(sym->st_value +
 						       (sh_src ? sh_src->sh_addr : 0)));
 			}
 		}
 	}
 
-	pr_out("static __maybe_unused compel_reloc_t %s[] = {\n", opts.var_name);
+	pr_out("static __maybe_unused compel_reloc_t %s_relocs[] = {\n", opts.prefix);
 
 	pr_debug("Relocations\n");
 	pr_debug("------------\n");
@@ -545,9 +545,9 @@ int __handle_elf(void *mem, size_t size)
 		}
 	}
 	pr_out("};\n");
-	pr_out("static __maybe_unused size_t %s = %zd;\n", opts.nrgotpcrel_name, nr_gotpcrel);
+	pr_out("static __maybe_unused size_t %s_nr_gotpcrel = %zd;\n", opts.prefix, nr_gotpcrel);
 
-	pr_out("static __maybe_unused const char %s[] = {\n\t", opts.stream_name);
+	pr_out("static __maybe_unused const char %s_blob[] = {\n\t", opts.prefix);
 
 	for (i = 0, k = 0; i < hdr->e_shnum; i++) {
 		Elf_Shdr *sh = sec_hdrs[i];
@@ -577,6 +577,30 @@ int __handle_elf(void *mem, size_t size)
 		}
 	}
 	pr_out("};\n");
+	pr_out("\n");
+	pr_out("void %s_setup_c_header(struct parasite_ctl *ctl)\n",
+			opts.prefix);
+	pr_out(
+"{\n"
+"	struct parasite_blob_desc *pbd;\n"
+"\n"
+"	pbd = compel_parasite_blob_desc(ctl);\n"
+"	pbd->parasite_type	= COMPEL_BLOB_CHEADER;\n"
+);
+	pr_out("\tpbd->hdr.mem		= %s_blob;\n", opts.prefix);
+	pr_out("\tpbd->hdr.bsize	= sizeof(%s_blob);\n", opts.prefix);
+	pr_out("\tpbd->hdr.nr_gotpcrel	= %s_nr_gotpcrel;\n", opts.prefix);
+	pr_out("\tpbd->hdr.parasite_ip_off	= "
+			"COMPEL_H_PARASITE_HEAD(%s_sym);\n", opts.prefix);
+	pr_out("\tpbd->hdr.addr_cmd_off	= "
+			"COMPEL_H_PARASITE_CMD(%s_sym);\n", opts.prefix);
+	pr_out("\tpbd->hdr.addr_arg_off	= "
+			"COMPEL_H_PARASITE_ARGS(%s_sym);\n", opts.prefix);
+	pr_out("\tpbd->hdr.relocs	= %s_relocs;\n", opts.prefix);
+	pr_out("\tpbd->hdr.nr_relocs	= "
+			"sizeof(%s_relocs) / sizeof(%s_relocs[0]);\n",
+			opts.prefix, opts.prefix);
+	pr_out("}\n");
 	ret = 0;
 err:
 	free(sec_hdrs);
diff --git a/compel/src/main.c b/compel/src/main.c
index 3d18d74..39652e9 100644
--- a/compel/src/main.c
+++ b/compel/src/main.c
@@ -30,10 +30,6 @@
 piegen_opt_t opts = {
 	.input_filename		= NULL,
 	.uapi_dir		= "piegen/uapi",
-	.stream_name		= "stream",
-	.prefix_name		= "__",
-	.var_name		= "elf_relocs",
-	.nrgotpcrel_name	= "nr_gotpcrel",
 	.fout			= NULL,
 };
 
@@ -121,16 +117,13 @@ int main(int argc, char *argv[])
 		},
 	};
 
-	static const char short_opts[] = "a:f:o:s:p:v:r:u:hVl:";
+	static const char short_opts[] = "a:f:o:u:p:hVl:";
 	static struct option long_opts[] = {
 		{ "arch",	required_argument,	0, 'a' },
 		{ "file",	required_argument,	0, 'f' },
 		{ "output",	required_argument,	0, 'o' },
-		{ "stream",	required_argument,	0, 's' },
 		{ "uapi-dir",	required_argument,	0, 'u' },
-		{ "sym-prefix",	required_argument,	0, 'p' },
-		{ "variable",	required_argument,	0, 'v' },
-		{ "pcrelocs",	required_argument,	0, 'r' },
+		{ "prefix",	required_argument,	0, 'p' },
 		{ "help",	required_argument,	0, 'h' },
 		{ "version",	no_argument,		0, 'V' },
 		{ "log-level",	required_argument,	0, 'l' },
@@ -166,17 +159,8 @@ int main(int argc, char *argv[])
 		case 'u':
 			opts.uapi_dir = optarg;
 			break;
-		case 's':
-			opts.stream_name = optarg;
-			break;
 		case 'p':
-			opts.prefix_name = optarg;
-			break;
-		case 'v':
-			opts.var_name = optarg;
-			break;
-		case 'r':
-			opts.nrgotpcrel_name = optarg;
+			opts.prefix = optarg;
 			break;
 		case 'l':
 			log_level = atoi(optarg);
diff --git a/compel/test/infect/spy.c b/compel/test/infect/spy.c
index 90428b1..b56b2b7 100644
--- a/compel/test/infect/spy.c
+++ b/compel/test/infect/spy.c
@@ -48,21 +48,7 @@ static int do_infection(int pid)
 	ictx = compel_infect_ctx(ctl);
 	ictx->log_fd = STDERR_FILENO;
 
-	/*
-	 * Next the blob descriptor. We've requested for hgen
-	 * in Makefile, so prepare this type of blob with the
-	 * values from parasite.h.
-	 */
-	pbd = compel_parasite_blob_desc(ctl);
-	pbd->parasite_type	= COMPEL_BLOB_CHEADER;
-	pbd->hdr.mem		= parasite_blob;
-	pbd->hdr.bsize		= sizeof(parasite_blob);
-	pbd->hdr.nr_gotpcrel	= parasite_nr_gotpcrel;
-	pbd->hdr.parasite_ip_off	= COMPEL_H_PARASITE_HEAD(parasite_sym);
-	pbd->hdr.addr_cmd_off	= COMPEL_H_PARASITE_CMD(parasite_sym);
-	pbd->hdr.addr_arg_off	= COMPEL_H_PARASITE_ARGS(parasite_sym);
-	pbd->hdr.relocs		= parasite_relocs;
-	pbd->hdr.nr_relocs		= sizeof(parasite_relocs) / sizeof(parasite_relocs[0]);
+	parasite_setup_c_header(ctl);
 
 	printf("Infecting\n");
 	if (compel_infect(ctl, 1, sizeof(int)))
diff --git a/criu/include/restorer.h b/criu/include/restorer.h
index 0afc60c..a569fa3 100644
--- a/criu/include/restorer.h
+++ b/criu/include/restorer.h
@@ -223,8 +223,7 @@ enum {
 	})
 
 
-/* the restorer_blob_offset__ prefix is added by gen_offsets.sh */
-#define __blob_offset(name)	restorer_blob_offset__ ## name
-#define restorer_sym(rblob, name)	(void*)(rblob + __blob_offset(name))
+#define __r_sym(name)			restorer_sym ## name
+#define restorer_sym(rblob, name)	(void*)(rblob + __r_sym(name))
 
 #endif /* __CR_RESTORER_H__ */
diff --git a/criu/parasite-syscall.c b/criu/parasite-syscall.c
index f7a812f..6ebb21c 100644
--- a/criu/parasite-syscall.c
+++ b/criu/parasite-syscall.c
@@ -532,37 +532,11 @@ static int make_sigframe(void *arg, struct rt_sigframe *sf, struct rt_sigframe *
 	return construct_sigframe(sf, rtsf, bs, (CoreEntry *)arg);
 }
 
-/* the parasite prefix is added by gen_offsets.sh */
-#define pblob_offset(ptype, symbol)					\
-	parasite_ ## ptype ## _blob_offset__ ## symbol
-
-#ifdef CONFIG_PIEGEN
-#define init_blob_relocs(bdesc, blob_type)						\
-	do {										\
-		bdesc->hdr.relocs = parasite_##blob_type##_relocs;			\
-		bdesc->hdr.nr_relocs = ARRAY_SIZE(parasite_##blob_type##_relocs);	\
-	} while (0)
-#else
-#define init_blob_relocs(bdesc, blob_type)
-#endif
-
-#define init_blob_desc(bdesc, blob_type) do {						\
-	bdesc->hdr.mem = parasite_##blob_type##_blob;					\
-	bdesc->hdr.bsize = sizeof(parasite_##blob_type##_blob);				\
-	bdesc->hdr.nr_gotpcrel = pie_nr_gotpcrel(parasite_##blob_type);			\
-	/* Setup the rest of a control block */						\
-	bdesc->hdr.parasite_ip_off = pblob_offset(blob_type, __export_parasite_head_start);\
-	bdesc->hdr.addr_cmd_off    = pblob_offset(blob_type, __export_parasite_cmd);	\
-	bdesc->hdr.addr_arg_off    = pblob_offset(blob_type, __export_parasite_args);	\
-	init_blob_relocs(bdesc, blob_type);						\
-	} while (0)
-
 struct parasite_ctl *parasite_infect_seized(pid_t pid, struct pstree_item *item,
 		struct vm_area_list *vma_area_list)
 {
 	struct parasite_ctl *ctl;
 	struct infect_ctx *ictx;
-	struct parasite_blob_desc *pbd;
 	unsigned long p;
 
 	BUG_ON(item->threads[0].real != pid);
@@ -604,14 +578,11 @@ struct parasite_ctl *parasite_infect_seized(pid_t pid, struct pstree_item *item,
 
 	ictx->log_fd = log_get_fd();
 
-	pbd = compel_parasite_blob_desc(ctl);
-	pbd->parasite_type = COMPEL_BLOB_CHEADER;
-
 	if (compel_mode_native(ctl))
-		init_blob_desc(pbd, native);
+		parasite_native_setup_c_header(ctl);
 #ifdef CONFIG_COMPAT
 	else
-		init_blob_desc(pbd, compat);
+		parasite_compat_setup_c_header(ctl);
 #endif
 
 	parasite_ensure_args_size(dump_pages_args_size(vma_area_list));
diff --git a/criu/pie/Makefile b/criu/pie/Makefile
index 5d0b43d..085b5bf 100644
--- a/criu/pie/Makefile
+++ b/criu/pie/Makefile
@@ -124,10 +124,7 @@ $(obj)/%-blob.h: $(obj)/%.built-in.bin.o $(SRC_DIR)/compel/compel-host
 	$(call msg-gen, $@)
 	$(Q) $(SRC_DIR)/compel/compel-host hgen -f $<			\
 		-l 4							\
-		-v $(call target-name,$@)_relocs			\
-		-p $(call target-name,$@)_blob_offset__			\
-		-s $(call target-name,$@)_blob				\
-		-r $(call target-name,$@)_nr_gotpcrel			\
+		-p $(call target-name,$@)				\
 		-u $(SRC_DIR)/compel/include/uapi			\
 		-o $@ $(piegen_stdout)
 
diff --git a/scripts/gen-offsets.sh b/scripts/gen-offsets.sh
index f5e3df2..16a0394 100644
--- a/scripts/gen-offsets.sh
+++ b/scripts/gen-offsets.sh
@@ -13,12 +13,12 @@ CROSS_COMPILE=
 fi
 
 INC_GUARD=__${NAME}_h__
-PREFIX=${NAME}_blob_offset__
+SYM=${NAME}_sym
 BLOB=${NAME}_blob
 OBJNAME=${FILE}.built-in.bin.o
 BINARY=${FILE}.built-in.bin
 
-AWK_CMD='$2 ~ /^[tTA]$/ { print "#define '$PREFIX'" $3 " 0x" $1; }'
+AWK_CMD='$2 ~ /^[tTA]$/ { print "#define '$SYM'" $3 " 0x" $1; }'
 
 cat << EOF
 /* Autogenerated by $0, do not edit */
-- 
2.7.4



More information about the CRIU mailing list