Commit a877a894 authored by Kirill Tkhai's avatar Kirill Tkhai Committed by Andrei Vagin

util: add a function to run an action is a child process

The action is run in a very lightweight process.
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent b5225915
...@@ -344,4 +344,6 @@ extern int epoll_del_rfd(int epfd, struct epoll_rfd *rfd); ...@@ -344,4 +344,6 @@ extern int epoll_del_rfd(int epfd, struct epoll_rfd *rfd);
extern int epoll_run_rfds(int epfd, struct epoll_event *evs, int nr_fds, int tmo); extern int epoll_run_rfds(int epfd, struct epoll_event *evs, int nr_fds, int tmo);
extern int epoll_prepare(int nr_events, struct epoll_event **evs); extern int epoll_prepare(int nr_events, struct epoll_event **evs);
extern int call_in_child_process(int (*fn)(void *), void *arg);
#endif /* __CR_UTIL_H__ */ #endif /* __CR_UTIL_H__ */
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
#include "namespaces.h" #include "namespaces.h"
#include "criu-log.h" #include "criu-log.h"
#include "clone-noasan.h"
#include "cr_options.h" #include "cr_options.h"
#include "servicefd.h" #include "servicefd.h"
#include "cr-service.h" #include "cr-service.h"
...@@ -1381,3 +1382,32 @@ free_events: ...@@ -1381,3 +1382,32 @@ free_events:
xfree(*events); xfree(*events);
return -1; return -1;
} }
int call_in_child_process(int (*fn)(void *), void *arg)
{
int status, ret = -1;
pid_t pid;
/*
* Parent freezes till child exit, so child may use the same stack.
* No SIGCHLD flag, so it's not need to block signal.
*/
pid = clone_noasan(fn, CLONE_VFORK | CLONE_VM | CLONE_FILES |
CLONE_IO | CLONE_SIGHAND | CLONE_SYSVSEM, arg);
if (pid == -1) {
pr_perror("Can't clone");
return -1;
}
errno = 0;
if (waitpid(pid, &status, __WALL) != pid || !WIFEXITED(status) || WEXITSTATUS(status)) {
pr_err("Can't wait or bad status: errno=%d, status=%d\n", errno, status);
goto out;
}
ret = 0;
/*
* Child opened PROC_SELF for pid. If we create one more child
* with the same pid later, it will try to reuse this /proc/self.
*/
out:
close_pid_proc();
return ret;
}
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