[CRIU] [PATCH 5/5] test: libcriu

Ruslan Kuprieiev kupruser at gmail.com
Tue Dec 17 13:04:41 PST 2013


This test is similiar to test/rpc, and can also be used as an
example of using libcriu.

Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
---
 Makefile              |  1 +
 test/libcriu/Makefile | 10 ++++++
 test/libcriu/loop.sh  |  4 +++
 test/libcriu/run.sh   | 63 ++++++++++++++++++++++++++++++++++
 test/libcriu/test.c   | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 172 insertions(+)
 create mode 100644 test/libcriu/Makefile
 create mode 100755 test/libcriu/loop.sh
 create mode 100755 test/libcriu/run.sh
 create mode 100644 test/libcriu/test.c

diff --git a/Makefile b/Makefile
index c9a6042..2abb29d 100644
--- a/Makefile
+++ b/Makefile
@@ -202,6 +202,7 @@ clean: clean-built
 	$(Q) $(RM) -r ./test/lib64/
 	$(Q) $(RM) protobuf-desc-gen.h
 	$(Q) $(MAKE) -C test/zdtm cleandep clean cleanout
+	$(Q) $(MAKE) -C test/libcriu clean
 
 distclean: clean
 	$(E) "  DISTCLEAN"
diff --git a/test/libcriu/Makefile b/test/libcriu/Makefile
new file mode 100644
index 0000000..e6f5da2
--- /dev/null
+++ b/test/libcriu/Makefile
@@ -0,0 +1,10 @@
+all: build/test
+
+build/test: build/test.o
+	gcc $^ -L ../../lib -lcriu -o $@
+
+build/test.o: test.c
+	gcc -c $^ -I ../../lib -o $@
+
+clean:
+	rm -rf build
diff --git a/test/libcriu/loop.sh b/test/libcriu/loop.sh
new file mode 100755
index 0000000..0ab34ce
--- /dev/null
+++ b/test/libcriu/loop.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+while :; do
+	sleep 1
+done
diff --git a/test/libcriu/run.sh b/test/libcriu/run.sh
new file mode 100755
index 0000000..04a8c10
--- /dev/null
+++ b/test/libcriu/run.sh
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+source ../env.sh || exit 1
+
+export PROTODIR=`readlink -f "${PWD}/../../protobuf"`
+
+echo $PROTODIR
+
+LOOP_PID=0
+
+function title_print {
+	echo -e "\n**************************************************"
+	echo -e "\t\t"$1
+	echo -e "**************************************************\n"
+
+}
+
+function _exit {
+	if [ $1 -ne 0 ]; then
+		echo "FAIL"
+	fi
+
+	if [ $LOOP_PID -ne 0 ]; then
+		kill -SIGTERM $LOOP_PID
+	fi
+
+	title_print "Shutdown service server"
+	kill -SIGTERM `cat pidfile`
+
+	exit $1
+}
+
+function check_and_term {
+	title_print "Check and term $1"
+	ps -C $1
+	pkill $1
+}
+
+title_print "Build programs"
+make clean
+mkdir build
+cd build
+mkdir imgs_loop
+mkdir imgs_test
+make -C ../ || { echo "FAIL"; exit 1; }
+
+title_print "Start service server"
+${CRIU} service -v4 -o service.log --address criu_service.socket -d --pidfile `pwd`/pidfile || { echo "FAIL"; exit 1; }
+
+title_print "Run loop.sh"
+setsid ../loop.sh < /dev/null &> loop.log &
+LOOP_PID=${!}
+echo "pid ${LOOP_PID}"
+
+title_print "Run test.c"
+LD_LIBRARY_PATH=../../../lib
+export LD_LIBRARY_PATH
+./test ${LOOP_PID} || _exit $?
+
+title_print "Restore test.c"
+${CRIU} restore -v4 -o restore-test.log -D imgs_test --shell-job || _exit $?
+
+_exit 0
diff --git a/test/libcriu/test.c b/test/libcriu/test.c
new file mode 100644
index 0000000..3850cce
--- /dev/null
+++ b/test/libcriu/test.c
@@ -0,0 +1,94 @@
+#include "criu.h"
+#include <fcntl.h>
+#include <stdio.h>
+#include <errno.h>
+
+static void what_err_ret_mean(ret)
+{
+	/* NOTE: errno is set by libcriu */
+	switch (ret) {
+	case -1:
+		perror("RPC has returned fail");
+		break;
+	case -ECONNREFUSED:
+		perror("Unable to connect to CRIU");
+		break;
+	case -ECOMM:
+		perror("Unable to send/recv msg to/from CRIU");
+		break;
+	case -EINVAL:
+		perror("CRIU doesn't support this type of request."
+		       "You should probably update CRIU");
+		break;
+	case -EBADMSG:
+		perror("Unexpected response from CRIU."
+		       "You should probably update CRIU");
+		break;
+	default:
+		perror("Unknown error type code."
+		       "You should probably update CRIU");
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	int ret, fd;
+
+	criu_set_service_address("criu_service.socket");
+
+	puts("--- Check ---");
+	ret = criu_check();
+	if (ret < 0) {
+		what_err_ret_mean(ret);
+		return -1;
+	} else
+		puts("Success");
+
+	puts("--- Dump loop ---");
+	criu_init_opts();
+	criu_set_pid(atoi(argv[1]));
+	criu_set_log_file("dump.log");
+	criu_set_log_level(4);
+	fd = open("imgs_loop", O_DIRECTORY);
+	criu_set_images_dir_fd(fd);
+
+	ret = criu_dump();
+	if (ret < 0) {
+		what_err_ret_mean(ret);
+		return -1;
+	} else if (ret == 0)
+		puts("Success");
+
+	puts("--- Restore loop ---");
+	criu_init_opts();
+	criu_set_log_level(4);
+	criu_set_log_file("restore.log");
+	criu_set_images_dir_fd(fd);
+
+	ret = criu_restore();
+	if (ret < 0) {
+		what_err_ret_mean(ret);
+		return -1;
+	} else if (ret > 0) {
+		puts("Success");
+		printf("pid %d\n", ret);
+	}
+
+	puts("--- Dump myself ---");
+	criu_init_opts();
+	criu_set_leave_running(true);
+	criu_set_shell_job(true);
+	criu_set_images_dir_fd(open("imgs_test", O_DIRECTORY));
+
+	ret = criu_dump();
+	if (ret < 0) {
+		what_err_ret_mean(ret);
+		return -1;
+	} else {
+		puts("Success");
+		if (ret == 1)
+			puts("Restored");
+	}
+
+	return 0;
+}
-- 
1.8.3.2



More information about the CRIU mailing list