-
Notifications
You must be signed in to change notification settings - Fork 565
/
test_self.c
108 lines (88 loc) · 1.78 KB
/
test_self.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "criu.h"
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include "lib.h"
#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_binary(argv[1]);
get_version();
if (!criu_check_version(31400)) {
printf("CRIU version check failed. CRIU too old\n");
return 1;
}
if (criu_check_version(INT_MAX)) {
printf("CRIU version check failed. CRIU too new.\n");
return 1;
}
criu_set_images_dir_fd(fd);
criu_set_log_level(CRIU_LOG_DEBUG);
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;
}