Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Refactor QubesDBusProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkin committed Sep 27, 2016
1 parent 571fb66 commit a6610ba
Showing 1 changed file with 49 additions and 68 deletions.
117 changes: 49 additions & 68 deletions qubesdbus/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import dbus
from qubes import Qubes
from qubes.ext import Extension, handler
from qubes.vm.qubesvm import QubesVM
from systemd.journal import JournalHandler

Expand All @@ -37,8 +38,9 @@
pass

log = logging.getLogger('qubesdbus.proxy')
log.addHandler(JournalHandler(SYSLOG_IDENTIFIER='qubesdbus.proxy'))
log.setLevel(logging.DEBUG)
log.addHandler(
JournalHandler(level=logging.DEBUG, SYSLOG_IDENTIFIER='qubesdbus.proxy'))
log.propagate = False

INTERFACE_NAME = "%s.QubesSignals%s" % (NAME_PREFIX, VERSION)
SESSION_BUS = dbus.SessionBus()
Expand All @@ -49,74 +51,43 @@
]


class QubesDbusProxy(object):
# pylint: disable=too-few-public-methods
def __init__(self, *args, **kwargs):
super(QubesDbusProxy, self).__init__(*args, **kwargs)
def is_garbage(event):
if event in ['domain-load', 'domain-init']:
return True
elif event.startswith('property-pre-set'):
return True
else:
return False


class QubesDbusProxy(Extension):
# pylint: disable=too-few-public-methods,no-self-use
def __init__(self):
super(QubesDbusProxy, self).__init__()
self.domains = {} # type: Dict[int, bool]

def forward(self, obj, event_name, *args, **kwargs):
# type: (Union[Qubes, QubesVM], str, *Any, **Any) -> None
# pylint: disable=redefined-variable-type
if isinstance(obj, QubesVM):
# let's play a game called guess in which state is the domain?
if not hasattr(obj, 'qid'):
# just reading domain from qubes.xml and preparing for
# populating it, we can ignore this event
log.info('%s some domain', event_name)
return
else:
qid = obj.qid
log.info('Received %s => %s', qid, event_name)
if qid not in self.domains.keys():
self.domains[qid] = False

if event_name == 'domain-load':
self.domains[qid] = True

if self.domains[qid]:
log.info("Would forward event to existing domain %s", qid)
try:
args = serialize(args)
kwargs = zip(kwargs.keys(), serialize(kwargs.values()))
log.info("%s : %s : %s", event_name, args, kwargs)
except TypeError as ex:
msg = "%s: %s" % (event_name, ex.message)
log.error(msg)

@staticmethod
def old_forward(obj, event_name, *args, **kwargs):
# pylint: disable=redefined-variable-type
try:
proxy = get_proxy(obj)
if event_name.startswith('property-set'):
log.debug('Received %s from %s', event_name, obj)
p_name = args[0]
p_value = str(args[1])
set_method = proxy.get_dbus_method(
'Set', 'org.freedesktop.DBus.Properties')
set_method('', p_name, p_value)
elif event_name in GARBAGE:
log.debug("Droping event %s", event_name)
elif isinstance(obj, QubesVM):
log.debug("Forwarding event %s", event_name)
forward_signal_func = proxy.get_dbus_method(
'ForwardSignal', 'org.qubes.Signals')
if not args:
args = ['']
if not kwargs:
kwargs = {'fpp': 'bar'}

forward_signal_func(event_name, args, kwargs)
else:
log.info("Do not know how to handle %s event", event_name)
if args:
log.info(args)
if kwargs:
log.warn(kwargs)
except TypeError as ex:
msg = "%s: %s" % (event_name, ex.message)
log.error(msg)
@handler('*')
def forward_vm_event(self, vm, event, *args, **kwargs):
if is_garbage(event):
log.debug('Drop %s from %s', event, vm)
return
elif event.startswith('property-set:'):
proxy = vm_proxy(vm.qid)
property_set(proxy, args[0], str(args[1]))
log.info('VM: %s %s %s %s', vm, event, args, kwargs)
else:
log.warn('Unknown %s from %s', event, vm)

@handler('*', system=True)
def forward_app_event(self, vm, event, *args, **kwargs):
log.debug('A: %s %s %s %s', vm, event, args, kwargs)


def property_set(proxy, name, value):
# type: (dbus.proxies.ProxyObject, str, Any) -> None
''' Helper for setting a property on a helper '''
func = proxy.get_dbus_method('Set', 'org.freedesktop.DBus.Properties')
func('', name, value)


def serialize(args):
Expand All @@ -129,6 +100,16 @@ def serialize(args):
return result


def vm_proxy(qid):
# type: (int) -> dbus.proxies.ProxyObject
domain_path = '/'.join([MANAGER_PATH, 'domains', str(qid)])
return SESSION_BUS.get_object(MANAGER_NAME, domain_path)


def app_proxy():
return SESSION_BUS.get_object(MANAGER_NAME, MANAGER_PATH)


def get_proxy(obj):
# type: (Union[Qubes, QubesVM]) -> dbus.proxies.ProxyObject
identifier = str(obj)
Expand Down

0 comments on commit a6610ba

Please sign in to comment.