2011-09-23 12:00:45 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2011-10-13 19:43:52 +04:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "crtools.h"
|
|
|
|
#include "util.h"
|
2011-12-19 18:52:50 +04:00
|
|
|
#include "log.h"
|
2011-12-26 22:12:03 +04:00
|
|
|
#include "sockets.h"
|
2011-09-23 12:00:45 +04:00
|
|
|
|
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-12-16 18:02:02 +04:00
|
|
|
char image_dir[PATH_MAX];
|
2011-09-23 12:00:45 +04:00
|
|
|
|
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-11-15 18:35:55 +04:00
|
|
|
struct cr_fd_desc_tmpl fdset_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] = {
|
2011-11-15 18:35:55 +04:00
|
|
|
.fmt = FMT_FNAME_FDINFO,
|
2011-09-23 12:00:45 +04:00
|
|
|
.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] = {
|
2011-11-15 18:35:55 +04:00
|
|
|
.fmt = FMT_FNAME_PAGES,
|
2011-09-23 12:00:45 +04:00
|
|
|
.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] = {
|
2011-11-15 18:35:55 +04:00
|
|
|
.fmt = FMT_FNAME_PAGES_SHMEM,
|
2011-09-23 12:00:45 +04:00
|
|
|
.magic = PAGES_MAGIC,
|
|
|
|
},
|
2011-10-13 13:33:32 +04:00
|
|
|
|
2011-11-15 18:35:55 +04:00
|
|
|
/* core data, such as regs and vmas and such */
|
2011-09-23 12:00:45 +04:00
|
|
|
[CR_FD_CORE] = {
|
2011-11-15 18:35:55 +04:00
|
|
|
.fmt = FMT_FNAME_CORE,
|
2011-09-23 12:00:45 +04:00
|
|
|
.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] = {
|
2011-11-15 18:35:55 +04:00
|
|
|
.fmt = FMT_FNAME_PIPES,
|
2011-09-23 12:00:45 +04:00
|
|
|
.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] = {
|
2011-11-15 18:35:55 +04:00
|
|
|
.fmt = FMT_FNAME_PSTREE,
|
2011-09-23 12:00:45 +04:00
|
|
|
.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] = {
|
2011-11-15 18:35:55 +04:00
|
|
|
.fmt = FMT_FNAME_SHMEM,
|
2011-09-23 12:00:45 +04:00
|
|
|
.magic = SHMEM_MAGIC,
|
|
|
|
},
|
2011-11-29 15:12:25 +03:00
|
|
|
|
2011-11-30 23:33:28 +04:00
|
|
|
/* info about signal handlers */
|
2011-11-29 15:12:25 +03:00
|
|
|
[CR_FD_SIGACT] = {
|
|
|
|
.fmt = FMT_FNAME_SIGACTS,
|
|
|
|
.magic = SIGACT_MAGIC,
|
|
|
|
},
|
2011-12-26 22:12:03 +04:00
|
|
|
|
|
|
|
/* info about unix sockets */
|
|
|
|
[CR_FD_UNIXSK] = {
|
|
|
|
.fmt = FMT_FNAME_UNIXSK,
|
|
|
|
.magic = UNIXSK_MAGIC,
|
|
|
|
},
|
2012-01-17 21:30:00 +04:00
|
|
|
|
|
|
|
/* info about inet sockets */
|
|
|
|
[CR_FD_INETSK] = {
|
|
|
|
.fmt = FMT_FNAME_INETSK,
|
|
|
|
.magic = INETSK_MAGIC,
|
|
|
|
},
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
};
|
|
|
|
|
2012-01-12 15:18:32 +04:00
|
|
|
static struct cr_fdset *alloc_cr_fdset(void)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
struct cr_fdset *cr_fdset;
|
|
|
|
unsigned int i;
|
|
|
|
|
2012-01-12 15:17:51 +04:00
|
|
|
cr_fdset = xmalloc(sizeof(*cr_fdset));
|
|
|
|
if (cr_fdset)
|
|
|
|
for (i = 0; i < CR_FD_MAX; i++)
|
|
|
|
cr_fdset->fds[i] = -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
return cr_fdset;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:18:32 +04:00
|
|
|
struct cr_fdset *prep_cr_fdset_for_dump(int pid, unsigned long use_mask)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int ret = -1;
|
2012-01-12 15:17:51 +04:00
|
|
|
char path[PATH_MAX];
|
2012-01-12 15:18:32 +04:00
|
|
|
struct cr_fdset *cr_fdset;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-12 15:18:32 +04:00
|
|
|
cr_fdset = alloc_cr_fdset();
|
2011-09-23 12:00:45 +04:00
|
|
|
if (!cr_fdset)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (i = 0; i < CR_FD_MAX; i++) {
|
|
|
|
if (!(use_mask & CR_FD_DESC_USE(i)))
|
|
|
|
continue;
|
|
|
|
|
2012-01-12 15:17:51 +04:00
|
|
|
ret = get_image_path(path, sizeof(path),
|
|
|
|
fdset_template[i].fmt, pid);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = unlink(path);
|
2011-09-23 12:00:45 +04:00
|
|
|
if (ret && errno != ENOENT) {
|
2012-01-12 15:17:51 +04:00
|
|
|
pr_perror("Unable to unlink %s (%s)\n", path, strerror(errno));
|
2011-09-23 12:00:45 +04:00
|
|
|
goto err;
|
2012-01-12 15:17:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = open(path, O_RDWR | O_CREAT | O_EXCL, CR_FD_PERM);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Unable to open %s (%s)\n", path, strerror(errno));
|
2011-09-23 12:00:45 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:17:51 +04:00
|
|
|
pr_debug("Opened %s with %d\n", path, ret);
|
|
|
|
write_ptr_safe(ret, &fdset_template[i].magic, err);
|
|
|
|
cr_fdset->fds[i] = ret;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
err:
|
2012-01-12 15:18:32 +04:00
|
|
|
return cr_fdset;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2012-01-12 15:18:32 +04:00
|
|
|
struct cr_fdset *prep_cr_fdset_for_restore(int pid, unsigned long use_mask)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int ret = -1;
|
2012-01-12 15:17:51 +04:00
|
|
|
char path[PATH_MAX];
|
2011-09-23 12:00:45 +04:00
|
|
|
u32 magic;
|
2012-01-12 15:18:32 +04:00
|
|
|
struct cr_fdset *cr_fdset;
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-12 15:18:32 +04:00
|
|
|
cr_fdset = alloc_cr_fdset();
|
2011-09-23 12:00:45 +04:00
|
|
|
if (!cr_fdset)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (i = 0; i < CR_FD_MAX; i++) {
|
|
|
|
if (!(use_mask & CR_FD_DESC_USE(i)))
|
|
|
|
continue;
|
|
|
|
|
2012-01-12 15:17:51 +04:00
|
|
|
ret = get_image_path(path, sizeof(path),
|
|
|
|
fdset_template[i].fmt, pid);
|
|
|
|
if (ret)
|
2011-09-23 12:00:45 +04:00
|
|
|
goto err;
|
|
|
|
|
2012-01-12 15:17:51 +04:00
|
|
|
ret = open(path, O_RDWR, CR_FD_PERM);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Unable to open %s (%s)\n", path, strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2012-01-12 15:17:51 +04:00
|
|
|
pr_debug("Opened %s with %d\n", path, ret);
|
|
|
|
read_ptr_safe(ret, &magic, err);
|
2012-01-12 15:14:36 +04:00
|
|
|
if (magic != fdset_template[i].magic) {
|
2012-01-12 15:17:51 +04:00
|
|
|
close(ret);
|
|
|
|
pr_err("Magic doesn't match for %s\n", path);
|
2011-09-23 12:00:45 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:17:51 +04:00
|
|
|
cr_fdset->fds[i] = ret;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
err:
|
2012-01-12 15:18:32 +04:00
|
|
|
return cr_fdset;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2012-01-12 21:25:19 +04:00
|
|
|
void close_cr_fdset(struct cr_fdset **cr_fdset)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
2012-01-12 21:25:19 +04:00
|
|
|
struct cr_fdset *fdset;
|
2011-09-23 12:00:45 +04:00
|
|
|
unsigned int i;
|
|
|
|
|
2012-01-12 21:25:19 +04:00
|
|
|
if (!cr_fdset || !*cr_fdset)
|
2011-09-23 12:00:45 +04:00
|
|
|
return;
|
|
|
|
|
2012-01-12 21:25:19 +04:00
|
|
|
fdset = *cr_fdset;
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
for (i = 0; i < CR_FD_MAX; i++) {
|
2012-01-12 21:25:19 +04:00
|
|
|
if (fdset->fds[i] == -1)
|
2011-09-23 12:00:45 +04:00
|
|
|
continue;
|
|
|
|
|
2012-01-12 21:25:19 +04:00
|
|
|
pr_debug("Closed %d/%d\n", i, fdset->fds[i]);
|
|
|
|
close(fdset->fds[i]);
|
|
|
|
fdset->fds[i] = -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2012-01-12 21:25:19 +04:00
|
|
|
xfree(fdset);
|
|
|
|
*cr_fdset = NULL;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2011-12-28 20:39:00 +04:00
|
|
|
pid_t pid = 0;
|
2011-09-23 12:00:45 +04:00
|
|
|
int ret = -1;
|
2011-10-13 19:43:52 +04:00
|
|
|
int opt, idx;
|
|
|
|
int action = -1;
|
2011-12-08 18:39:00 +04:00
|
|
|
int log_inited = 0;
|
2011-10-13 19:43:52 +04:00
|
|
|
|
2011-12-09 12:37:08 +04:00
|
|
|
static const char short_opts[] = "drsf:p:t:hcD:o:";
|
2011-10-13 19:43:52 +04:00
|
|
|
static const struct option long_opts[] = {
|
|
|
|
{ "dump", no_argument, NULL, 'd' },
|
|
|
|
{ "restore", no_argument, NULL, 'r' },
|
|
|
|
{ "show", no_argument, NULL, 's' },
|
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
goto usage;
|
|
|
|
|
2011-10-23 00:55:26 +04:00
|
|
|
memzero_p(&zero_page_entry);
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-11-22 22:09:28 +04:00
|
|
|
/* Default options */
|
2011-12-09 12:37:08 +04:00
|
|
|
opts.final_state = CR_TASK_KILL;
|
2011-11-22 22:09:28 +04:00
|
|
|
|
2011-10-13 19:43:52 +04:00
|
|
|
for (opt = getopt_long(argc, argv, short_opts, long_opts, &idx); opt != -1;
|
|
|
|
opt = getopt_long(argc, argv, short_opts, long_opts, &idx)) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'p':
|
|
|
|
pid = atoi(optarg);
|
|
|
|
opts.leader_only = true;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
pid = atoi(optarg);
|
|
|
|
opts.leader_only = false;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
action = opt;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
action = opt;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
action = opt;
|
|
|
|
break;
|
2011-12-05 15:57:47 +04:00
|
|
|
case 'c':
|
|
|
|
opts.show_pages_content = true;
|
2012-01-11 13:29:35 +04:00
|
|
|
opts.final_state = CR_TASK_RUN;
|
2011-12-05 15:57:47 +04:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
opts.show_dump_file = optarg;
|
|
|
|
break;
|
2011-12-07 13:48:00 +04:00
|
|
|
case 'D':
|
|
|
|
if (chdir(optarg)) {
|
|
|
|
pr_perror("can't change working directory");
|
2011-12-19 16:51:26 +04:00
|
|
|
return -1;
|
2011-12-07 13:48:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2011-12-08 18:39:00 +04:00
|
|
|
case 'o':
|
2011-12-19 18:52:50 +04:00
|
|
|
if (init_log(optarg))
|
2011-12-19 16:51:26 +04:00
|
|
|
return -1;
|
2011-12-08 18:39:00 +04:00
|
|
|
log_inited = 1;
|
|
|
|
break;
|
2011-10-13 19:43:52 +04:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
goto usage;
|
|
|
|
}
|
2011-10-04 01:50:19 +04:00
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2011-12-13 15:03:33 +04:00
|
|
|
if (!log_inited) {
|
2011-12-19 18:52:50 +04:00
|
|
|
ret = init_log(NULL);
|
2011-12-13 15:03:33 +04:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2011-12-08 18:39:00 +04:00
|
|
|
|
2011-12-13 15:07:06 +04:00
|
|
|
if (!getcwd(image_dir, sizeof(image_dir))) {
|
2011-12-07 13:48:00 +04:00
|
|
|
pr_perror("can't get currect directory\n");
|
2011-12-13 15:03:33 +04:00
|
|
|
return -1;
|
2011-12-07 13:48:00 +04:00
|
|
|
}
|
|
|
|
|
2011-12-28 20:39:00 +04:00
|
|
|
if (!pid && (action != 's' || !opts.show_dump_file))
|
|
|
|
goto opt_pid_missing;
|
2011-12-20 19:26:37 +04:00
|
|
|
|
2011-10-13 19:43:52 +04:00
|
|
|
switch (action) {
|
|
|
|
case 'd':
|
2011-10-04 01:50:19 +04:00
|
|
|
ret = cr_dump_tasks(pid, &opts);
|
2011-10-13 19:43:52 +04:00
|
|
|
break;
|
|
|
|
case 'r':
|
2011-10-04 01:50:19 +04:00
|
|
|
ret = cr_restore_tasks(pid, &opts);
|
2011-10-13 19:43:52 +04:00
|
|
|
break;
|
|
|
|
case 's':
|
2011-10-04 01:50:19 +04:00
|
|
|
ret = cr_show(pid, &opts);
|
2011-10-13 19:43:52 +04:00
|
|
|
break;
|
|
|
|
default:
|
2011-09-23 12:00:45 +04:00
|
|
|
goto usage;
|
2011-10-13 19:43:52 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
usage:
|
|
|
|
printk("\nUsage:\n");
|
2011-12-18 12:06:08 +04:00
|
|
|
printk(" %s --dump|-d [-c] -p|-t pid\n", argv[0]);
|
|
|
|
printk(" %s --restore|-r -p|-t pid\n", argv[0]);
|
|
|
|
printk(" %s --show|-s [-c] (-p|-t pid)|(-f file)\n", argv[0]);
|
|
|
|
|
|
|
|
printk("\nGeneral parameters:\n");
|
|
|
|
printk(" --dump,-d checkpoint a process identified by pid\n");
|
|
|
|
printk(" --restore,-r restore a process identified by pid\n");
|
|
|
|
printk(" --show,-s show dump contents of a process identified by pid\n");
|
|
|
|
printk(" -p checkpoint/restore only a single process identified by pid\n");
|
|
|
|
printk(" -t checkpoint/restore the whole process tree identified by pid\n");
|
|
|
|
printk(" -f show contents of a checkpoint file\n");
|
|
|
|
printk(" -c in case of checkpoint -- continue running the process after\n"
|
|
|
|
" checkpoint complete, in case of showing file contents --\n"
|
|
|
|
" show contents of pages dumped in hexdump format\n");
|
|
|
|
|
|
|
|
printk("\nAdditional common parameters:\n");
|
|
|
|
printk(" -D dir save checkpoint files in specified directory\n");
|
2011-12-05 15:57:47 +04:00
|
|
|
printk("\n");
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
return -1;
|
2011-12-20 19:47:06 +04:00
|
|
|
|
|
|
|
opt_pid_missing:
|
|
|
|
printk("No pid specified, -t or -p option missed?\n");
|
|
|
|
return -1;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|