Commit 4d06aa12 authored by Andrei Vagin's avatar Andrei Vagin

fdstore: add a storage for file descriptors (v2)

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
travis-ci: success for A few fixes to c/r a docker container with a console (rev3)
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
parent 6afe523d
......@@ -79,6 +79,7 @@ obj-y += util.o
obj-y += uts_ns.o
obj-y += path.o
obj-y += autofs.o
obj-y += fdstore.o
ifeq ($(VDSO),y)
obj-y += pie-util-vdso.o
......
#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;
}
#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
......@@ -21,6 +21,7 @@ enum sfd_type {
NS_FD_OFF, /* Node's net namespace fd */
TRANSPORT_FD_OFF, /* to transfer file descriptors */
RPC_SK_OFF,
FDSTORE_SK_OFF,
SERVICE_FD_MAX
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment