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

Commit

Permalink
Fordward domain creation / deletion events
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkin committed Sep 27, 2016
1 parent aecb889 commit c76aa46
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
32 changes: 30 additions & 2 deletions qubesdbus/domain_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
from gi.repository import GLib
from systemd.journal import JournalHandler

from qubesdbus.service import PropertiesObject
from qubesdbus.domain import Domain
import qubesdbus.serialize
from qubesdbus.domain import Domain
from qubesdbus.service import PropertiesObject

try:
# Check mypy types. pylint: disable=ungrouped-imports, unused-import
Expand All @@ -44,6 +44,7 @@
SYSLOG_IDENTIFIER='qubesdbus.domain_manager'))
log.propagate = True


class DomainManager(PropertiesObject):
def __init__(self, data, domains):
# type: (Dict[dbus.String, Any], List[Dict[Union[str,dbus.String], Any]]) -> None
Expand All @@ -60,6 +61,33 @@ def GetManagedObjects(self):
"%s.domains.%s" % (self.bus_name._name, d.properties['qid'])
for d in self.domains}

@dbus.service.method(dbus_interface='org.qubes.DomainManager1',
in_signature='a{sv}b')
def AddDomain(self, vm, execute=False):
# type: (Dict[dbus.String, Any], bool) -> bool
if execute:
log.error('Creating domains via DBus is not implemented yet')
return False
else:
vm['qid'] = len(self.domains)
self.domains.append(self._proxify_domain(vm))
log.info('Added domain %s', vm['name'])
return True

@dbus.service.method(dbus_interface='org.qubes.DomainManager1',
in_signature='ob', out_signature='b')
def DelDomain(self, vm_dbus_path, execute=False):
# type: (dbus.ObjectPath, bool) -> bool
if execute:
log.error('Creating domains via DBus is not implemented yet')
return False
for vm in self.domains:
if vm._object_path == vm_dbus_path: # pylint: disable=protected-access
vm.remove_from_connection()
self.domains.remove(vm)
return True
return False

def _proxify_domain(self, vm):
# type: (Dict[Union[str,dbus.String], Any]) -> Domain
return Domain(self.bus, self.bus_name, self.bus_path, vm)
Expand Down
45 changes: 37 additions & 8 deletions qubesdbus/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from qubes.vm.qubesvm import QubesVM
from systemd.journal import JournalHandler

import qubesdbus.serialize

from .constants import NAME_PREFIX, PATH_PREFIX, VERSION

try:
Expand All @@ -39,7 +41,7 @@

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

INTERFACE_NAME = "%s.QubesSignals%s" % (NAME_PREFIX, VERSION)
Expand All @@ -48,11 +50,12 @@
MANAGER_PATH = "%s/DomainManager%s" % (PATH_PREFIX, VERSION)
GARBAGE = [
'domain-is-fully-usable', # only important for internal core-admin?
'domain-add'
]


def is_garbage(event):
if event in ['domain-load', 'domain-init']:
if event in ['domain-load']:
return True
elif event.startswith('property-pre-set'):
return True
Expand All @@ -65,30 +68,56 @@ class QubesDbusProxy(Extension):
def __init__(self):
super(QubesDbusProxy, self).__init__()
self.domains = {} # type: Dict[int, bool]
self.new_vm = []

@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:'):
elif event.startswith('property-set:') and not self.new_vm:
proxy = vm_proxy(vm.qid)
property_set(proxy, args[0], str(args[1]))
property_set(proxy, args[0],
qubesdbus.serialize.serialize_val(args[1]))
log.info('VM: %s %s %s %s', vm, event, args, kwargs)
elif event == 'domain-init' and vm.storage is None:
# looks like a new vm is created
self.new_vm.append(vm)
log.info('VM %s creation begins', vm)
elif event == 'domain-create-on-disk':
proxy = app_proxy()
func = proxy.get_dbus_method('AddDomain',
'org.qubes.DomainManager1')
data = qubesdbus.serialize.serialize_val(vm)
create = False
if not func(data, create):
log.error('Could not add vm via to dbus DomainManager')
log.info('Added VM %s', data)
self.new_vm.remove(vm)
else:
log.warn('Unknown %s from %s', event, vm)
log.warn('Unknown %s from %s %s %s', event, vm, args, kwargs)

@handler('*', system=True)
def forward_app_event(self, vm, event, *args, **kwargs):
proxy = app_proxy()
if is_garbage(event):
log.debug('Drop %s from %s', event, vm)
return
elif event.startswith('property-set:'):
proxy = app_proxy()
property_set(proxy, args[0], str(args[1]))
property_set(proxy, args[0],
qubesdbus.serialize.serialize_val(args[1]))
log.info('App: %s %s %s %s', vm, event, args, kwargs)
elif event == 'domain-delete':
func = proxy.get_dbus_method('DelDomain',
'org.qubes.DomainManager1')
vm = args[0]
vm_dbus_path = '/org/qubes/DomainManager1/domains/%s' % vm.qid

if not func(vm_dbus_path, False):
log.error('Could not add vm via to dbus DomainManager')
log.info("Removed VM %s", vm)
else:
log.warn('Unknown %s from %s', event, vm)
log.warn('Unknown %s from %s %s %s', event, vm, args, kwargs)


def property_set(proxy, name, value):
Expand Down

0 comments on commit c76aa46

Please sign in to comment.