2011-09-23 12:00:45 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
#include <sys/wait.h>
|
2011-12-01 18:21:17 +04:00
|
|
|
#include <sys/file.h>
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
#include "image.h"
|
|
|
|
#include "util.h"
|
2011-12-19 18:52:50 +04:00
|
|
|
#include "log.h"
|
2011-10-26 17:35:50 +04:00
|
|
|
#include "syscall.h"
|
2011-10-24 22:23:06 +04:00
|
|
|
#include "restorer.h"
|
2011-12-26 22:12:03 +04:00
|
|
|
#include "sockets.h"
|
2011-12-26 20:33:09 +04:00
|
|
|
#include "lock.h"
|
2012-01-10 18:03:00 +04:00
|
|
|
#include "files.h"
|
2012-01-13 20:52:35 +04:00
|
|
|
#include "proc_parse.h"
|
2011-09-23 12:00:45 +04:00
|
|
|
#include "crtools.h"
|
|
|
|
|
2011-10-13 17:31:52 +04:00
|
|
|
/*
|
2011-10-13 18:15:09 +04:00
|
|
|
* real_pid member formerly served cases when
|
|
|
|
* no fork-with-pid functionality were in kernel,
|
|
|
|
* so now it is being kept here just in case if
|
|
|
|
* we need it again.
|
2011-10-13 17:31:52 +04:00
|
|
|
*/
|
|
|
|
|
2011-09-30 09:00:45 +04:00
|
|
|
#define PIPE_NONE (0 << 0)
|
|
|
|
#define PIPE_RDONLY (1 << 1)
|
|
|
|
#define PIPE_WRONLY (1 << 2)
|
|
|
|
#define PIPE_RDWR (PIPE_RDONLY | PIPE_WRONLY)
|
|
|
|
#define PIPE_MODE_MASK (0x7)
|
|
|
|
#define PIPE_CREATED (1 << 3)
|
|
|
|
|
|
|
|
#define pipe_is_rw(p) (((p)->status & PIPE_MODE_MASK) == PIPE_RDWR)
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
struct pipe_info {
|
2011-09-29 00:50:09 +04:00
|
|
|
unsigned int pipeid;
|
2011-09-23 12:00:45 +04:00
|
|
|
int pid;
|
2011-12-26 21:00:13 +04:00
|
|
|
u32 real_pid; /* futex */
|
2011-09-23 12:00:45 +04:00
|
|
|
int read_fd;
|
|
|
|
int write_fd;
|
2011-09-30 09:00:45 +04:00
|
|
|
int status;
|
2011-12-26 21:00:13 +04:00
|
|
|
u32 users; /* futex */
|
2011-09-23 12:00:45 +04:00
|
|
|
};
|
|
|
|
|
2011-09-28 14:45:30 +04:00
|
|
|
struct shmem_id {
|
|
|
|
struct shmem_id *next;
|
|
|
|
unsigned long addr;
|
|
|
|
unsigned long end;
|
2011-09-28 15:55:12 +04:00
|
|
|
unsigned long shmid;
|
2011-09-28 14:45:30 +04:00
|
|
|
};
|
|
|
|
|
2011-12-09 15:39:08 +04:00
|
|
|
struct pipe_list_entry {
|
|
|
|
struct list_head list;
|
|
|
|
struct pipe_entry e;
|
|
|
|
off_t offset;
|
|
|
|
};
|
|
|
|
|
2011-09-28 14:45:30 +04:00
|
|
|
static struct shmem_id *shmem_ids;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
static struct shmems *shmems;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
static struct pipe_info *pipes;
|
|
|
|
static int nr_pipes;
|
|
|
|
|
2011-11-13 12:57:16 +04:00
|
|
|
static pid_t pstree_pid;
|
|
|
|
|
2012-01-15 19:17:00 +04:00
|
|
|
static int restore_task_with_children(int my_pid);
|
2011-11-13 12:57:16 +04:00
|
|
|
static void sigreturn_restore(pid_t pstree_pid, pid_t pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
static void show_saved_shmems(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pr_info("\tSaved shmems:\n");
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
for (i = 0; i < shmems->nr_shmems; i++)
|
2011-09-28 15:55:12 +04:00
|
|
|
pr_info("\t\tstart: %016lx shmid: %lx pid: %d\n",
|
2011-12-26 21:15:30 +04:00
|
|
|
shmems->entries[i].start,
|
|
|
|
shmems->entries[i].shmid,
|
|
|
|
shmems->entries[i].pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_saved_pipes(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pr_info("\tSaved pipes:\n");
|
|
|
|
for (i = 0; i < nr_pipes; i++)
|
2011-09-30 09:00:45 +04:00
|
|
|
pr_info("\t\tpipeid %x pid %d users %d status %d\n",
|
|
|
|
pipes[i].pipeid, pipes[i].pid,
|
|
|
|
pipes[i].users, pipes[i].status);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 14:45:30 +04:00
|
|
|
static struct pipe_info *find_pipe(unsigned int pipeid)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
struct pipe_info *pi;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_pipes; i++) {
|
|
|
|
pi = pipes + i;
|
2011-09-29 00:50:09 +04:00
|
|
|
if (pi->pipeid == pipeid)
|
2011-09-23 12:00:45 +04:00
|
|
|
return pi;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void shmem_update_real_pid(int vpid, int rpid)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
for (i = 0; i < shmems->nr_shmems; i++)
|
|
|
|
if (shmems->entries[i].pid == vpid)
|
|
|
|
shmems->entries[i].real_pid = rpid;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int shmem_wait_and_open(struct shmem_info *si)
|
|
|
|
{
|
2011-12-26 23:50:45 +04:00
|
|
|
unsigned long time = 1;
|
2011-12-09 15:14:16 +04:00
|
|
|
char path[128];
|
2011-12-26 23:50:45 +04:00
|
|
|
int ret;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
sprintf(path, "/proc/%d/map_files/%lx-%lx",
|
|
|
|
si->real_pid, si->start, si->end);
|
|
|
|
|
2012-01-01 13:18:42 +04:00
|
|
|
pr_info("%d: Waiting for [%s] to appear\n", getpid(), path);
|
|
|
|
cr_wait_until(&si->lock, 1);
|
2011-12-26 23:50:45 +04:00
|
|
|
|
2012-01-02 00:57:33 +04:00
|
|
|
pr_info("%d: Opening shmem [%s] \n", getpid(), path);
|
2011-12-26 23:50:45 +04:00
|
|
|
ret = open(path, O_RDWR);
|
|
|
|
if (ret >= 0)
|
|
|
|
return ret;
|
2012-01-03 13:07:45 +04:00
|
|
|
else if (ret < 0)
|
2011-12-26 23:50:45 +04:00
|
|
|
pr_perror(" %d: Can't stat shmem at %s\n",
|
|
|
|
si->real_pid, path);
|
2012-01-03 13:07:45 +04:00
|
|
|
return ret;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 01:09:34 +04:00
|
|
|
static int collect_shmem(int pid, struct shmem_entry *e)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
int i;
|
2011-12-26 21:15:30 +04:00
|
|
|
struct shmem_info *entries = shmems->entries;
|
|
|
|
int nr_shmems = shmems->nr_shmems;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
for (i = 0; i < nr_shmems; i++) {
|
2011-12-26 21:15:30 +04:00
|
|
|
if (entries[i].start != e->start ||
|
|
|
|
entries[i].shmid != e->shmid)
|
2011-09-23 12:00:45 +04:00
|
|
|
continue;
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
if (entries[i].end != e->end) {
|
2011-09-30 14:37:12 +04:00
|
|
|
pr_err("Bogus shmem\n");
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 15:55:12 +04:00
|
|
|
/*
|
2011-11-14 17:23:23 +04:00
|
|
|
* Only the shared mapping with a lowest
|
2011-09-28 15:55:12 +04:00
|
|
|
* pid will be created in real, other processes
|
|
|
|
* will wait until the kernel propagate this mapping
|
|
|
|
* into /proc
|
|
|
|
*/
|
2011-12-26 21:15:30 +04:00
|
|
|
if (entries[i].pid > pid)
|
|
|
|
entries[i].pid = pid;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
if ((nr_shmems + 1) * sizeof(struct shmem_info) +
|
|
|
|
sizeof (struct shmems) >= SHMEMS_SIZE) {
|
2011-09-23 12:00:45 +04:00
|
|
|
pr_panic("OOM storing shmems\n");
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
memset(&shmems->entries[nr_shmems], 0, sizeof(shmems->entries[0]));
|
2011-09-30 01:13:16 +04:00
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
entries[nr_shmems].start = e->start;
|
|
|
|
entries[nr_shmems].end = e->end;
|
|
|
|
entries[nr_shmems].shmid = e->shmid;
|
|
|
|
entries[nr_shmems].pid = pid;
|
|
|
|
entries[nr_shmems].real_pid = 0;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-02 20:29:39 +04:00
|
|
|
cr_wait_init(&entries[nr_shmems].lock);
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
shmems->nr_shmems++;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-28 01:09:34 +04:00
|
|
|
static int collect_pipe(int pid, struct pipe_entry *e, int p_fd)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-09-28 01:09:34 +04:00
|
|
|
/*
|
|
|
|
* All pipes get collected into the one array,
|
|
|
|
* note the highest PID is the sign of which
|
|
|
|
* process pipe should be really created, all other
|
|
|
|
* processes (if they have pipes with pipeid matched)
|
|
|
|
* will be attached.
|
|
|
|
*/
|
2011-09-23 12:00:45 +04:00
|
|
|
for (i = 0; i < nr_pipes; i++) {
|
2011-09-29 00:50:09 +04:00
|
|
|
if (pipes[i].pipeid != e->pipeid)
|
2011-09-23 12:00:45 +04:00
|
|
|
continue;
|
|
|
|
|
2011-09-30 09:00:45 +04:00
|
|
|
if (pipes[i].pid > pid && !pipe_is_rw(&pipes[i])) {
|
2011-09-23 12:00:45 +04:00
|
|
|
pipes[i].pid = pid;
|
2011-12-08 16:27:00 +04:00
|
|
|
pipes[i].status = 0;
|
|
|
|
pipes[i].read_fd = -1;
|
|
|
|
pipes[i].write_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipes[i].pid == pid) {
|
2011-11-30 10:51:29 +03:00
|
|
|
switch (e->flags & O_ACCMODE) {
|
2011-09-30 09:00:45 +04:00
|
|
|
case O_RDONLY:
|
|
|
|
pipes[i].status |= PIPE_RDONLY;
|
2011-12-08 16:27:00 +04:00
|
|
|
pipes[i].read_fd = e->fd;
|
2011-09-30 09:00:45 +04:00
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
pipes[i].status |= PIPE_WRONLY;
|
2011-12-08 16:27:00 +04:00
|
|
|
pipes[i].write_fd = e->fd;
|
2011-09-30 09:00:45 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-12-08 16:27:00 +04:00
|
|
|
} else
|
|
|
|
pipes[i].users++;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nr_pipes + 1) * sizeof(struct pipe_info) >= 4096) {
|
2011-09-28 01:09:34 +04:00
|
|
|
pr_panic("OOM storing pipes\n");
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&pipes[nr_pipes], 0, sizeof(pipes[nr_pipes]));
|
|
|
|
|
2011-09-29 00:50:09 +04:00
|
|
|
pipes[nr_pipes].pipeid = e->pipeid;
|
2011-09-28 01:09:34 +04:00
|
|
|
pipes[nr_pipes].pid = pid;
|
2011-12-08 16:27:00 +04:00
|
|
|
pipes[nr_pipes].users = 0;
|
2011-12-16 15:32:00 +04:00
|
|
|
pipes[nr_pipes].read_fd = -1;
|
|
|
|
pipes[nr_pipes].write_fd = -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-11-30 10:51:29 +03:00
|
|
|
switch (e->flags & O_ACCMODE) {
|
2011-09-30 09:00:45 +04:00
|
|
|
case O_RDONLY:
|
|
|
|
pipes[nr_pipes].status = PIPE_RDONLY;
|
2011-12-08 16:27:00 +04:00
|
|
|
pipes[i].read_fd = e->fd;
|
2011-09-30 09:00:45 +04:00
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
pipes[nr_pipes].status = PIPE_WRONLY;
|
2011-12-08 16:27:00 +04:00
|
|
|
pipes[i].write_fd = e->fd;
|
2011-09-30 09:00:45 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
nr_pipes++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prepare_shmem_pid(int pid)
|
|
|
|
{
|
|
|
|
int sh_fd;
|
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
sh_fd = open_image_ro(CR_FD_SHMEM, pid);
|
|
|
|
if (sh_fd < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct shmem_entry e;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read(sh_fd, &e, sizeof(e));
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ret != sizeof(e)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't read shmem entry\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 01:09:34 +04:00
|
|
|
if (collect_shmem(pid, &e))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
close(sh_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prepare_pipes_pid(int pid)
|
|
|
|
{
|
|
|
|
int p_fd;
|
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
p_fd = open_image_ro(CR_FD_PIPES, pid);
|
|
|
|
if (p_fd < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct pipe_entry e;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read(p_fd, &e, sizeof(e));
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
if (ret != sizeof(e)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Read pipes failed %d (expected %li)\n",
|
|
|
|
pid, ret, sizeof(e));
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 01:09:34 +04:00
|
|
|
if (collect_pipe(pid, &e, p_fd))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
if (e.bytes)
|
|
|
|
lseek(p_fd, e.bytes, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(p_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-13 14:02:38 +04:00
|
|
|
static int shmem_remap(struct shmems *old_addr, struct shmems *new_addr)
|
2011-12-26 21:27:03 +04:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
int fd;
|
|
|
|
void *ret;
|
|
|
|
|
2012-01-13 14:02:38 +04:00
|
|
|
sprintf(path, "/proc/self/map_files/%p-%p",
|
|
|
|
old_addr, (void *)old_addr + SHMEMS_SIZE);
|
2011-12-26 21:27:03 +04:00
|
|
|
|
|
|
|
fd = open(path, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_perror("open(%s) failed\n", path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-02 20:23:35 +04:00
|
|
|
ret = mmap(new_addr, SHMEMS_SIZE, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED | MAP_FIXED, fd, 0);
|
2011-12-26 21:27:03 +04:00
|
|
|
if (ret != new_addr) {
|
|
|
|
pr_perror("mmap failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-16 00:55:28 +04:00
|
|
|
if (new_addr->nr_shmems != old_addr->nr_shmems) {
|
2011-12-26 21:27:03 +04:00
|
|
|
pr_err("shmem_remap failed\n");
|
2012-01-16 00:55:28 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2011-12-26 21:27:03 +04:00
|
|
|
|
2012-01-16 00:55:28 +04:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
2011-12-26 21:27:03 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
static int prepare_shared(int ps_fd)
|
|
|
|
{
|
|
|
|
pr_info("Preparing info about shared resources\n");
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
shmems = mmap(NULL, SHMEMS_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, 0, 0);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (shmems == MAP_FAILED) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("Can't map shmem\n");
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
shmems->nr_shmems = 0;
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
pipes = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, 0, 0);
|
|
|
|
if (pipes == MAP_FAILED) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("Can't map pipes\n");
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2012-01-11 15:45:00 +04:00
|
|
|
if (prepare_fdinfo_global())
|
|
|
|
return -1;
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
while (1) {
|
|
|
|
struct pstree_entry e;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read(ps_fd, &e, sizeof(e));
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ret != sizeof(e)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("Can't read pstree_entry\n");
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prepare_shmem_pid(e.pid))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
if (prepare_pipes_pid(e.pid))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-11 15:45:00 +04:00
|
|
|
if (prepare_fd_pid(e.pid))
|
|
|
|
return -1;
|
|
|
|
|
2011-11-07 20:18:38 +04:00
|
|
|
lseek(ps_fd, e.nr_children * sizeof(u32) + e.nr_threads * sizeof(u32), SEEK_CUR);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
lseek(ps_fd, sizeof(u32), SEEK_SET);
|
|
|
|
|
|
|
|
show_saved_shmems();
|
|
|
|
show_saved_pipes();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long find_shmem_id(unsigned long addr)
|
|
|
|
{
|
2011-09-28 14:45:30 +04:00
|
|
|
struct shmem_id *si;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-09-28 14:45:30 +04:00
|
|
|
for (si = shmem_ids; si; si = si->next)
|
2011-09-23 12:00:45 +04:00
|
|
|
if (si->addr <= addr && si->end >= addr)
|
2011-09-28 15:55:12 +04:00
|
|
|
return si->shmid;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void save_shmem_id(struct shmem_entry *e)
|
|
|
|
{
|
2011-09-28 14:45:30 +04:00
|
|
|
struct shmem_id *si;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
si = malloc(sizeof(*si));
|
|
|
|
si->addr = e->start;
|
|
|
|
si->end = e->end;
|
2011-09-28 15:55:12 +04:00
|
|
|
si->shmid = e->shmid;
|
2011-09-28 14:45:30 +04:00
|
|
|
si->next = shmem_ids;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-09-28 14:45:30 +04:00
|
|
|
shmem_ids = si;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int prepare_shmem(int pid)
|
|
|
|
{
|
|
|
|
int sh_fd;
|
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
sh_fd = open_image_ro(CR_FD_SHMEM, pid);
|
|
|
|
if (sh_fd < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct shmem_entry e;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read(sh_fd, &e, sizeof(e));
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
if (ret != sizeof(e)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't read shmem entry\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
save_shmem_id(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sh_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-04 18:31:48 +04:00
|
|
|
static struct shmem_info *
|
|
|
|
find_shmem(struct shmems *shms, unsigned long start, unsigned long shmid)
|
|
|
|
{
|
|
|
|
struct shmem_info *si;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < shms->nr_shmems; i++) {
|
|
|
|
si = &shms->entries[i];
|
|
|
|
if (si->start == start &&
|
|
|
|
si->end > start &&
|
|
|
|
si->shmid == shmid)
|
|
|
|
return si;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct shmem_info *
|
|
|
|
find_shmem_page(struct shmems *shms, unsigned long addr, unsigned long shmid)
|
|
|
|
{
|
|
|
|
struct shmem_info *si;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < shms->nr_shmems; i++) {
|
|
|
|
si = &shms->entries[i];
|
|
|
|
if (si->start <= addr &&
|
|
|
|
si->end > addr &&
|
|
|
|
si->shmid == shmid)
|
|
|
|
return si;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
static int try_fixup_shared_map(int pid, struct vma_entry *vi, int fd)
|
|
|
|
{
|
|
|
|
struct shmem_info *si;
|
2011-09-28 15:55:12 +04:00
|
|
|
unsigned long shmid;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-09-28 15:55:12 +04:00
|
|
|
shmid = find_shmem_id(vi->start);
|
|
|
|
if (!shmid)
|
2011-09-23 12:00:45 +04:00
|
|
|
return 0;
|
|
|
|
|
2011-12-26 21:15:30 +04:00
|
|
|
si = find_shmem(shmems, vi->start, shmid);
|
2011-09-23 12:00:45 +04:00
|
|
|
pr_info("%d: Search for %016lx shmem %p/%d\n", pid, vi->start, si, si ? si->pid : -1);
|
|
|
|
|
2011-09-28 15:55:12 +04:00
|
|
|
if (!si) {
|
2011-09-30 14:37:12 +04:00
|
|
|
pr_err("Can't find my shmem %016lx\n", vi->start);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (si->pid != pid) {
|
|
|
|
int sh_fd;
|
|
|
|
|
|
|
|
sh_fd = shmem_wait_and_open(si);
|
2011-09-27 01:14:38 +04:00
|
|
|
pr_info("%d: Fixing %lx vma to %lx/%d shmem -> %d\n",
|
2011-09-28 15:55:12 +04:00
|
|
|
pid, vi->start, si->shmid, si->pid, sh_fd);
|
2011-11-14 17:23:23 +04:00
|
|
|
if (sh_fd < 0) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't open shmem\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
lseek(fd, -sizeof(*vi), SEEK_CUR);
|
|
|
|
vi->fd = sh_fd;
|
2011-11-14 17:23:23 +04:00
|
|
|
pr_info("%d: Fixed %lx vma %lx/%d shmem -> %d\n",
|
|
|
|
pid, vi->start, si->shmid, si->pid, sh_fd);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (write(fd, vi, sizeof(*vi)) != sizeof(*vi)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't write img\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fixup_vma_fds(int pid, int fd)
|
|
|
|
{
|
2011-09-26 18:14:39 +04:00
|
|
|
int offset = GET_FILE_OFF_AFTER(struct core_entry);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct vma_entry vi;
|
2011-11-23 01:18:01 +04:00
|
|
|
int ret = 0;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-11-23 01:18:01 +04:00
|
|
|
ret = read(fd, &vi, sizeof(vi));
|
|
|
|
if (ret < 0) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't read vma_entry\n", pid);
|
2011-11-23 01:18:01 +04:00
|
|
|
} else if (ret != sizeof(vi)) {
|
|
|
|
pr_err("%d: Incomplete vma_entry (%d != %d)\n",
|
|
|
|
pid, ret, sizeof(vi));
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-11-15 17:12:29 +04:00
|
|
|
if (final_vma_entry(&vi))
|
2011-09-23 12:00:45 +04:00
|
|
|
return 0;
|
|
|
|
|
2011-11-15 17:12:29 +04:00
|
|
|
if (!(vma_entry_is(&vi, VMA_AREA_REGULAR)))
|
2011-09-23 12:00:45 +04:00
|
|
|
continue;
|
|
|
|
|
2011-11-15 17:12:29 +04:00
|
|
|
if (vma_entry_is(&vi, VMA_FILE_PRIVATE) ||
|
|
|
|
vma_entry_is(&vi, VMA_FILE_SHARED) ||
|
|
|
|
vma_entry_is(&vi, VMA_ANON_SHARED)) {
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-09-27 01:14:38 +04:00
|
|
|
pr_info("%d: Fixing %016lx-%016lx %016lx vma\n",
|
|
|
|
pid, vi.start, vi.end, vi.pgoff);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (try_fixup_file_map(pid, &vi, fd))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
if (try_fixup_shared_map(pid, &vi, fd))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 18:44:46 +04:00
|
|
|
static inline bool should_restore_page(int pid, unsigned long va)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
struct shmem_info *si;
|
2011-09-28 18:44:46 +04:00
|
|
|
unsigned long shmid;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-09-28 18:44:46 +04:00
|
|
|
/*
|
|
|
|
* If this is not a shmem virtual address
|
|
|
|
* we should restore such page.
|
|
|
|
*/
|
|
|
|
shmid = find_shmem_id(va);
|
|
|
|
if (!shmid)
|
|
|
|
return true;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-04 18:31:48 +04:00
|
|
|
si = find_shmem_page(shmems, va, shmid);
|
2011-09-23 12:00:45 +04:00
|
|
|
return si->pid == pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char zpage[PAGE_SIZE];
|
|
|
|
|
|
|
|
static int fixup_pages_data(int pid, int fd)
|
|
|
|
{
|
|
|
|
int shfd;
|
2011-09-28 21:20:53 +04:00
|
|
|
u64 va;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-10-12 18:34:15 +04:00
|
|
|
pr_info("%d: Reading shmem pages img\n", pid);
|
2011-09-29 01:24:23 +04:00
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
shfd = open_image_ro(CR_FD_PAGES_SHMEM, pid);
|
|
|
|
if (shfd < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-09-28 21:20:53 +04:00
|
|
|
/*
|
|
|
|
* Find out the last page, which must be a zero page.
|
|
|
|
*/
|
2011-09-23 12:00:45 +04:00
|
|
|
lseek(fd, -sizeof(struct page_entry), SEEK_END);
|
2011-09-28 21:20:53 +04:00
|
|
|
read(fd, &va, sizeof(va));
|
|
|
|
if (va) {
|
|
|
|
pr_panic("Zero-page expected but got %lx\n", (unsigned long)va);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
2011-09-28 21:20:53 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Since we're to update pages we suppress old zero-page
|
|
|
|
* and will write new one at the end.
|
|
|
|
*/
|
2011-09-23 12:00:45 +04:00
|
|
|
lseek(fd, -sizeof(struct page_entry), SEEK_END);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int ret;
|
|
|
|
|
2011-09-28 21:20:53 +04:00
|
|
|
ret = read(shfd, &va, sizeof(va));
|
2011-09-23 12:00:45 +04:00
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
2011-09-28 21:20:53 +04:00
|
|
|
if (ret < 0 || ret != sizeof(va)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't read virtual address\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:20:53 +04:00
|
|
|
if (va == 0)
|
2011-09-23 12:00:45 +04:00
|
|
|
break;
|
|
|
|
|
2011-09-28 21:20:53 +04:00
|
|
|
if (!should_restore_page(pid, va)) {
|
2011-09-23 12:00:45 +04:00
|
|
|
lseek(shfd, PAGE_SIZE, SEEK_CUR);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:20:53 +04:00
|
|
|
pr_info("%d: Restoring shared page: %16lx\n",
|
|
|
|
pid, va);
|
|
|
|
|
|
|
|
write(fd, &va, sizeof(va));
|
2011-09-23 12:00:45 +04:00
|
|
|
sendfile(fd, shfd, NULL, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(shfd);
|
2011-09-28 21:20:53 +04:00
|
|
|
va = 0;
|
|
|
|
write(fd, &va, sizeof(va));
|
2011-09-23 12:00:45 +04:00
|
|
|
write(fd, zpage, sizeof(zpage));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prepare_image_maps(int fd, int pid)
|
|
|
|
{
|
2011-11-21 20:38:35 +04:00
|
|
|
pr_info("%d: Fixing maps\n", pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
if (fixup_vma_fds(pid, fd))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
if (fixup_pages_data(pid, fd))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-14 17:23:23 +04:00
|
|
|
static int prepare_and_sigreturn(int pid)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
2011-12-18 12:15:30 +04:00
|
|
|
char path[PATH_MAX];
|
2011-09-23 12:00:45 +04:00
|
|
|
int fd, fd_new;
|
|
|
|
struct stat buf;
|
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
fd = open_image_ro_nocheck(FMT_FNAME_CORE, pid);
|
|
|
|
if (fd < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-12-29 19:56:34 +04:00
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
if (fstat(fd, &buf)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't stat\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-12-18 12:15:30 +04:00
|
|
|
if (get_image_path(path, sizeof(path), FMT_FNAME_CORE_OUT, pid))
|
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-16 00:52:56 +04:00
|
|
|
fd_new = open(path, O_RDWR | O_CREAT | O_TRUNC, CR_FD_PERM);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (fd_new < 0) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't open new image\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-11-13 13:07:00 +04:00
|
|
|
pr_info("%d: Preparing restore image %s (%li bytes)\n", pid, path, buf.st_size);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (sendfile(fd_new, fd, NULL, buf.st_size) != buf.st_size) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: sendfile failed\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (fstat(fd_new, &buf)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't stat\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("fd_new: %li bytes\n", buf.st_size);
|
|
|
|
|
|
|
|
if (prepare_image_maps(fd_new, pid))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
close(fd_new);
|
2011-11-13 13:07:00 +04:00
|
|
|
sigreturn_restore(pstree_pid, pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-11-13 13:07:00 +04:00
|
|
|
return 0;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-11-30 10:51:29 +03:00
|
|
|
#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
|
|
|
|
|
|
|
|
static int set_fd_flags(int fd, int flags)
|
|
|
|
{
|
|
|
|
int old;
|
|
|
|
|
|
|
|
old = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (old < 0)
|
|
|
|
return old;
|
|
|
|
|
|
|
|
flags = (SETFL_MASK & flags) | (old & ~SETFL_MASK);
|
|
|
|
|
|
|
|
return fcntl(fd, F_SETFL, flags);
|
|
|
|
}
|
|
|
|
|
2012-01-16 09:40:00 +04:00
|
|
|
static int reopen_pipe(int src, int *dst, int *other, int *pipes_fd)
|
2011-12-09 20:22:00 +04:00
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
if (*dst != -1) {
|
2011-12-26 21:48:00 +04:00
|
|
|
if (move_img_fd(other, *dst))
|
|
|
|
return -1;
|
2011-12-09 20:22:00 +04:00
|
|
|
|
2012-01-16 09:40:00 +04:00
|
|
|
if (move_img_fd(pipes_fd, *dst))
|
|
|
|
return -1;
|
|
|
|
|
2012-01-16 11:57:45 +04:00
|
|
|
return reopen_fd_as(*dst, src);
|
2011-12-26 21:47:00 +04:00
|
|
|
}
|
2011-12-09 20:22:00 +04:00
|
|
|
|
2011-12-26 21:47:00 +04:00
|
|
|
*dst = src;
|
2011-12-09 20:22:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-09 20:22:00 +04:00
|
|
|
static int restore_pipe_data(struct pipe_entry *e, int wfd, int pipes_fd)
|
|
|
|
{
|
|
|
|
int ret, size = 0;
|
|
|
|
|
2012-01-15 20:04:13 +04:00
|
|
|
pr_info("\t%x: Splicing data to %d\n", e->pipeid, wfd);
|
2011-12-09 20:22:00 +04:00
|
|
|
|
|
|
|
while (size != e->bytes) {
|
|
|
|
ret = splice(pipes_fd, NULL, wfd, NULL, e->bytes, 0);
|
|
|
|
if (ret < 0) {
|
2012-01-15 20:04:13 +04:00
|
|
|
pr_perror("\t%x: Error splicing data\n", e->pipeid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-12-09 20:22:00 +04:00
|
|
|
}
|
|
|
|
if (ret == 0) {
|
2012-01-15 20:04:13 +04:00
|
|
|
pr_err("\t%x: Wanted to restore %d bytes, but got %d\n",
|
2011-12-09 20:22:00 +04:00
|
|
|
e->pipeid, e->bytes, size);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-12-09 20:22:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size =+ ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-16 09:40:00 +04:00
|
|
|
static int create_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi, int *pipes_fd)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
unsigned long time = 1000;
|
2011-09-27 20:23:26 +04:00
|
|
|
int pfd[2], tmp;
|
2011-12-26 21:00:13 +04:00
|
|
|
u32 real_pid;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
pr_info("\t%d: Creating pipe %x%s\n", pid, e->pipeid, pipe_is_rw(pi) ? "(rw)" : "");
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
if (pipe(pfd) < 0) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't create pipe\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2012-01-16 09:40:00 +04:00
|
|
|
if (restore_pipe_data(e, pfd[1], *pipes_fd))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-16 09:40:00 +04:00
|
|
|
if (reopen_pipe(pfd[0], &pi->read_fd, &pfd[1], pipes_fd))
|
2011-12-09 20:22:00 +04:00
|
|
|
return -1;
|
2012-01-16 09:40:00 +04:00
|
|
|
if (reopen_pipe(pfd[1], &pi->write_fd, &pi->read_fd, pipes_fd))
|
2011-12-09 20:22:00 +04:00
|
|
|
return -1;
|
2011-12-08 16:27:00 +04:00
|
|
|
|
2011-12-26 21:00:13 +04:00
|
|
|
cr_wait_set(&pi->real_pid, getpid());
|
2011-09-30 09:00:45 +04:00
|
|
|
|
|
|
|
pi->status |= PIPE_CREATED;
|
|
|
|
|
2011-09-28 01:09:34 +04:00
|
|
|
pr_info("\t%d: Done, waiting for others (users %d) on %d pid with r:%d w:%d\n",
|
2011-12-08 16:27:00 +04:00
|
|
|
pid, pi->users, pi->real_pid, pi->read_fd, pi->write_fd);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-16 09:40:00 +04:00
|
|
|
if (!pipe_is_rw(pi)) {
|
|
|
|
pr_info("\t%d: Waiting for %x pipe to attach (%d users left)\n",
|
2011-12-08 16:27:00 +04:00
|
|
|
pid, e->pipeid, pi->users);
|
2012-01-16 09:40:00 +04:00
|
|
|
|
2011-12-26 21:00:13 +04:00
|
|
|
cr_wait_until(&pi->users, 0);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
if ((e->flags & O_ACCMODE) == O_WRONLY)
|
2011-09-30 09:00:45 +04:00
|
|
|
close_safe(&pi->read_fd);
|
2011-12-08 16:27:00 +04:00
|
|
|
else
|
2011-09-30 09:00:45 +04:00
|
|
|
close_safe(&pi->write_fd);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-12-09 20:22:00 +04:00
|
|
|
tmp = 0;
|
|
|
|
if (pi->write_fd != e->fd && pi->read_fd != e->fd) {
|
2012-01-16 09:40:00 +04:00
|
|
|
if (move_img_fd(pipes_fd, e->fd))
|
|
|
|
return -1;
|
|
|
|
|
2011-12-09 20:22:00 +04:00
|
|
|
switch (e->flags & O_ACCMODE) {
|
|
|
|
case O_WRONLY:
|
|
|
|
tmp = dup2(pi->write_fd, e->fd);
|
|
|
|
break;
|
|
|
|
case O_RDONLY:
|
|
|
|
tmp = dup2(pi->read_fd, e->fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tmp < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-12-09 20:22:00 +04:00
|
|
|
|
2011-11-30 10:51:29 +03:00
|
|
|
tmp = set_fd_flags(e->fd, e->flags);
|
2011-09-27 18:48:57 +04:00
|
|
|
if (tmp < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
pr_info("\t%d: All is ok - reopening pipe for %d\n", pid, e->fd);
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-16 09:40:00 +04:00
|
|
|
static int attach_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi, int *pipes_fd)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
char path[128];
|
|
|
|
int tmp, fd;
|
|
|
|
|
2011-09-28 01:09:34 +04:00
|
|
|
pr_info("\t%d: Wating for pipe %x to appear\n",
|
|
|
|
pid, e->pipeid);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-26 21:00:13 +04:00
|
|
|
cr_wait_while(&pi->real_pid, 0);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-16 09:40:00 +04:00
|
|
|
if (move_img_fd(pipes_fd, e->fd))
|
|
|
|
return -1;
|
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
if ((e->flags & O_ACCMODE) == O_WRONLY)
|
2011-09-23 12:00:45 +04:00
|
|
|
tmp = pi->write_fd;
|
|
|
|
else
|
|
|
|
tmp = pi->read_fd;
|
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
if (pid == pi->pid) {
|
|
|
|
if (tmp != e->fd)
|
|
|
|
tmp = dup2(tmp, e->fd);
|
|
|
|
|
|
|
|
if (tmp < 0) {
|
|
|
|
pr_perror("%d: Can't duplicate %d->%d\n",
|
|
|
|
pid, tmp, e->fd);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-12-08 16:27:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
goto out;
|
2011-09-27 20:23:26 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
sprintf(path, "/proc/%d/fd/%d", pi->real_pid, tmp);
|
2011-09-28 01:09:34 +04:00
|
|
|
pr_info("\t%d: Attaching pipe %s (%d users left)\n",
|
|
|
|
pid, path, pi->users - 1);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
fd = open(path, e->flags);
|
|
|
|
if (fd < 0) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't attach pipe\n", pid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("\t%d: Done, reopening for %d\n", pid, e->fd);
|
2012-01-16 11:57:45 +04:00
|
|
|
if (reopen_fd_as(e->fd, fd))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-11-22 13:12:30 +04:00
|
|
|
|
2011-12-26 21:00:13 +04:00
|
|
|
cr_wait_dec(&pi->users);
|
2011-12-08 16:27:00 +04:00
|
|
|
out:
|
2011-11-30 10:51:29 +03:00
|
|
|
tmp = set_fd_flags(e->fd, e->flags);
|
|
|
|
if (tmp < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-11-30 10:51:29 +03:00
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_pipe(int pid, struct pipe_entry *e, int *pipes_fd)
|
|
|
|
{
|
|
|
|
struct pipe_info *pi;
|
|
|
|
|
|
|
|
pr_info("\t%d: Opening pipe %x on fd %d\n", pid, e->pipeid, e->fd);
|
|
|
|
|
2011-09-28 14:45:30 +04:00
|
|
|
pi = find_pipe(e->pipeid);
|
2011-09-28 01:09:34 +04:00
|
|
|
if (!pi) {
|
2011-09-30 14:37:12 +04:00
|
|
|
pr_err("BUG: can't find my pipe %x\n", e->pipeid);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 01:09:34 +04:00
|
|
|
/*
|
2011-09-30 09:00:45 +04:00
|
|
|
* This is somewhat tricky -- in case if a process uses
|
|
|
|
* both pipe ends the pipe should be created but only one
|
|
|
|
* pipe end get connected immediately in create_pipe the
|
|
|
|
* other pipe end should be connected via pipe attaching.
|
2011-09-28 01:09:34 +04:00
|
|
|
*/
|
2011-09-30 09:00:45 +04:00
|
|
|
if (pi->pid == pid && !(pi->status & PIPE_CREATED))
|
2012-01-16 09:40:00 +04:00
|
|
|
return create_pipe(pid, e, pi, pipes_fd);
|
2011-09-23 12:00:45 +04:00
|
|
|
else
|
2012-01-16 09:40:00 +04:00
|
|
|
return attach_pipe(pid, e, pi, pipes_fd);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-11-29 15:12:25 +03:00
|
|
|
static int prepare_sigactions(int pid)
|
|
|
|
{
|
2011-12-03 17:24:05 +04:00
|
|
|
rt_sigaction_t act, oact;
|
2011-11-29 15:12:25 +03:00
|
|
|
int fd_sigact, ret;
|
2011-12-02 23:17:30 +04:00
|
|
|
struct sa_entry e;
|
2011-12-03 17:24:05 +04:00
|
|
|
int sig, i;
|
2011-11-29 15:12:25 +03:00
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
fd_sigact = open_image_ro(CR_FD_SIGACT, pid);
|
|
|
|
if (fd_sigact < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-12-01 17:15:00 +04:00
|
|
|
|
2011-11-29 15:12:25 +03:00
|
|
|
for (sig = 1; sig < SIGMAX; sig++) {
|
|
|
|
if (sig == SIGKILL || sig == SIGSTOP)
|
|
|
|
continue;
|
|
|
|
|
2011-12-02 23:17:30 +04:00
|
|
|
ret = read(fd_sigact, &e, sizeof(e));
|
|
|
|
if (ret != sizeof(e)) {
|
2011-11-29 15:12:25 +03:00
|
|
|
pr_err("%d: Bad sigaction entry: %d (%m)\n", pid, ret);
|
|
|
|
ret = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2011-12-03 17:24:05 +04:00
|
|
|
ASSIGN_TYPED(act.rt_sa_handler, e.sigaction);
|
|
|
|
ASSIGN_TYPED(act.rt_sa_flags, e.flags);
|
|
|
|
ASSIGN_TYPED(act.rt_sa_restorer, e.restorer);
|
2011-12-08 19:04:07 +04:00
|
|
|
ASSIGN_TYPED(act.rt_sa_mask.sig[0], e.mask);
|
2011-12-02 23:17:30 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A pure syscall is used, because glibc
|
|
|
|
* sigaction overwrites se_restorer.
|
|
|
|
*/
|
2011-11-29 15:12:25 +03:00
|
|
|
ret = sys_sigaction(sig, &act, &oact);
|
|
|
|
if (ret == -1) {
|
|
|
|
pr_err("%d: Can't restore sigaction: %m\n", pid);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
close(fd_sigact);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
static int prepare_pipes(int pid)
|
|
|
|
{
|
2012-01-10 20:02:00 +04:00
|
|
|
u32 err = -1, ret;
|
2011-09-23 12:00:45 +04:00
|
|
|
int pipes_fd;
|
2011-12-08 16:27:00 +04:00
|
|
|
|
|
|
|
struct pipe_list_entry *le, *buf;
|
|
|
|
int buf_size = PAGE_SIZE;
|
|
|
|
int nr = 0;
|
|
|
|
|
|
|
|
LIST_HEAD(head);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
pr_info("%d: Opening pipes\n", pid);
|
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
pipes_fd = open_image_ro(CR_FD_PIPES, pid);
|
|
|
|
if (pipes_fd < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
buf = malloc(buf_size);
|
|
|
|
if (!buf) {
|
|
|
|
pr_perror("Can't allocate memory\n");
|
|
|
|
close(pipes_fd);
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-12-08 16:27:00 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
while (1) {
|
2011-12-08 16:27:00 +04:00
|
|
|
struct list_head *cur;
|
|
|
|
struct pipe_list_entry *cur_entry;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
le = &buf[nr];
|
|
|
|
|
|
|
|
ret = read(pipes_fd, &le->e, sizeof(le->e));
|
|
|
|
if (ret == 0)
|
2011-09-30 09:00:45 +04:00
|
|
|
break;
|
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
if (ret != sizeof(le->e)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Bad pipes entry\n", pid);
|
2011-12-08 16:27:00 +04:00
|
|
|
goto err_free;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
list_for_each(cur, &head) {
|
|
|
|
cur_entry = list_entry(cur, struct pipe_list_entry, list);
|
|
|
|
if (cur_entry->e.pipeid > le->e.pipeid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add_tail(&le->list, cur);
|
|
|
|
|
|
|
|
le->offset = lseek(pipes_fd, 0, SEEK_CUR);
|
|
|
|
lseek(pipes_fd, le->e.bytes, SEEK_CUR);
|
|
|
|
|
|
|
|
nr++;
|
|
|
|
if (nr > buf_size / sizeof(*le)) {
|
|
|
|
pr_err("OOM storing pipes");
|
|
|
|
goto err_free;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
2011-09-30 09:00:45 +04:00
|
|
|
|
2011-12-08 16:27:00 +04:00
|
|
|
list_for_each_entry(le, &head, list) {
|
|
|
|
lseek(pipes_fd, le->offset, SEEK_SET);
|
|
|
|
if (open_pipe(pid, &le->e, &pipes_fd))
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
2011-12-09 20:23:00 +04:00
|
|
|
err = 0;
|
2011-12-08 16:27:00 +04:00
|
|
|
err_free:
|
|
|
|
free(buf);
|
|
|
|
close(pipes_fd);
|
2011-12-09 20:23:00 +04:00
|
|
|
return err;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int restore_one_task(int pid)
|
|
|
|
{
|
|
|
|
pr_info("%d: Restoring resources\n", pid);
|
|
|
|
|
|
|
|
if (prepare_pipes(pid))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-26 22:12:03 +04:00
|
|
|
if (prepare_sockets(pid))
|
|
|
|
return -1;
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
if (prepare_fds(pid))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
if (prepare_shmem(pid))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-11-29 15:12:25 +03:00
|
|
|
if (prepare_sigactions(pid))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-11-29 15:12:25 +03:00
|
|
|
|
2011-11-14 17:23:23 +04:00
|
|
|
return prepare_and_sigreturn(pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2012-01-15 19:17:00 +04:00
|
|
|
static inline int fork_with_pid(int pid)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
2011-12-01 18:21:17 +04:00
|
|
|
int ret = -1, fd = -1;
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%d", pid - 1);
|
|
|
|
|
|
|
|
fd = open(LAST_PID_PATH, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_perror("%d: Can't open %s\n", pid, LAST_PID_PATH);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flock(fd, LOCK_EX)) {
|
|
|
|
pr_perror("%d: Can't lock %s\n", pid, LAST_PID_PATH);
|
|
|
|
goto err;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-12-01 18:21:17 +04:00
|
|
|
write_safe(fd, buf, strlen(buf), err_unlock);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-01 18:21:17 +04:00
|
|
|
ret = fork();
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Can't fork for %d\n", pid);
|
|
|
|
goto err_unlock;
|
|
|
|
} else if (!ret) {
|
|
|
|
int my_pid = getpid();
|
|
|
|
|
|
|
|
close_safe(&fd);
|
|
|
|
|
|
|
|
if (my_pid != pid) {
|
|
|
|
pr_err("%d: Pids do not match got %d but expected %d\n",
|
|
|
|
my_pid, my_pid, pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-15 19:17:00 +04:00
|
|
|
ret = restore_task_with_children(my_pid);
|
2012-01-10 20:02:00 +04:00
|
|
|
pr_err("%d: Something failed with code %d\n", pid, ret);
|
2011-12-09 20:23:00 +04:00
|
|
|
exit(1);
|
2011-12-01 18:21:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
err_unlock:
|
|
|
|
if (flock(fd, LOCK_UN))
|
|
|
|
pr_perror("%d: Can't unlock %s\n", pid, LAST_PID_PATH);
|
|
|
|
|
|
|
|
err:
|
|
|
|
close_safe(&fd);
|
2011-09-23 12:00:45 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-15 19:17:00 +04:00
|
|
|
static int restore_task_with_children(int my_pid)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
int *pids;
|
|
|
|
int fd, ret, i;
|
|
|
|
struct pstree_entry e;
|
2011-12-02 16:06:00 +04:00
|
|
|
sigset_t blockmask;
|
|
|
|
|
|
|
|
/* The block mask will be restored in sigresturn
|
|
|
|
* This code should be removed, when a freezer will be added */
|
|
|
|
sigfillset(&blockmask);
|
|
|
|
ret = sigprocmask(SIG_BLOCK, &blockmask, NULL);
|
|
|
|
if (ret) {
|
|
|
|
pr_perror("%d: Can't block signals\n", my_pid);
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
pr_info("%d: Starting restore\n", my_pid);
|
|
|
|
|
2012-01-15 19:17:00 +04:00
|
|
|
fd = open_image_ro_nocheck(FMT_FNAME_PSTREE, pstree_pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (fd < 0) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't reopen pstree image\n", my_pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
lseek(fd, sizeof(u32), SEEK_SET);
|
|
|
|
while (1) {
|
|
|
|
ret = read(fd, &e, sizeof(e));
|
2011-11-07 20:18:38 +04:00
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
if (ret != sizeof(e)) {
|
2011-09-30 14:37:12 +04:00
|
|
|
pr_err("%d: Read returned %d\n", my_pid, ret);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (ret < 0)
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't read pstree\n", my_pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.pid != my_pid) {
|
2011-11-07 20:18:38 +04:00
|
|
|
lseek(fd, e.nr_children * sizeof(u32) + e.nr_threads * sizeof(u32), SEEK_CUR);
|
2011-09-23 12:00:45 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.nr_children > 0) {
|
|
|
|
i = e.nr_children * sizeof(int);
|
|
|
|
pids = malloc(i);
|
|
|
|
ret = read(fd, pids, i);
|
|
|
|
if (ret != i) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("%d: Can't read children pids\n", my_pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
pr_info("%d: Restoring %d children:\n", my_pid, e.nr_children);
|
|
|
|
for (i = 0; i < e.nr_children; i++) {
|
|
|
|
pr_info("\tFork %d from %d\n", pids[i], my_pid);
|
2012-01-15 19:17:00 +04:00
|
|
|
ret = fork_with_pid(pids[i]);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (ret < 0)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
shmem_update_real_pid(my_pid, getpid());
|
|
|
|
|
|
|
|
return restore_one_task(my_pid);
|
|
|
|
}
|
|
|
|
|
2012-01-15 19:17:00 +04:00
|
|
|
static int restore_root_task(int fd)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
struct pstree_entry e;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read(fd, &e, sizeof(e));
|
|
|
|
if (ret != sizeof(e)) {
|
2011-11-23 01:12:38 +04:00
|
|
|
pr_perror("Can't read root pstree entry\n");
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
pr_info("Forking root with %d pid\n", e.pid);
|
2012-01-15 19:17:00 +04:00
|
|
|
ret = fork_with_pid(e.pid);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (ret < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
wait(NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int restore_all_tasks(pid_t pid)
|
|
|
|
{
|
|
|
|
int pstree_fd;
|
|
|
|
u32 type = 0;
|
|
|
|
|
2012-01-15 19:17:00 +04:00
|
|
|
pstree_fd = open_image_ro(CR_FD_PSTREE, pstree_pid);
|
|
|
|
if (pstree_fd < 0)
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
if (prepare_shared(pstree_fd))
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-15 19:17:00 +04:00
|
|
|
return restore_root_task(pstree_fd);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
static long restorer_get_vma_hint(pid_t pid, struct list_head *self_vma_list, long vma_len)
|
2011-11-06 01:49:57 +04:00
|
|
|
{
|
|
|
|
struct vma_area *vma_area;
|
|
|
|
long prev_vma_end, hint;
|
|
|
|
struct vma_entry vma;
|
|
|
|
int fd = -1, ret;
|
|
|
|
|
|
|
|
hint = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we need some heuristics -- the VMA which restorer will
|
|
|
|
* belong to should not be unmapped, so we need to gueess out
|
|
|
|
* where to put it in.
|
|
|
|
*
|
|
|
|
* Yes, I know it's an O(n^2) algorithm, but usually there are
|
|
|
|
* not that many VMAs presented so instead of consuming memory
|
|
|
|
* better to stick with it.
|
|
|
|
*/
|
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
fd = open_image_ro_nocheck(FMT_FNAME_CORE, pid);
|
|
|
|
if (fd < 0)
|
2011-11-06 01:49:57 +04:00
|
|
|
goto err_or_found;
|
|
|
|
|
|
|
|
prev_vma_end = 0;
|
|
|
|
|
|
|
|
sys_lseek(fd, GET_FILE_OFF_AFTER(struct core_entry), SEEK_SET);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ret = sys_read(fd, &vma, sizeof(vma));
|
|
|
|
if (ret && ret != sizeof(vma)) {
|
2011-12-28 20:40:00 +04:00
|
|
|
pr_perror("Can't read vma entry from core-%d\n", pid);
|
2011-11-06 01:49:57 +04:00
|
|
|
goto err_or_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prev_vma_end) {
|
|
|
|
prev_vma_end = vma.end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((vma.start - prev_vma_end) > vma_len) {
|
|
|
|
list_for_each_entry(vma_area, self_vma_list, list) {
|
|
|
|
if (vma_area->vma.start <= prev_vma_end &&
|
|
|
|
vma_area->vma.end >= prev_vma_end)
|
|
|
|
goto err_or_found;
|
|
|
|
}
|
|
|
|
hint = prev_vma_end;
|
|
|
|
goto err_or_found;
|
|
|
|
} else
|
|
|
|
prev_vma_end = vma.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
err_or_found:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
2011-11-13 12:57:16 +04:00
|
|
|
static void sigreturn_restore(pid_t pstree_pid, pid_t pid)
|
2011-10-24 22:23:06 +04:00
|
|
|
{
|
2011-11-12 19:26:40 +04:00
|
|
|
long restore_task_code_len, restore_task_vma_len;
|
|
|
|
long restore_thread_code_len, restore_thread_vma_len;
|
2011-12-26 21:27:03 +04:00
|
|
|
long restore_shmem_vma_len;
|
2011-11-16 18:19:24 +04:00
|
|
|
|
2011-11-12 19:26:40 +04:00
|
|
|
void *exec_mem = MAP_FAILED;
|
|
|
|
void *restore_thread_exec_start;
|
|
|
|
void *restore_task_exec_start;
|
2012-01-01 13:10:12 +04:00
|
|
|
void *shmems_ref;
|
2011-11-16 18:19:24 +04:00
|
|
|
|
|
|
|
long new_sp, exec_mem_hint;
|
2011-10-25 21:25:42 +04:00
|
|
|
long ret;
|
2011-10-24 22:23:06 +04:00
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
struct task_restore_core_args *task_args;
|
|
|
|
struct thread_restore_args *thread_args;
|
|
|
|
|
2011-12-18 12:15:30 +04:00
|
|
|
char self_vmas_path[PATH_MAX];
|
2011-10-26 11:16:00 +04:00
|
|
|
|
2011-10-26 22:50:46 +04:00
|
|
|
LIST_HEAD(self_vma_list);
|
|
|
|
struct vma_area *vma_area;
|
2011-11-16 18:19:24 +04:00
|
|
|
int fd_self_vmas = -1;
|
|
|
|
int fd_core = -1;
|
2011-11-12 19:26:40 +04:00
|
|
|
int num;
|
|
|
|
|
|
|
|
struct pstree_entry pstree_entry;
|
|
|
|
int *fd_core_threads;
|
|
|
|
int fd_pstree = -1;
|
2012-01-12 23:50:45 +04:00
|
|
|
int pid_dir;
|
2011-11-12 19:26:40 +04:00
|
|
|
|
2012-01-01 13:12:37 +04:00
|
|
|
pr_info("%d: Restore via sigreturn\n", pid);
|
|
|
|
|
2011-11-12 19:26:40 +04:00
|
|
|
restore_task_code_len = 0;
|
|
|
|
restore_task_vma_len = 0;
|
|
|
|
restore_thread_code_len = 0;
|
2012-01-01 02:32:32 +04:00
|
|
|
restore_thread_vma_len = 0;
|
|
|
|
restore_shmem_vma_len = SHMEMS_SIZE;
|
2011-10-26 22:50:46 +04:00
|
|
|
|
2012-01-12 23:50:45 +04:00
|
|
|
pid_dir = open_pid_proc(pid);
|
|
|
|
if (pid_dir < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = parse_maps(getpid(), pid_dir, &self_vma_list, false);
|
|
|
|
close(pid_dir);
|
|
|
|
if (ret)
|
2011-10-26 22:50:46 +04:00
|
|
|
goto err;
|
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
/* pr_info_vma_list(&self_vma_list); */
|
2011-10-27 18:59:21 +04:00
|
|
|
|
2011-11-12 19:26:40 +04:00
|
|
|
BUILD_BUG_ON(sizeof(struct task_restore_core_args) & 1);
|
|
|
|
BUILD_BUG_ON(sizeof(struct thread_restore_args) & 1);
|
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
fd_pstree = open_image_ro_nocheck(FMT_FNAME_PSTREE, pstree_pid);
|
|
|
|
if (fd_pstree < 0)
|
2011-11-12 19:26:40 +04:00
|
|
|
goto err;
|
|
|
|
|
2011-12-29 19:56:34 +04:00
|
|
|
fd_core = open_image_ro_nocheck(FMT_FNAME_CORE_OUT, pid);
|
|
|
|
if (fd_core < 0)
|
2011-12-28 20:40:00 +04:00
|
|
|
pr_perror("Can't open core-out-%d\n", pid);
|
2011-11-16 18:19:24 +04:00
|
|
|
|
2011-12-18 12:15:30 +04:00
|
|
|
if (get_image_path(self_vmas_path, sizeof(self_vmas_path), FMT_FNAME_VMAS, getpid()))
|
|
|
|
goto err;
|
2012-01-14 14:24:20 +04:00
|
|
|
|
|
|
|
fd_self_vmas = open(self_vmas_path, O_CREAT | O_RDWR | O_TRUNC, CR_FD_PERM);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a temporary file used to pass vma info to
|
|
|
|
* restorer code, thus unlink it early to make it disappear
|
|
|
|
* as soon as we close it
|
|
|
|
*/
|
2012-01-16 00:53:52 +04:00
|
|
|
// unlink(self_vmas_path);
|
2012-01-14 14:24:20 +04:00
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
if (fd_self_vmas < 0) {
|
2011-12-28 20:40:00 +04:00
|
|
|
pr_perror("Can't open %s\n", self_vmas_path);
|
2011-11-04 19:18:53 +04:00
|
|
|
goto err;
|
2011-10-26 22:50:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
num = 0;
|
|
|
|
list_for_each_entry(vma_area, &self_vma_list, list) {
|
2011-11-16 18:19:24 +04:00
|
|
|
ret = write(fd_self_vmas, &vma_area->vma, sizeof(vma_area->vma));
|
2011-10-26 22:50:46 +04:00
|
|
|
if (ret != sizeof(vma_area->vma)) {
|
|
|
|
pr_perror("\nUnable to write vma entry (%li written)\n", num);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
2011-11-03 19:11:27 +04:00
|
|
|
free_mappings(&self_vma_list);
|
2011-10-26 22:50:46 +04:00
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
restore_task_code_len = restore_task(RESTORE_CMD__GET_SELF_LEN, NULL) - (long)restore_task;
|
2011-11-12 19:26:40 +04:00
|
|
|
restore_task_code_len = round_up(restore_task_code_len, 16);
|
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
restore_task_vma_len = round_up(restore_task_code_len + sizeof(*task_args), PAGE_SIZE);
|
2011-11-12 19:26:40 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread statistics
|
|
|
|
*/
|
|
|
|
lseek(fd_pstree, MAGIC_OFFSET, SEEK_SET);
|
|
|
|
while (1) {
|
|
|
|
ret = read_ptr_safe_eof(fd_pstree, &pstree_entry, err);
|
|
|
|
if (!ret) {
|
|
|
|
pr_perror("Pid %d not found in process tree\n", pid);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2011-11-13 12:57:16 +04:00
|
|
|
if (pstree_entry.pid != pid) {
|
|
|
|
lseek(fd_pstree,
|
|
|
|
(pstree_entry.nr_children +
|
|
|
|
pstree_entry.nr_threads) *
|
|
|
|
sizeof(u32), SEEK_CUR);
|
2011-11-12 19:26:40 +04:00
|
|
|
continue;
|
2011-11-13 12:57:16 +04:00
|
|
|
}
|
2011-11-12 19:26:40 +04:00
|
|
|
|
|
|
|
if (!pstree_entry.nr_threads)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute how many memory we will need
|
|
|
|
* to restore all threads, every thread
|
2011-11-16 18:19:24 +04:00
|
|
|
* requires own stack and heap, it's ~40K
|
|
|
|
* per thread.
|
2011-11-12 19:26:40 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
restore_thread_code_len = restore_thread(RESTORE_CMD__GET_SELF_LEN, NULL) - (long)restore_thread;
|
|
|
|
restore_thread_code_len = round_up(restore_thread_code_len, 16);
|
2011-11-12 19:26:40 +04:00
|
|
|
|
|
|
|
restore_thread_vma_len = sizeof(*thread_args) * pstree_entry.nr_threads;
|
|
|
|
restore_thread_vma_len = round_up(restore_thread_vma_len, 16);
|
2011-10-26 11:16:00 +04:00
|
|
|
|
2011-11-12 19:26:40 +04:00
|
|
|
restore_thread_vma_len+= restore_thread_code_len;
|
|
|
|
|
|
|
|
pr_info("%d: %d threads require %dK of memory\n",
|
2011-11-24 15:07:03 +04:00
|
|
|
pid, pstree_entry.nr_threads,
|
|
|
|
KBYTES(restore_thread_vma_len));
|
2011-11-12 19:26:40 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-26 21:27:03 +04:00
|
|
|
restore_thread_vma_len = round_up(restore_thread_vma_len, PAGE_SIZE);
|
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
exec_mem_hint = restorer_get_vma_hint(pid, &self_vma_list,
|
|
|
|
restore_task_vma_len +
|
2011-12-26 21:27:03 +04:00
|
|
|
restore_thread_vma_len +
|
|
|
|
restore_shmem_vma_len);
|
2011-11-16 18:19:24 +04:00
|
|
|
if (exec_mem_hint == -1) {
|
2011-11-12 19:26:40 +04:00
|
|
|
pr_err("No suitable area for task_restore bootstrap (%dK)\n",
|
|
|
|
restore_task_vma_len + restore_thread_vma_len);
|
2011-11-06 01:49:57 +04:00
|
|
|
goto err;
|
2011-11-16 18:19:24 +04:00
|
|
|
} else {
|
|
|
|
pr_info("Found bootstrap VMA hint at: %lx (needs ~%dK)\n",
|
|
|
|
exec_mem_hint,
|
2011-11-24 15:07:03 +04:00
|
|
|
KBYTES(restore_task_vma_len + restore_thread_vma_len));
|
2011-11-16 18:19:24 +04:00
|
|
|
}
|
2011-10-27 00:57:01 +04:00
|
|
|
|
2011-11-12 19:26:40 +04:00
|
|
|
/* VMA we need to run task_restore code */
|
2011-11-16 18:19:24 +04:00
|
|
|
exec_mem = mmap((void *)exec_mem_hint,
|
|
|
|
restore_task_vma_len + restore_thread_vma_len,
|
2011-10-26 11:16:00 +04:00
|
|
|
PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANON, 0, 0);
|
2011-10-24 22:23:06 +04:00
|
|
|
if (exec_mem == MAP_FAILED) {
|
2011-10-26 11:16:00 +04:00
|
|
|
pr_err("Can't mmap section for restore code\n");
|
2011-11-06 01:49:57 +04:00
|
|
|
goto err;
|
2011-10-24 22:23:06 +04:00
|
|
|
}
|
|
|
|
|
2011-10-26 11:16:00 +04:00
|
|
|
/*
|
2011-11-16 18:19:24 +04:00
|
|
|
* Prepare a memory map for restorer. Note a thread space
|
|
|
|
* might be completely unused so it's here just for convenience.
|
2011-10-26 11:16:00 +04:00
|
|
|
*/
|
2011-11-16 18:19:24 +04:00
|
|
|
restore_task_exec_start = exec_mem;
|
|
|
|
restore_thread_exec_start = restore_task_exec_start + restore_task_vma_len;
|
|
|
|
task_args = restore_task_exec_start + restore_task_code_len;
|
|
|
|
thread_args = restore_thread_exec_start + restore_thread_code_len;
|
2011-10-26 11:16:00 +04:00
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
memzero_p(task_args);
|
|
|
|
memzero_p(thread_args);
|
2011-10-26 11:16:00 +04:00
|
|
|
|
2011-10-26 17:35:50 +04:00
|
|
|
/*
|
2011-11-16 18:19:24 +04:00
|
|
|
* Code at a new place.
|
2011-10-26 17:35:50 +04:00
|
|
|
*/
|
2011-11-16 18:19:24 +04:00
|
|
|
memcpy(restore_task_exec_start, &restore_task, restore_task_code_len);
|
|
|
|
memcpy(restore_thread_exec_start, &restore_thread, restore_thread_code_len);
|
2011-11-12 19:26:40 +04:00
|
|
|
|
|
|
|
/*
|
2011-11-16 18:19:24 +04:00
|
|
|
* Adjust stack.
|
2011-11-12 19:26:40 +04:00
|
|
|
*/
|
2011-11-16 18:19:24 +04:00
|
|
|
new_sp = RESTORE_ALIGN_STACK((long)task_args->mem_zone.stack, sizeof(task_args->mem_zone.stack));
|
2011-10-24 22:23:06 +04:00
|
|
|
|
2011-10-26 00:30:41 +04:00
|
|
|
/*
|
2012-01-01 13:10:12 +04:00
|
|
|
* Get a reference to shared memory area which is
|
|
|
|
* used to signal if shmem restoration complete
|
|
|
|
* from low-level restore code.
|
|
|
|
*
|
|
|
|
* This shmem area is mapped right after the whole area of
|
|
|
|
* sigreturn rt code. Note we didn't allocated it before
|
|
|
|
* but this area is taken into account for 'hint' memory
|
|
|
|
* address.
|
2011-10-26 00:30:41 +04:00
|
|
|
*/
|
2012-01-01 13:10:12 +04:00
|
|
|
shmems_ref = (struct shmems *)(exec_mem_hint +
|
|
|
|
restore_task_vma_len +
|
|
|
|
restore_thread_vma_len);
|
2012-01-13 14:02:38 +04:00
|
|
|
ret = shmem_remap(shmems, shmems_ref);
|
2012-01-03 13:05:50 +04:00
|
|
|
if (ret < 0)
|
2011-12-26 21:27:03 +04:00
|
|
|
goto err;
|
|
|
|
|
2012-01-01 13:10:12 +04:00
|
|
|
/*
|
|
|
|
* Arguments for task restoration.
|
|
|
|
*/
|
2011-11-16 18:19:24 +04:00
|
|
|
task_args->pid = pid;
|
2012-01-01 13:10:12 +04:00
|
|
|
task_args->shmems = shmems_ref;
|
2011-11-16 18:19:24 +04:00
|
|
|
task_args->fd_core = fd_core;
|
|
|
|
task_args->fd_self_vmas = fd_self_vmas;
|
2011-11-18 16:09:01 +04:00
|
|
|
|
2011-12-26 20:33:09 +04:00
|
|
|
cr_mutex_init(&task_args->rst_lock);
|
2011-11-12 19:26:40 +04:00
|
|
|
|
2011-12-02 11:42:41 +04:00
|
|
|
strncpy(task_args->ns_last_pid_path,
|
|
|
|
LAST_PID_PATH,
|
|
|
|
sizeof(task_args->ns_last_pid_path));
|
|
|
|
|
2011-11-12 19:26:40 +04:00
|
|
|
if (pstree_entry.nr_threads) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now prepare run-time data for threads restore.
|
|
|
|
*/
|
2011-11-16 18:19:24 +04:00
|
|
|
task_args->nr_threads = pstree_entry.nr_threads;
|
|
|
|
task_args->clone_restore_fn = (void *)restore_thread_exec_start;
|
|
|
|
task_args->thread_args = thread_args;
|
2011-11-03 11:58:45 +04:00
|
|
|
|
2011-11-12 19:26:40 +04:00
|
|
|
/*
|
|
|
|
* Fill up per-thread data.
|
|
|
|
*/
|
|
|
|
lseek(fd_pstree, sizeof(u32) * pstree_entry.nr_children, SEEK_CUR);
|
|
|
|
for (i = 0; i < pstree_entry.nr_threads; i++) {
|
|
|
|
read_ptr_safe(fd_pstree, &thread_args[i].pid, err);
|
|
|
|
|
2012-01-16 00:54:43 +04:00
|
|
|
/* skip self */
|
|
|
|
if (thread_args[i].pid == pid)
|
|
|
|
continue;
|
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
/* Core files are to be opened */
|
2011-12-29 19:56:34 +04:00
|
|
|
thread_args[i].fd_core = open_image_ro_nocheck(FMT_FNAME_CORE, thread_args[i].pid);
|
|
|
|
if (thread_args[i].fd_core < 0)
|
2011-11-12 19:26:40 +04:00
|
|
|
goto err;
|
|
|
|
|
2011-11-17 00:59:08 +04:00
|
|
|
thread_args[i].rst_lock = &task_args->rst_lock;
|
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
pr_info("Thread %4d stack %8p heap %8p rt_sigframe %8p\n",
|
|
|
|
i, (long)thread_args[i].mem_zone.stack,
|
|
|
|
thread_args[i].mem_zone.heap,
|
|
|
|
thread_args[i].mem_zone.rt_sigframe);
|
2011-11-12 19:26:40 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 18:19:24 +04:00
|
|
|
pr_info("task_args: %p\n"
|
|
|
|
"task_args->pid: %d\n"
|
|
|
|
"task_args->fd_core: %d\n"
|
|
|
|
"task_args->fd_self_vmas: %d\n"
|
|
|
|
"task_args->nr_threads: %d\n"
|
|
|
|
"task_args->clone_restore_fn: %p\n"
|
|
|
|
"task_args->thread_args: %p\n",
|
|
|
|
task_args, task_args->pid,
|
|
|
|
task_args->fd_core, task_args->fd_self_vmas,
|
|
|
|
task_args->nr_threads, task_args->clone_restore_fn,
|
|
|
|
task_args->thread_args);
|
|
|
|
|
2011-11-12 19:26:40 +04:00
|
|
|
close_safe(&fd_pstree);
|
2011-12-19 18:52:50 +04:00
|
|
|
fini_log();
|
2011-11-03 11:58:45 +04:00
|
|
|
|
2011-10-26 17:35:50 +04:00
|
|
|
/*
|
2011-11-12 19:26:40 +04:00
|
|
|
* An indirect call to task_restore, note it never resturns
|
2011-10-26 17:35:50 +04:00
|
|
|
* and restoreing core is extremely destructive.
|
|
|
|
*/
|
2011-10-26 11:16:00 +04:00
|
|
|
asm volatile(
|
2011-11-12 19:26:40 +04:00
|
|
|
"movq %0, %%rbx \n"
|
|
|
|
"movq %1, %%rax \n"
|
2011-11-16 18:19:24 +04:00
|
|
|
"movq %2, %%rsi \n"
|
2011-11-12 19:26:40 +04:00
|
|
|
"movl $"__stringify(RESTORE_CMD__RESTORE_CORE)", %%edi \n"
|
|
|
|
"movq %%rbx, %%rsp \n"
|
|
|
|
"callq *%%rax \n"
|
2011-10-26 17:35:50 +04:00
|
|
|
:
|
2011-11-16 18:19:24 +04:00
|
|
|
: "g"(new_sp),
|
|
|
|
"g"(restore_task_exec_start),
|
|
|
|
"g"(task_args)
|
|
|
|
: "rsp", "rdi", "rsi", "rbx", "rax", "memory");
|
2011-10-26 11:16:00 +04:00
|
|
|
|
2011-10-26 22:50:46 +04:00
|
|
|
err:
|
2011-11-06 01:49:57 +04:00
|
|
|
free_mappings(&self_vma_list);
|
2011-11-12 19:26:40 +04:00
|
|
|
close_safe(&fd_pstree);
|
2011-11-16 18:19:24 +04:00
|
|
|
close_safe(&fd_core);
|
|
|
|
close_safe(&fd_self_vmas);
|
2011-11-12 19:26:40 +04:00
|
|
|
|
|
|
|
if (exec_mem != MAP_FAILED)
|
|
|
|
munmap(exec_mem, restore_task_vma_len + restore_thread_vma_len);
|
2011-11-06 01:49:57 +04:00
|
|
|
|
2011-10-26 17:35:50 +04:00
|
|
|
/* Just to be sure */
|
|
|
|
sys_exit(0);
|
2011-10-24 22:23:06 +04:00
|
|
|
}
|
|
|
|
|
2011-10-04 01:50:19 +04:00
|
|
|
int cr_restore_tasks(pid_t pid, struct cr_options *opts)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
2011-11-07 19:14:28 +04:00
|
|
|
#if 0
|
2011-11-13 12:57:16 +04:00
|
|
|
sigreturn_restore(pid, pid);
|
2011-11-07 19:14:28 +04:00
|
|
|
#endif
|
2011-10-24 22:23:06 +04:00
|
|
|
|
2011-11-13 12:57:16 +04:00
|
|
|
pstree_pid = pid;
|
|
|
|
|
2011-10-04 01:50:19 +04:00
|
|
|
if (opts->leader_only)
|
2011-09-23 12:00:45 +04:00
|
|
|
return restore_one_task(pid);
|
|
|
|
return restore_all_tasks(pid);
|
|
|
|
}
|