Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import os
- display_env = 'DISPLAY='
- dbus_env = 'DBUS_SESSION_BUS_ADDRESS='
- user_env = 'USER='
- def re_pid_environ(pid):
- """
- read environ of 1 process
- returns tuple with USER, DBUS, DISPLAY like follow:
- ('user', 'DISPLAY=:0',
- 'DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus')
- returns None if these vars is not in /proc/[pid]/environ
- """
- try:
- with open('/proc/' + pid + '/environ', 'rb') as f:
- env = f.read().decode('utf-8', 'ignore')
- except FileNotFoundError:
- return None
- except ProcessLookupError:
- return None
- if display_env in env and dbus_env in env and user_env in env:
- env_list = env.split('\x00')
- print(pid, env_list, '\n')
- # iterating over a list of process environment variables
- for i in env_list:
- # exclude Display Manager's user
- if i.startswith('HOME=/var'):
- return None
- if i.startswith(user_env):
- user = i
- if user == 'USER=root':
- return None
- continue
- if i.startswith(display_env):
- display = i[:10]
- continue
- if i.startswith(dbus_env):
- dbus = i
- continue
- try:
- return user.partition('USER=')[2], display, dbus
- except UnboundLocalError:
- return None
- def root_notify_env():
- """return set(user, display, dbus)"""
- unsorted_envs_list = []
- # iterates over processes, find processes with suitable env
- for pid in os.listdir('/proc'):
- # os.path.exists('/proc/[pid]/exe') indicates process existanse
- if os.path.exists('/proc/' + pid + '/exe') is True:
- one_env = re_pid_environ(pid)
- unsorted_envs_list.append(one_env)
- env = set(unsorted_envs_list)
- env.discard(None)
- # deduplicate dbus
- new_env = []
- end = []
- for i in env:
- key = i[0] + i[1]
- if key not in end:
- end.append(key)
- new_env.append(i)
- else:
- continue
- return new_env
- print(root_notify_env())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement