From d1bd30be19bea6abddbbf3863902224a719467db Mon Sep 17 00:00:00 2001 From: Bahtiar `kalkin-` Gadimov Date: Mon, 3 Oct 2016 20:35:01 +0200 Subject: [PATCH] Fix startup/shutdown support --- qubesdbus/domain.py | 25 ++++++++++++++++++++++--- qubesdbus/proxy.py | 9 +++++++++ qubesdbus/serialize.py | 27 +++++++++++++-------------- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/qubesdbus/domain.py b/qubesdbus/domain.py index 9653b79..bbe3114 100644 --- a/qubesdbus/domain.py +++ b/qubesdbus/domain.py @@ -17,10 +17,10 @@ # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - ''' D-Bus Domain object ''' import dbus.service +import qubes import qubesdbus.service @@ -38,12 +38,14 @@ class Domain(qubesdbus.service.PropertiesObject): ''' `Domain` is managed by `DomainManager1` and represents a domain. Its D-Bus object path is `/org/qubes/DomainManager1/domains/QID` ''' + def __init__(self, bus, bus_name, bus_path, data): # type: (SessionBus, BusName , str, Dict[Union[str,dbus.String], Any]) -> None self.properties = data bus_path = '/'.join([bus_path, 'domains', str(data['qid'])]) - name = data['name'] - super(Domain, self).__init__(name, 'org.qubes.Domain1', data, bus=bus, bus_name=bus_name, + self.name = data['name'] + super(Domain, self).__init__(self.name, 'org.qubes.Domain1', data, + bus=bus, bus_name=bus_name, bus_path=bus_path) @dbus.service.signal( @@ -57,3 +59,20 @@ def StateSignal(self, name): signature='s') def StartingSignal(self, name): self.properties['state'] = name + + @dbus.service.method("org.qubes.Domain", out_signature="b") + def Shutdown(self): + app = qubes.Qubes() + name = str(self.name) + vm = app.domains[name] + vm.shutdown(wait=True) + self.properties['state'] = 'halted' + return True + + @dbus.service.method("org.qubes.Domain", out_signature="b") + def Start(self): + app = qubes.Qubes() + name = str(self.name) + vm = app.domains[name] + vm.start() + return True diff --git a/qubesdbus/proxy.py b/qubesdbus/proxy.py index e12711f..1bcafd5 100644 --- a/qubesdbus/proxy.py +++ b/qubesdbus/proxy.py @@ -92,6 +92,15 @@ def forward_vm_event(self, vm, event, *args, **kwargs): log.error('Could not add vm via to dbus DomainManager') log.info('Added VM %s', data) self.new_vm.remove(vm) + elif event == 'domain-start': + proxy = vm_proxy(vm.qid) + if kwargs['start_guid'] == True: + property_set(proxy, 'state', 'qrexec_running') + else: + property_set(proxy, 'state', 'running') + elif event == 'domain-shutdown': + proxy = vm_proxy(vm.qid) + property_set(proxy, 'state', 'halted') else: log.warn('Unknown %s from %s %s %s', event, vm, args, kwargs) diff --git a/qubesdbus/serialize.py b/qubesdbus/serialize.py index a711ce2..e5d08a4 100644 --- a/qubesdbus/serialize.py +++ b/qubesdbus/serialize.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - ''' Collection of serialization helpers ''' import dbus @@ -110,20 +109,20 @@ def domain_data(vm): result = dbus.Dictionary({'state': 'halted'}, signature='ss') for name in DOMAIN_PROPERTIES: if name in DOMAIN_STATE_PROPERTIES: - key = 'state' - elif name.startswith("is_"): - _, key = name.split("_", 1) + if getattr(vm, name)() is True: + _, value = name.split("_", 1) + name = 'state' + else: + continue else: - key = name - - key = dbus.String(key) - try: - - value = serialize_val(getattr(vm, name)) - - result[key] = value - except (AttributeError, libvirtError): - result[key] = dbus.String('') + try: + value = serialize_val(getattr(vm, name)) + except (AttributeError, libvirtError): + value = dbus.String('') + if name.startswith("is_"): + _, name = name.split("_", 1) + + result[name] = value return result