[CRIU] [PATCH 31/78] infect: Move parasite RPC code into infect-rpc.c

Cyrill Gorcunov gorcunov at openvz.org
Mon Nov 7 08:36:16 PST 2016


From: Pavel Emelyanov <xemul at virtuozzo.com>

Signed-off-by: Pavel Emelyanov <xemul at virtuozzo.com>
---
 criu/Makefile.crtools           |   1 +
 criu/aio.c                      |   3 +-
 criu/include/infect-priv.h      |   3 +
 criu/include/infect-rpc.h       |  20 +++++++
 criu/include/parasite-syscall.h |  10 ----
 criu/include/parasite.h         |  12 ----
 criu/infect-rpc.c               |  97 +++++++++++++++++++++++++++++++
 criu/infect.c                   |  10 ++--
 criu/mem.c                      |   9 +--
 criu/parasite-syscall.c         | 124 +++++++---------------------------------
 criu/pie/parasite.c             |   1 +
 criu/vdso.c                     |   3 +-
 12 files changed, 157 insertions(+), 136 deletions(-)
 create mode 100644 criu/include/infect-rpc.h
 create mode 100644 criu/infect-rpc.c

diff --git a/criu/Makefile.crtools b/criu/Makefile.crtools
index 444358d4ac82..8b6535c26679 100644
--- a/criu/Makefile.crtools
+++ b/criu/Makefile.crtools
@@ -4,6 +4,7 @@ ccflags-y		+= -iquote compel/include
 ccflags-y		+= -iquote compel/arch/$(ARCH)/plugins/std
 
 obj-y			+= infect.o
+obj-y			+= infect-rpc.o
 obj-y			+= action-scripts.o
 obj-y			+= external.o
 obj-y			+= aio.o
diff --git a/criu/aio.c b/criu/aio.c
index c031e39190d9..011a1afcf135 100644
--- a/criu/aio.c
+++ b/criu/aio.c
@@ -12,6 +12,7 @@
 #include "parasite-syscall.h"
 #include "images/mm.pb-c.h"
 #include "infect.h"
+#include "infect-rpc.h"
 
 #define NR_IOEVENTS_IN_NPAGES(npages) ((PAGE_SIZE * npages - sizeof(struct aio_ring)) / sizeof(struct io_event))
 
@@ -119,7 +120,7 @@ int parasite_collect_aios(struct parasite_ctl *ctl, struct vm_area_list *vmas)
 	}
 	aa->nr_rings = vmas->nr_aios;
 
-	if (parasite_execute_daemon(PARASITE_CMD_CHECK_AIOS, ctl))
+	if (compel_rpc_call_sync(PARASITE_CMD_CHECK_AIOS, ctl))
 		return -1;
 
 	return 0;
