fix GUI notifications

This commit is contained in:
Alexey Avramov 2018-08-01 05:35:01 +09:00
parent 86c9c10355
commit b1a6565a99

51
nohang
View File

@ -11,7 +11,6 @@ from sys import stdout
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
sig_dict = {signal.SIGKILL: 'SIGKILL', sig_dict = {signal.SIGKILL: 'SIGKILL',
signal.SIGTERM: 'SIGTERM'} signal.SIGTERM: 'SIGTERM'}
@ -39,8 +38,6 @@ conf_err_mess = '\nSet up the path to the valid conf' \
# found experimentally # found experimentally
zram_disksize_factor = 0.0042 zram_disksize_factor = 0.0042
name_strip_string = '\'"`\\!-$'
########################################################################## ##########################################################################
# function definition section # function definition section
@ -50,31 +47,27 @@ name_strip_string = '\'"`\\!-$'
# username, DISPLAY and DBUS_SESSION_BUS_ADDRESS # username, DISPLAY and DBUS_SESSION_BUS_ADDRESS
def root_notify_env(): def root_notify_env():
ps_output_list = Popen(['ps', 'aue'], stdout=PIPE ps_output_list = Popen(['ps', 'ae'], stdout=PIPE
).communicate()[0].decode().split('\n') ).communicate()[0].decode().split('\n')
di = 'DISPLAY='
db = 'DBUS_SESSION_BUS_ADDRESS='
us = 'USER='
ro = 'root '
lines_with_displays = [] lines_with_displays = []
for line in ps_output_list: for line in ps_output_list:
if di in line and db in line and us in line and not line.startswith(ro): if ' DISPLAY=' in line and ' DBUS_SESSION_BUS_ADDRES' \
'S=' in line and ' USER=' in line:
lines_with_displays.append(line) lines_with_displays.append(line)
# list of tuples with all necessary # list of tuples with needments
deus = [] deus = []
for i in lines_with_displays: for i in lines_with_displays:
for i in i.split(' '): for i in i.split(' '):
if i.startswith(us): if i.startswith('USER='):
user = i.strip('\n').split('=')[1] user = i.strip('\n').split('=')[1]
continue continue
if i.startswith(di): if i.startswith('DISPLAY='):
disp_value = i.strip('\n').split('=')[1][0:2] disp_value = i.strip('\n').split('=')[1][0:2]
disp = di + disp_value disp = 'DISPLAY=' + disp_value
continue continue
if i.startswith(db): if i.startswith('DBUS_SESSION_BUS_ADDRESS='):
dbus = i.strip('\n') dbus = i.strip('\n')
deus.append(tuple([user, disp, dbus])) deus.append(tuple([user, disp, dbus]))
@ -242,30 +235,23 @@ def send_notify_warn():
def send_notify(signal, name, pid, oom_score, vm_rss, vm_swap): def send_notify(signal, name, pid, oom_score, vm_rss, vm_swap):
# текст отправляемого уведомления head = 'Preventing OOM'
info = '"<u>Nohang</u> sent <u>{}</u> \nto the process <b>{}</b> \n<i>P' \ body = '<u>Nohang</u> sent <u>{}</u> \nto the process <b>{}</b> \n<i>P' \
'id:</i> <b>{}</b> \n<i>Badness:</i> <b>{}</b> \n<i>VmRSS:</i> <b' \ 'id:</i> <b>{}</b> \n<i>Badness:</i> <b>{}</b> \n<i>VmRSS:</i> <b' \
'>{} MiB</b> \n<i>VmSwap:</i> <b>{} MiB</b>" &'.format( '>{} MiB</b> \n<i>VmSwap:</i> <b>{} MiB</b>'.format(
sig_dict[signal], name.strip(name_strip_string), pid, oom_score, vm_rss, vm_swap) sig_dict[signal], name, pid, oom_score, vm_rss, vm_swap)
if root: if root:
# отправляем уведомление всем залогиненным пользователям # отправляем уведомление всем залогиненным пользователям
b = root_notify_env() b = root_notify_env()
if len(b) > 0: if len(b) > 0:
for i in b: for i in b:
username = i[0] username, display_env, dbus_env = i[0], i[1], i[2]
display_env = i[1] Popen(['sudo', '-u', username, 'env', display_env,
dbus_env = i[2] dbus_env, 'notify-send', '{}'.format(head),
# su username - -c "env DISPLAY=... DBUS_SESSION_BUS_ADDRESS=... notify-send 'head' 'body'" '{}'.format(body)])
root_notify_command = 'sudo -u {} {} {} notify-send "Preventing OOM" '.format(
username, display_env, dbus_env)
os.system(root_notify_command + info)
else: else:
# отправляем уведомление пользователю, который запустил nohang # отправляем уведомление пользователю, который запустил nohang
user_notify_command = 'notify-send {} "Preventing OOM" '.format( Popen(['notify-send', '{}'.format(head), '{}'.format(body)])
notify_options)
os.system(user_notify_command + info)
def sleep_after_send_signal(signal): def sleep_after_send_signal(signal):
@ -366,6 +352,9 @@ def find_victim_and_send_signal(signal):
except IndexError: except IndexError:
vm_rss = 0 vm_rss = 0
vm_swap = 0 vm_swap = 0
except ValueError:
vm_rss = 0
vm_swap = 0
if name in etc_dict and signal is 15: if name in etc_dict and signal is 15:
command = etc_dict[name] command = etc_dict[name]