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 <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>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "list.h"
|
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "crtools.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2011-10-04 01:50:19 +04:00
|
|
|
static struct cr_options opts;
|
2011-09-23 12:00:45 +04:00
|
|
|
struct page_entry zero_page_entry;
|
|
|
|
|
2011-10-13 13:33:32 +04:00
|
|
|
/*
|
|
|
|
* The cr fd set is the set of files where the information
|
|
|
|
* about dumped processes is stored. Each file carries some
|
|
|
|
* small portion of info about the whole picture, see below
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
static struct cr_fd_desc_tmpl template[CR_FD_MAX] = {
|
2011-10-13 13:33:32 +04:00
|
|
|
|
|
|
|
/* info about file descriptiors */
|
2011-09-23 12:00:45 +04:00
|
|
|
[CR_FD_FDINFO] = {
|
|
|
|
.fmt = "fdinfo-%li.img",
|
|
|
|
.magic = FDINFO_MAGIC,
|
|
|
|
},
|
2011-10-13 13:33:32 +04:00
|
|
|
|
|
|
|
/* private memory pages data */
|
2011-09-23 12:00:45 +04:00
|
|
|
[CR_FD_PAGES] = {
|
|
|
|
.fmt = "pages-%li.img",
|
|
|
|
.magic = PAGES_MAGIC,
|
|
|
|
},
|
2011-10-13 13:33:32 +04:00
|
|
|
|
|
|
|
/* shared memory pages data */
|
2011-09-23 12:00:45 +04:00
|
|
|
[CR_FD_PAGES_SHMEM] = {
|
|
|
|
.fmt = "pages-shmem-%li.img",
|
|
|
|
.magic = PAGES_MAGIC,
|
|
|
|
},
|
2011-10-13 13:33:32 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The main part of restoring a single process is calling
|
|
|
|
* execve syscall on an ELF image which contains memory
|
|
|
|
* and arch-specific (regs, fpu) data about a process.
|
|
|
|
*
|
|
|
|
* Thus this file contains the almost-ready for execve image.
|
|
|
|
*/
|
2011-09-23 12:00:45 +04:00
|
|
|
[CR_FD_CORE] = {
|
|
|
|
.fmt = "core-%li.img",
|
|
|
|
.magic = CORE_MAGIC,
|
|
|
|
},
|
2011-10-13 13:33:32 +04:00
|
|
|
|
|
|
|
/* info about pipes - fds, pipe id and pipe data */
|
2011-09-23 12:00:45 +04:00
|
|
|
[CR_FD_PIPES] = {
|
|
|
|
.fmt = "pipes-%li.img",
|
|
|
|
.magic = PIPES_MAGIC,
|
|
|
|
},
|
2011-10-13 13:33:32 +04:00
|
|
|
|
|
|
|
/* info about process linkage */
|
2011-09-23 12:00:45 +04:00
|
|
|
[CR_FD_PSTREE] = {
|
|
|
|
.fmt = "pstree-%li.img",
|
|
|
|
.magic = PSTREE_MAGIC,
|
|
|
|
},
|
2011-10-13 13:33:32 +04:00
|
|
|
|
|
|
|
/* info about which memory areas are shared */
|
2011-09-23 12:00:45 +04:00
|
|
|
[CR_FD_SHMEM] = {
|
|
|
|
.fmt = "shmem-%li.img",
|
|
|
|
.magic = SHMEM_MAGIC,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cr_fdset *alloc_cr_fdset(pid_t pid)
|
|
|
|
{
|
|
|
|
struct cr_fdset *cr_fdset;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
cr_fdset = xzalloc(sizeof(*cr_fdset));
|
|
|
|
if (!cr_fdset)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (i = 0; i < CR_FD_MAX; i++) {
|
|
|
|
cr_fdset->desc[i].tmpl = &template[i];
|
|
|
|
snprintf(cr_fdset->desc[i].name,
|
|
|
|
sizeof(cr_fdset->desc[i].name),
|
|
|
|
cr_fdset->desc[i].tmpl->fmt,
|
|
|
|
(long)pid);
|
|
|
|
cr_fdset->desc[i].fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
return cr_fdset;
|
|
|
|
}
|
|
|
|
|
|
|
|
int prep_cr_fdset_for_dump(struct cr_fdset *cr_fdset,
|
|
|
|
unsigned long use_mask)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
u32 magic;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!cr_fdset)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
cr_fdset->use_mask = use_mask;
|
|
|
|
|
|
|
|
for (i = 0; i < CR_FD_MAX; i++) {
|
|
|
|
if (!(use_mask & CR_FD_DESC_USE(i)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = unlink(cr_fdset->desc[i].name);
|
|
|
|
if (ret && errno != ENOENT) {
|
|
|
|
pr_perror("Unable to unlink %s (%s)\n",
|
|
|
|
cr_fdset->desc[i].name,
|
|
|
|
strerror(errno));
|
|
|
|
goto err;
|
|
|
|
} else
|
|
|
|
ret = -1;
|
|
|
|
cr_fdset->desc[i].fd = open(cr_fdset->desc[i].name,
|
|
|
|
O_RDWR | O_CREAT | O_EXCL,
|
|
|
|
CR_FD_PERM);
|
|
|
|
if (cr_fdset->desc[i].fd < 0) {
|
|
|
|
pr_perror("Unable to open %s (%s)\n",
|
|
|
|
cr_fdset->desc[i].name,
|
|
|
|
strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_debug("Opened %s with %d\n",
|
|
|
|
cr_fdset->desc[i].name,
|
|
|
|
cr_fdset->desc[i].fd);
|
|
|
|
|
|
|
|
magic = cr_fdset->desc[i].tmpl->magic;
|
|
|
|
write_ptr_safe(cr_fdset->desc[i].fd, &magic, err);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int prep_cr_fdset_for_restore(struct cr_fdset *cr_fdset,
|
|
|
|
unsigned long use_mask)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int ret = -1;
|
|
|
|
u32 magic;
|
|
|
|
|
|
|
|
if (!cr_fdset)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
cr_fdset->use_mask = use_mask;
|
|
|
|
|
|
|
|
for (i = 0; i < CR_FD_MAX; i++) {
|
|
|
|
if (!(use_mask & CR_FD_DESC_USE(i)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
cr_fdset->desc[i].fd = open(cr_fdset->desc[i].name,
|
|
|
|
O_RDWR, CR_FD_PERM);
|
|
|
|
if (cr_fdset->desc[i].fd < 0) {
|
|
|
|
pr_perror("Unable to open %s (%s)\n",
|
|
|
|
cr_fdset->desc[i].name,
|
|
|
|
strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_debug("Opened %s with %d\n",
|
|
|
|
cr_fdset->desc[i].name,
|
|
|
|
cr_fdset->desc[i].fd);
|
|
|
|
|
|
|
|
read_ptr_safe(cr_fdset->desc[i].fd, &magic, err);
|
|
|
|
if (magic != cr_fdset->desc[i].tmpl->magic) {
|
2011-09-30 14:37:12 +04:00
|
|
|
pr_err("Magic doesn't match for %s\n",
|
|
|
|
cr_fdset->desc[i].name);
|
2011-09-23 12:00:45 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_cr_fdset(struct cr_fdset *cr_fdset)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!cr_fdset)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < CR_FD_MAX; i++) {
|
|
|
|
if (!(cr_fdset->use_mask & CR_FD_DESC_USE(i)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cr_fdset->desc[i].fd >= 0) {
|
|
|
|
pr_debug("Closed %s with %d\n",
|
|
|
|
cr_fdset->desc[i].name,
|
|
|
|
cr_fdset->desc[i].fd);
|
|
|
|
close(cr_fdset->desc[i].fd);
|
|
|
|
cr_fdset->desc[i].fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_cr_fdset(struct cr_fdset **cr_fdset)
|
|
|
|
{
|
|
|
|
if (cr_fdset && *cr_fdset) {
|
|
|
|
free(*cr_fdset);
|
|
|
|
*cr_fdset = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
memset(&zero_page_entry, 0, sizeof(zero_page_entry));
|
|
|
|
|
2011-10-04 01:50:19 +04:00
|
|
|
switch (argv[2][1]) {
|
|
|
|
case 'p':
|
|
|
|
pid = atol(argv[3]);
|
|
|
|
opts.leader_only = true;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
pid = atol(argv[3]);
|
|
|
|
opts.leader_only = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto usage;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-10-04 01:50:19 +04:00
|
|
|
if (!strcmp(argv[1], "dump")) {
|
|
|
|
ret = cr_dump_tasks(pid, &opts);
|
2011-09-23 12:00:45 +04:00
|
|
|
} else if (!strcmp(argv[1], "restore")) {
|
2011-10-04 01:50:19 +04:00
|
|
|
ret = cr_restore_tasks(pid, &opts);
|
2011-09-23 12:00:45 +04:00
|
|
|
} else if (!strcmp(argv[1], "show")) {
|
2011-10-04 01:50:19 +04:00
|
|
|
ret = cr_show(pid, &opts);
|
2011-09-23 12:00:45 +04:00
|
|
|
} else
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
usage:
|
|
|
|
printk("\nUsage:\n");
|
|
|
|
printk("\tcrtools (dump|show|restore) (-p|-t) pid\n\n");
|
|
|
|
return -1;
|
|
|
|
}
|