[CRIU] [PATCH 4/5] eventfd: Add checkpoint/restore support

Cyrill Gorcunov gorcunov at openvz.org
Fri Apr 27 18:13:27 EDT 2012


Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Makefile          |    1 +
 cr-dump.c         |   22 +++++++
 cr-restore.c      |    4 +
 cr-show.c         |   23 +++++++
 crtools.c         |    7 ++
 eventfs.c         |  165 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/crtools.h |    3 +
 include/eventfs.h |   20 +++++++
 include/image.h   |    9 +++
 9 files changed, 254 insertions(+), 0 deletions(-)
 create mode 100644 eventfs.c
 create mode 100644 include/eventfs.h

diff --git a/Makefile b/Makefile
index 8ad48c6..a13b80b 100644
--- a/Makefile
+++ b/Makefile
@@ -48,6 +48,7 @@ OBJS		+= file-ids.o
 OBJS		+= namespaces.o
 OBJS		+= uts_ns.o
 OBJS		+= ipc_ns.o
+OBJS		+= eventfs.o
 
 DEPS		:= $(patsubst %.o,%.d,$(OBJS))
 
diff --git a/cr-dump.c b/cr-dump.c
index e6384e9..b466a92 100644
--- a/cr-dump.c
+++ b/cr-dump.c
@@ -34,6 +34,7 @@
 #include "parasite.h"
 #include "parasite-syscall.h"
 #include "files.h"
+#include "eventfs.h"
 
 #ifndef CONFIG_X86_64
 # error No x86-32 support yet
@@ -427,6 +428,9 @@ static int do_dump_gen_file(const struct fd_parms *p, int lfd,
 		case FDINFO_PIPE:
 			ret = dump_one_pipe(lfd, e.id, p);
 			break;
+		case FDINFO_EVENTFD:
+			ret = dump_one_eventfd(lfd, e.id, p);
+			break;
 		default:
 			ret = dump_one_reg_file(lfd, e.id, p);
 			break;
@@ -559,10 +563,18 @@ static int dump_chrdev(struct fd_parms *p, int lfd, const struct cr_fdset *set)
 	return dump_unsupp_fd(p);
 }
 
