Skip to content

Commit

Permalink
fallback if it fails to find a home user, will probably fix builds on…
Browse files Browse the repository at this point in the history
… launchpad for 23.10 and later
  • Loading branch information
elesiuta committed Dec 13, 2023
1 parent 8ac0bdc commit 340bbe0
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions picosnitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,31 @@
print("Warning: picosnitch does not function properly with the -O (optimize) flag", file=sys.stderr)

# set constants and RLIMIT_NOFILE if configured
if os.getuid() == 0:
if os.getenv("SUDO_UID"):
home_user = pwd.getpwuid(int(os.getenv("SUDO_UID"))).pw_name
elif os.getenv("SUDO_USER"):
home_user = os.getenv("SUDO_USER")
try:
if os.getuid() == 0:
if os.getenv("SUDO_UID"):
home_user = pwd.getpwuid(int(os.getenv("SUDO_UID"))).pw_name
elif os.getenv("SUDO_USER"):
home_user = os.getenv("SUDO_USER")
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
if not os.getenv("SUDO_UID"):
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:
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
if not os.getenv("SUDO_UID"):
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:
home_dir = os.path.expanduser("~")
if sys.executable.startswith("/snap/"):
home_dir = home_dir.split("/snap/picosnitch")[0]
except Exception:
# fallback if there are no regular users on the system, this will likely be /root
home_dir = os.path.expanduser("~")
if sys.executable.startswith("/snap/"):
home_dir = home_dir.split("/snap/picosnitch")[0]
BASE_PATH: typing.Final[str] = os.path.join(home_dir, ".config", "picosnitch")
try:
file_path = os.path.join(BASE_PATH, "config.json")
Expand Down

0 comments on commit 340bbe0

Please sign in to comment.