fix self-defense with SIGSTOP

This commit is contained in:
Alexey Avramov 2019-02-06 00:34:31 +09:00
parent 11336d707c
commit e264c001b1
2 changed files with 1730 additions and 12 deletions

1715
n2 Executable file

File diff suppressed because it is too large Load Diff

27
nohang
View File

@ -50,15 +50,17 @@ print_states_debug = False
# define functions
def pid_to_oom_score(pid):
with open('/proc/' + pid + '/oom_score') as f:
return f.read().rstrip()
def pid_to_state(pid):
return rline1('/proc/' + pid + '/stat').rpartition(')')[2][1]
def stop():
print()
print('Stop running processes...')
#print()
#print('Stop running processes...')
t1 = time()
t2 = time()
stopped_list = []
@ -68,13 +70,14 @@ def stop():
continue
try:
# print(pid)
if pid_to_state(pid) == 'R':
oom_score_r = pid_to_oom_score(pid)
#oom_score_r = rline1('/proc/' + pid + '/oom_score')
#print([oom_score_r])
if len(oom_score_r) > 1:
uid_r = pid_to_uid(pid)
oom_score_r = int(
rline1('/proc/' + pid + '/oom_score')
)
print('PID: {}, State: {}, oom_score {}'.format(pid, pid_to_state(pid), oom_score_r))
if uid_r != '0' and oom_score_r > 10:
#print('PID: {}, State: {}, oom_score {}'.format(pid, pid_to_state(pid), oom_score_r))
if uid_r != '0':
stopped_list.append(pid)
print('Send SIGSTOP to {}, {}, {}...'.format(
pid, pid_to_name(pid), pid_to_cmdline(pid)[:40]))
@ -91,11 +94,11 @@ def stop():
def cont(stopped_list):
print()
#print('Continue stopped processes...')
print('Continue stopped processes...')
t1 = time()
if len(stopped_list) > 0:
for pid in stopped_list:
#print('Send SIGCONT to', [pid], pid_to_name(pid))
print('Send SIGCONT to', [pid], pid_to_name(pid))
try:
os.kill(int(pid), SIGCONT)
except FileNotFoundError:
@ -103,7 +106,7 @@ def cont(stopped_list):
except ProcessLookupError:
continue
t2 = time()
#print('All cont time: ', t2 - t1)
print('All cont time: ', t2 - t1)
def print_states():
if print_states_debug: