1
0
Fork 0

Merge pull request #489 from yarda/dbus-traceback-fix

D-Bus: only send tracebacks through the D-Bus if in the debug mode
This commit is contained in:
Jaroslav Škarvada 2023-02-08 02:04:25 +01:00 committed by GitHub
commit f11b403b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,10 +6,14 @@ import threading
import signal
import tuned.logs
import tuned.consts as consts
import traceback
import logging
from inspect import ismethod
from tuned.utils.polkit import polkit
from gi.repository import GLib
from types import FunctionType
from dbus.exceptions import DBusException
from dbus.lowlevel import ErrorMessage
try:
# Python3 version
@ -26,6 +30,29 @@ except ImportError:
log = tuned.logs.get()
# This is mostly copy of the code from the dbus.service module without the
# code that sends tracebacks through the D-Bus (i.e. no library tracebacks
# are exposed on the D-Bus now).
def _method_reply_error(connection, message, exception):
name = getattr(exception, '_dbus_error_name', None)
if name is not None:
pass
elif getattr(exception, '__module__', '') in ('', '__main__'):
name = 'org.freedesktop.DBus.Python.%s' % exception.__class__.__name__
else:
name = 'org.freedesktop.DBus.Python.%s.%s' % (exception.__module__, exception.__class__.__name__)
if isinstance(exception, DBusException):
contents = exception.get_dbus_message()
else:
contents = ''.join(traceback.format_exception_only(exception.__class__,
exception))
reply = ErrorMessage(message, name, contents)
if not message.get_no_reply():
connection.send_message(reply)
class DBusExporter(interfaces.ExporterInterface):
"""
Export method calls through DBus Interface.
@ -37,6 +64,14 @@ class DBusExporter(interfaces.ExporterInterface):
"""
def __init__(self, bus_name, interface_name, object_name):
# 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
# the D-Bus library. Just setting the exception.include_traceback to False doesn't
# seem to help because there is only a subset of exceptions that support this flag.
if log.getEffectiveLevel() != logging.DEBUG:
dbus.service._method_reply_error = _method_reply_error
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self._dbus_object_cls = None