diff --git a/criu/include/infect-priv.h b/criu/include/infect-priv.h
index fe8e22d111b9..b33a635161d0 100644
--- a/criu/include/infect-priv.h
+++ b/criu/include/infect-priv.h
@@ -35,6 +35,9 @@ struct parasite_ctl {
 #define MEMFD_FNAME	"CRIUMFD"
 #define MEMFD_FNAME_SZ	sizeof(MEMFD_FNAME)
 
+struct ctl_msg;
+int parasite_wait_ack(int sockfd, unsigned int cmd, struct ctl_msg *m);
+
 /* XXX -- remove with cr-exec.c */
 extern int compel_map_exchange(struct parasite_ctl *ctl, unsigned long size);
 #endif
diff --git a/criu/include/infect-rpc.h b/criu/include/infect-rpc.h
new file mode 100644
index 000000000000..fd03a096721a
--- /dev/null
+++ b/criu/include/infect-rpc.h
@@ -0,0 +1,20 @@
+#ifndef __COMPEL_INFECT_RPC_H__
+#define __COMPEL_INFECT_RPC_H__
+extern int compel_rpc_sync(unsigned int cmd, struct parasite_ctl *ctl);
+extern int compel_rpc_call(unsigned int cmd, struct parasite_ctl *ctl);
+extern int compel_rpc_call_sync(unsigned int cmd, struct parasite_ctl *ctl);
+extern int compel_rpc_sock(struct parasite_ctl *ctl);
+
+struct ctl_msg {
+	u32	cmd;			/* command itself */
+	u32	ack;			/* ack on command */
+	s32	err;			/* error code on reply */
+};
+
+#define ctl_msg_cmd(_cmd)		\
+	(struct ctl_msg){.cmd = _cmd, }
+
+#define ctl_msg_ack(_cmd, _err)	\
+	(struct ctl_msg){.cmd = _cmd, .ack = _cmd, .err = _err, }
+
+#endif
diff --git a/criu/include/parasite-syscall.h b/criu/include/parasite-syscall.h
index bcdc8b369b32..0d7d86f574fa 100644
--- a/criu/include/parasite-syscall.h
+++ b/criu/include/parasite-syscall.h
@@ -58,16 +58,6 @@ extern int parasite_dump_posix_timers_seized(struct proc_posix_timers_stat *proc
 
 extern int parasite_send_fd(struct parasite_ctl *ctl, int fd);
 
-/*
- * Execute a command in parasite when it's in daemon mode.
- * The __-ed version is asyncronous (doesn't wait for ack).
- */
-extern int parasite_execute_daemon(unsigned int cmd, struct parasite_ctl *ctl);
-extern int __parasite_execute_daemon(unsigned int cmd, struct parasite_ctl *ctl);
-
-extern int __parasite_wait_daemon_ack(unsigned int cmd,
-					      struct parasite_ctl *ctl);
-
 extern int parasite_dump_misc_seized(struct parasite_ctl *ctl, struct parasite_dump_misc *misc);
 extern int parasite_dump_creds(struct parasite_ctl *ctl, struct _CredsEntry *ce);
 extern int parasite_dump_thread_leader_seized(struct parasite_ctl *ctl, int pid, struct _CoreEntry *core);
diff --git a/criu/include/parasite.h b/criu/include/parasite.h
index b103279c1679..f7daa095371f 100644
--- a/criu/include/parasite.h
+++ b/criu/include/parasite.h
@@ -57,18 +57,6 @@ enum {
 	PARASITE_CMD_MAX,
 };
 
-struct ctl_msg {
-	u32	cmd;			/* command itself */
-	u32	ack;			/* ack on command */
-	s32	err;			/* error code on reply */
-};
-
-#define ctl_msg_cmd(_cmd)		\
-	(struct ctl_msg){.cmd = _cmd, }
-
-#define ctl_msg_ack(_cmd, _err)	\
-	(struct ctl_msg){.cmd = _cmd, .ack = _cmd, .err = _err, }
-
 struct parasite_init_args {
 	s32				h_addr_len;
 	struct sockaddr_un		h_addr;
diff --git a/criu/infect-rpc.c b/criu/infect-rpc.c
new file mode 100644
index 000000000000..51b9132ca077
--- /dev/null
+++ b/criu/infect-rpc.c
@@ -0,0 +1,97 @@
+#include "xmalloc.h"
+#include "types.h"
+#include "parasite.h"
+#include "parasite-syscall.h"
+#include "infect.h"
+#include "infect-priv.h"
+#include "infect-rpc.h"
+
+static int __parasite_send_cmd(int sockfd, struct ctl_msg *m)
+{
+	int ret;
+
+	ret = send(sockfd, m, sizeof(*m), 0);
+	if (ret == -1) {
+		pr_perror("Failed to send command %d to daemon", m->cmd);
+		return -1;
+	} else if (ret != sizeof(*m)) {
+		pr_err("Message to daemon is trimmed (%d/%d)\n",
+		       (int)sizeof(*m), ret);
+		return -1;
+	}
+
+	pr_debug("Sent msg to daemon %d %d %d\n", m->cmd, m->ack, m->err);
+	return 0;
+}
+
+int parasite_wait_ack(int sockfd, unsigned int cmd, struct ctl_msg *m)
+{
+	int ret;
+
+	pr_debug("Wait for ack %d on daemon socket\n", cmd);
+
+	while (1) {
+		memzero(m, sizeof(*m));
+
+		ret = recv(sockfd, m, sizeof(*m), MSG_WAITALL);
+		if (ret == -1) {
+			pr_perror("Failed to read ack");
+			return -1;
+		} else if (ret != sizeof(*m)) {
+			pr_err("Message reply from daemon is trimmed (%d/%d)\n",
+			       (int)sizeof(*m), ret);
+			return -1;
+		}
+		pr_debug("Fetched ack: %d %d %d\n",
+			 m->cmd, m->ack, m->err);
+
+		if (m->cmd != cmd || m->ack != cmd) {
+			pr_err("Communication error, this is not "
+			       "the ack we expected\n");
+			return -1;
+		}
+		return 0;
+	}
+
+	return -1;
+}
+
+int compel_rpc_sync(unsigned int cmd, struct parasite_ctl *ctl)
+{
+	struct ctl_msg m;
+
+	if (parasite_wait_ack(ctl->tsock, cmd, &m))
+		return -1;
+
+	if (m.err != 0) {
+		pr_err("Command %d for daemon failed with %d\n",
+		       cmd, m.err);
+		return -1;
+	}
+
+	return 0;
+}
+
+int compel_rpc_call(unsigned int cmd, struct parasite_ctl *ctl)
+{
+	struct ctl_msg m;
+
+	m = ctl_msg_cmd(cmd);
+	return __parasite_send_cmd(ctl->tsock, &m);
+}
+
+int compel_rpc_call_sync(unsigned int cmd, struct parasite_ctl *ctl)
+{
+	int ret;
+
+	ret = compel_rpc_call(cmd, ctl);
+	if (!ret)
+		ret = compel_rpc_sync(cmd, ctl);
+
+	return ret;
+}
+
+int compel_rpc_sock(struct parasite_ctl *ctl)
+{
+	return ctl->tsock;
+}
diff --git a/criu/infect.c b/criu/infect.c
index a2782179c087..10c89a909b02 100644
--- a/criu/infect.c
+++ b/criu/infect.c
@@ -6,6 +6,7 @@
 #include <signal.h>
 #include <linux/seccomp.h>
 
+#include "infect.h"
 #include "ptrace.h"
 #include "signal.h"
 #include "asm/parasite-syscall.h"
@@ -16,13 +17,10 @@
 #include "pie-relocs.h"
 #include "parasite-blob.h"
 #include "sigframe.h"
-#include "log.h"
-#include "infect.h"
+#include "criu-log.h"
+#include "infect-rpc.h"
 #include "infect-priv.h"
 
-/* XXX will be removed soon */
-extern int parasite_wait_ack(int sockfd, unsigned int cmd, struct ctl_msg *m);
-
 #define PTRACE_EVENT_STOP	128
 
 #ifndef SECCOMP_MODE_DISABLED
@@ -896,7 +894,7 @@ static int parasite_fini_seized(struct parasite_ctl *ctl)
 		return -1;
 	}
 
-	ret = __parasite_execute_daemon(PARASITE_CMD_FINI, ctl);
+	ret = compel_rpc_call(PARASITE_CMD_FINI, ctl);
 	close_safe(&ctl->tsock);
 	if (ret)
 		return -1;
diff --git a/criu/mem.c b/criu/mem.c
index 05d24471e66f..a4d576ebc534 100644
--- a/criu/mem.c
+++ b/criu/mem.c
@@ -28,6 +28,7 @@
 #include "pagemap-cache.h"
 #include "fault-injection.h"
 #include "infect.h"
+#include "infect-rpc.h"
 
 #include "protobuf.h"
 #include "images/pagemap.pb-c.h"
@@ -250,14 +251,14 @@ static int drain_pages(struct page_pipe *pp, struct parasite_ctl *ctl,
 		pr_debug("PPB: %d pages %d segs %u pipe %d off\n",
 				args->nr_pages, args->nr_segs, ppb->pipe_size, args->off);
 
-		ret = __parasite_execute_daemon(PARASITE_CMD_DUMPPAGES, ctl);
+		ret = compel_rpc_call(PARASITE_CMD_DUMPPAGES, ctl);
 		if (ret < 0)
 			return -1;
 		ret = parasite_send_fd(ctl, ppb->p[1]);
 		if (ret)
 			return -1;
 
-		ret = __parasite_wait_daemon_ack(PARASITE_CMD_DUMPPAGES, ctl);
+		ret = compel_rpc_sync(PARASITE_CMD_DUMPPAGES, ctl);
 		if (ret < 0)
 			return -1;
 
@@ -444,7 +445,7 @@ int parasite_dump_pages_seized(struct pstree_item *item,
 	 */
 
 	pargs->add_prot = PROT_READ;
-	ret = parasite_execute_daemon(PARASITE_CMD_MPROTECT_VMAS, ctl);
+	ret = compel_rpc_call_sync(PARASITE_CMD_MPROTECT_VMAS, ctl);
 	if (ret) {
 		pr_err("Can't dump unprotect vmas with parasite\n");
 		return ret;
@@ -463,7 +464,7 @@ int parasite_dump_pages_seized(struct pstree_item *item,
 	}
 
 	pargs->add_prot = 0;
-	if (parasite_execute_daemon(PARASITE_CMD_MPROTECT_VMAS, ctl)) {
+	if (compel_rpc_call_sync(PARASITE_CMD_MPROTECT_VMAS, ctl)) {
 		pr_err("Can't rollback unprotected vmas with parasite\n");
 		ret = -1;
 	}
diff --git a/criu/parasite-syscall.c b/criu/parasite-syscall.c
index 9e43db20dca1..4f97e626c519 100644
--- a/criu/parasite-syscall.c
+++ b/criu/parasite-syscall.c
@@ -42,6 +42,7 @@
 #include "pie/pie-relocs.h"
 
 #include "infect.h"
+#include "infect-rpc.h"
 #include "infect-priv.h"
 
 unsigned long get_exec_start(struct vm_area_list *vmas)
@@ -93,95 +94,12 @@ bool seized_native(struct parasite_ctl *ctl)
 	return user_regs_native(&ctl->orig.regs);
 }
 
-static int __parasite_send_cmd(int sockfd, struct ctl_msg *m)
-{
-	int ret;
-
-	ret = send(sockfd, m, sizeof(*m), 0);
-	if (ret == -1) {
-		pr_perror("Failed to send command %d to daemon", m->cmd);
-		return -1;
-	} else if (ret != sizeof(*m)) {
-		pr_err("Message to daemon is trimmed (%d/%d)\n",
-		       (int)sizeof(*m), ret);
-		return -1;
-	}
-
-	pr_debug("Sent msg to daemon %d %d %d\n", m->cmd, m->ack, m->err);
-	return 0;
-}
-
-int parasite_wait_ack(int sockfd, unsigned int cmd, struct ctl_msg *m)
-{
-	int ret;
-
-	pr_debug("Wait for ack %d on daemon socket\n", cmd);
-
-	while (1) {
-		memzero(m, sizeof(*m));
-
-		ret = recv(sockfd, m, sizeof(*m), MSG_WAITALL);
-		if (ret == -1) {
-			pr_perror("Failed to read ack");
-			return -1;
-		} else if (ret != sizeof(*m)) {
-			pr_err("Message reply from daemon is trimmed (%d/%d)\n",
-			       (int)sizeof(*m), ret);
-			return -1;
-		}
-		pr_debug("Fetched ack: %d %d %d\n",
-			 m->cmd, m->ack, m->err);
-
-		if (m->cmd != cmd || m->ack != cmd) {
-			pr_err("Communication error, this is not "
-			       "the ack we expected\n");
-			return -1;
-		}
-		return 0;
-	}
-
-	return -1;
-}
-
-int __parasite_wait_daemon_ack(unsigned int cmd,
-					struct parasite_ctl *ctl)
-{
-	struct ctl_msg m;
-
-	if (parasite_wait_ack(ctl->tsock, cmd, &m))
-		return -1;
-
-	if (m.err != 0) {
-		pr_err("Command %d for daemon failed with %d\n",
-		       cmd, m.err);
-		return -1;
-	}
-
-	return 0;
-}
-
-int __parasite_execute_daemon(unsigned int cmd, struct parasite_ctl *ctl)
-{
-	struct ctl_msg m;
-
-	m = ctl_msg_cmd(cmd);
-	return __parasite_send_cmd(ctl->tsock, &m);
-}
-
-int parasite_execute_daemon(unsigned int cmd, struct parasite_ctl *ctl)
-{
-	int ret;
-
-	ret = __parasite_execute_daemon(cmd, ctl);
-	if (!ret)
-		ret = __parasite_wait_daemon_ack(cmd, ctl);
-
-	return ret;
-}
-
 int parasite_send_fd(struct parasite_ctl *ctl, int fd)
 {
-	if (send_fd(ctl->tsock, NULL, 0, fd) < 0) {
+	int sk;
+
+	sk = compel_rpc_sock(ctl);
+	if (send_fd(sk, NULL, 0, fd) < 0) {
 		pr_perror("Can't send file descriptor");
 		return -1;
 	}
@@ -272,7 +190,7 @@ int parasite_dump_thread_leader_seized(struct parasite_ctl *ctl, int pid, CoreEn
 
 	pc->cap_last_cap = kdat.last_cap;
 
-	ret = parasite_execute_daemon(PARASITE_CMD_DUMP_THREAD, ctl);
+	ret = compel_rpc_call_sync(PARASITE_CMD_DUMP_THREAD, ctl);
 	if (ret < 0)
 		return ret;
 
@@ -370,7 +288,7 @@ int parasite_dump_sigacts_seized(struct parasite_ctl *ctl, struct cr_imgset *cr_
 	else
 		args_c = compel_parasite_args(ctl, struct parasite_dump_sa_args_compat);
 
-	ret = parasite_execute_daemon(PARASITE_CMD_DUMP_SIGACTS, ctl);
+	ret = compel_rpc_call_sync(PARASITE_CMD_DUMP_SIGACTS, ctl);
 	if (ret < 0)
 		return ret;
 
@@ -422,7 +340,7 @@ int parasite_dump_itimers_seized(struct parasite_ctl *ctl, struct pstree_item *i
 	else
 		args_c = compel_parasite_args(ctl, struct parasite_dump_itimers_args_compat);
 
-	ret = parasite_execute_daemon(PARASITE_CMD_DUMP_ITIMERS, ctl);
+	ret = compel_rpc_call_sync(PARASITE_CMD_DUMP_ITIMERS, ctl);
 	if (ret < 0)
 		return ret;
 
@@ -519,7 +437,7 @@ int parasite_dump_posix_timers_seized(struct proc_posix_timers_stat *proc_args,
 		i++;
 	}
 
-	ret = parasite_execute_daemon(PARASITE_CMD_DUMP_POSIX_TIMERS, ctl);
+	ret = compel_rpc_call_sync(PARASITE_CMD_DUMP_POSIX_TIMERS, ctl);
 	if (ret < 0)
 		goto end_posix;
 
@@ -541,7 +459,7 @@ int parasite_dump_misc_seized(struct parasite_ctl *ctl, struct parasite_dump_mis
 	struct parasite_dump_misc *ma;
 
 	ma = compel_parasite_args(ctl, struct parasite_dump_misc);
-	if (parasite_execute_daemon(PARASITE_CMD_DUMP_MISC, ctl) < 0)
+	if (compel_rpc_call_sync(PARASITE_CMD_DUMP_MISC, ctl) < 0)
 		return -1;
 
 	*misc = *ma;
@@ -556,7 +474,7 @@ struct parasite_tty_args *parasite_dump_tty(struct parasite_ctl *ctl, int fd, in
 	p->fd = fd;
 	p->type = type;
 
-	if (parasite_execute_daemon(PARASITE_CMD_DUMP_TTY, ctl) < 0)
+	if (compel_rpc_call_sync(PARASITE_CMD_DUMP_TTY, ctl) < 0)
 		return NULL;
 
 	return p;
@@ -566,7 +484,7 @@ int parasite_drain_fds_seized(struct parasite_ctl *ctl,
 		struct parasite_drain_fd *dfds, int nr_fds, int off,
 		int *lfds, struct fd_opts *opts)
 {
-	int ret = -1, size;
+	int ret = -1, size, sk;
 	struct parasite_drain_fd *args;
 
 	size = drain_fds_size(dfds);
@@ -574,35 +492,37 @@ int parasite_drain_fds_seized(struct parasite_ctl *ctl,
 	args->nr_fds = nr_fds;
 	memcpy(&args->fds, dfds->fds + off, sizeof(int) * nr_fds);
 
-	ret = __parasite_execute_daemon(PARASITE_CMD_DRAIN_FDS, ctl);
+	ret = compel_rpc_call(PARASITE_CMD_DRAIN_FDS, ctl);
 	if (ret) {
 		pr_err("Parasite failed to drain descriptors\n");
 		goto err;
 	}
 
-	ret = recv_fds(ctl->tsock, lfds, nr_fds, opts);
+	sk = compel_rpc_sock(ctl);
+	ret = recv_fds(sk, lfds, nr_fds, opts);
 	if (ret)
 		pr_err("Can't retrieve FDs from socket\n");
 
-	ret |= __parasite_wait_daemon_ack(PARASITE_CMD_DRAIN_FDS, ctl);
+	ret |= compel_rpc_sync(PARASITE_CMD_DRAIN_FDS, ctl);
 err:
 	return ret;
 }
 
 int parasite_get_proc_fd_seized(struct parasite_ctl *ctl)
 {
-	int ret = -1, fd;
+	int ret = -1, fd, sk;
 
-	ret = __parasite_execute_daemon(PARASITE_CMD_GET_PROC_FD, ctl);
+	ret = compel_rpc_call(PARASITE_CMD_GET_PROC_FD, ctl);
 	if (ret) {
 		pr_err("Parasite failed to get proc fd\n");
 		return ret;
 	}
 
-	fd = recv_fd(ctl->tsock);
+	sk = compel_rpc_sock(ctl);
+	fd = recv_fd(sk);
 	if (fd < 0)
 		pr_err("Can't retrieve FD from socket\n");
-	if (__parasite_wait_daemon_ack(PARASITE_CMD_GET_PROC_FD, ctl)) {
+	if (compel_rpc_sync(PARASITE_CMD_GET_PROC_FD, ctl)) {
 		close_safe(&fd);
 		return -1;
 	}
@@ -618,7 +538,7 @@ int parasite_dump_cgroup(struct parasite_ctl *ctl, struct parasite_dump_cgroup_a
 	struct parasite_dump_cgroup_args *ca;
 
 	ca = compel_parasite_args(ctl, struct parasite_dump_cgroup_args);
-	ret = parasite_execute_daemon(PARASITE_CMD_DUMP_CGROUP, ctl);
+	ret = compel_rpc_call_sync(PARASITE_CMD_DUMP_CGROUP, ctl);
 	if (ret) {
 		pr_err("Parasite failed to dump /proc/self/cgroup\n");
 		return ret;
diff --git a/criu/pie/parasite.c b/criu/pie/parasite.c
index 665f62b67501..433493e3b3b4 100644
--- a/criu/pie/parasite.c
+++ b/criu/pie/parasite.c
@@ -18,6 +18,7 @@
 #include "criu-log.h"
 #include "tty.h"
 #include "aio.h"
+#include "infect-rpc.h"
 
 #include "asm/parasite.h"
 #include "restorer.h"
diff --git a/criu/vdso.c b/criu/vdso.c
index bd39cf0fe4ed..0c92e83d6088 100644
--- a/criu/vdso.c
+++ b/criu/vdso.c
@@ -20,6 +20,7 @@
 #include "mem.h"
 #include "vma.h"
 #include "infect.h"
+#include "infect-rpc.h"
 
 #ifdef LOG_PREFIX
 # undef LOG_PREFIX
@@ -111,7 +112,7 @@ int parasite_fixup_vdso(struct parasite_ctl *ctl, pid_t pid,
 			args->try_fill_symtable = (fd < 0) ? true : false;
 		args->is_vdso = false;
 
-		if (parasite_execute_daemon(PARASITE_CMD_CHECK_VDSO_MARK, ctl)) {
+		if (compel_rpc_call_sync(PARASITE_CMD_CHECK_VDSO_MARK, ctl)) {
 			pr_err("Parasite failed to poke for mark\n");
 			goto err;
 		}
-- 
2.7.4



More information about the CRIU mailing list