From 626f1835628d78d95eb9444e7938a63dcae53835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Pierret=20=28fepitre=29?= Date: Fri, 10 May 2024 16:23:21 +0200 Subject: [PATCH] qvm_start_daemon: improve reset property handling --- qubesadmin/tools/qvm_start_daemon.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/qubesadmin/tools/qvm_start_daemon.py b/qubesadmin/tools/qvm_start_daemon.py index c551deb3..6c3edd7a 100644 --- a/qubesadmin/tools/qvm_start_daemon.py +++ b/qubesadmin/tools/qvm_start_daemon.py @@ -736,7 +736,7 @@ def on_domain_stopped(self, vm, _event, **_kwargs): self.cleanup_guid(stubdom_xid) self.cleanup_pacat_process(stubdom_xid) - def on_property_audiovm_set(self, vm, _event, **_kwargs): + def on_property_audiovm_set(self, vm, event, **kwargs): """Handler for catching event related to dynamic AudioVM set/unset""" if vm.name not in self.xid_cache: try: @@ -745,7 +745,13 @@ def on_property_audiovm_set(self, vm, _event, **_kwargs): log.error("vm.name: failed to determine XID: %s", str(e)) return xid, stubdom_xid = self.xid_cache[vm.name] - newvalue = _kwargs.get("newvalue", None) + # We ensure that on_property_audiovm_set is really called with + # newvalue as kwarg to not fallback all the times to vm.audiovm + # in order to have newvalue=None as requested. + if "newvalue" in kwargs: # pylint: disable=consider-using-get + newvalue = kwargs["newvalue"] + else: + newvalue = str(getattr(vm, "audiovm", None)) if newvalue != self.app.local_name: if xid != -1: self.cleanup_pacat_process(xid) @@ -756,8 +762,11 @@ def on_property_audiovm_set(self, vm, _event, **_kwargs): del self.xid_cache[vm.name] except KeyError: return - elif (newvalue == self.app.local_name and - vm.get_power_state() == "Running"): + if ( + event in ["property-set:audiovm", "property-reset:audiovm"] + and newvalue == self.app.local_name + and vm.get_power_state() == "Running" + ): asyncio.ensure_future(self.start_audio(vm)) def cleanup_guid(self, xid): @@ -799,8 +808,12 @@ def register_events(self, events): self.on_connection_established) events.add_handler('domain-stopped', self.on_domain_stopped) - for event in ["property-set:audiovm", "property-pre-set:audiovm", - "property-pre-del:audiovm"]: + for event in [ + "property-set:audiovm", + "property-pre-set:audiovm", + "property-pre-reset:audiovm", + "property-reset:audiovm" + ]: events.add_handler(event, self.on_property_audiovm_set) def is_watched(self, vm):