[CRIU] [PATCH 3/5] unix: add ability to set callbacks for external sockets (v2)

Andrey Vagin avagin at openvz.org
Thu Dec 5 09:58:57 PST 2013


We don't know a state behind an external socket. It depends on logic
of the program, which handles this socket.

This patch adds ability to load a library with callbacks for dumping
and restoring external sockets.

This patch introduces two callbacks cr_plugin_dump_unix_sk and
cr_plugin_restore_unix_sk. If a callback can not handle a socket, it
must return CRIU_CB_SKIP.

The main questions, what kind of information should be tranfered in
these callbacks. Pls, think a few minutes about that and send me
your opinion.

v2: Use uflags instread of adding a new field

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 include/criu-plugin.h | 12 ++++++++++
 include/image.h       |  1 +
 include/plugin.h      |  9 ++++++++
 lib.c                 | 56 +++++++++++++++++++++++++++++++++++++++++++++
 sk-unix.c             | 63 +++++++++++++++++++++++++++++++++++++++++++--------
 5 files changed, 131 insertions(+), 10 deletions(-)

diff --git a/include/criu-plugin.h b/include/criu-plugin.h
index 44abb2a..963d1d0 100644
--- a/include/criu-plugin.h
+++ b/include/criu-plugin.h
@@ -20,7 +20,19 @@
 #ifndef __CRIU_PLUGIN_H__
 #define __CRIU_PLUGIN_H__
 
+#include <limits.h>
+
 typedef int (cr_plugin_init_t)(void);
 typedef void (cr_plugin_fini_t)(void);
 
+#define CRIU_CB_SKIP INT_MIN
+/*
+ * The callbacks for unix sockets resturn:
+ * - CR_CB_SKIP if this callback is not suitable for this socket
+ * - negative values, if something failed
+ * - zero, if the socket was dumped or restored
+ */
+typedef int (cr_plugin_dump_unix_sk_t)(int fd, int ino, int peer_ino);
+typedef int (cr_plugin_restore_unix_sk_t)(int ino, char *name, int len);
+
 #endif
diff --git a/include/image.h b/include/image.h
index 099f510..32ee0af 100644
--- a/include/image.h
+++ b/include/image.h
@@ -33,6 +33,7 @@
 
 #define USK_EXTERN	(1 << 0)
 #define USK_SERVICE	(1 << 1)
+#define USK_CALLBACK	(1 << 2)
 
 #define VMA_AREA_NONE		(0 <<  0)
 #define VMA_AREA_REGULAR	(1 <<  0)	/* Dumpable area */
diff --git a/include/plugin.h b/include/plugin.h
index 8653a5b..8538a1b 100644
--- a/include/plugin.h
+++ b/include/plugin.h
@@ -2,10 +2,13 @@
 #define __CR_PLUGIN_H__
 
 #include "criu-plugin.h"
+#include <limits.h>
 
 struct cr_plugin_entry {
 	union {
 		cr_plugin_fini_t *cr_fini;
+		cr_plugin_dump_unix_sk_t *cr_plugin_dump_unix_sk;
+		cr_plugin_restore_unix_sk_t *cr_plugin_restore_unix_sk;
 	};
 
 	struct cr_plugin_entry *next;
@@ -13,10 +16,16 @@ struct cr_plugin_entry {
 
 struct cr_plugins {
 	struct cr_plugin_entry *cr_fini;
+	struct cr_plugin_entry *cr_plugin_dump_unix_sk;
+	struct cr_plugin_entry *cr_plugin_restore_unix_sk;
 };
 
 extern struct cr_plugins cr_plugins;
 
 void cr_lib_fini(void);
 int cr_lib_init(void);
+
+int cr_plugin_dump_unix_sk(int fd, int ino, int peer_ino);
+int cr_plugin_restore_unix_sk(int ino, char *name, int len);
+
 #endif
diff --git a/lib.c b/lib.c
index 387bb84..28ab101 100644
--- a/lib.c
+++ b/lib.c
@@ -12,6 +12,48 @@
 
 struct cr_plugins cr_plugins;
 
+#define add_plugin_func(name)						\
+	do {								\
+		name ## _t *name;					\
+		name = dlsym(h, #name);					\
+		if (name) {						\
+			struct cr_plugin_entry *__ce;			\
+			__ce = xmalloc(sizeof(struct cr_plugin_entry));	\
+			if (__ce == NULL)				\
+				return -1;				\
+			__ce->name = name;				\
+			__ce->next = cr_plugins.name;			\
+			cr_plugins.name = __ce;				\
+		}							\
+	} while (0);							\
+
+
+#define run_plugin_funcs(name, ...) ({					\
+		struct cr_plugin_entry *__ce = cr_plugins.name;		\
+		int __ret = CRIU_CB_SKIP;				\
+									\
+		while (__ce) {						\
+			__ret = __ce->name(__VA_ARGS__);		\
+			if (__ret == CRIU_CB_SKIP) {			\
+				__ce = __ce->next;			\
+				continue;				\
+			}						\
+			break;						\
+		}							\
+									\
+		__ret;							\
+	})								\
+
+int cr_plugin_dump_unix_sk(int fd, int ino, int peer_ino)
+{
+	return run_plugin_funcs(cr_plugin_dump_unix_sk, fd, ino, peer_ino);
+}
+
+int cr_plugin_restore_unix_sk(int ino, char *name, int len)
+{
+	return run_plugin_funcs(cr_plugin_restore_unix_sk, ino, name, len);
+}
+
 static int cr_lib_load(char *path)
 {
 	struct cr_plugin_entry *ce;
@@ -25,6 +67,9 @@ static int cr_lib_load(char *path)
 		return -1;
 	}
 
+	add_plugin_func(cr_plugin_dump_unix_sk);
+	add_plugin_func(cr_plugin_restore_unix_sk);
+
 	ce = NULL;
 	f_fini = dlsym(h, "cr_plugin_fini");
 	if (f_fini) {
@@ -48,10 +93,21 @@ static int cr_lib_load(char *path)
 	return 0;
 }
 
+#define cr_plugin_free(name) do {				\
+	while (cr_plugins.name) {				\
+		ce = cr_plugins.name;				\
+		cr_plugins.name = cr_plugins.name->next;	\
+		xfree(ce);					\
+	}							\
+} while (0)							\
+
 void cr_lib_fini(void)
 {
 	struct cr_plugin_entry *ce;
 
+	cr_plugin_free(cr_plugin_dump_unix_sk);
+	cr_plugin_free(cr_plugin_restore_unix_sk);
+
 	while (cr_plugins.cr_fini) {
 		ce = cr_plugins.cr_fini;
 		cr_plugins.cr_fini = cr_plugins.cr_fini->next;
diff --git a/sk-unix.c b/sk-unix.c
index 8980efd..9dd8280 100644
--- a/sk-unix.c
+++ b/sk-unix.c
@@ -7,6 +7,7 @@
 #include <fcntl.h>
 #include <sys/un.h>
 #include <stdlib.h>
+#include <dlfcn.h>
 
 #include "asm/types.h"
 #include "libnetlink.h"
@@ -23,6 +24,7 @@
 #include "sk-queue.h"
 #include "mount.h"
 #include "cr-service.h"
+#include "plugin.h"
 
 #include "protobuf.h"
 #include "protobuf/sk-unix.pb-c.h"
@@ -469,6 +471,30 @@ int unix_receive_one(struct nlmsghdr *h, void *arg)
 	return unix_collect_one(m, tb);
 }
 
+static int try_to_dump_with_callback(struct unix_sk_desc *peer, bool *need_callback)
+{
+	struct unix_sk_desc *sk;
+	int ret = -1;
+
+	*need_callback = false;
+	list_for_each_entry(sk, &peer->peer_list, peer_node) {
+		ret = cr_plugin_dump_unix_sk(sk->fd, sk->sd.ino, sk->peer_ino);
+		if (ret == CRIU_CB_SKIP) {
+			if (!*need_callback)
+				return 0;
+
+			pr_err("The callback is not suitable for all peer sockets\n");
+			return -1;
+		}
+		if (ret < 0)
+			return -1;
+
+		*need_callback = true;
+	}
+
+	return ret;
+}
+
 int fix_external_unix_sockets(void)
 {
 	struct unix_sk_desc *sk;
@@ -479,22 +505,28 @@ int fix_external_unix_sockets(void)
 		UnixSkEntry e = UNIX_SK_ENTRY__INIT;
 		FownEntry fown = FOWN_ENTRY__INIT;
 		SkOptsEntry skopts = SK_OPTS_ENTRY__INIT;
+		bool need_callback;
 
 		show_one_unix("Dumping extern", sk);
 
 		BUG_ON(sk->sd.already_dumped);
 
-		if (!opts.ext_unix_sk) {
-			show_one_unix("Runaway socket", sk);
-			pr_err("External socket is used. "
-					"Consider using --" USK_EXT_PARAM " option.\n");
+		if (try_to_dump_with_callback(sk, &need_callback))
 			goto err;
-		}
 
-		if (sk->type != SOCK_DGRAM) {
-			show_one_unix("Ext stream not supported", sk);
-			pr_err("Can't dump half of stream unix connection.\n");
-			goto err;
+		if (!need_callback) {
+			if (!opts.ext_unix_sk) {
+				show_one_unix("Runaway socket", sk);
+				pr_err("External socket is used. "
+						"Consider using --" USK_EXT_PARAM " option.\n");
+				goto err;
+			}
+
+			if (sk->type != SOCK_DGRAM) {
+				show_one_unix("Ext stream not supported", sk);
+				pr_err("Can't dump half of stream unix connection.\n");
+				goto err;
+			}
 		}
 
 		e.id		= fd_id_generate_special();
@@ -507,6 +539,8 @@ int fix_external_unix_sockets(void)
 		e.peer		= 0;
 		e.fown		= &fown;
 		e.opts		= &skopts;
+		if (need_callback)
+			e.uflags |= USK_CALLBACK;
 
 		if (pb_write_one(fdset_fd(glob_fdset, CR_FD_UNIXSK), &e, PB_UNIX_SK))
 			goto err;
@@ -576,6 +610,8 @@ static int post_open_unix_sk(struct file_desc *d, int fd)
 
 	if (peer == NULL)
 		return 0;
+	if (peer->ue->uflags & USK_CALLBACK)
+		return 0;
 
 	pr_info("\tConnect %#x to %#x\n", ui->ue->ino, peer->ue->ino);
 
@@ -781,6 +817,12 @@ static int open_unixsk_standalone(struct unix_sk_info *ui)
 
 		close(sks[1]);
 		sk = sks[0];
+	} else if (ui->peer && ui->peer->ue->uflags & USK_CALLBACK) {
+		sk = cr_plugin_restore_unix_sk(ui->ue->ino, ui->name, ui->ue->name.len);
+		if (sk < 0)
+			return -1;
+
+		goto out;
 	} else if ((ui->ue->state == TCP_ESTABLISHED) && !ui->ue->peer) {
 		int ret, sks[2];
 
@@ -831,7 +873,7 @@ static int open_unixsk_standalone(struct unix_sk_info *ui)
 			return -1;
 		}
 	}
-
+out:
 	if (rst_file_params(sk, ui->ue->fown, ui->ue->flags))
 		return -1;
 
@@ -932,6 +974,7 @@ int resolve_unix_peers(void)
 		 * special option to be passed.
 		 */
 		if ((peer->ue->uflags & USK_EXTERN) &&
+				!(peer->ue->uflags & USK_CALLBACK) &&
 				!(opts.ext_unix_sk)) {
 			pr_err("External socket found in image. "
 					"Consider using the --" USK_EXT_PARAM " option "
-- 
1.8.3.1



More information about the CRIU mailing list