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