Skip to content

Commit

Permalink
needs-restarting: Small refinement of NoUnitForPID handling
Browse files Browse the repository at this point in the history
Limit handling of the issue to only this specific use case, otherwise throw the exception as any other DBUS-related call.
  • Loading branch information
jan-kolarik authored and kontura committed May 15, 2023
1 parent c56cdce commit 3c61526
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions plugins/needs_restarting.py
Expand Up @@ -138,20 +138,23 @@ def get_service_dbus(pid):
systemd_manager_object,
'org.freedesktop.systemd1.Manager'
)
service_proxy = None

service_unit_path = None
try:
service_proxy = bus.get_object(
'org.freedesktop.systemd1',
systemd_manager_interface.GetUnitByPID(pid)
)
service_unit_path = systemd_manager_interface.GetUnitByPID(pid)
except dbus.DBusException as e:
# There is no unit for the pid. Usually error is 'NoUnitForPid'.
# Considering what we do at the bottom (just return if not service)
# Then there's really no reason to exit here on that exception.
# Log what's happened then move on.
msg = str(e)
logger.warning("Failed to get systemd unit for PID {}: {}".format(pid, msg))
return
if msg.startswith('org.freedesktop.systemd1.NoUnitForPID'):
logger.warning("Failed to get systemd unit for PID {}: {}".format(pid, msg))
return
else:
raise

service_proxy = bus.get_object('org.freedesktop.systemd1', service_unit_path)
service_properties = dbus.Interface(
service_proxy, dbus_interface="org.freedesktop.DBus.Properties")
name = service_properties.Get(
Expand Down

0 comments on commit 3c61526

Please sign in to comment.