Commit a0ec1002 authored by Kinsbursky Stanislav's avatar Kinsbursky Stanislav Committed by Cyrill Gorcunov

crtools: cleanup fdset initalization

v2: wrappers names become less obfuscating

This patch:
1) Updates function cr_fdset_open() to be suitable for handling fdset creation
for dump and show stages.
2) Replaces cr_fdset_open() by new wrapper function cr_fdset_dump().
3) Replaces prep_cr_fdset_for_restore() by new wrapper function cr_fdset_show().
Signed-off-by: 's avatarStanislav Kinsbursky <skinsbursky@parallels.com>
Acked-by: 's avatarPavel Emelyanov <xemul@parallels.com>
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
parent fb1d1aa2
...@@ -1187,7 +1187,7 @@ static int dump_one_zombie(struct pstree_item *item, struct proc_pid_stat *pps, ...@@ -1187,7 +1187,7 @@ static int dump_one_zombie(struct pstree_item *item, struct proc_pid_stat *pps,
{ {
struct core_entry *core; struct core_entry *core;
cr_fdset = cr_fdset_open(item->pid, CR_FD_DESC_CORE, cr_fdset); cr_fdset = cr_dump_fdset_open(item->pid, CR_FD_DESC_CORE, cr_fdset);
if (cr_fdset == NULL) if (cr_fdset == NULL)
return -1; return -1;
...@@ -1216,7 +1216,7 @@ static int dump_task_threads(struct pstree_item *item) ...@@ -1216,7 +1216,7 @@ static int dump_task_threads(struct pstree_item *item)
if (item->pid == item->threads[i]) if (item->pid == item->threads[i])
continue; continue;
cr_fdset_thread = cr_fdset_open(item->threads[i], CR_FD_DESC_CORE, NULL); cr_fdset_thread = cr_dump_fdset_open(item->threads[i], CR_FD_DESC_CORE, NULL);
if (!cr_fdset_thread) if (!cr_fdset_thread)
goto err; goto err;
...@@ -1266,7 +1266,7 @@ static int dump_one_task(struct pstree_item *item, struct cr_fdset *cr_fdset) ...@@ -1266,7 +1266,7 @@ static int dump_one_task(struct pstree_item *item, struct cr_fdset *cr_fdset)
return dump_one_zombie(item, &pps_buf, cr_fdset); return dump_one_zombie(item, &pps_buf, cr_fdset);
ret = -1; ret = -1;
if (!cr_fdset_open(item->pid, CR_FD_DESC_TASK, cr_fdset)) if (!cr_dump_fdset_open(item->pid, CR_FD_DESC_TASK, cr_fdset))
goto err; goto err;
ret = collect_mappings(pid, pid_dir, &vma_area_list); ret = collect_mappings(pid, pid_dir, &vma_area_list);
...@@ -1383,12 +1383,12 @@ int cr_dump_tasks(pid_t pid, struct cr_options *opts) ...@@ -1383,12 +1383,12 @@ int cr_dump_tasks(pid_t pid, struct cr_options *opts)
collect_sockets(); collect_sockets();
list_for_each_entry(item, &pstree_list, list) { list_for_each_entry(item, &pstree_list, list) {
cr_fdset = cr_fdset_open(item->pid, CR_FD_DESC_NONE, NULL); cr_fdset = cr_dump_fdset_open(item->pid, CR_FD_DESC_NONE, NULL);
if (!cr_fdset) if (!cr_fdset)
goto err; goto err;
if (item->pid == pid) { if (item->pid == pid) {
if (!cr_fdset_open(item->pid, CR_FD_DESC_USE(CR_FD_PSTREE), cr_fdset)) if (!cr_dump_fdset_open(item->pid, CR_FD_DESC_USE(CR_FD_PSTREE), cr_fdset))
goto err; goto err;
if (dump_pstree(pid, &pstree_list, cr_fdset)) if (dump_pstree(pid, &pstree_list, cr_fdset))
goto err; goto err;
......
...@@ -535,7 +535,7 @@ static int cr_show_all(unsigned long pid, struct cr_options *opts) ...@@ -535,7 +535,7 @@ static int cr_show_all(unsigned long pid, struct cr_options *opts)
LIST_HEAD(pstree_list); LIST_HEAD(pstree_list);
int i, ret = -1; int i, ret = -1;
cr_fdset = prep_cr_fdset_for_restore(pid, CR_FD_DESC_PSTREE); cr_fdset = cr_show_fdset_open(pid, CR_FD_DESC_PSTREE);
if (!cr_fdset) if (!cr_fdset)
goto out; goto out;
...@@ -551,7 +551,7 @@ static int cr_show_all(unsigned long pid, struct cr_options *opts) ...@@ -551,7 +551,7 @@ static int cr_show_all(unsigned long pid, struct cr_options *opts)
list_for_each_entry(item, &pstree_list, list) { list_for_each_entry(item, &pstree_list, list) {
cr_fdset = prep_cr_fdset_for_restore(item->pid, CR_FD_DESC_TASK); cr_fdset = cr_show_fdset_open(item->pid, CR_FD_DESC_TASK);
if (!cr_fdset) if (!cr_fdset)
goto out; goto out;
...@@ -566,7 +566,7 @@ static int cr_show_all(unsigned long pid, struct cr_options *opts) ...@@ -566,7 +566,7 @@ static int cr_show_all(unsigned long pid, struct cr_options *opts)
if (item->threads[i] == item->pid) if (item->threads[i] == item->pid)
continue; continue;
cr_fdset_th = prep_cr_fdset_for_restore(item->threads[i], CR_FD_DESC_CORE); cr_fdset_th = cr_show_fdset_open(item->threads[i], CR_FD_DESC_CORE);
if (!cr_fdset_th) if (!cr_fdset_th)
goto out; goto out;
......
...@@ -157,7 +157,8 @@ void close_cr_fdset(struct cr_fdset **cr_fdset) ...@@ -157,7 +157,8 @@ void close_cr_fdset(struct cr_fdset **cr_fdset)
*cr_fdset = NULL; *cr_fdset = NULL;
} }
struct cr_fdset *cr_fdset_open(int pid, unsigned long use_mask, struct cr_fdset *cr_fdset) static struct cr_fdset *cr_fdset_open(int pid, unsigned long use_mask,
unsigned long flags, struct cr_fdset *cr_fdset)
{ {
struct cr_fdset *fdset; struct cr_fdset *fdset;
unsigned int i; unsigned int i;
...@@ -186,22 +187,38 @@ struct cr_fdset *cr_fdset_open(int pid, unsigned long use_mask, struct cr_fdset ...@@ -186,22 +187,38 @@ struct cr_fdset *cr_fdset_open(int pid, unsigned long use_mask, struct cr_fdset
if (ret) if (ret)
goto err; goto err;
ret = unlink(path); if (flags & O_EXCL) {
if (ret && errno != ENOENT) { ret = unlink(path);
pr_perror("Unable to unlink %s", path); if (ret && errno != ENOENT) {
goto err; pr_perror("Unable to unlink %s", path);
goto err;
}
} }
ret = open(path, O_RDWR | O_CREAT | O_EXCL, CR_FD_PERM); ret = open(path, flags, CR_FD_PERM);
if (ret < 0) { if (ret < 0) {
if (!(flags & O_CREAT))
/* caller should check himself */
continue;
pr_perror("Unable to open %s", path); pr_perror("Unable to open %s", path);
goto err; goto err;
} }
fdset->fds[i] = ret; fdset->fds[i] = ret;
pr_debug("Opened %s with %d\n", path, ret); pr_debug("Opened %s with %d\n", path, ret);
if (write_img(ret, &fdset_template[i].magic)) if (flags == O_RDONLY) {
goto err; u32 magic;
if (read_img(ret, &magic) < 0)
goto err;
if (magic != fdset_template[i].magic) {
pr_err("Magic doesn't match for %s\n", path);
goto err;
}
} else {
if (write_img(ret, &fdset_template[i].magic))
goto err;
}
} }
return fdset; return fdset;
...@@ -214,49 +231,16 @@ err: ...@@ -214,49 +231,16 @@ err:
return NULL; return NULL;
} }
struct cr_fdset *prep_cr_fdset_for_restore(int pid, unsigned long use_mask) struct cr_fdset *cr_dump_fdset_open(int pid, unsigned long use_mask,
struct cr_fdset *cr_fdset)
{ {
unsigned int i; return cr_fdset_open(pid, use_mask, O_RDWR | O_CREAT | O_EXCL,
int ret = -1; cr_fdset);
char path[PATH_MAX]; }
u32 magic;
struct cr_fdset *cr_fdset;
cr_fdset = alloc_cr_fdset();
if (!cr_fdset)
goto err;
for (i = 0; i < CR_FD_MAX; i++) {
if (!(use_mask & CR_FD_DESC_USE(i)))
continue;
ret = get_image_path(path, sizeof(path),
fdset_template[i].fmt, pid);
if (ret)
goto err;
ret = open(path, O_RDWR, CR_FD_PERM);
if (ret < 0) {
pr_perror("Unable to open %s", path);
goto err;
}
cr_fdset->fds[i] = ret;
pr_debug("Opened %s with %d\n", path, ret);
if (read_img(ret, &magic) < 0)
goto err;
if (magic != fdset_template[i].magic) {
pr_err("Magic doesn't match for %s\n", path);
goto err;
}
}
return cr_fdset;
err: struct cr_fdset *cr_show_fdset_open(int pid, unsigned long use_mask)
close_cr_fdset(&cr_fdset); {
return NULL; return cr_fdset_open(pid, use_mask, O_RDONLY, NULL);
} }
static int parse_ns_string(const char *ptr, unsigned int *flags) static int parse_ns_string(const char *ptr, unsigned int *flags)
......
...@@ -119,8 +119,8 @@ int cr_restore_tasks(pid_t pid, struct cr_options *opts); ...@@ -119,8 +119,8 @@ int cr_restore_tasks(pid_t pid, struct cr_options *opts);
int cr_show(unsigned long pid, struct cr_options *opts); int cr_show(unsigned long pid, struct cr_options *opts);
int convert_to_elf(char *elf_path, int fd_core); int convert_to_elf(char *elf_path, int fd_core);
struct cr_fdset *cr_fdset_open(int pid, unsigned long use_mask, struct cr_fdset *); struct cr_fdset *cr_dump_fdset_open(int pid, unsigned long use_mask, struct cr_fdset *);
struct cr_fdset *prep_cr_fdset_for_restore(int pid, unsigned long use_mask); struct cr_fdset *cr_show_fdset_open(int pid, unsigned long use_mask);
void close_cr_fdset(struct cr_fdset **cr_fdset); void close_cr_fdset(struct cr_fdset **cr_fdset);
void free_mappings(struct list_head *vma_area_list); void free_mappings(struct list_head *vma_area_list);
......
...@@ -33,7 +33,7 @@ static int do_dump_namespaces(int ns_pid, unsigned int ns_flags) ...@@ -33,7 +33,7 @@ static int do_dump_namespaces(int ns_pid, unsigned int ns_flags)
struct cr_fdset *fdset; struct cr_fdset *fdset;
int ret = 0; int ret = 0;
fdset = cr_fdset_open(ns_pid, CR_FD_DESC_NS, NULL); fdset = cr_dump_fdset_open(ns_pid, CR_FD_DESC_NS, NULL);
if (fdset == NULL) if (fdset == NULL)
return -1; return -1;
...@@ -118,7 +118,7 @@ int try_show_namespaces(int ns_pid) ...@@ -118,7 +118,7 @@ int try_show_namespaces(int ns_pid)
{ {
struct cr_fdset *fdset; struct cr_fdset *fdset;
fdset = prep_cr_fdset_for_restore(ns_pid, CR_FD_DESC_NS); fdset = cr_show_fdset_open(ns_pid, CR_FD_DESC_NS);
if (!fdset) if (!fdset)
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment