mirror of
https://github.com/checkpoint-restore/criu.git
synced 2026-07-17 16:47:50 +00:00
test: libcriu
This test is similiar to test/rpc, and can also be used as an example of using libcriu. Signed-off-by: Ruslan Kuprieiev <kupruser@gmail.com> Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
This commit is contained in:
parent
906a75ca03
commit
ca1f60d267
5 changed files with 172 additions and 0 deletions
1
Makefile
1
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"
|
||||
|
|
|
|||
10
test/libcriu/Makefile
Normal file
10
test/libcriu/Makefile
Normal file
|
|
@ -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
|
||||
4
test/libcriu/loop.sh
Executable file
4
test/libcriu/loop.sh
Executable file
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/bash
|
||||
while :; do
|
||||
sleep 1
|
||||
done
|
||||
63
test/libcriu/run.sh
Executable file
63
test/libcriu/run.sh
Executable file
|
|
@ -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
|
||||
94
test/libcriu/test.c
Normal file
94
test/libcriu/test.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue