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
|
#!/usr/bin/env python
import os, pty, sys, subprocess
import termios, fcntl, time
cr_bin = "../../../criu/criu"
os.chdir(os.getcwd())
def create_pty():
(fd1, fd2) = pty.openpty()
return (os.fdopen(fd1, "wb"), os.fdopen(fd2, "wb"))
if not os.access("work", os.X_OK):
os.mkdir("work", 0o755)
open("running", "w").close()
m, s = create_pty()
p = os.pipe()
pr = os.fdopen(p[0], "r")
pw = os.fdopen(p[1], "w")
pid = os.fork()
if pid == 0:
m.close()
os.setsid()
os.dup2(s.fileno(), 0)
os.dup2(s.fileno(), 1)
os.dup2(s.fileno(), 2)
fcntl.ioctl(s.fileno(), termios.TIOCSCTTY, 1)
pr.close()
pw.close()
while True:
if not os.access("running", os.F_OK):
sys.exit(0)
time.sleep(1)
sys.exit(1)
pw.close()
pr.read(1)
cmd = [cr_bin, "dump", "-j", "-t", str(pid), "-D", "work", "-v"]
print("Run: %s" % " ".join(cmd))
ret = subprocess.Popen(cmd).wait()
if ret != 0:
sys.exit(1)
os.wait()
os.unlink("running")
m, s = create_pty()
cpid = os.fork()
if cpid == 0:
os.setsid()
fcntl.ioctl(m.fileno(), termios.TIOCSCTTY, 1)
cmd = [cr_bin, "restore", "-j", "-D", "work", "-v"]
print("Run: %s" % " ".join(cmd))
ret = subprocess.Popen([cr_bin, "restore", "-j", "-D", "work",
"-v"]).wait()
if ret != 0:
sys.exit(1)
sys.exit(0)
pid, status = os.wait()
if status != 0:
print("A child process exited with %d" % status)
sys.exit(1)
|