[CRIU] [PATCH 1/4] crtools: preload libraries

Andrey Vagin avagin at openvz.org
Sat Nov 30 09:43:53 PST 2013


Libraries is going to be used for dumping and restoring
external dependencies (e.g. dbus, systemd journal sockets)

A libary can have the cr_init() function for initialization.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 Makefile             |  4 ++--
 crtools.c            | 37 +++++++++++++++++++++++++++++++++++++
 include/cr-lib.h     |  6 ++++++
 include/cr_options.h |  6 ++++++
 4 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 include/cr-lib.h

diff --git a/Makefile b/Makefile
index 25b9fe8..9c53fc5 100644
--- a/Makefile
+++ b/Makefile
@@ -82,7 +82,7 @@ cflags-y		+= -iquote $(ARCH_DIR) -iquote $(ARCH_DIR)/include
 cflags-y		+= -fno-strict-aliasing
 export cflags-y
 
-LIBS		:= -lrt -lpthread -lprotobuf-c
+LIBS		:= -lrt -lpthread -lprotobuf-c -ldl
 
 DEFINES		+= -D_FILE_OFFSET_BITS=64
 DEFINES		+= -D_GNU_SOURCE
@@ -167,7 +167,7 @@ PROGRAM-BUILTINS	+= $(ARCH_DIR)/vdso-pie.o
 
 $(PROGRAM): $(SYSCALL-LIB) $(ARCH-LIB) $(PROGRAM-BUILTINS)
 	$(E) "  LINK    " $@
-	$(Q) $(CC) $(CFLAGS) $^ $(LIBS) $(LDFLAGS) -o $@
+	$(Q) $(CC) $(CFLAGS) $^ $(LIBS) $(LDFLAGS) -rdynamic -o $@
 
 zdtm: all
 	$(Q) $(MAKE) -C test/zdtm all
diff --git a/crtools.c b/crtools.c
index ab885e6..8c43167 100644
--- a/crtools.c
+++ b/crtools.c
@@ -16,6 +16,8 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
+#include <dlfcn.h>
+
 #include "asm/types.h"
 
 #include "compiler.h"
@@ -31,6 +33,7 @@
 #include "tty.h"
 #include "file-lock.h"
 #include "cr-service.h"
+#include "cr-lib.h"
 
 struct cr_options opts;
 
@@ -42,6 +45,7 @@ void init_opts(void)
 	opts.final_state = TASK_DEAD;
 	INIT_LIST_HEAD(&opts.veth_pairs);
 	INIT_LIST_HEAD(&opts.scripts);
+	INIT_LIST_HEAD(&opts.libs);
 }
 
 static int parse_ns_string(const char *ptr)
@@ -72,6 +76,35 @@ bad_ns:
 	return -1;
 }
 
+static int cr_lib_add(char *path)
+{
+	struct cr_lib *l;
+	cr_init_t *f_init;
+	void *h;
+
+	h = dlopen(path, RTLD_LAZY);
+	if (h == NULL) {
+		pr_err("Unable to load %s: %s", path, dlerror());
+		return -1;
+	}
+
+	f_init = dlsym(h, "cr_init");
+	if (f_init && f_init())
+		return -1;
+
+	l = xmalloc(sizeof(struct cr_lib));
+	if (l == NULL)
+		goto err;
+
+	l->handle = h;
+	list_add(&l->node, &opts.libs);
+
+	return 0;
+err:
+	dlclose(h);
+	return -1;
+}
+
 int main(int argc, char *argv[])
 {
 	pid_t pid = 0, tree_id = 0;
@@ -129,6 +162,7 @@ int main(int argc, char *argv[])
 			{ "prev-images-dir", required_argument, 0, 53},
 			{ "ms", no_argument, 0, 54},
 			{ "track-mem", no_argument, 0, 55},
+			{ "lib", required_argument, 0, 56},
 			{ },
 		};
 
@@ -271,6 +305,9 @@ int main(int argc, char *argv[])
 		case 54:
 			opts.check_ms_kernel = true;
 			break;
+		case 56:
+			cr_lib_add(optarg);
+			break;
 		case 'V':
 			pr_msg("Version: %s\n", CRIU_VERSION);
 			if (strcmp(CRIU_GITID, "0"))
diff --git a/include/cr-lib.h b/include/cr-lib.h
new file mode 100644
index 0000000..75042aa
--- /dev/null
+++ b/include/cr-lib.h
@@ -0,0 +1,6 @@
+#ifndef __CR_LIB_H__
+#define __CR_LIB_H__
+
+typedef int (cr_init_t)(void);
+
+#endif
diff --git a/include/cr_options.h b/include/cr_options.h
index b90b4e1..6ce300b 100644
--- a/include/cr_options.h
+++ b/include/cr_options.h
@@ -10,6 +10,11 @@ struct script {
 	char *path;
 };
 
+struct cr_lib {
+	struct list_head node;
+	void *handle;
+};
+
 struct cr_options {
 	int			final_state;
 	char			*show_dump_file;
@@ -29,6 +34,7 @@ struct cr_options {
 	char			*pidfile;
 	struct list_head	veth_pairs;
 	struct list_head	scripts;
+	struct list_head	libs;
 	bool			use_page_server;
 	unsigned short		ps_port;
 	char			*addr;
-- 
1.8.3.1



More information about the CRIU mailing list