Commit 1f147669 authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by Cyrill Gorcunov

Generalize file opening

Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@gmail.com>
parent 5a397e8a
...@@ -34,10 +34,25 @@ ...@@ -34,10 +34,25 @@
#include "parasite-syscall.h" #include "parasite-syscall.h"
#include "parasite-blob.h" #include "parasite-blob.h"
#include <stdarg.h>
#ifndef CONFIG_X86_64 #ifndef CONFIG_X86_64
# error No x86-32 support yet # error No x86-32 support yet
#endif #endif
static FILE *fopen_proc(char *fmt, char *mode, ...)
{
va_list args;
char fname[128];
sprintf(fname, "/proc/");
va_start(args, mode);
vsnprintf(fname + 6, sizeof(fname) - 6, fmt, args);
va_end(args);
return fopen(fname, mode);
}
static LIST_HEAD(vma_area_list); static LIST_HEAD(vma_area_list);
static LIST_HEAD(pstree_list); static LIST_HEAD(pstree_list);
...@@ -291,21 +306,18 @@ static int dump_one_fd(char *pid_fd_dir, int dir, char *fd_name, unsigned long p ...@@ -291,21 +306,18 @@ static int dump_one_fd(char *pid_fd_dir, int dir, char *fd_name, unsigned long p
static int read_fd_params(pid_t pid, char *fd, unsigned long *pos, unsigned int *flags) static int read_fd_params(pid_t pid, char *fd, unsigned long *pos, unsigned int *flags)
{ {
char fd_str[128];
FILE *file; FILE *file;
snprintf(fd_str, sizeof(fd_str), "/proc/%d/fdinfo/%s", pid, fd); file = fopen_proc("%d/fdinfo/%s", "r", pid, fd);
file = fopen(fd_str, "r");
if (!file) { if (!file) {
pr_perror("Can't open %s\n", fd_str); pr_perror("Can't open %d's %s fdinfo\n", pid, fd);
return -1; return -1;
} }
fscanf(file, "pos:\t%li\nflags:\t%o\n", pos, flags); fscanf(file, "pos:\t%li\nflags:\t%o\n", pos, flags);
fclose(file); fclose(file);
pr_info("%s: pos: %16lx flags: %16lx\n", fd_str, *pos, *flags); pr_info("%d fdinfo %s: pos: %16lx flags: %16lx\n", pid, fd, *pos, *flags);
return 0; return 0;
} }
...@@ -428,10 +440,9 @@ static int get_task_stat(pid_t pid, u8 *comm, u32 *flags, ...@@ -428,10 +440,9 @@ static int get_task_stat(pid_t pid, u8 *comm, u32 *flags,
* '0' symbol at argument 20 in format string. * '0' symbol at argument 20 in format string.
*/ */
snprintf(loc_buf, sizeof(loc_buf), "/proc/%d/stat", pid); file = fopen_proc("%d/stat", "r", pid);
file = fopen(loc_buf, "r");
if (!file) { if (!file) {
pr_perror("Can't open %s", loc_buf); pr_perror("Can't open %d stat", pid);
goto err; goto err;
} }
...@@ -527,10 +538,9 @@ static int get_task_personality(pid_t pid, u32 *personality) ...@@ -527,10 +538,9 @@ static int get_task_personality(pid_t pid, u32 *personality)
FILE *file = NULL; FILE *file = NULL;
int ret = -1; int ret = -1;
snprintf(loc_buf, sizeof(loc_buf), "/proc/%d/personality", pid); file = fopen_proc("%d/personality", "r", pid);
file = fopen(loc_buf, "r");
if (!file) { if (!file) {
perror("Can't open task personality"); pr_perror("Can't open %d personality", pid);
goto err; goto err;
} }
...@@ -558,10 +568,9 @@ static int dump_task_tls(pid_t pid, struct desc_struct *tls_array, int size) ...@@ -558,10 +568,9 @@ static int dump_task_tls(pid_t pid, struct desc_struct *tls_array, int size)
goto err; goto err;
} }
snprintf(loc_buf, sizeof(loc_buf), "/proc/%d/tls", pid); file = fopen_proc("%d/tls", "r", pid);
file = fopen(loc_buf, "r");
if (!file) { if (!file) {
perror("Can't open task tls"); pr_perror("Can't open %d tls", pid);
goto err; goto err;
} }
...@@ -727,10 +736,9 @@ static struct pstree_item *find_children(pid_t pid) ...@@ -727,10 +736,9 @@ static struct pstree_item *find_children(pid_t pid)
pr_debug("pid: %d\n", pid); pr_debug("pid: %d\n", pid);
snprintf(loc_buf, sizeof(loc_buf), "/proc/%d/status", pid); file = fopen_proc("%d/status", "r", pid);
file = fopen(loc_buf, "r");
if (!file) { if (!file) {
perror("Can't open task status"); pr_perror("Can't open %d status", pid);
goto err; goto err;
} }
......
...@@ -82,6 +82,14 @@ static int nr_pipes; ...@@ -82,6 +82,14 @@ static int nr_pipes;
static int restore_task_with_children(int my_pid, char *pstree_path); static int restore_task_with_children(int my_pid, char *pstree_path);
static int open_fmt(char *fmt, int pid, int mode)
{
char fname[64];
snprintf(fname, sizeof(fname), fmt, pid);
return open(fname, mode);
}
static void show_saved_shmems(void) static void show_saved_shmems(void)
{ {
int i; int i;
...@@ -282,12 +290,10 @@ static int collect_pipe(int pid, struct pipe_entry *e, int p_fd) ...@@ -282,12 +290,10 @@ static int collect_pipe(int pid, struct pipe_entry *e, int p_fd)
static int prepare_shmem_pid(int pid) static int prepare_shmem_pid(int pid)
{ {
char path[64];
int sh_fd; int sh_fd;
u32 type = 0; u32 type = 0;
sprintf(path, "shmem-%d.img", pid); sh_fd = open_fmt("shmem-%d.img", pid, O_RDONLY);
sh_fd = open(path, O_RDONLY);
if (sh_fd < 0) { if (sh_fd < 0) {
perror("Can't open shmem info"); perror("Can't open shmem info");
return 1; return 1;
...@@ -322,12 +328,10 @@ static int prepare_shmem_pid(int pid) ...@@ -322,12 +328,10 @@ static int prepare_shmem_pid(int pid)
static int prepare_pipes_pid(int pid) static int prepare_pipes_pid(int pid)
{ {
char path[64];
int p_fd; int p_fd;
u32 type = 0; u32 type = 0;
sprintf(path, "pipes-%d.img", pid); p_fd = open_fmt("pipes-%d.img", pid, O_RDONLY);
p_fd = open(path, O_RDONLY);
if (p_fd < 0) { if (p_fd < 0) {
perror("Can't open pipes image"); perror("Can't open pipes image");
return 1; return 1;
...@@ -347,8 +351,8 @@ static int prepare_pipes_pid(int pid) ...@@ -347,8 +351,8 @@ static int prepare_pipes_pid(int pid)
if (ret == 0) if (ret == 0)
break; break;
if (ret != sizeof(e)) { if (ret != sizeof(e)) {
pr_perror("Read pipes for %s failed %d of %li read\n", pr_perror("Read pipes for %d failed %d of %li read\n",
path, ret, sizeof(e)); pid, ret, sizeof(e));
return 1; return 1;
} }
...@@ -504,21 +508,19 @@ static int open_fmap(int pid, struct fdinfo_entry *fe, int fd) ...@@ -504,21 +508,19 @@ static int open_fmap(int pid, struct fdinfo_entry *fe, int fd)
static int prepare_fds(int pid) static int prepare_fds(int pid)
{ {
u32 mag; u32 mag;
char path[64];
int fdinfo_fd; int fdinfo_fd;
sprintf(path, "fdinfo-%d.img", pid); pr_info("%d: Opening files img\n", pid);
pr_info("%d: Opening files in %s\n", pid, path);
fdinfo_fd = open(path, O_RDONLY); fdinfo_fd = open_fmt("fdinfo-%d.img", pid, O_RDONLY);
if (fdinfo_fd < 0) { if (fdinfo_fd < 0) {
pr_perror("Can't open %s", path); pr_perror("Can't open %d fdinfo", pid);
return 1; return 1;
} }
read(fdinfo_fd, &mag, 4); read(fdinfo_fd, &mag, 4);
if (mag != FDINFO_MAGIC) { if (mag != FDINFO_MAGIC) {
pr_err("Bad file magic number in %s\n", path); pr_err("Bad %d fdinfo magic number\n", pid);
return 1; return 1;
} }
...@@ -533,12 +535,12 @@ static int prepare_fds(int pid) ...@@ -533,12 +535,12 @@ static int prepare_fds(int pid)
} }
if (ret < 0) { if (ret < 0) {
pr_perror("Error reading %s\n", path); pr_perror("Error reading %d fdinfo\n", pid);
return 1; return 1;
} }
if (ret != sizeof(fe)) { if (ret != sizeof(fe)) {
pr_err("Corrupted file %s\n", path); pr_err("Corrupted %d fdinfo\n", pid);
return 1; return 1;
} }
...@@ -554,7 +556,7 @@ static int prepare_fds(int pid) ...@@ -554,7 +556,7 @@ static int prepare_fds(int pid)
return 1; return 1;
break; break;
default: default:
pr_err("Unknown type in %s\n", path); pr_err("Unknown %d fdinfo file type\n", pid);
return 1; return 1;
} }
} }
...@@ -586,12 +588,10 @@ static void save_shmem_id(struct shmem_entry *e) ...@@ -586,12 +588,10 @@ static void save_shmem_id(struct shmem_entry *e)
static int prepare_shmem(int pid) static int prepare_shmem(int pid)
{ {
char path[64];
int sh_fd; int sh_fd;
u32 type = 0; u32 type = 0;
sprintf(path, "shmem-%d.img", pid); sh_fd = open_fmt("shmem-%d.img", pid, O_RDONLY);
sh_fd = open(path, O_RDONLY);
if (sh_fd < 0) { if (sh_fd < 0) {
perror("Can't open shmem info"); perror("Can't open shmem info");
return 1; return 1;
...@@ -739,23 +739,21 @@ static char zpage[PAGE_SIZE]; ...@@ -739,23 +739,21 @@ static char zpage[PAGE_SIZE];
static int fixup_pages_data(int pid, int fd) static int fixup_pages_data(int pid, int fd)
{ {
char path[128];
int shfd; int shfd;
u32 magic; u32 magic;
u64 va; u64 va;
sprintf(path, "pages-shmem-%d.img", pid); pr_info("%d: Reading shmem pages img\n", pid);
pr_info("%d: Reading shmem pages from %s\n", pid, path);
shfd = open(path, O_RDONLY); shfd = open_fmt("pages-shmem-%d.img", pid, O_RDONLY);
if (shfd < 0) { if (shfd < 0) {
pr_perror("Can't open shmem image %s", path); pr_perror("Can't open %d shmem image %s", pid);
return 1; return 1;
} }
read(shfd, &magic, sizeof(magic)); read(shfd, &magic, sizeof(magic));
if (magic != PAGES_MAGIC) { if (magic != PAGES_MAGIC) {
pr_err("Bad shmem file magic number %s\n", path); pr_err("Bad %d shmem file magic number\n", pid);
return 1; return 1;
} }
...@@ -829,8 +827,7 @@ static int execute_image(int pid) ...@@ -829,8 +827,7 @@ static int execute_image(int pid)
int fd, fd_new; int fd, fd_new;
struct stat buf; struct stat buf;
sprintf(path, "core-%d.img", pid); fd = open_fmt("core-%d.img", pid, O_RDONLY);
fd = open(path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
perror("Can't open exec image"); perror("Can't open exec image");
return 1; return 1;
...@@ -1032,14 +1029,12 @@ static int open_pipe(int pid, struct pipe_entry *e, int *pipes_fd) ...@@ -1032,14 +1029,12 @@ static int open_pipe(int pid, struct pipe_entry *e, int *pipes_fd)
static int prepare_pipes(int pid) static int prepare_pipes(int pid)
{ {
char path[64];
int pipes_fd; int pipes_fd;
u32 type = 0; u32 type = 0;
pr_info("%d: Opening pipes\n", pid); pr_info("%d: Opening pipes\n", pid);
sprintf(path, "pipes-%d.img", pid); pipes_fd = open_fmt("pipes-%d.img", pid, O_RDONLY);
pipes_fd = open(path, O_RDONLY);
if (pipes_fd < 0) { if (pipes_fd < 0) {
perror("Can't open pipes img"); perror("Can't open pipes img");
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