2012-01-10 18:03:00 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2012-01-11 15:45:00 +04:00
|
|
|
#include <errno.h>
|
2012-01-10 18:03:00 +04:00
|
|
|
|
|
|
|
#include <linux/limits.h>
|
|
|
|
|
2012-01-11 15:45:00 +04:00
|
|
|
#include <sys/types.h>
|
2012-02-07 19:32:11 +04:00
|
|
|
#include <sys/prctl.h>
|
2012-01-11 15:45:00 +04:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2012-05-29 20:11:00 +04:00
|
|
|
#include <stdlib.h>
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-01-10 18:03:00 +04:00
|
|
|
#include "crtools.h"
|
|
|
|
|
|
|
|
#include "files.h"
|
2012-06-22 16:24:00 +04:00
|
|
|
#include "files-reg.h"
|
2012-01-10 18:03:00 +04:00
|
|
|
#include "image.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "util.h"
|
2012-02-01 13:00:49 +03:00
|
|
|
#include "util-net.h"
|
2012-01-10 18:03:00 +04:00
|
|
|
#include "lock.h"
|
2012-03-27 12:42:59 +04:00
|
|
|
#include "sockets.h"
|
2012-06-26 14:51:00 +04:00
|
|
|
#include "pstree.h"
|
2012-09-12 20:00:54 +04:00
|
|
|
#include "tty.h"
|
2012-01-10 18:03:00 +04:00
|
|
|
|
2012-07-12 13:06:00 +04:00
|
|
|
#include "protobuf.h"
|
2012-07-17 07:28:38 +04:00
|
|
|
#include "protobuf/fs.pb-c.h"
|
2012-07-12 13:06:00 +04:00
|
|
|
|
2012-04-06 20:30:48 +04:00
|
|
|
#define FDESC_HASH_SIZE 64
|
2012-06-09 12:17:00 +04:00
|
|
|
static struct list_head file_desc_hash[FDESC_HASH_SIZE];
|
2012-04-06 20:30:48 +04:00
|
|
|
|
2012-02-22 18:51:27 +04:00
|
|
|
int prepare_shared_fdinfo(void)
|
|
|
|
{
|
2012-04-06 20:30:48 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < FDESC_HASH_SIZE; i++)
|
2012-06-09 12:17:00 +04:00
|
|
|
INIT_LIST_HEAD(&file_desc_hash[i]);
|
2012-04-06 20:30:48 +04:00
|
|
|
|
2012-02-22 18:51:27 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-09 12:17:00 +04:00
|
|
|
void file_desc_add(struct file_desc *d, u32 id, struct file_desc_ops *ops)
|
2012-04-06 20:03:31 +04:00
|
|
|
{
|
2012-04-06 20:30:48 +04:00
|
|
|
d->id = id;
|
2012-04-06 20:39:48 +04:00
|
|
|
d->ops = ops;
|
2012-04-06 20:03:31 +04:00
|
|
|
INIT_LIST_HEAD(&d->fd_info_head);
|
2012-04-06 20:30:48 +04:00
|
|
|
|
2012-06-09 12:17:00 +04:00
|
|
|
list_add_tail(&d->hash, &file_desc_hash[id % FDESC_HASH_SIZE]);
|
2012-04-06 20:30:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct file_desc *find_file_desc_raw(int type, u32 id)
|
|
|
|
{
|
|
|
|
struct file_desc *d;
|
|
|
|
struct list_head *chain;
|
|
|
|
|
2012-06-09 12:17:00 +04:00
|
|
|
chain = &file_desc_hash[id % FDESC_HASH_SIZE];
|
2012-04-06 20:30:48 +04:00
|
|
|
list_for_each_entry(d, chain, hash)
|
2012-05-04 15:41:05 +04:00
|
|
|
if (d->ops->type == type && d->id == id)
|
2012-04-06 20:30:48 +04:00
|
|
|
return d;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-12 13:06:00 +04:00
|
|
|
static inline struct file_desc *find_file_desc(FdinfoEntry *fe)
|
2012-04-06 20:30:48 +04:00
|
|
|
{
|
|
|
|
return find_file_desc_raw(fe->type, fe->id);
|
2012-04-06 20:03:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct fdinfo_list_entry *file_master(struct file_desc *d)
|
|
|
|
{
|
2012-07-28 09:11:12 +04:00
|
|
|
if (list_empty(&d->fd_info_head)) {
|
|
|
|
pr_err("Empty list on file desc id %#x\n", d->id);
|
2012-08-27 23:22:25 +04:00
|
|
|
BUG();
|
2012-07-28 09:11:12 +04:00
|
|
|
}
|
|
|
|
|
2012-04-06 20:03:31 +04:00
|
|
|
return list_first_entry(&d->fd_info_head,
|
2012-04-18 15:46:04 +04:00
|
|
|
struct fdinfo_list_entry, desc_list);
|
2012-04-06 20:03:31 +04:00
|
|
|
}
|
|
|
|
|
2012-04-05 12:48:57 +04:00
|
|
|
void show_saved_files(void)
|
|
|
|
{
|
|
|
|
int i;
|
2012-04-06 20:30:48 +04:00
|
|
|
struct file_desc *fd;
|
2012-04-05 12:48:57 +04:00
|
|
|
|
2012-04-06 20:30:48 +04:00
|
|
|
pr_info("File descs:\n");
|
|
|
|
for (i = 0; i < FDESC_HASH_SIZE; i++)
|
2012-06-09 12:17:00 +04:00
|
|
|
list_for_each_entry(fd, &file_desc_hash[i], hash) {
|
2012-04-05 12:48:57 +04:00
|
|
|
struct fdinfo_list_entry *le;
|
|
|
|
|
2012-05-04 15:41:05 +04:00
|
|
|
pr_info(" `- type %d ID %#x\n", fd->ops->type, fd->id);
|
2012-04-18 15:46:04 +04:00
|
|
|
list_for_each_entry(le, &fd->fd_info_head, desc_list)
|
2012-07-07 01:03:00 +04:00
|
|
|
pr_info(" `- FD %d pid %d\n", le->fe->fd, le->pid);
|
2012-04-05 12:48:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-19 09:39:00 +04:00
|
|
|
int restore_fown(int fd, FownEntry *fown)
|
2012-04-10 22:54:00 +04:00
|
|
|
{
|
|
|
|
struct f_owner_ex owner;
|
|
|
|
uid_t uids[3];
|
|
|
|
pid_t pid = getpid();
|
|
|
|
|
|
|
|
if (fown->signum) {
|
|
|
|
if (fcntl(fd, F_SETSIG, fown->signum)) {
|
|
|
|
pr_perror("%d: Can't set signal", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* May be untouched */
|
|
|
|
if (!fown->pid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (getresuid(&uids[0], &uids[1], &uids[2])) {
|
|
|
|
pr_perror("%d: Can't get current UIDs", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setresuid(fown->uid, fown->euid, uids[2])) {
|
|
|
|
pr_perror("%d: Can't set UIDs", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
owner.type = fown->pid_type;
|
|
|
|
owner.pid = fown->pid;
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETOWN_EX, &owner)) {
|
|
|
|
pr_perror("%d: Can't setup %d file owner pid",
|
|
|
|
pid, fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setresuid(uids[0], uids[1], uids[2])) {
|
|
|
|
pr_perror("%d: Can't revert UIDs back", pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-19 09:39:00 +04:00
|
|
|
int rst_file_params(int fd, FownEntry *fown, int flags)
|
2012-04-26 14:03:49 +04:00
|
|
|
{
|
|
|
|
if (set_fd_flags(fd, flags) < 0)
|
|
|
|
return -1;
|
|
|
|
if (restore_fown(fd, fown) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-14 21:53:51 +04:00
|
|
|
static struct list_head *select_ps_list(struct file_desc *desc, struct rst_info *ri)
|
2012-09-11 21:33:19 +04:00
|
|
|
{
|
2012-09-14 21:53:51 +04:00
|
|
|
if (desc->ops->select_ps_list)
|
|
|
|
return desc->ops->select_ps_list(desc, ri);
|
|
|
|
else
|
2012-09-11 21:33:19 +04:00
|
|
|
return &ri->fds;
|
|
|
|
}
|
|
|
|
|
2012-07-12 13:06:00 +04:00
|
|
|
static int collect_fd(int pid, FdinfoEntry *e, struct rst_info *rst_info)
|
2012-01-11 15:45:00 +04:00
|
|
|
{
|
2012-08-27 23:24:06 +04:00
|
|
|
struct fdinfo_list_entry *le, *new_le;
|
2012-04-06 20:03:31 +04:00
|
|
|
struct file_desc *fdesc;
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-04-13 19:44:00 +04:00
|
|
|
pr_info("Collect fdinfo pid=%d fd=%d id=0x%16x\n",
|
2012-04-09 16:18:33 +04:00
|
|
|
pid, e->fd, e->id);
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-08-27 23:24:06 +04:00
|
|
|
new_le = shmalloc(sizeof(*new_le));
|
|
|
|
if (!new_le)
|
2012-01-16 23:10:41 +03:00
|
|
|
return -1;
|
|
|
|
|
2012-08-27 23:24:06 +04:00
|
|
|
futex_init(&new_le->real_pid);
|
|
|
|
new_le->pid = pid;
|
|
|
|
new_le->fe = e;
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-04-06 20:03:31 +04:00
|
|
|
fdesc = find_file_desc(e);
|
|
|
|
if (fdesc == NULL) {
|
2012-04-09 16:18:33 +04:00
|
|
|
pr_err("No file for fd %d id %d\n", e->fd, e->id);
|
2012-04-05 16:54:10 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-08-27 23:24:06 +04:00
|
|
|
list_for_each_entry(le, &fdesc->fd_info_head, desc_list) {
|
|
|
|
if (le->pid > new_le->pid)
|
2012-04-05 16:54:10 +04:00
|
|
|
break;
|
2012-08-27 23:24:06 +04:00
|
|
|
}
|
2012-04-05 16:54:10 +04:00
|
|
|
|
2012-08-27 23:24:06 +04:00
|
|
|
list_add_tail(&new_le->desc_list, &le->desc_list);
|
|
|
|
new_le->desc = fdesc;
|
2012-09-14 21:53:51 +04:00
|
|
|
list_add_tail(&new_le->ps_list, select_ps_list(fdesc, rst_info));
|
2012-09-05 20:00:06 +04:00
|
|
|
|
2012-04-05 16:54:10 +04:00
|
|
|
return 0;
|
2012-01-11 15:45:00 +04:00
|
|
|
}
|
|
|
|
|
2012-09-12 20:00:58 +04:00
|
|
|
int prepare_ctl_tty(int pid, struct rst_info *rst_info, u32 ctl_tty_id)
|
|
|
|
{
|
2012-09-12 20:09:05 +04:00
|
|
|
FdinfoEntry *e;
|
|
|
|
|
|
|
|
if (!ctl_tty_id)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pr_info("Requesting for ctl tty %#x into service fd\n", ctl_tty_id);
|
|
|
|
|
|
|
|
e = xmalloc(sizeof(*e));
|
2012-09-12 20:00:58 +04:00
|
|
|
if (!e)
|
|
|
|
return -1;
|
2012-09-12 20:09:05 +04:00
|
|
|
|
2012-09-12 20:00:58 +04:00
|
|
|
fdinfo_entry__init(e);
|
|
|
|
|
|
|
|
e->id = ctl_tty_id;
|
|
|
|
e->fd = get_service_fd(CTL_TTY_OFF);
|
|
|
|
e->type = FD_TYPES__TTY;
|
|
|
|
|
|
|
|
if (collect_fd(pid, e, rst_info)) {
|
|
|
|
xfree(e);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-11 18:16:25 +04:00
|
|
|
int prepare_fd_pid(struct pstree_item *item)
|
2012-01-11 15:45:00 +04:00
|
|
|
{
|
2012-02-07 15:49:04 +04:00
|
|
|
int fdinfo_fd, ret = 0;
|
2013-01-11 18:16:25 +04:00
|
|
|
pid_t pid = item->pid.virt;
|
|
|
|
struct rst_info *rst_info = item->rst;
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-04-18 16:24:08 +04:00
|
|
|
INIT_LIST_HEAD(&rst_info->fds);
|
2012-05-04 16:31:00 +04:00
|
|
|
INIT_LIST_HEAD(&rst_info->eventpoll);
|
2012-09-12 20:00:52 +04:00
|
|
|
INIT_LIST_HEAD(&rst_info->tty_slaves);
|
2012-04-18 16:24:08 +04:00
|
|
|
|
2013-01-11 18:16:25 +04:00
|
|
|
if (!fdinfo_per_id) {
|
|
|
|
fdinfo_fd = open_image_ro(CR_FD_FDINFO, pid);
|
|
|
|
if (fdinfo_fd < 0) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (item->ids == NULL) /* zombie */
|
2012-01-22 20:26:51 +04:00
|
|
|
return 0;
|
2013-01-11 18:16:25 +04:00
|
|
|
|
|
|
|
if (item->rst->fdt && item->rst->fdt->pid != item->pid.virt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fdinfo_fd = open_image_ro(CR_FD_FDINFO, item->ids->files_id);
|
|
|
|
if (fdinfo_fd < 0)
|
2012-01-22 20:26:51 +04:00
|
|
|
return -1;
|
2012-01-11 15:45:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2012-07-12 13:06:00 +04:00
|
|
|
FdinfoEntry *e;
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-08-07 02:42:58 +04:00
|
|
|
ret = pb_read_one_eof(fdinfo_fd, &e, PB_FDINFO);
|
2012-02-07 15:49:04 +04:00
|
|
|
if (ret <= 0)
|
2012-01-11 15:45:00 +04:00
|
|
|
break;
|
2012-02-07 15:49:04 +04:00
|
|
|
|
2012-07-07 01:03:00 +04:00
|
|
|
ret = collect_fd(pid, e, rst_info);
|
2012-07-12 13:06:00 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
fdinfo_entry__free_unpacked(e, NULL);
|
2012-02-07 15:49:04 +04:00
|
|
|
break;
|
2012-07-12 13:06:00 +04:00
|
|
|
}
|
2012-01-11 15:45:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fdinfo_fd);
|
2012-02-07 15:49:04 +04:00
|
|
|
return ret;
|
2012-01-11 15:45:00 +04:00
|
|
|
}
|
|
|
|
|
2012-05-30 21:06:00 +04:00
|
|
|
#define SETFL_MASK (O_APPEND | O_ASYNC | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
|
2012-04-11 13:20:03 +04:00
|
|
|
int set_fd_flags(int fd, int flags)
|
|
|
|
{
|
2012-05-05 19:20:00 +04:00
|
|
|
int ret;
|
2012-04-11 13:20:03 +04:00
|
|
|
|
2012-05-05 19:20:00 +04:00
|
|
|
ret = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2012-04-11 13:20:03 +04:00
|
|
|
|
2012-05-05 19:20:00 +04:00
|
|
|
flags = (SETFL_MASK & flags) | (ret & ~SETFL_MASK);
|
2012-04-11 13:20:03 +04:00
|
|
|
|
2012-05-05 19:20:00 +04:00
|
|
|
ret = fcntl(fd, F_SETFL, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
pr_perror("fcntl call on fd %d (flags %x) failed", fd, flags);
|
|
|
|
return -1;
|
2012-04-11 13:20:03 +04:00
|
|
|
}
|
|
|
|
|
2012-04-06 21:20:28 +04:00
|
|
|
static void transport_name_gen(struct sockaddr_un *addr, int *len,
|
2012-04-09 16:18:33 +04:00
|
|
|
int pid, int fd)
|
2012-03-23 11:02:06 +03:00
|
|
|
{
|
|
|
|
addr->sun_family = AF_UNIX;
|
2012-04-09 16:18:33 +04:00
|
|
|
snprintf(addr->sun_path, UNIX_PATH_MAX, "x/crtools-fd-%d-%d", pid, fd);
|
2012-03-23 11:02:06 +03:00
|
|
|
*len = SUN_LEN(addr);
|
|
|
|
*addr->sun_path = '\0';
|
|
|
|
}
|
|
|
|
|
2012-07-12 13:06:00 +04:00
|
|
|
static int should_open_transport(FdinfoEntry *fe, struct file_desc *fd)
|
2012-04-05 20:02:00 +04:00
|
|
|
{
|
2012-04-06 20:39:48 +04:00
|
|
|
if (fd->ops->want_transport)
|
|
|
|
return fd->ops->want_transport(fe, fd);
|
|
|
|
else
|
|
|
|
return 0;
|
2012-04-05 20:02:00 +04:00
|
|
|
}
|
|
|
|
|
2012-06-03 23:43:35 +04:00
|
|
|
static int open_transport_fd(int pid, struct fdinfo_list_entry *fle)
|
2012-01-11 15:45:00 +04:00
|
|
|
{
|
2012-06-03 23:43:35 +04:00
|
|
|
struct fdinfo_list_entry *flem;
|
2012-01-11 15:45:00 +04:00
|
|
|
struct sockaddr_un saddr;
|
|
|
|
int sock;
|
|
|
|
int ret, sun_len;
|
|
|
|
|
2012-06-03 23:43:35 +04:00
|
|
|
flem = file_master(fle->desc);
|
2012-04-05 20:02:00 +04:00
|
|
|
|
2012-06-03 23:43:35 +04:00
|
|
|
if (flem->pid == pid) {
|
2012-07-07 01:03:00 +04:00
|
|
|
if (flem->fe->fd != fle->fe->fd)
|
2012-06-04 00:23:49 +04:00
|
|
|
/* dup-ed file. Will be opened in the open_fd */
|
2012-04-05 20:02:00 +04:00
|
|
|
return 0;
|
2012-06-04 00:23:49 +04:00
|
|
|
|
2012-07-07 01:03:00 +04:00
|
|
|
if (!should_open_transport(fle->fe, fle->desc))
|
2012-06-04 00:23:49 +04:00
|
|
|
/* pure master file */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* some master file, that wants a transport, e.g.
|
|
|
|
* a pipe or unix socket pair 'slave' end
|
|
|
|
*/
|
2012-04-05 20:02:00 +04:00
|
|
|
}
|
2012-01-16 19:35:35 +04:00
|
|
|
|
2012-07-07 01:03:00 +04:00
|
|
|
transport_name_gen(&saddr, &sun_len, getpid(), fle->fe->fd);
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-05-02 14:42:00 +04:00
|
|
|
pr_info("\t\tCreate transport fd %s\n", saddr.sun_path + 1);
|
2012-01-11 15:45:00 +04:00
|
|
|
|
|
|
|
|
|
|
|
sock = socket(PF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
pr_perror("Can't create socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = bind(sock, &saddr, sun_len);
|
|
|
|
if (ret < 0) {
|
2012-01-31 15:13:05 +04:00
|
|
|
pr_perror("Can't bind unix socket %s", saddr.sun_path + 1);
|
2012-01-11 15:45:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-01-10 18:03:00 +04:00
|
|
|
|
2012-07-07 01:03:00 +04:00
|
|
|
ret = reopen_fd_as(fle->fe->fd, sock);
|
2012-01-11 15:45:00 +04:00
|
|
|
if (ret < 0)
|
2012-01-10 18:03:00 +04:00
|
|
|
return -1;
|
|
|
|
|
2012-07-07 01:03:00 +04:00
|
|
|
pr_info("\t\tWake up fdinfo pid=%d fd=%d\n", fle->pid, fle->fe->fd);
|
2012-03-26 23:11:00 +04:00
|
|
|
futex_set_and_wake(&fle->real_pid, getpid());
|
2012-01-11 15:45:00 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-10 18:03:00 +04:00
|
|
|
|
2012-08-17 15:43:22 +04:00
|
|
|
int send_fd_to_peer(int fd, struct fdinfo_list_entry *fle, int sock)
|
2012-04-06 21:20:28 +04:00
|
|
|
{
|
|
|
|
struct sockaddr_un saddr;
|
|
|
|
int len;
|
|
|
|
|
2012-07-07 01:03:00 +04:00
|
|
|
pr_info("\t\tWait fdinfo pid=%d fd=%d\n", fle->pid, fle->fe->fd);
|
2012-04-06 21:20:28 +04:00
|
|
|
futex_wait_while(&fle->real_pid, 0);
|
|
|
|
transport_name_gen(&saddr, &len,
|
2012-07-07 01:03:00 +04:00
|
|
|
futex_get(&fle->real_pid), fle->fe->fd);
|
2012-05-02 14:42:00 +04:00
|
|
|
pr_info("\t\tSend fd %d to %s\n", fd, saddr.sun_path + 1);
|
2012-08-17 15:43:22 +04:00
|
|
|
return send_fd(sock, &saddr, len, fd);
|
2012-04-06 21:20:28 +04:00
|
|
|
}
|
|
|
|
|
2012-09-11 22:27:21 +04:00
|
|
|
static int send_fd_to_self(int fd, struct fdinfo_list_entry *fle, int *sock)
|
|
|
|
{
|
|
|
|
int dfd = fle->fe->fd;
|
|
|
|
|
|
|
|
if (fd == dfd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pr_info("\t\t\tGoing to dup %d into %d\n", fd, dfd);
|
|
|
|
if (move_img_fd(sock, dfd))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (dup2(fd, dfd) != dfd) {
|
|
|
|
pr_perror("Can't dup local fd %d -> %d", fd, dfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(dfd, F_SETFD, fle->fe->flags);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
static int post_open_fd(int pid, struct fdinfo_list_entry *fle)
|
2012-08-20 17:50:01 +04:00
|
|
|
{
|
2012-09-13 00:17:57 +04:00
|
|
|
struct file_desc *d = fle->desc;
|
2012-08-20 17:50:01 +04:00
|
|
|
|
2012-09-11 22:07:21 +04:00
|
|
|
if (!d->ops->post_open)
|
|
|
|
return 0;
|
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
if (is_service_fd(fle->fe->fd, CTL_TTY_OFF))
|
|
|
|
return d->ops->post_open(d, fle->fe->fd);
|
2012-09-12 20:00:58 +04:00
|
|
|
|
2012-09-13 00:21:14 +04:00
|
|
|
if (fle != file_master(d))
|
2012-08-20 17:50:01 +04:00
|
|
|
return 0;
|
|
|
|
|
2012-09-13 00:21:14 +04:00
|
|
|
return d->ops->post_open(d, fle->fe->fd);
|
2012-08-20 17:50:01 +04:00
|
|
|
}
|
2012-09-11 22:07:21 +04:00
|
|
|
|
2012-09-13 00:08:39 +04:00
|
|
|
|
|
|
|
static int serve_out_fd(int pid, int fd, struct file_desc *d)
|
2012-01-11 15:45:00 +04:00
|
|
|
{
|
2012-09-13 00:08:39 +04:00
|
|
|
int sock, ret;
|
2012-01-11 15:45:00 +04:00
|
|
|
struct fdinfo_list_entry *fle;
|
|
|
|
|
|
|
|
sock = socket(PF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
pr_perror("Can't create socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-13 00:08:39 +04:00
|
|
|
pr_info("\t\tCreate fd for %d\n", fd);
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-04-18 15:46:04 +04:00
|
|
|
list_for_each_entry(fle, &d->fd_info_head, desc_list) {
|
2012-09-11 22:27:21 +04:00
|
|
|
if (pid == fle->pid)
|
2012-09-13 00:08:39 +04:00
|
|
|
ret = send_fd_to_self(fd, fle, &sock);
|
2012-09-11 22:27:21 +04:00
|
|
|
else
|
2012-09-13 00:08:39 +04:00
|
|
|
ret = send_fd_to_peer(fd, fle, sock);
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-09-11 22:27:21 +04:00
|
|
|
if (ret) {
|
2012-09-13 00:08:39 +04:00
|
|
|
pr_err("Can't sent fd %d to %d\n", fd, fle->pid);
|
2012-01-11 15:45:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
static int open_fd(int pid, struct fdinfo_list_entry *fle)
|
2012-09-13 00:08:39 +04:00
|
|
|
{
|
2012-09-13 00:17:57 +04:00
|
|
|
struct file_desc *d = fle->desc;
|
2012-09-13 00:08:39 +04:00
|
|
|
int new_fd;
|
|
|
|
|
2012-09-13 00:21:14 +04:00
|
|
|
if (fle != file_master(d))
|
2012-09-13 00:08:39 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
new_fd = d->ops->open(d);
|
|
|
|
if (new_fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
if (reopen_fd_as(fle->fe->fd, new_fd))
|
2012-09-13 00:08:39 +04:00
|
|
|
return -1;
|
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
fcntl(fle->fe->fd, F_SETFD, fle->fe->flags);
|
2012-09-13 00:08:39 +04:00
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
return serve_out_fd(pid, fle->fe->fd, d);
|
2012-09-13 00:08:39 +04:00
|
|
|
}
|
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
static int receive_fd(int pid, struct fdinfo_list_entry *fle)
|
2012-01-11 15:45:00 +04:00
|
|
|
{
|
2012-01-16 19:18:31 +04:00
|
|
|
int tmp;
|
2012-09-13 00:17:57 +04:00
|
|
|
struct fdinfo_list_entry *flem;
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
flem = file_master(fle->desc);
|
|
|
|
if (flem->pid == pid)
|
2012-01-11 15:45:00 +04:00
|
|
|
return 0;
|
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
pr_info("\tReceive fd for %d\n", fle->fe->fd);
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
tmp = recv_fd(fle->fe->fd);
|
2012-01-11 15:45:00 +04:00
|
|
|
if (tmp < 0) {
|
2012-02-01 13:00:49 +03:00
|
|
|
pr_err("Can't get fd %d\n", tmp);
|
2012-01-11 15:45:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-09-13 00:17:57 +04:00
|
|
|
close(fle->fe->fd);
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
if (reopen_fd_as(fle->fe->fd, tmp) < 0)
|
2012-04-10 18:36:59 +04:00
|
|
|
return -1;
|
|
|
|
|
2012-09-13 00:17:57 +04:00
|
|
|
fcntl(tmp, F_SETFD, fle->fe->flags);
|
2012-04-10 18:36:59 +04:00
|
|
|
return 0;
|
2012-01-10 18:03:00 +04:00
|
|
|
}
|
|
|
|
|
2012-09-13 00:34:37 +04:00
|
|
|
struct fd_open_state {
|
|
|
|
char *name;
|
|
|
|
int (*cb)(int, struct fdinfo_list_entry *);
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct fd_open_state states[] = {
|
|
|
|
{ "prepare", open_transport_fd, },
|
|
|
|
{ "create", open_fd, },
|
|
|
|
{ "receive", receive_fd, },
|
|
|
|
{ "post_create", post_open_fd, },
|
2012-06-19 12:30:00 +04:00
|
|
|
};
|
|
|
|
|
2012-06-03 22:56:02 +04:00
|
|
|
static int open_fdinfo(int pid, struct fdinfo_list_entry *fle, int state)
|
2012-01-10 18:03:00 +04:00
|
|
|
{
|
2012-06-03 22:56:02 +04:00
|
|
|
pr_info("\tRestoring fd %d (state -> %s)\n",
|
2012-09-13 00:34:37 +04:00
|
|
|
fle->fe->fd, states[state].name);
|
|
|
|
return states[state].cb(pid, fle);
|
2012-01-11 15:45:00 +04:00
|
|
|
}
|
|
|
|
|
2012-09-11 21:47:14 +04:00
|
|
|
static int open_fdinfos(int pid, struct list_head *list, int state)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct fdinfo_list_entry *fle;
|
|
|
|
|
|
|
|
list_for_each_entry(fle, list, ps_list) {
|
|
|
|
ret = open_fdinfo(pid, fle, state);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-11 18:16:24 +04:00
|
|
|
int close_old_fds(struct pstree_item *me)
|
2012-06-27 20:57:42 +04:00
|
|
|
{
|
2012-09-29 11:01:53 +04:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *de;
|
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
dir = opendir_proc(getpid(), "fd");
|
|
|
|
if (dir == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while ((de = readdir(dir))) {
|
2012-11-29 21:12:51 +03:00
|
|
|
if (dir_dots(de))
|
2012-09-29 11:01:53 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = sscanf(de->d_name, "%d", &fd);
|
|
|
|
if (ret != 1) {
|
|
|
|
pr_err("Can't parse %s\n", de->d_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!is_any_service_fd(fd)) && (dirfd(dir) != fd))
|
|
|
|
close_safe(&fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
close_pid_proc();
|
2012-06-27 20:57:42 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 16:27:46 +04:00
|
|
|
int prepare_fds(struct pstree_item *me)
|
2012-01-11 15:45:00 +04:00
|
|
|
{
|
2013-01-12 00:44:26 +04:00
|
|
|
u32 ret = 0;
|
2012-01-11 15:45:00 +04:00
|
|
|
int state;
|
|
|
|
|
2012-05-02 14:42:00 +04:00
|
|
|
pr_info("Opening fdinfo-s\n");
|
2012-01-11 15:45:00 +04:00
|
|
|
|
2013-01-12 00:44:26 +04:00
|
|
|
if (me->rst->fdt) {
|
|
|
|
struct fdt *fdt = me->rst->fdt;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait all tasks, who share a current fd table.
|
|
|
|
* We should be sure, that nobody use any file
|
|
|
|
* descriptor while fdtable is being restored.
|
|
|
|
*/
|
|
|
|
futex_inc_and_wake(&fdt->fdt_lock);
|
|
|
|
futex_wait_while_lt(&fdt->fdt_lock, fdt->nr);
|
|
|
|
|
|
|
|
if (fdt->pid != me->pid.virt) {
|
|
|
|
pr_info("File descriptor talbe is shared with %d\n", fdt->pid);
|
|
|
|
futex_wait_until(&fdt->fdt_lock, fdt->nr + 1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-13 00:34:37 +04:00
|
|
|
for (state = 0; state < ARRAY_SIZE(states); state++) {
|
2012-09-11 21:47:14 +04:00
|
|
|
ret = open_fdinfos(me->pid.virt, &me->rst->fds, state);
|
|
|
|
if (ret)
|
|
|
|
break;
|
2012-03-23 11:15:58 +03:00
|
|
|
|
2012-09-12 20:00:52 +04:00
|
|
|
/*
|
|
|
|
* Now handle TTYs. Slaves are delayed to be sure masters
|
|
|
|
* are already opened.
|
|
|
|
*/
|
|
|
|
ret = open_fdinfos(me->pid.virt, &me->rst->tty_slaves, state);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
2012-05-04 16:31:00 +04:00
|
|
|
/*
|
|
|
|
* The eventpoll descriptors require all the other ones
|
|
|
|
* to be already restored, thus we store them in a separate
|
|
|
|
* list and restore at the very end.
|
|
|
|
*/
|
2012-09-11 21:47:14 +04:00
|
|
|
ret = open_fdinfos(me->pid.virt, &me->rst->eventpoll, state);
|
|
|
|
if (ret)
|
|
|
|
break;
|
2012-05-04 16:31:00 +04:00
|
|
|
}
|
2012-09-11 21:47:14 +04:00
|
|
|
|
2013-01-12 00:44:26 +04:00
|
|
|
if (me->rst->fdt)
|
|
|
|
futex_inc_and_wake(&me->rst->fdt->fdt_lock);
|
|
|
|
out:
|
2012-10-18 15:51:56 +04:00
|
|
|
tty_fini_fds();
|
2012-04-10 18:47:00 +04:00
|
|
|
return ret;
|
2012-01-10 18:03:00 +04:00
|
|
|
}
|
|
|
|
|
2012-04-09 13:41:05 +04:00
|
|
|
int prepare_fs(int pid)
|
|
|
|
{
|
2012-07-17 07:28:38 +04:00
|
|
|
int ifd, cwd, ret = -1;
|
|
|
|
FsEntry *fe;
|
2012-04-09 13:41:05 +04:00
|
|
|
|
|
|
|
ifd = open_image_ro(CR_FD_FS, pid);
|
|
|
|
if (ifd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-10-24 16:51:50 +04:00
|
|
|
if (pb_read_one(ifd, &fe, PB_FS) < 0) {
|
|
|
|
close_safe(&ifd);
|
2012-04-09 13:41:05 +04:00
|
|
|
return -1;
|
2012-10-24 16:51:50 +04:00
|
|
|
}
|
2012-04-09 13:41:05 +04:00
|
|
|
|
2012-07-17 07:28:38 +04:00
|
|
|
cwd = open_reg_by_id(fe->cwd_id);
|
2012-10-24 16:51:50 +04:00
|
|
|
if (cwd < 0) {
|
|
|
|
close_safe(&ifd);
|
2012-07-17 07:28:38 +04:00
|
|
|
goto err;
|
2012-10-24 16:51:50 +04:00
|
|
|
}
|
2012-04-09 13:41:05 +04:00
|
|
|
|
|
|
|
if (fchdir(cwd) < 0) {
|
|
|
|
pr_perror("Can't change root");
|
2012-10-24 16:51:50 +04:00
|
|
|
goto close;
|
2012-04-09 13:41:05 +04:00
|
|
|
}
|
|
|
|
|
2012-04-09 13:52:42 +04:00
|
|
|
/*
|
|
|
|
* FIXME: restore task's root. Don't want to do it now, since
|
|
|
|
* it's not yet clean how we're going to resolve tasks' paths
|
|
|
|
* relative to the dumper/restorer and all this logic is likely
|
|
|
|
* to be hidden in a couple of calls (open_fe_fd is one od them)
|
|
|
|
* but for chroot there's no fchroot call, we have to chroot
|
|
|
|
* by path thus exposing this (yet unclean) logic here.
|
|
|
|
*/
|
|
|
|
|
2013-01-10 12:48:31 +03:00
|
|
|
if (fe->has_umask) {
|
|
|
|
pr_info("Restoring umask to %o\n", fe->umask);
|
|
|
|
umask(fe->umask);
|
|
|
|
}
|
|
|
|
|
2012-07-17 07:28:38 +04:00
|
|
|
ret = 0;
|
2012-10-24 16:51:50 +04:00
|
|
|
close:
|
|
|
|
close_safe(&cwd);
|
|
|
|
close_safe(&ifd);
|
2012-07-17 07:28:38 +04:00
|
|
|
err:
|
|
|
|
fs_entry__free_unpacked(fe, NULL);
|
|
|
|
return ret;
|
2012-04-09 13:41:05 +04:00
|
|
|
}
|
|
|
|
|
2012-07-19 12:43:36 +04:00
|
|
|
int get_filemap_fd(int pid, VmaEntry *vma_entry)
|
2012-01-10 18:03:00 +04:00
|
|
|
{
|
2012-04-09 15:57:05 +04:00
|
|
|
return open_reg_by_id(vma_entry->shmid);
|
2012-01-10 18:03:00 +04:00
|
|
|
}
|
2013-01-19 00:30:42 +04:00
|
|
|
|
|
|
|
int shared_fdt_prepare(struct pstree_item *item)
|
|
|
|
{
|
|
|
|
struct pstree_item *parent = item->parent;
|
|
|
|
struct fdt *fdt;
|
|
|
|
|
|
|
|
if (!parent->rst->fdt) {
|
|
|
|
fdt = shmalloc(sizeof(*item->rst->fdt));
|
|
|
|
if (fdt == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
parent->rst->fdt = fdt;
|
|
|
|
|
|
|
|
futex_init(&fdt->fdt_lock);
|
|
|
|
fdt->nr = 1;
|
|
|
|
fdt->pid = parent->pid.virt;
|
|
|
|
} else
|
|
|
|
fdt = parent->rst->fdt;
|
|
|
|
|
|
|
|
item->rst->fdt = fdt;
|
|
|
|
item->rst->service_fd_id = fdt->nr;
|
|
|
|
fdt->nr++;
|
|
|
|
if (fdt->pid > item->pid.virt)
|
|
|
|
fdt->pid = item->pid.virt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|