[CRIU] [PATCH 3/3] files: Sanitize helpers for scattering two-headed files
Pavel Emelyanov
xemul at virtuozzo.com
Tue Feb 14 08:56:43 PST 2017
Signed-off-by: Pavel Emelyanov <xemul at virtuozzo.com>
---
criu/files.c | 23 +++++++++++++++++++++--
criu/include/files.h | 4 ++--
criu/pipes.c | 19 ++++---------------
criu/sk-unix.c | 13 ++-----------
criu/tty.c | 16 ++++------------
5 files changed, 33 insertions(+), 42 deletions(-)
diff --git a/criu/files.c b/criu/files.c
index 1a76313..a114e18 100644
--- a/criu/files.c
+++ b/criu/files.c
@@ -885,7 +885,7 @@ static int plant_fd(struct fdinfo_list_entry *fle, int fd)
return reopen_fd_as(fle->fe->fd, fd);
}
-int recv_fd_from_peer(struct fdinfo_list_entry *fle)
+static int recv_fd_from_peer(struct fdinfo_list_entry *fle)
{
struct fdinfo_list_entry *tmp;
int fd, ret, tsock;
@@ -913,7 +913,7 @@ int recv_fd_from_peer(struct fdinfo_list_entry *fle)
return 0;
}
-int send_fd_to_peer(int fd, struct fdinfo_list_entry *fle)
+static int send_fd_to_peer(int fd, struct fdinfo_list_entry *fle)
{
struct sockaddr_un saddr;
int len, sock, ret;
@@ -928,6 +928,25 @@ int send_fd_to_peer(int fd, struct fdinfo_list_entry *fle)
return set_fds_event(fle->pid);
}
+/*
+ * Helpers to scatter file_desc across users for those files, that
+ * create two descriptors from a single system call at once (e.g.
+ * ... or better i.e. -- pipes, socketpairs and ttys)
+ */
+int recv_desc_from_peer(struct file_desc *d, int *fd)
+{
+ struct fdinfo_list_entry *fle;
+
+ fle = file_master(d);
+ *fd = fle->fe->fd;
+ return recv_fd_from_peer(fle);
+}
+
+int send_desc_to_peer(int fd, struct file_desc *d)
+{
+ return send_fd_to_peer(fd, file_master(d));
+}
+
static int send_fd_to_self(int fd, struct fdinfo_list_entry *fle)
{
int dfd = fle->fe->fd;
diff --git a/criu/include/files.h b/criu/include/files.h
index 39ef23b..6e0d0a2 100644
--- a/criu/include/files.h
+++ b/criu/include/files.h
@@ -148,8 +148,8 @@ extern int file_desc_add(struct file_desc *d, u32 id, struct file_desc_ops *ops)
extern struct fdinfo_list_entry *file_master(struct file_desc *d);
extern struct file_desc *find_file_desc_raw(int type, u32 id);
-extern int recv_fd_from_peer(struct fdinfo_list_entry *fle);
-extern int send_fd_to_peer(int fd, struct fdinfo_list_entry *fle);
+extern int recv_desc_from_peer(struct file_desc *d, int *fd);
+extern int send_desc_to_peer(int fd, struct file_desc *d);
extern int restore_fown(int fd, FownEntry *fown);
extern int rst_file_params(int fd, FownEntry *fown, int flags);
diff --git a/criu/pipes.c b/criu/pipes.c
index 7be1d5f..d665a59 100644
--- a/criu/pipes.c
+++ b/criu/pipes.c
@@ -229,21 +229,14 @@ static int reopen_pipe(int fd, int flags)
static int recv_pipe_fd(struct pipe_info *pi, int *new_fd)
{
- struct fdinfo_list_entry *fle;
int tmp, fd, ret;
- fle = file_master(&pi->d);
- fd = fle->fe->fd;
-
- pr_info("\tWaiting fd for %d\n", fd);
-
- ret = recv_fd_from_peer(fle);
+ ret = recv_desc_from_peer(&pi->d, &tmp);
if (ret != 0) {
if (ret != 1)
- pr_err("Can't get fd %d\n", fd);
+ pr_err("Can't get fd %d\n", tmp);
return ret;
}
- tmp = fd;
if (pi->reopen)
fd = reopen_pipe(tmp, pi->pe->flags);
@@ -304,13 +297,9 @@ int open_pipe(struct file_desc *d, int *new_fd)
return -1;
list_for_each_entry(p, &pi->pipe_list, pipe_list) {
- struct fdinfo_list_entry *fle;
- int fd;
-
- fle = file_master(&p->d);
- fd = pfd[p->pe->flags & O_WRONLY];
+ int fd = pfd[p->pe->flags & O_WRONLY];
- if (send_fd_to_peer(fd, fle)) {
+ if (send_desc_to_peer(fd, &p->d)) {
pr_perror("Can't send file descriptor");
return -1;
}
diff --git a/criu/sk-unix.c b/criu/sk-unix.c
index df8da52..2f53471 100644
--- a/criu/sk-unix.c
+++ b/criu/sk-unix.c
@@ -1085,7 +1085,6 @@ static int open_unixsk_pair_master(struct unix_sk_info *ui, int *new_fd)
{
int sk[2];
struct unix_sk_info *peer = ui->peer;
- struct fdinfo_list_entry *fle;
pr_info("Opening pair master (id %#x ino %#x peer %#x)\n",
ui->ue->id, ui->ue->ino, ui->ue->peer);
@@ -1106,8 +1105,7 @@ static int open_unixsk_pair_master(struct unix_sk_info *ui, int *new_fd)
if (restore_sk_common(sk[0], ui))
return -1;
- fle = file_master(&peer->d);
- if (send_fd_to_peer(sk[1], fle)) {
+ if (send_desc_to_peer(sk[1], &peer->d)) {
pr_err("Can't send pair slave\n");
return -1;
}
@@ -1120,21 +1118,14 @@ static int open_unixsk_pair_master(struct unix_sk_info *ui, int *new_fd)
static int open_unixsk_pair_slave(struct unix_sk_info *ui, int *new_fd)
{
- struct fdinfo_list_entry *fle;
int sk, ret;
- fle = file_master(&ui->d);
-
- pr_info("Opening pair slave (id %#x ino %#x peer %#x) on %d\n",
- ui->ue->id, ui->ue->ino, ui->ue->peer, fle->fe->fd);
-
- ret = recv_fd_from_peer(fle);
+ ret = recv_desc_from_peer(&ui->d, &sk);
if (ret != 0) {
if (ret != 1)
pr_err("Can't recv pair slave\n");
return ret;
}
- sk = fle->fe->fd;
if (bind_unix_sk(sk, ui))
return -1;
diff --git a/criu/tty.c b/criu/tty.c
index c1bb95c..7ea8a9b 100644
--- a/criu/tty.c
+++ b/criu/tty.c
@@ -892,7 +892,6 @@ static void pty_restore_queued_data(struct tty_info *info, int fd)
static int pty_open_slaves(struct tty_info *info)
{
int fd = -1, ret = -1;
- struct fdinfo_list_entry *fle;
struct tty_info *slave;
list_for_each_entry(slave, &info->sibling, sibling) {
@@ -907,12 +906,10 @@ static int pty_open_slaves(struct tty_info *info)
if (restore_tty_params(fd, slave))
goto err;
- fle = file_master(&slave->d);
-
- pr_debug("send slave %#x fd %d connected on %s (pid %d)\n",
- slave->tfe->id, fd, path_from_reg(slave->reg_d), fle->pid);
+ pr_debug("send slave %#x fd %d connected on %s\n",
+ slave->tfe->id, fd, path_from_reg(slave->reg_d));
- if (send_fd_to_peer(fd, fle)) {
+ if (send_desc_to_peer(fd, &slave->d)) {
pr_err("Can't send file descriptor\n");
goto err;
}
@@ -930,14 +927,9 @@ err:
static int receive_tty(struct tty_info *info, int *new_fd)
{
- struct fdinfo_list_entry *fle;
int fd, ret;
- fle = file_master(&info->d);
- pr_info("\tWaiting tty fd %d (pid %d)\n", fle->fe->fd, fle->pid);
-
- fd = fle->fe->fd;
- ret = recv_fd_from_peer(fle);
+ ret = recv_desc_from_peer(&info->d, &fd);
if (ret != 0) {
if (ret != 1)
pr_err("Can't get fd %d\n", fd);
--
2.1.4
More information about the CRIU
mailing list