Radostin Stoyanov 46abdd7
From e4ccdc694a8739125da2c7e8e57f8a30c7451eaf Mon Sep 17 00:00:00 2001
Radostin Stoyanov 46abdd7
From: Adrian Reber <areber@redhat.com>
Radostin Stoyanov 46abdd7
Date: Tue, 14 Dec 2021 13:09:19 +0000
Radostin Stoyanov 46abdd7
Subject: [PATCH 081/120] tests: added test for single pre-dump support
Radostin Stoyanov 46abdd7
Radostin Stoyanov 46abdd7
Signed-off-by: Adrian Reber <areber@redhat.com>
Radostin Stoyanov 46abdd7
---
Radostin Stoyanov 46abdd7
 test/others/libcriu/.gitignore      |   1 +
Radostin Stoyanov 46abdd7
 test/others/libcriu/Makefile        |   1 +
Radostin Stoyanov 46abdd7
 test/others/libcriu/lib.h           |   2 +
Radostin Stoyanov 46abdd7
 test/others/libcriu/run.sh          |   1 +
Radostin Stoyanov 46abdd7
 test/others/libcriu/test_iters.c    |   2 -
Radostin Stoyanov 46abdd7
 test/others/libcriu/test_notify.c   |   2 -
Radostin Stoyanov 46abdd7
 test/others/libcriu/test_pre_dump.c | 151 ++++++++++++++++++++++++++++
Radostin Stoyanov 46abdd7
 test/others/libcriu/test_sub.c      |   2 -
Radostin Stoyanov 46abdd7
 8 files changed, 156 insertions(+), 6 deletions(-)
Radostin Stoyanov 46abdd7
 create mode 100644 test/others/libcriu/test_pre_dump.c
Radostin Stoyanov 46abdd7
Radostin Stoyanov 46abdd7
diff --git a/test/others/libcriu/.gitignore b/test/others/libcriu/.gitignore
Radostin Stoyanov 46abdd7
index cf1342de2..15abf07ac 100644
Radostin Stoyanov 46abdd7
--- a/test/others/libcriu/.gitignore
Radostin Stoyanov 46abdd7
+++ b/test/others/libcriu/.gitignore
Radostin Stoyanov 46abdd7
@@ -4,5 +4,6 @@ test_notify
Radostin Stoyanov 46abdd7
 test_self
Radostin Stoyanov 46abdd7
 test_sub
Radostin Stoyanov 46abdd7
 test_join_ns
Radostin Stoyanov 46abdd7
+test_pre_dump
Radostin Stoyanov 46abdd7
 output/
Radostin Stoyanov 46abdd7
 libcriu.so.*
Radostin Stoyanov 46abdd7
diff --git a/test/others/libcriu/Makefile b/test/others/libcriu/Makefile
Radostin Stoyanov 46abdd7
index 734e66c1a..581574da0 100644
Radostin Stoyanov 46abdd7
--- a/test/others/libcriu/Makefile
Radostin Stoyanov 46abdd7
+++ b/test/others/libcriu/Makefile
Radostin Stoyanov 46abdd7
@@ -6,6 +6,7 @@ TESTS += test_notify
Radostin Stoyanov 46abdd7
 TESTS += test_iters
Radostin Stoyanov 46abdd7
 TESTS += test_errno
Radostin Stoyanov 46abdd7
 TESTS += test_join_ns
Radostin Stoyanov 46abdd7
+TESTS += test_pre_dump
Radostin Stoyanov 46abdd7
 
Radostin Stoyanov 46abdd7
 all: $(TESTS)
Radostin Stoyanov 46abdd7
 .PHONY: all
Radostin Stoyanov 46abdd7
diff --git a/test/others/libcriu/lib.h b/test/others/libcriu/lib.h
Radostin Stoyanov 46abdd7
index 6fdf8aef2..59372fca5 100644
Radostin Stoyanov 46abdd7
--- a/test/others/libcriu/lib.h
Radostin Stoyanov 46abdd7
+++ b/test/others/libcriu/lib.h
Radostin Stoyanov 46abdd7
@@ -1,3 +1,5 @@
Radostin Stoyanov 46abdd7
 void what_err_ret_mean(int ret);
Radostin Stoyanov 46abdd7
 int chk_exit(int status, int want);
