From 4502d7abd7fe2e52d3e623ce7e7e9004e131f433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavol=20=C5=BD=C3=A1=C4=8Dik?= Date: Thu, 7 Dec 2023 15:18:52 +0100 Subject: [PATCH] Make DBusExporter reusable for other services Do not hardcode Polkit namespace into DBusExporter, provide it as a parameter in the constructor. --- tuned.py | 2 +- tuned/daemon/application.py | 4 ++-- tuned/exports/dbus_exporter.py | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tuned.py b/tuned.py index d133cfc..460482b 100755 --- a/tuned.py +++ b/tuned.py @@ -79,7 +79,7 @@ if __name__ == "__main__": args.no_socket = True if not args.no_dbus: - app.attach_to_dbus(consts.DBUS_BUS, consts.DBUS_OBJECT, consts.DBUS_INTERFACE) + app.attach_to_dbus(consts.DBUS_BUS, consts.DBUS_OBJECT, consts.DBUS_INTERFACE, consts.NAMESPACE) if not args.no_socket: app.attach_to_unix_socket() diff --git a/tuned/daemon/application.py b/tuned/daemon/application.py index fe12a65..d1bf72a 100644 --- a/tuned/daemon/application.py +++ b/tuned/daemon/application.py @@ -71,11 +71,11 @@ class Application(object): self._handle_signal(signal.SIGINT, self._controller.terminate) self._handle_signal(signal.SIGTERM, self._controller.terminate) - def attach_to_dbus(self, bus_name, object_name, interface_name): + def attach_to_dbus(self, bus_name, object_name, interface_name, namespace): if self._dbus_exporter is not None: raise TunedException("DBus interface is already initialized.") - self._dbus_exporter = exports.dbus.DBusExporter(bus_name, interface_name, object_name) + self._dbus_exporter = exports.dbus.DBusExporter(bus_name, interface_name, object_name, namespace) exports.register_exporter(self._dbus_exporter) def attach_to_unix_socket(self): diff --git a/tuned/exports/dbus_exporter.py b/tuned/exports/dbus_exporter.py index 8073b49..b0a9f9b 100644 --- a/tuned/exports/dbus_exporter.py +++ b/tuned/exports/dbus_exporter.py @@ -63,7 +63,7 @@ class DBusExporter(interfaces.ExporterInterface): to an object we dynamically construct. """ - def __init__(self, bus_name, interface_name, object_name): + def __init__(self, bus_name, interface_name, object_name, namespace): # Monkey patching of the D-Bus library _method_reply_error() to reply # tracebacks via D-Bus only if in the debug mode. It doesn't seem there is a # more simple way how to cover all possible exceptions that could occur in @@ -82,6 +82,7 @@ class DBusExporter(interfaces.ExporterInterface): self._bus_name = bus_name self._interface_name = interface_name self._object_name = object_name + self._namespace = namespace self._thread = None self._bus_object = None self._polkit = polkit() @@ -130,7 +131,7 @@ class DBusExporter(interfaces.ExporterInterface): raise Exception("Method with this name is already exported.") def wrapper(owner, *args, **kwargs): - action_id = consts.NAMESPACE + "." + method.__name__ + action_id = self._namespace + "." + method.__name__ caller = args[-1] log.debug("checking authorization for action '%s' requested by caller '%s'" % (action_id, caller)) ret = self._polkit.check_authorization(caller, action_id)