+static int dump_eventfd(struct fd_parms *p, int lfd, const struct cr_fdset *set)
+{
+	p->id = MAKE_FD_GENID(p->stat.st_dev, p->stat.st_ino, p->pos);
+	p->type	= FDINFO_EVENTFD;
+	return do_dump_gen_file(p, lfd, set);
+}
+
 static int dump_one_file(pid_t pid, int fd, int lfd, char fd_flags,
 		       const struct cr_fdset *cr_fdset)
 {
 	struct fd_parms p;
+	struct statfs statfs;
 
 	if (fill_fd_params(pid, fd, lfd, fd_flags, &p) < 0) {
 		pr_perror("Can't get stat on %d", fd);
@@ -575,6 +587,16 @@ static int dump_one_file(pid_t pid, int fd, int lfd, char fd_flags,
 	if (S_ISCHR(p.stat.st_mode))
 		return dump_chrdev(&p, lfd, cr_fdset);
 
+	if (fstatfs(lfd, &statfs)) {
+		pr_perror("Can't obtain statfs on fd %d\n", fd);
+		return -1;
+	}
+
+	if (is_anon_inode(&statfs)) {
+		if (is_eventfd_link(lfd))
+			return dump_eventfd(&p, lfd, cr_fdset);
+	}
+
 	if (S_ISREG(p.stat.st_mode) ||
             S_ISDIR(p.stat.st_mode) ||
             S_ISFIFO(p.stat.st_mode))
diff --git a/cr-restore.c b/cr-restore.c
index 5c337d3..2ecf5a4 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -33,6 +33,7 @@
 #include "sockets.h"
 #include "lock.h"
 #include "files.h"
+#include "eventfs.h"
 #include "proc_parse.h"
 #include "restorer-blob.h"
 #include "crtools.h"
@@ -291,6 +292,9 @@ static int prepare_shared(void)
 	if (collect_unix_sockets())
 		return -1;
 
+	if (collect_eventfd())
+		return -1;
+
 	list_for_each_entry(pi, &tasks, list) {
 		ret = prepare_shmem_pid(pi->pid);
 		if (ret < 0)
diff --git a/cr-show.c b/cr-show.c
index c0193f4..f1263a3 100644
--- a/cr-show.c
+++ b/cr-show.c
@@ -62,6 +62,7 @@ static char *fdtype2s(u8 type)
 		[FDINFO_INETSK] = "isk",
 		[FDINFO_PIPE] = "pipe",
 		[FDINFO_UNIXSK] = "usk",
+		[FDINFO_EVENTFD] = "efd",
 	};
 
 	if (type > FDINFO_UND && type < FD_INFO_MAX)
@@ -99,6 +100,28 @@ void show_fown_cont(fown_t *fown)
 	       fown->uid, fown->euid, fown->signum, fown->pid_type, fown->pid);
 }
 
+void show_eventfds(int fd, struct cr_options *o)
+{
+	struct eventfd_file_entry efe;
+
+	pr_img_head(CR_FD_EVENTFD);
+
+	while (1) {
+		int ret;
+
+		ret = read_img_eof(fd, &efe);
+		if (ret <= 0)
+			goto out;
+		pr_msg("id: %#08x flags %#04x counter: %#016lx ",
+		       efe.id, efe.flags, efe.counter);
+		show_fown_cont(&efe.fown);
+		pr_msg("\n");
+	}
+
+out:
+	pr_img_tail(CR_FD_EVENTFD);
+}
+
 void show_reg_files(int fd_reg_files, struct cr_options *o)
 {
 	struct reg_file_entry rfe;
diff --git a/crtools.c b/crtools.c
index a5a8eaa..e939d86 100644
--- a/crtools.c
+++ b/crtools.c
@@ -61,6 +61,13 @@ struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX] = {
 		.show	= show_reg_files,
 	},
 
+	/* eventfd */
+	[CR_FD_EVENTFD] = {
+		.fmt	= FMT_FNAME_EVENTFD,
+		.magic	= EVENTFD_MAGIC,
+		.show	= show_eventfds,
+	},
+
 	/* core data, such as regs and vmas and such */
 	[CR_FD_CORE] = {
 		.fmt	= FMT_FNAME_CORE,
diff --git a/eventfs.c b/eventfs.c
new file mode 100644
index 0000000..dfea4c9
--- /dev/null
+++ b/eventfs.c
@@ -0,0 +1,165 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <sys/stat.h>
+#include <sys/statfs.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/eventfd.h>
+
+#include "compiler.h"
+#include "types.h"
+#include "eventfs.h"
+
+#include "crtools.h"
+#include "image.h"
+#include "util.h"
+#include "log.h"
+
+struct eventfd_file_info {
+	struct eventfd_file_entry	efe;
+	struct file_desc		d;
+};
+
+/* Checks if file desciptor @lfd is eventfd */
+int is_eventfd_link(int lfd)
+{
+	char link[PATH_MAX], path[32];
+	ssize_t ret;
+
+	snprintf(path, sizeof(path), "/proc/self/fd/%d", lfd);
+	ret = readlink(path, link, sizeof(link));
+	if (ret < 0) {
+		pr_perror("Can't read link of fd %d\n", lfd);
+		return 0;
+	}
+	link[ret] = 0;
+	if (!strcmp(link, "anon_inode:[eventfd]"))
+		return 1;
+
+	return 0;
+}
+
+static void pr_info_eventfd(char *action, struct eventfd_file_entry *efe)
+{
+	pr_info("%seventfd: id %#08x flags %#04x counter %#016lx\n",
+		action, efe->id, efe->flags, efe->counter);
+}
+
+int dump_one_eventfd(int lfd, u32 id, const struct fd_parms *p)
+{
+	int image_fd = fdset_fd(glob_fdset, CR_FD_EVENTFD);
+	struct eventfd_file_entry efe;
+	char buf[64];
+	char *pos;
+	int ret, fdinfo;
+
+	efe.id		= id;
+	efe.flags	= p->flags;
+	efe.fown	= p->fown;
+
+	snprintf(buf, sizeof(buf), "/proc/self/fdinfo/%d", lfd);
+	fdinfo = open(buf, O_RDONLY);
+	if (fdinfo < 0) {
+		pr_perror("Can't open  %d (%d)", p->fd, lfd);
+		return -1;
+	}
+
+	ret = read(fdinfo, buf, sizeof(buf));
+	close(fdinfo);
+
+	if (ret <= 0) {
+		pr_perror("Reading eventfd from %d (%d) failed", p->fd, lfd);
+		return -1;
+	}
+
+	pos = strstr(buf, "count-raw:");
+	if (!pos || !sscanf(pos, "count-raw: %lx", &efe.counter)) {
+		pr_err("Counter value is not found for %d (%d)\n", p->fd, lfd);
+		return -1;
+	}
+
+	pr_info_eventfd("Dumping: ", &efe);
+	if (write_img(image_fd, &efe))
+		return -1;
+
+	return 0;
+}
+
+static int eventfd_open(struct file_desc *d)
+{
+	struct eventfd_file_info *info;
+	size_t size;
+	int tmp;
+
+	info = container_of(d, struct eventfd_file_info, d);
+
+	tmp = eventfd(0, 0);
+	if (tmp < 0) {
+		pr_perror("Can't create eventfd %#08x",
+			  info->efe.id);
+		return -1;
+	}
+
+	if (fcntl(tmp, F_SETFD, info->efe.flags)) {
+		pr_perror("Can't set flags on eventfd %#08x",
+			  info->efe.id);
+		goto err_close;
+	}
+
+	if (restore_fown(tmp, &info->efe.fown))
+		goto err_close;
+
+	size = sizeof(info->efe.counter);
+	if (write(tmp, &info->efe.counter, size) != size) {
+		pr_perror("Can't write counter to eventfd %#08x",
+			  info->efe.id);
+		goto err_close;
+	}
+
+	return tmp;
+
+err_close:
+	close(tmp);
+	return -1;
+}
+
+static struct file_desc_ops eventfd_desc_ops = {
+	.open = eventfd_open,
+};
+
+int collect_eventfd(void)
+{
+	struct eventfd_file_info *info = NULL;
+	int ret, image_fd;
+
+	image_fd = open_image_ro(CR_FD_EVENTFD);
+	if (image_fd < 0)
+		return -1;
+
+	while (1) {
+		ret = -1;
+
+		info = xmalloc(sizeof(*info));
+		if (!info)
+			break;
+
+		ret = read_img_eof(image_fd, &info->efe);
+		if (ret < 0)
+			goto err;
+		else if (!ret)
+			break;
+		pr_info_eventfd("Collected: ", &info->efe);
+		file_desc_add(&info->d, FDINFO_EVENTFD, info->efe.id, &eventfd_desc_ops);
+	}
+
+err:
+	xfree(info);
+	close(image_fd);
+	return ret;
+}
diff --git a/include/crtools.h b/include/crtools.h
index 6625448..78cf968 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -54,6 +54,7 @@ enum {
 	CR_FD_PIPES,
 	CR_FD_PIPES_DATA,
 	CR_FD_REMAP_FPATH,
+	CR_FD_EVENTFD,
 	_CR_FD_GLOB_TO,
 
 	CR_FD_MAX
@@ -100,6 +101,7 @@ void show_fs(int fd, struct cr_options *o);
 void show_remap_files(int fd, struct cr_options *o);
 void show_ghost_file(int fd, struct cr_options *o);
 void show_fown_cont(fown_t *fown);
+void show_eventfds(int fd, struct cr_options *o);
 
 extern void print_data(unsigned long addr, unsigned char *data, size_t size);
 extern struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX];
@@ -117,6 +119,7 @@ extern struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX];
 #define FMT_FNAME_SIGACTS	"sigacts-%d.img"
 #define FMT_FNAME_UNIXSK	"unixsk.img"
 #define FMT_FNAME_INETSK	"inetsk.img"
+#define FMT_FNAME_EVENTFD	"eventfd.img"
 #define FMT_FNAME_ITIMERS	"itimers-%d.img"
 #define FMT_FNAME_CREDS		"creds-%d.img"
 #define FMT_FNAME_UTSNS		"utsns-%d.img"
diff --git a/include/eventfs.h b/include/eventfs.h
new file mode 100644
index 0000000..ce3a7b7
--- /dev/null
+++ b/include/eventfs.h
@@ -0,0 +1,20 @@
+#ifndef EVENTFS_H__
+#define EVENTFS_H__
+
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "compiler.h"
+#include "types.h"
+#include "files.h"
+#include "crtools.h"
+
+#ifndef EFD_RAW_COUNTER
+#define EFD_RAW_COUNTER	0xc0044510
+#endif
+
+extern int is_eventfd_link(int lfd);
+extern int dump_one_eventfd(int lfd, u32 id, const struct fd_parms *p);
+extern int collect_eventfd(void);
+
+#endif /* EVENTFS_H__ */
diff --git a/include/image.h b/include/image.h
index 8d1c434..2349e37 100644
--- a/include/image.h
+++ b/include/image.h
@@ -32,6 +32,7 @@
 #define MM_MAGIC		0x57492820 /* Pskov */
 #define REMAP_FPATH_MAGIC	0x59133954 /* Vologda */
 #define GHOST_FILE_MAGIC	0x52583605 /* Oryol */
+#define EVENTFD_MAGIC		0x44523722 /* Anapa */
 
 enum fd_types {
 	FDINFO_UND,
@@ -39,6 +40,7 @@ enum fd_types {
 	FDINFO_PIPE,
 	FDINFO_INETSK,
 	FDINFO_UNIXSK,
+	FDINFO_EVENTFD,
 
 	FD_INFO_MAX
 };
@@ -81,6 +83,13 @@ struct ghost_file_entry {
 	u32	mode;
 } __packed;
 
+struct eventfd_file_entry {
+	u32	id;
+	u16	flags;
+	fown_t	fown;
+	u64	counter;
+} __packed;
+
 struct fdinfo_entry {
 	u32	fd;
 	u8	type;
-- 
1.7.7.6



More information about the CRIU mailing list