-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq3.py
189 lines (156 loc) · 5.29 KB
/
q3.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import ptrace
import subprocess
import signal
import os
import sys
var = False
"""
Files descriptors information (via /proc/$pid/fd and /proc/$pid/fdinfo).
Pipes parameters.
Memory maps (via /proc/$pid/maps and /proc/$pid/map_files/).
The $pid of a process group leader is obtained from the command line (--tree option).
By using this $pid the dumper walks though /proc/$pid/task/ directory collecting threads
and through the /proc/$pid/task/$tid/children to gathers children recursively.
While walking tasks are stopped using the ptrace's PTRACE_SEIZE command.
"""
def get_info(pid):
os.system('mkdir img-dir_' + str(pid))
os.chdir('/proc/' + str(pid))
for root, dirnames, filenames in os.walk('./fd/'):
files = filenames
for f in files:
os.system('cp ' + f + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
for root, dirnames, filenames in os.walk('./fdinfo/'):
files = filenames
for f in files:
os.system('cp ' + f + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
for root, dirnames, filenames in os.walk('./map_files/'):
files = filenames
for f in files:
os.system('cp ' + f + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
for root, dirnames, filenames in os.walk('./task/'):
files = filenames
for f in files:
os.system('cp ' + f + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
os.system('cp maps' + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
os.system('cp mem' + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
os.system('cp pagemap' + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
os.system('cp smaps' + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
os.system('cp status' + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
os.system('cp syscall' + ' /home/mpiuser/ChotaBaadal/img-dir_' + str(pid))
properties = process_info()
return properties
# The $pid of a process group leader is obtained from the command line
def get_gpid(pid):
command = 'ps -f ' + str(pid)
process_out = subprocess.Popen(command, shell=True)
return process_out
def freeze_process_tree(pid):
ptrace.ptrace(PTRACE_SEIZE, pid)
def save_process(pid):
t = PtraceProcess(pid)
t.syscall()
t.detach()
registerValues = t.getregs() # read all registers
fo = fopen("img-dir/stats.img", "wb")
fo.write(registerValues);
fo.close()
t.dumpCode() # dump code (as assembler or hexa is the disassembler is missing)
fo = fopen("img-dir/fd.img", "wb")
fo.write(t.fileDescriptors);
fo.close()
t.dumpStack() # dump stack (memory words around ESP)
fo = fopen("img-dir/signals.img", "wb")
fo.write(t.signalMask)
fo.close()
def check_pid(pid):
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
def checkpoint(pid):
global var;
if(var):
save_process(pid)
gpid = get_gpid(pid)
freeze_process_tree(pid)
properties = get_info()
helper(pid)
def helper(pid):
var1 = 'iu'
if not (check_pid(pid)):
print "Process with this pid doesn't exist."
return
os.system("mkdir img-dir_" + str(pid))
var2 = 'cr'
command = 'echo 123 | sudo -S ' + str(var2) + str(var1) + ' dump -t ' + str(pid) +' --images-dir ./img-dir_' + str(pid) + '/ --shell-job -vvv -o dump.log'
var3 = 'it'
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
command = str(var2) + str(var3) + " show img-dir_" + str(pid) + "/core-" + str(pid) + ".img"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
while True:
line = process.stdout.readline()
if line != '':
#the real code does filtering here
print line.rstrip()
else:
break
print "Process successfully checkpointed."
command = str(var2) + str(var3) + " show img-dir_" + str(pid) + "/mm-" + str(pid) + ".img"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
while True:
line = process.stdout.readline()
if line != '':
#the real code does filtering here
print line.rstrip()
else:
break
print "Process successfully checkpointed."
command = str(var2) + str(var3) + " show img-dir_" + str(pid) + "/tty.img"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
while True:
line = process.stdout.readline()
if line != '':
#the real code does filtering here
print line.rstrip()
else:
break
print "Process successfully checkpointed."
command = str(var2) + str(var3) + " show img-dir_" + str(pid) + "/pagemap-" + str(pid) + ".img"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
while True:
line = process.stdout.readline()
if line != '':
#the real code does filtering here
print line.rstrip()
else:
break
print "Process successfully checkpointed."
command = str(var2) + str(var3) + " show img-dir_" + str(pid) + "/pstree.img"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
while True:
line = process.stdout.readline()
if line != '':
#the real code does filtering here
print line.rstrip()
else:
break
print "Process successfully checkpointed."
def restore(pid, container):
if os.path.isdir("./img-dir_" + pid):
pass
if __name__ == "__main__":
if sys.argv[1] == 'c':
checkpoint(int(sys.argv[2]))
elif sys.argv[1] == 'r':
restore(int(sys.argv[2]))
else:
print "Invalid command."