Skip to content

Commit

Permalink
util: allow to run criu under strace
Browse files Browse the repository at this point in the history
fork_and_ptrace_attach has to fork a child with CLONE_UNTRACED,
so that strace doesn't trace it.

Signed-off-by: Andrei Vagin <[email protected]>
  • Loading branch information
avagin committed Oct 5, 2023
1 parent 1e4f5fb commit 8eca587
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions criu/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,40 +661,54 @@ int cr_system_userns(int in, int out, int err, char *cmd, char *const argv[], un
return ret;
}

struct child_args {
int *sk_pair;
int (*child_setup)(void);
};

static int child_func(void *_args)
{
struct child_args *args = _args;
int sk, *sk_pair = args->sk_pair;
char c = 0;

sk = sk_pair[1];
close(sk_pair[0]);

if (args->child_setup && args->child_setup() != 0)
exit(1);

if (write(sk, &c, 1) != 1) {
pr_perror("write");
exit(1);
}

while (1)
sleep(1000);
exit(1);
}

pid_t fork_and_ptrace_attach(int (*child_setup)(void))
{
pid_t pid;
int sk_pair[2], sk;
char c = 0;
struct child_args cargs = {
.sk_pair = sk_pair,
.child_setup = child_setup,
};

if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair)) {
pr_perror("socketpair");
return -1;
}

pid = fork();
pid = clone_noasan(child_func, CLONE_UNTRACED | SIGCHLD, &cargs);
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]);

Expand Down

0 comments on commit 8eca587

Please sign in to comment.