Fix notifications when using doas

This commit is contained in:
Fuxino
2026-10-03 21:04:17 +02:00
parent c715188973
commit 47aec456be
+34 -12
View File
@@ -12,8 +12,10 @@ Classes:
""" """
# Import libraries # Import libraries
import traceback
import sys import sys
import os import os
import pwd
from typing import Callable, List, Optional, ParamSpec, TypeVar, Union from typing import Callable, List, Optional, ParamSpec, TypeVar, Union
import warnings import warnings
from functools import wraps from functools import wraps
@@ -813,26 +815,46 @@ def _read_config(config_file, user: Optional[str] = None):
return config_args return config_args
def _notify(text: str) -> None: def _target_uid() -> int | None:
euid = os.geteuid() euid = os.geteuid()
if euid == 0: if euid != 0:
uid = os.getenv('SUDO_UID') return euid
else:
uid = euid if (val := os.getenv('SUDO_UID')) is not None:
return int(val)
if (name := os.getenv('DOAS_USER')) is not None:
try:
return pwd.getpwnam(name).pw_uid
except KeyError:
return None
return None
def _notify(text: str) -> None:
euid = os.geteuid()
uid = _target_uid()
if uid is None: if uid is None:
print('notify: no target uid (neither SUDO_UID nor DOAS_USER set)', file=sys.stderr)
return return
os.seteuid(int(uid)) os.seteuid(uid)
os.environ['DBUS_SESSION_BUS_ADDRESS'] = f'unix:path=/run/user/{uid}/bus'
obj = dbus.SessionBus().get_object('org.freedesktop.Notifications', try:
'/org/freedesktop/Notifications') os.environ['DBUS_SESSION_BUS_ADDRESS'] = f'unix:path=/run/user/{uid}/bus'
obj = dbus.Interface(obj, 'org.freedesktop.Notifications')
obj.Notify('', 0, '', 'simple_backup', text, [], {'urgency': 1}, 10000)
os.seteuid(int(euid)) obj = dbus.SessionBus().get_object('org.freedesktop.Notifications',
'/org/freedesktop/Notifications')
obj = dbus.Interface(obj, 'org.freedesktop.Notifications')
obj.Notify('simple_backup', 0, '', 'simple_backup', text, [], {'urgency': dbus.Byte(1)}, 10000)
except Exception:
traceback.print_exc()
finally:
os.seteuid(euid)
def simple_backup() -> int: def simple_backup() -> int: