diff --git a/criu/image.c b/criu/image.c index 922248205..cfc7f5f30 100644 --- a/criu/image.c +++ b/criu/image.c @@ -327,14 +327,20 @@ static int img_write_magic(struct cr_img *img, int oflags, int type) int do_open_remote_image(int dfd, char *path, int flags) { char *snapshot_id = NULL; - int ret; + int ret, save; /* When using namespaces, the current dir is changed so we need to * change to previous working dir and back to correctly open the image * proxy and cache sockets. */ - int save = dirfd(opendir(".")); + save = open(".", O_RDONLY); + if (save < 0) { + pr_perror("unable to open current working directory"); + return -1; + } + if (fchdir(get_service_fd(IMG_FD_OFF)) < 0) { - pr_debug("fchdir to dfd failed!\n"); + pr_perror("fchdir to dfd failed!\n"); + close(save); return -1; } @@ -353,7 +359,8 @@ int do_open_remote_image(int dfd, char *path, int flags) } if (fchdir(save) < 0) { - pr_debug("fchdir to save failed!\n"); + pr_perror("fchdir to save failed"); + close(save); return -1; } close(save); diff --git a/criu/util.c b/criu/util.c index 6b3eda69a..f58554b56 100644 --- a/criu/util.c +++ b/criu/util.c @@ -564,9 +564,15 @@ int copy_file(int fd_in, int fd_out, size_t bytes) { ssize_t written = 0; size_t chunk = bytes ? bytes : 4096; - char *buffer = (char*) malloc(chunk); + char *buffer; ssize_t ret; + buffer = xmalloc(chunk); + if (buffer == NULL) { + pr_perror("failed to allocate buffer to copy file"); + return -1; + } + while (1) { if (opts.remote) { ret = read(fd_in, buffer, chunk); @@ -602,7 +608,7 @@ int copy_file(int fd_in, int fd_out, size_t bytes) written += ret; } err: - free(buffer); + xfree(buffer); return ret; }