forked from ton-blockchain/mytonctrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxrestart.py
73 lines (61 loc) · 1.4 KB
/
xrestart.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
import os
import sys
import pwd
import time
import threading
import subprocess
def Xguard():
timestamp = int(sys.argv[1])
args = sys.argv[2:]
while True:
time.sleep(1)
timenow = int(time.time())
if timenow > timestamp:
Xcmd(args)
print("exit")
sys.exit(0)
#end define
def Xcmd(inputArgs):
print("inputArgs:", inputArgs)
# stop validator
args = ["systemctl", "stop", "validator"]
subprocess.run(args)
file = open("/etc/systemd/system/validator.service", 'rt')
text = file.read()
file.close()
lines = text.split('\n')
for line in lines:
if "ExecStart" not in line:
continue
ExecStart = line.replace("ExecStart = ", '')
args = ExecStart.split(' ')
print("ExecStart args:", args)
args += inputArgs
#end for
pw_record = pwd.getpwnam("validator")
user_uid = pw_record.pw_uid
user_gid = pw_record.pw_gid
# start with args
print("args:", args)
process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=demote(user_uid, user_gid))
process.wait()
text = process.stdout.read().decode()
print("text:", text)
# Exit program
sys.exit(0)
#end define
def demote(user_uid, user_gid):
def result():
os.setgid(user_gid)
os.setuid(user_uid)
os.system("ulimit -n 1")
os.system("ulimit -u 1")
os.system("ulimit -l 1")
return result
#end define
###
### Start of the program
###
if __name__ == "__main__":
Xguard()
#end if