[CRIU] [PATCH 2/5] fdstore: add a storage for file descriptors (v2)

Andrei Vagin avagin at openvz.org
Tue Feb 7 00:43:28 PST 2017


From: Andrei Vagin <avagin at virtuozzo.com>

We need a storage for file descriptors which is shared between processes
and doesn't use a lot of file descriptors. We are going to use it on
restore and if it will use file descriptors, we will have to find
descriptors which don't used by all restored processes to not confilict
with their descriptors.

There are two solutions. The first one is a service (process) which
handles to command push_fd(id, fd) and pop_fd(id, fd).

Another solution is to save descriptros in a unix socket.  It requires
only one extra descriptor which we can register as a service fd. Each
unix socket has a buffer and can fit a number of file descriptros. We
can use SK_PEEK_OFF and MSG_PEEK to get file descriptros from a socket
as many times as we need.

This patch implements the second solution.

v2: call recvmsg with MSG_PEEK
Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
 criu/Makefile.crtools    |  1 +
 criu/fdstore.c           | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 criu/include/fdstore.h   | 17 +++++++++
 criu/include/servicefd.h |  1 +
 4 files changed, 113 insertions(+)
 create mode 100644 criu/fdstore.c
 create mode 100644 criu/include/fdstore.h

diff --git a/criu/Makefile.crtools b/criu/Makefile.crtools
index 8188129..b85d776 100644
--- a/criu/Makefile.crtools
+++ b/criu/Makefile.crtools
@@ -79,6 +79,7 @@ obj-y			+= uts_ns.o
 obj-y			+= path.o
 obj-y			+= autofs.o
 obj-y			+= uffd.o
+obj-y			+= fdstore.o
 
 ifeq ($(VDSO),y)
 obj-y			+= pie-util-vdso.o
diff --git a/criu/fdstore.c b/criu/fdstore.c
new file mode 100644
index 0000000..c622091
--- /dev/null
+++ b/criu/fdstore.c
@@ -0,0 +1,94 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include "common/scm.h"
+#include "servicefd.h"
+#include "fdstore.h"
+#include "xmalloc.h"
+#include "log.h"
+
+static int next_id;
+
+int fdstore_init(void)
+{
+	struct sockaddr_un addr;
+	unsigned int addrlen;
+	struct stat st;
+	int sk, ret;
+
+	sk = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0);
+	if (sk < 0) {
+		pr_perror("Unable to create a socket");
+		return -1;
+	}
+
+	if (fstat(sk, &st)) {
+		pr_perror("Unable to stat a file descriptor");
+		close(sk);
+		return -1;
+	}
+
+	addr.sun_family = AF_UNIX;
+	addrlen = snprintf(addr.sun_path, sizeof(addr.sun_path), "X/criu-fdstore-%"PRIx64, st.st_ino);
+	addrlen += sizeof(addr.sun_family);
+
+	addr.sun_path[0] = 0;
+
+	/*
+	 * This socket is connected to itself, so all messages are queued to
+	 * its receive queue. Here we are going to use this socket to store
+	 * file descriptors. For that we need to send a file descriptor in
+	 * a queue and remeber its sequence number. Then we can set SO_PEEK_OFF
+	 * to get a file descriptor without dequeuing it.
+	 */
+	if (bind(sk, (struct sockaddr *) &addr, addrlen)) {
+		pr_perror("Unable to bind a socket");
+		close(sk);
+		return -1;
+	}
+	if (connect(sk, (struct sockaddr *) &addr, addrlen)) {
+		pr_perror("Unable to connect a socket");
+		close(sk);
+		return -1;
+	}
+
+	ret = install_service_fd(FDSTORE_SK_OFF, sk);
+	close(sk);
+	if (ret < 0)
+		return -1;
+
+	return 0;
+}
+
+int fdstore_add(int fd)
+{
+	int sk = get_service_fd(FDSTORE_SK_OFF);
+
+	if (send_fd(sk, NULL, 0, fd))
+		return -1;
+
+	next_id++;
+
+	return next_id - 1;
+}
+
+int fdstore_get(int id)
+{
+	int sk = get_service_fd(FDSTORE_SK_OFF);
+	int fd;
+
+	if (setsockopt(sk, SOL_SOCKET, SO_PEEK_OFF, &id, sizeof(id))) {
+		pr_perror("Unable to a peek offset");
+		return -1;
+	}
+
+	if (__recv_fds(sk, &fd, 1, NULL, 0, MSG_PEEK) < 0) {
+		pr_perror("Unable to get a file descriptor with the %d id", id);
+		return -1;
+	}
+	return fd;
+}
diff --git a/criu/include/fdstore.h b/criu/include/fdstore.h
new file mode 100644
index 0000000..bdfb5fe
--- /dev/null
+++ b/criu/include/fdstore.h
@@ -0,0 +1,17 @@
+#ifndef __CRIU_FDSTORE_H__
+#define __CRIU_FDSTORE_H__
+
+/*
+ * fdstore is a storage for file descriptors which is shared
+ * between processes.
+ */
+
+int fdstore_init(void);
+
+/* Add a file descriptor to the storage and return its id */
+int fdstore_add(int fd);
+
+/* Get a file descriptor from a storage by id */
+int fdstore_get(int id);
+
+#endif
diff --git a/criu/include/servicefd.h b/criu/include/servicefd.h
index 1b48e90..3e0e376 100644
--- a/criu/include/servicefd.h
+++ b/criu/include/servicefd.h
@@ -22,6 +22,7 @@ enum sfd_type {
 	TRANSPORT_FD_OFF, /* to transfer file descriptors */
 	LAZY_PAGES_SK_OFF, /* socket for communication with lazy-pages daemon */
 	RPC_SK_OFF,
+	FDSTORE_SK_OFF,
 
 	SERVICE_FD_MAX
 };
-- 
2.7.4



More information about the CRIU mailing list