[CRIU] [crtools-bot] parasite: create unix socket in parasite for transfering fd (v3)

Cyrill Gorcunov gorcunov at openvz.org
Wed Feb 1 10:26:50 EST 2012


The commit is pushed to "master" and will appear on git://github.com/cyrillos/crtools.git
------>
commit 13faf4ee7f346e3ac271899a70f9e44978e506e3
Author: Andrey Vagin <avagin at openvz.org>
Date:   Wed Feb 1 16:23:50 2012 +0300

    parasite: create unix socket in parasite for transfering fd (v3)
    
    Before we used absolute pathes to images and wrote logging messages in
    stderr of a target process.
    
    Now we are able to create fd and send it to parasite.
    
    Signed-off-by: Andrey Vagin <avagin at openvz.org>
    Acked-by: Pavel Emelyanov <xemul at parallels.com>
    Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/parasite.h |   11 ++++++
 parasite-syscall.c |   97 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 parasite.c         |   45 +++++++++++++++++++++++-
 3 files changed, 152 insertions(+), 1 deletions(-)

diff --git a/include/parasite.h b/include/parasite.h
index c6eb6bc..a79d54c 100644
--- a/include/parasite.h
+++ b/include/parasite.h
@@ -2,6 +2,7 @@
 #define CR_PARASITE_H_
 
 #include <sys/types.h>
+#include <sys/un.h>
 #include <limits.h>
 
 #include "compiler.h"
