diff options
author | Alexander Mikhalitsyn <alexander.mikhalitsyn@virtuozzo.com> | 2021-12-21 21:17:16 +0300 |
---|---|---|
committer | Andrei Vagin <avagin@gmail.com> | 2022-04-29 03:53:52 +0300 |
commit | ca54dfcac9aac6306756d14fb5ee6c8320fd06a3 (patch) | |
tree | 1540b9dac90c77e78753fea3aa35ff28e0fbcc64 | |
parent | 8b3a76b6400bf71d4156a560f1910165dafc8674 (diff) |
util: move fork_and_ptrace_attach helper from cr-check
Signed-off-by: Alexander Mikhalitsyn <alexander.mikhalitsyn@virtuozzo.com>
-rw-r--r-- | criu/cr-check.c | 57 | ||||
-rw-r--r-- | criu/include/util.h | 1 | ||||
-rw-r--r-- | criu/util.c | 59 |
3 files changed, 60 insertions, 57 deletions
diff --git a/criu/cr-check.c b/criu/cr-check.c index 52393cc8b..381184374 100644 --- a/criu/cr-check.c +++ b/criu/cr-check.c @@ -537,63 +537,6 @@ static int check_sigqueuinfo(void) return 0; } -static pid_t fork_and_ptrace_attach(int (*child_setup)(void)) -{ - pid_t pid; - int sk_pair[2], sk; - char c = 0; - - if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair)) { - pr_perror("socketpair"); - return -1; - } - - pid = fork(); - if (pid < 0) { - pr_perror("fork"); - return -1; - } else if (pid == 0) { - sk = sk_pair[1]; - close(sk_pair[0]); - - if (child_setup && child_setup() != 0) - exit(1); - - if (write(sk, &c, 1) != 1) { - pr_perror("write"); - exit(1); - } - - while (1) - sleep(1000); - exit(1); - } - - sk = sk_pair[0]; - close(sk_pair[1]); - - if (read(sk, &c, 1) != 1) { - close(sk); - kill(pid, SIGKILL); - waitpid(pid, NULL, 0); - pr_perror("read"); - return -1; - } - - close(sk); - - if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) { - pr_perror("Unable to ptrace the child"); - kill(pid, SIGKILL); - waitpid(pid, NULL, 0); - return -1; - } - - waitpid(pid, NULL, 0); - - return pid; -} - static int check_ptrace_peeksiginfo(void) { struct ptrace_peeksiginfo_args arg; diff --git a/criu/include/util.h b/criu/include/util.h index 5ccd24722..4e29c079e 100644 --- a/criu/include/util.h +++ b/criu/include/util.h @@ -166,6 +166,7 @@ extern int is_anon_link_type(char *link, char *type); extern int cr_system(int in, int out, int err, char *cmd, char *const argv[], unsigned flags); extern int cr_system_userns(int in, int out, int err, char *cmd, char *const argv[], unsigned flags, int userns_pid); +extern pid_t fork_and_ptrace_attach(int (*child_setup)(void)); extern int cr_daemon(int nochdir, int noclose, int close_fd); extern int status_ready(void); extern int is_root_user(void); diff --git a/criu/util.c b/criu/util.c index 5c075cd15..40b12bace 100644 --- a/criu/util.c +++ b/criu/util.c @@ -660,6 +660,65 @@ out: return ret; } +pid_t fork_and_ptrace_attach(int (*child_setup)(void)) +{ + pid_t pid; + int sk_pair[2], sk; + char c = 0; + + if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair)) { + pr_perror("socketpair"); + return -1; + } + + pid = fork(); + if (pid < 0) { + pr_perror("fork"); + return -1; + } + + if (pid == 0) { + sk = sk_pair[1]; + close(sk_pair[0]); + + if (child_setup && child_setup() != 0) + exit(1); + + if (write(sk, &c, 1) != 1) { + pr_perror("write"); + exit(1); + } + + while (1) + sleep(1000); + exit(1); + } + + sk = sk_pair[0]; + close(sk_pair[1]); + + if (read(sk, &c, 1) != 1) { + close(sk); + kill(pid, SIGKILL); + waitpid(pid, NULL, 0); + pr_perror("read"); + return -1; + } + + close(sk); + + if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) { + pr_perror("Unable to ptrace the child"); + kill(pid, SIGKILL); + waitpid(pid, NULL, 0); + return -1; + } + + waitpid(pid, NULL, 0); + + return pid; +} + int status_ready(void) { char c = 0; |