mirror of
https://github.com/checkpoint-restore/criu
synced 2025-08-30 13:58:34 +00:00
restore: Switch to the new ns_last_pid kernel facility while forking children
Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org> Acked-by: Pavel Emelyanov <xemul@parallels.com>
This commit is contained in:
54
cr-restore.c
54
cr-restore.c
@@ -16,6 +16,7 @@
|
|||||||
#include <sys/ptrace.h>
|
#include <sys/ptrace.h>
|
||||||
#include <sys/user.h>
|
#include <sys/user.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
#include <sys/file.h>
|
||||||
|
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
|
|
||||||
@@ -1154,29 +1155,50 @@ static int restore_one_task(int pid)
|
|||||||
return prepare_and_sigreturn(pid);
|
return prepare_and_sigreturn(pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_child(void *arg)
|
|
||||||
{
|
|
||||||
return restore_task_with_children(getpid(), arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int fork_with_pid(int pid, char *pstree_path)
|
static inline int fork_with_pid(int pid, char *pstree_path)
|
||||||
{
|
{
|
||||||
const int stack_size = 32 << 10;
|
int ret = -1, fd = -1;
|
||||||
int ret = 0;
|
char buf[32];
|
||||||
void *stack;
|
|
||||||
|
|
||||||
stack = mmap(0, stack_size, PROT_READ | PROT_WRITE,
|
snprintf(buf, sizeof(buf), "%d", pid - 1);
|
||||||
MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, 0, 0);
|
|
||||||
if (stack == MAP_FAILED) {
|
fd = open(LAST_PID_PATH, O_RDWR);
|
||||||
pr_perror("%d: mmap failed\n", pid);
|
if (fd < 0) {
|
||||||
|
pr_perror("%d: Can't open %s\n", pid, LAST_PID_PATH);
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flock(fd, LOCK_EX)) {
|
||||||
|
pr_perror("%d: Can't lock %s\n", pid, LAST_PID_PATH);
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
write_safe(fd, buf, strlen(buf), err_unlock);
|
||||||
|
|
||||||
|
ret = fork();
|
||||||
|
if (ret < 0) {
|
||||||
|
pr_perror("Can't fork for %d\n", pid);
|
||||||
|
goto err_unlock;
|
||||||
|
} else if (!ret) {
|
||||||
|
int my_pid = getpid();
|
||||||
|
|
||||||
|
close_safe(&fd);
|
||||||
|
|
||||||
|
if (my_pid != pid) {
|
||||||
|
pr_err("%d: Pids do not match got %d but expected %d\n",
|
||||||
|
my_pid, my_pid, pid);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
stack += stack_size;
|
return restore_task_with_children(my_pid, pstree_path);
|
||||||
ret = clone(do_child, stack, SIGCHLD | CLONE_CHILD_USEPID, pstree_path, NULL, NULL, &pid);
|
}
|
||||||
if (ret < 0)
|
|
||||||
pr_perror("%d: clone failed\n", pid);
|
|
||||||
|
|
||||||
|
err_unlock:
|
||||||
|
if (flock(fd, LOCK_UN))
|
||||||
|
pr_perror("%d: Can't unlock %s\n", pid, LAST_PID_PATH);
|
||||||
|
|
||||||
|
err:
|
||||||
|
close_safe(&fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -54,6 +54,8 @@ struct cr_fd_desc_tmpl {
|
|||||||
#define FMT_FNAME_VMAS "vmas-%d.img"
|
#define FMT_FNAME_VMAS "vmas-%d.img"
|
||||||
#define FMT_FNAME_SIGACTS "sigacts-%d.img"
|
#define FMT_FNAME_SIGACTS "sigacts-%d.img"
|
||||||
|
|
||||||
|
#define LAST_PID_PATH "/proc/sys/kernel/ns_last_pid"
|
||||||
|
|
||||||
/* file descriptors */
|
/* file descriptors */
|
||||||
struct cr_fd_desc {
|
struct cr_fd_desc {
|
||||||
struct cr_fd_desc_tmpl *tmpl; /* template we refer to */
|
struct cr_fd_desc_tmpl *tmpl; /* template we refer to */
|
||||||
|
Reference in New Issue
Block a user