@@ -30,6 +31,10 @@
 
 enum {
 	PARASITE_CMD_PINGME,
+	PARASITE_CMD_INIT,
+	PARASITE_CMD_SET_LOGFD,
+	PARASITE_CMD_FINI,
+
 	PARASITE_CMD_DUMPPAGES,
 	PARASITE_CMD_DUMP_SIGACTS,
 	PARASITE_CMD_DUMP_ITIMERS,
@@ -57,6 +62,12 @@ typedef struct  {
 		(st)->line	= __LINE__;		\
 	} while (0)
 
+struct parasite_init_args {
+	parasite_status_t args;
+	int sun_len;
+	struct sockaddr_un saddr;
+};
+
 struct parasite_dump_file_args {
 	parasite_status_t	status;
 	unsigned long		open_mode;
diff --git a/parasite-syscall.c b/parasite-syscall.c
index 7fcabc0..0df4ea2 100644
--- a/parasite-syscall.c
+++ b/parasite-syscall.c
@@ -13,6 +13,7 @@
 #include <sys/mman.h>
 #include <sys/user.h>
 #include <sys/wait.h>
+#include <sys/socket.h>
 
 #include "crtools.h"
 #include "compiler.h"
@@ -20,6 +21,8 @@
 #include "types.h"
 #include "ptrace.h"
 #include "util.h"
+#include "util-net.h"
+#include "log.h"
 
 #include "parasite-syscall.h"
 #include "parasite-blob.h"
@@ -342,6 +345,44 @@ err:
 	return ret;
 }
 
+static int get_socket_name(struct sockaddr_un *saddr, pid_t pid)
+{
+	int sun_len;
+
+	saddr->sun_family = AF_UNIX;
+	snprintf(saddr->sun_path, UNIX_PATH_MAX,
+			"X/crtools-pr-%d", pid);
+
+	sun_len = SUN_LEN(saddr);
+	*saddr->sun_path = '\0';
+
+	return sun_len;
+}
+
+static int parasite_send_fd(struct parasite_ctl *ctl, int fd)
+{
+	struct sockaddr_un saddr;
+	int sun_len, ret = -1;
+	int sock;
+
+	sun_len = get_socket_name(&saddr, ctl->pid);
+
+	sock = socket(PF_UNIX, SOCK_DGRAM, 0);
+	if (sock < 0) {
+		pr_perror("Can't create socket");
+		return -1;
+	}
+
+	if (send_fd(sock, &saddr, sun_len, fd) < 0) {
+		pr_perror("Can't send file descriptor");
+		goto out;
+	}
+	ret = 0;
+out:
+	close(sock);
+	return ret;
+}
+
 static int parasite_prep_file(int type, struct parasite_dump_file_args *fa,
 		struct parasite_ctl *ctl, struct cr_fdset *fdset)
 {
@@ -384,6 +425,35 @@ out:
 	return ret;
 }
 
+static int parasite_init(struct parasite_ctl *ctl, pid_t pid)
+{
+	struct parasite_init_args args = { };
+	int ret;
+
+	args.sun_len = get_socket_name(&args.saddr, pid);
+
+	ret = parasite_execute(PARASITE_CMD_INIT, ctl,
+			(parasite_status_t *)&args, sizeof(args));
+	return ret;
+}
+
+static int parasite_set_logfd(struct parasite_ctl *ctl, pid_t pid)
+{
+	parasite_status_t args = { };
+	int ret;
+
+	ret = parasite_send_fd(ctl, get_logfd());
+	if (ret)
+		return ret;
+
+	ret = parasite_execute(PARASITE_CMD_SET_LOGFD, ctl,
+			&args, sizeof(args));
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
 int parasite_dump_sigacts_seized(struct parasite_ctl *ctl, struct cr_fdset *cr_fdset)
 {
 	return parasite_file_cmd(PARASITE_CMD_DUMP_SIGACTS, CR_FD_SIGACT, ctl, cr_fdset);
@@ -503,6 +573,14 @@ int parasite_cure_seized(struct parasite_ctl *ctl, struct list_head *vma_area_li
 	user_regs_struct_t regs, regs_orig;
 	struct vma_area *vma_area;
 	int ret = -1;
+	parasite_status_t args = { };
+
+	ret = parasite_execute(PARASITE_CMD_FINI, ctl,
+			&args, sizeof(args));
+	if (ret) {
+		pr_err("Can't finalize parasite (pid: %d) task\n", ctl->pid);
+		goto err;
+	}
 
 	jerr(ptrace(PTRACE_GETREGS, ctl->pid, NULL, &regs), err);
 
@@ -534,10 +612,12 @@ err:
 
 struct parasite_ctl *parasite_infect_seized(pid_t pid, struct list_head *vma_area_list)
 {
+	parasite_status_t args = { };
 	user_regs_struct_t regs, regs_orig;
 	struct parasite_ctl *ctl = NULL;
 	struct vma_area *vma_area;
 	void *mmaped;
+	int ret;
 
 	ctl = xzalloc(sizeof(*ctl));
 	if (!ctl) {
@@ -592,8 +672,25 @@ struct parasite_ctl *parasite_infect_seized(pid_t pid, struct list_head *vma_are
 
 	jerr(ptrace(PTRACE_SETREGS, pid, NULL, &regs_orig), err_munmap_restore);
 
+	ret = parasite_init(ctl, pid);
+	if (ret) {
+		pr_err("%d: Can't create a transport socket\n", pid);
+		goto err_munmap_restore;
+	}
+
+	ret = parasite_set_logfd(ctl, pid);
+	if (ret) {
+		pr_err("%d: Can't set a logging descriptor\n", pid);
+		goto err_munmap_restore;
+	}
+
 	return ctl;
 
+err_fini:
+	ret = parasite_execute(PARASITE_CMD_FINI, ctl,
+				&args, sizeof(args));
+	if (ret)
+		pr_panic("Can't finalize parasite (pid: %d) task\n", ctl->pid);
 err_munmap_restore:
 	regs = regs_orig, regs.ip = vma_area->vma.start;
 	if (munmap_seized(pid, &regs, mmaped, parasite_size))
diff --git a/parasite.c b/parasite.c
index 5ef08d8..e353177 100644
--- a/parasite.c
+++ b/parasite.c
@@ -5,6 +5,8 @@
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 #include <fcntl.h>
 #include <unistd.h>
 
@@ -14,6 +16,7 @@
 #include "parasite.h"
 #include "image.h"
 #include "util.h"
+#include "util-net.h"
 #include "crtools.h"
 
 #ifdef CONFIG_X86_64
@@ -22,6 +25,8 @@ static void *brk_start, *brk_end, *brk_tail;
 
 static struct page_entry page;
 static struct vma_entry vma;
+static int logfd = -1;
+static int tsock = -1;
 
 static void brk_init(void *brk)
 {
@@ -74,7 +79,7 @@ static void sys_write_msg(const char *msg)
 	int size = 0;
 	while (msg[size])
 		size++;
-	sys_write(1, msg, size);
+	sys_write(logfd, msg, size);
 }
 
 static inline int should_dump_page(struct vma_entry *vmae, unsigned char mincore_flags)
@@ -358,6 +363,36 @@ static int dump_misc(struct parasite_dump_misc *args)
 	return 0;
 }
 
+static int init(struct parasite_init_args *args)
+{
+	int ret;
+
+	tsock = sys_socket(PF_UNIX, SOCK_DGRAM, 0);
+	if (tsock < 0) {
+		return -1;
+	}
+
+	ret = sys_bind(tsock, (struct sockaddr *) &args->saddr, args->sun_len);
+	if (ret < 0) {
+		return -1;
+	}
+
+	return 0;
+}
+
+static int set_logfd()
+{
+	logfd = recv_fd(tsock);
+	return logfd;
+}
+
+static int fini()
+{
+	sys_close(logfd);
+	sys_close(tsock);
+	return 0;
+}
+
 static int __used parasite_service(unsigned long cmd, void *args, void *brk)
 {
 	brk_init(brk);
@@ -368,6 +403,12 @@ static int __used parasite_service(unsigned long cmd, void *args, void *brk)
 	switch (cmd) {
 	case PARASITE_CMD_PINGME:
 		return 0;
+	case PARASITE_CMD_INIT:
+		return init((struct parasite_init_args *) args);
+	case PARASITE_CMD_FINI:
+		return fini();
+	case PARASITE_CMD_SET_LOGFD:
+		return set_logfd();
 	case PARASITE_CMD_DUMPPAGES:
 		return dump_pages((struct parasite_dump_pages_args *)args);
 	case PARASITE_CMD_DUMP_SIGACTS:
@@ -413,6 +454,8 @@ static void __parasite_head __used parasite_head(void)
 		     ".long 0									\n\t");
 }
 
+#include "util-net.c"
+
 #else /* CONFIG_X86_64 */
 # error x86-32 bit mode not yet implemented
 #endif /* CONFIG_X86_64 */


More information about the CRIU mailing list