Skip to content

Commit

Permalink
fix setting DBUS_SESSION_BUS_ADDRESS if only SUDO_UID is set in envir…
Browse files Browse the repository at this point in the history
…onment

also cleanup a bit
  • Loading branch information
elesiuta committed Dec 24, 2021
1 parent 7526d63 commit 4b09402
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions picosnitch.py
Expand Up @@ -60,18 +60,18 @@
PAGE_CNT: typing.Final[int] = 8
if sys.platform.startswith("linux") and os.getuid() == 0:
if os.getenv("SUDO_USER"):
home_dir = os.path.join("/home", os.getenv("SUDO_USER"))
home_user = os.getenv("SUDO_USER")
elif os.getenv("SUDO_UID"):
home_dir = pwd.getpwuid(int(os.getenv("SUDO_UID"))).pw_dir
home_user = pwd.getpwuid(int(os.getenv("SUDO_UID"))).pw_name
else:
for home_user in os.listdir("/home"):
try:
if pwd.getpwnam(home_user).pw_uid >= 1000:
break
except Exception:
pass
home_dir = pwd.getpwnam(home_user).pw_dir
os.environ["SUDO_UID"] = str(pwd.getpwnam(home_user).pw_uid)
home_dir = pwd.getpwnam(home_user).pw_dir
os.environ["SUDO_UID"] = str(pwd.getpwnam(home_user).pw_uid)
if not os.getenv("DBUS_SESSION_BUS_ADDRESS"):
os.environ["DBUS_SESSION_BUS_ADDRESS"] = f"unix:path=/run/user/{pwd.getpwnam(home_user).pw_uid}/bus"
else:
Expand Down Expand Up @@ -1358,8 +1358,6 @@ def start_picosnitch():
WantedBy=multi-user.target
""")
if sys.platform.startswith("linux"):
if os.path.expanduser("~") == "/root" and not os.getenv("DBUS_SESSION_BUS_ADDRESS"):
print("Warning: picosnitch was run as root without preserving environment, notifications won't work", file=sys.stderr)
if len(sys.argv) == 2:
if sys.argv[1] == "help":
print(readme)
Expand Down

0 comments on commit 4b09402

Please sign in to comment.