Radostin Stoyanov 46abdd7
 int get_version(void);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+#define SUCC_ECODE 42
Radostin Stoyanov 46abdd7
diff --git a/test/others/libcriu/run.sh b/test/others/libcriu/run.sh
Radostin Stoyanov 46abdd7
index 48f25a5f6..1b6c73448 100755
Radostin Stoyanov 46abdd7
--- a/test/others/libcriu/run.sh
Radostin Stoyanov 46abdd7
+++ b/test/others/libcriu/run.sh
Radostin Stoyanov 46abdd7
@@ -58,6 +58,7 @@ run_test test_notify
Radostin Stoyanov 46abdd7
 if [ "$(uname -m)" = "x86_64" ]; then
Radostin Stoyanov 46abdd7
 	# Skip this on aarch64 as aarch64 has no dirty page tracking
Radostin Stoyanov 46abdd7
 	run_test test_iters
Radostin Stoyanov 46abdd7
+	run_test test_pre_dump
Radostin Stoyanov 46abdd7
 fi
Radostin Stoyanov 46abdd7
 run_test test_errno
Radostin Stoyanov 46abdd7
 run_test test_join_ns
Radostin Stoyanov 46abdd7
diff --git a/test/others/libcriu/test_iters.c b/test/others/libcriu/test_iters.c
Radostin Stoyanov 46abdd7
index b7e325abb..edbaf87f6 100644
Radostin Stoyanov 46abdd7
--- a/test/others/libcriu/test_iters.c
Radostin Stoyanov 46abdd7
+++ b/test/others/libcriu/test_iters.c
Radostin Stoyanov 46abdd7
@@ -46,8 +46,6 @@ static int next_iter(criu_predump_info pi)
Radostin Stoyanov 46abdd7
 	return cur_iter < MAX_ITERS;
Radostin Stoyanov 46abdd7
 }
Radostin Stoyanov 46abdd7
 
Radostin Stoyanov 46abdd7
-#define SUCC_ECODE 42
Radostin Stoyanov 46abdd7
-
Radostin Stoyanov 46abdd7
 int main(int argc, char **argv)
Radostin Stoyanov 46abdd7
 {
Radostin Stoyanov 46abdd7
 	int pid, ret, p[2];
Radostin Stoyanov 46abdd7
diff --git a/test/others/libcriu/test_notify.c b/test/others/libcriu/test_notify.c
Radostin Stoyanov 46abdd7
index 9a54b812a..80ad3ffdc 100644
Radostin Stoyanov 46abdd7
--- a/test/others/libcriu/test_notify.c
Radostin Stoyanov 46abdd7
+++ b/test/others/libcriu/test_notify.c
Radostin Stoyanov 46abdd7
@@ -10,8 +10,6 @@
Radostin Stoyanov 46abdd7
 
Radostin Stoyanov 46abdd7
 #include "lib.h"
Radostin Stoyanov 46abdd7
 
Radostin Stoyanov 46abdd7
-#define SUCC_ECODE 42
Radostin Stoyanov 46abdd7
-
Radostin Stoyanov 46abdd7
 static int actions_called = 0;
Radostin Stoyanov 46abdd7
 static int notify(char *action, criu_notify_arg_t na)
Radostin Stoyanov 46abdd7
 {
Radostin Stoyanov 46abdd7
diff --git a/test/others/libcriu/test_pre_dump.c b/test/others/libcriu/test_pre_dump.c
Radostin Stoyanov 46abdd7
new file mode 100644
Radostin Stoyanov 46abdd7
index 000000000..ed9cd2125
Radostin Stoyanov 46abdd7
--- /dev/null
Radostin Stoyanov 46abdd7
+++ b/test/others/libcriu/test_pre_dump.c
Radostin Stoyanov 46abdd7
@@ -0,0 +1,151 @@
Radostin Stoyanov 46abdd7
+#include "criu.h"
Radostin Stoyanov 46abdd7
+#include <fcntl.h>
Radostin Stoyanov 46abdd7
+#include <stdio.h>
Radostin Stoyanov 46abdd7
+#include <errno.h>
Radostin Stoyanov 46abdd7
+#include <stdlib.h>
Radostin Stoyanov 46abdd7
+#include <signal.h>
Radostin Stoyanov 46abdd7
+#include <fcntl.h>
Radostin Stoyanov 46abdd7
+#include <sys/stat.h>
Radostin Stoyanov 46abdd7
+#include <sys/wait.h>
Radostin Stoyanov 46abdd7
+#include <unistd.h>
Radostin Stoyanov 46abdd7
+#include "lib.h"
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+static int wdir_fd, cur_imgdir = -1;
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+static int stop = 0;
Radostin Stoyanov 46abdd7
+static void sh(int sig)
Radostin Stoyanov 46abdd7
+{
Radostin Stoyanov 46abdd7
+	stop = 1;
Radostin Stoyanov 46abdd7
+}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+static void open_imgdir(void)
Radostin Stoyanov 46abdd7
+{
Radostin Stoyanov 46abdd7
+	char p[10];
Radostin Stoyanov 46abdd7
+	static int id = 0;
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	if (id > 0) {
Radostin Stoyanov 46abdd7
+		sprintf(p, "../dir-%d", id);
Radostin Stoyanov 46abdd7
+		criu_set_parent_images(p);
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+	if (cur_imgdir != -1)
Radostin Stoyanov 46abdd7
+		close(cur_imgdir);
Radostin Stoyanov 46abdd7
+	sprintf(p, "dir-%d", ++id);
Radostin Stoyanov 46abdd7
+	mkdirat(wdir_fd, p, 0700);
Radostin Stoyanov 46abdd7
+	cur_imgdir = openat(wdir_fd, p, O_DIRECTORY);
Radostin Stoyanov 46abdd7
+	criu_set_images_dir_fd(cur_imgdir);
Radostin Stoyanov 46abdd7
+}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+int main(int argc, char **argv)
Radostin Stoyanov 46abdd7
+{
Radostin Stoyanov 46abdd7
+	int pid, ret, p[2];
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	wdir_fd = open(argv[2], O_DIRECTORY);
Radostin Stoyanov 46abdd7
+	if (wdir_fd < 0) {
Radostin Stoyanov 46abdd7
+		perror("Can't open wdir");
Radostin Stoyanov 46abdd7
+		return 1;
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	printf("--- Start loop ---\n");
Radostin Stoyanov 46abdd7
+	pipe(p);
Radostin Stoyanov 46abdd7
+	pid = fork();
Radostin Stoyanov 46abdd7
+	if (pid < 0) {
Radostin Stoyanov 46abdd7
+		perror("Can't");
Radostin Stoyanov 46abdd7
+		return -1;
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	if (!pid) {
Radostin Stoyanov 46abdd7
+		printf("   `- loop: initializing\n");
Radostin Stoyanov 46abdd7
+		if (setsid() < 0)
Radostin Stoyanov 46abdd7
+			exit(1);
Radostin Stoyanov 46abdd7
+		if (signal(SIGUSR1, sh) == SIG_ERR)
Radostin Stoyanov 46abdd7
+			exit(1);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+		close(0);
Radostin Stoyanov 46abdd7
+		close(1);
Radostin Stoyanov 46abdd7
+		close(2);
Radostin Stoyanov 46abdd7
+		close(p[0]);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+		ret = SUCC_ECODE;
Radostin Stoyanov 46abdd7
+		write(p[1], &ret, sizeof(ret));
Radostin Stoyanov 46abdd7
+		close(p[1]);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+		while (!stop)
Radostin Stoyanov 46abdd7
+			sleep(1);
Radostin Stoyanov 46abdd7
+		exit(SUCC_ECODE);
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	close(p[1]);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	/* Wait for kid to start */
Radostin Stoyanov 46abdd7
+	ret = -1;
Radostin Stoyanov 46abdd7
+	read(p[0], &ret, sizeof(ret));
Radostin Stoyanov 46abdd7
+	if (ret != SUCC_ECODE) {
Radostin Stoyanov 46abdd7
+		printf("Error starting loop\n");
Radostin Stoyanov 46abdd7
+		goto err;
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	/* Wait for pipe to get closed, then dump */
Radostin Stoyanov 46abdd7
+	read(p[0], &ret, 1);
Radostin Stoyanov 46abdd7
+	close(p[0]);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	printf("--- Dump loop ---\n");
Radostin Stoyanov 46abdd7
+	criu_init_opts();
Radostin Stoyanov 46abdd7
+	criu_set_service_binary(argv[1]);
Radostin Stoyanov 46abdd7
+	criu_set_pid(pid);
Radostin Stoyanov 46abdd7
+	criu_set_log_file("dump.log");
Radostin Stoyanov 46abdd7
+	criu_set_log_level(CRIU_LOG_DEBUG);
Radostin Stoyanov 46abdd7
+	criu_set_track_mem(true);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	open_imgdir();
Radostin Stoyanov 46abdd7
+	ret = criu_pre_dump();
Radostin Stoyanov 46abdd7
+	if (ret < 0) {
Radostin Stoyanov 46abdd7
+		what_err_ret_mean(ret);
Radostin Stoyanov 46abdd7
+		kill(pid, SIGKILL);
Radostin Stoyanov 46abdd7
+		goto err;
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	printf("   `- Pre Dump 1 succeeded\n");
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	open_imgdir();
Radostin Stoyanov 46abdd7
+	ret = criu_pre_dump();
Radostin Stoyanov 46abdd7
+	if (ret < 0) {
Radostin Stoyanov 46abdd7
+		what_err_ret_mean(ret);
Radostin Stoyanov 46abdd7
+		kill(pid, SIGKILL);
Radostin Stoyanov 46abdd7
+		goto err;
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	printf("   `- Pre Dump 2 succeeded\n");
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	open_imgdir();
Radostin Stoyanov 46abdd7
+	ret = criu_dump();
Radostin Stoyanov 46abdd7
+	if (ret < 0) {
Radostin Stoyanov 46abdd7
+		what_err_ret_mean(ret);
Radostin Stoyanov 46abdd7
+		kill(pid, SIGKILL);
Radostin Stoyanov 46abdd7
+		goto err;
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	printf("   `- Final Dump succeeded\n");
Radostin Stoyanov 46abdd7
+	waitpid(pid, NULL, 0);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	printf("--- Restore ---\n");
Radostin Stoyanov 46abdd7
+	criu_init_opts();
Radostin Stoyanov 46abdd7
+	criu_set_log_level(CRIU_LOG_DEBUG);
Radostin Stoyanov 46abdd7
+	criu_set_log_file("restore.log");
Radostin Stoyanov 46abdd7
+	criu_set_images_dir_fd(cur_imgdir);
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	pid = criu_restore_child();
Radostin Stoyanov 46abdd7
+	if (pid <= 0) {
Radostin Stoyanov 46abdd7
+		what_err_ret_mean(pid);
Radostin Stoyanov 46abdd7
+		return -1;
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	printf("   `- Restore returned pid %d\n", pid);
Radostin Stoyanov 46abdd7
+	kill(pid, SIGUSR1);
Radostin Stoyanov 46abdd7
+err:
Radostin Stoyanov 46abdd7
+	if (waitpid(pid, &ret, 0) < 0) {
Radostin Stoyanov 46abdd7
+		perror("   Can't wait kid");
Radostin Stoyanov 46abdd7
+		return -1;
Radostin Stoyanov 46abdd7
+	}
Radostin Stoyanov 46abdd7
+
Radostin Stoyanov 46abdd7
+	return chk_exit(ret, SUCC_ECODE);
Radostin Stoyanov 46abdd7
+}
Radostin Stoyanov 46abdd7
diff --git a/test/others/libcriu/test_sub.c b/test/others/libcriu/test_sub.c
Radostin Stoyanov 46abdd7
index 697abf5d5..af1e09408 100644
Radostin Stoyanov 46abdd7
--- a/test/others/libcriu/test_sub.c
Radostin Stoyanov 46abdd7
+++ b/test/others/libcriu/test_sub.c
Radostin Stoyanov 46abdd7
@@ -15,8 +15,6 @@ static void sh(int sig)
Radostin Stoyanov 46abdd7
 	stop = 1;
Radostin Stoyanov 46abdd7
 }
Radostin Stoyanov 46abdd7
 
Radostin Stoyanov 46abdd7
-#define SUCC_ECODE 42
Radostin Stoyanov 46abdd7
-
Radostin Stoyanov 46abdd7
 int main(int argc, char **argv)
Radostin Stoyanov 46abdd7
 {
Radostin Stoyanov 46abdd7
 	int pid, ret, fd, p[2];
Radostin Stoyanov 46abdd7
-- 
Radostin Stoyanov 46abdd7
2.34.1
Radostin Stoyanov 46abdd7