util: move fork_and_ptrace_attach helper from cr-check

Signed-off-by: Alexander Mikhalitsyn <alexander.mikhalitsyn@virtuozzo.com>
This commit is contained in:
Alexander Mikhalitsyn 2021-12-21 21:17:16 +03:00 committed by Andrei Vagin
parent 8b3a76b640
commit ca54dfcac9
3 changed files with 60 additions and 57 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;