Commit f929839a authored by Pavel Emelyanov's avatar Pavel Emelyanov

test: Add test for self dump

It's derived from test.c, but is more self-contained
and explicitly checks for both C and R results.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent a137d9fd
all: build/test test_sub
all: build/test test_sub test_self
.PHONY: all
run: all
......@@ -10,6 +10,12 @@ test_sub: test_sub.o
test_sub.o: test_sub.c
gcc -c $^ -I ../../lib -o $@
test_self: test_self.o
gcc $^ -L ../../lib -lcriu -o $@
test_self.o: test_self.c
gcc -c $^ -I ../../lib -o $@
build/test: build/test.o
gcc $^ -L ../../lib -lcriu -o $@
......@@ -17,5 +23,5 @@ build/test.o: test.c
gcc -c $^ -I ../../lib -o $@
clean:
rm -rf build test_sub test_sub.o
rm -rf build test_sub test_sub.o test_self test_sub.o
.PHONY: clean
#include "criu.h"
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
static void what_err_ret_mean(ret)
{
/* NOTE: errno is set by libcriu */
switch (ret) {
case -EBADE:
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");
}
}
static inline int chk_exit(int status, int want)
{
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) == want) {
printf(" `- Success\n");
return 0;
}
printf(" `- FAIL (exit %d)\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status))
printf(" `- FAIL (die %d)\n", WTERMSIG(status));
else
printf(" `- FAIL (%#x)\n", status);
return 1;
}
#define SUCC_DUMP_ECODE 41
#define SUCC_RSTR_ECODE 43
int main(int argc, char *argv[])
{
int ret, fd, pid;
fd = open(argv[2], O_DIRECTORY);
if (fd < 0) {
perror("Can't open images dir");
return 1;
}
criu_init_opts();
criu_set_service_address(argv[1]);
criu_set_images_dir_fd(fd);
criu_set_log_level(4);
printf("--- Start child ---\n");
pid = fork();
if (pid < 0) {
perror("Can't");
return 1;
}
if (!pid) {
/*
* Child process -- dump itself, then
* parent would restore us.
*/
close(0);
close(1);
close(2);
if (setsid() < 0)
exit(1);
criu_set_log_file("dump.log");
criu_set_leave_running(true);
ret = criu_dump();
if (ret < 0) {
what_err_ret_mean(ret);
exit(1);
}
if (ret == 0)
ret = SUCC_DUMP_ECODE; /* dumped OK */
else if (ret == 1)
ret = SUCC_RSTR_ECODE; /* restored OK */
else
ret = 1;
exit(ret);
}
printf("--- Wait for self-dump ---\n");
if (waitpid(pid, &ret, 0) < 0) {
perror("Can't wait child");
goto errk;
}
if (chk_exit(ret, SUCC_DUMP_ECODE))
goto errk;
printf("--- Restore ---\n");
criu_set_log_file("restore.log");
pid = criu_restore_child();
if (pid <= 0) {
what_err_ret_mean(pid);
goto err;
}
if (waitpid(pid, &ret, 0) < 0) {
perror("Can't wait rchild");
goto errk;
}
return chk_exit(ret, SUCC_RSTR_ECODE);
errk:
kill(pid, SIGKILL);
err:
return 1;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment