From 865ab10a0cc097f06a959d952d954903c31904c3 Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Wed, 1 Mar 2017 17:17:10 +0100 Subject: [PATCH] qubesd+mgmt: convert mgmt functions to coroutines QubesOS/qubes-issues#2622 --- qubes/mgmt.py | 9 +++++++-- qubes/tools/qubesd.py | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/qubes/mgmt.py b/qubes/mgmt.py index 7ee77e90..dec12b3f 100644 --- a/qubes/mgmt.py +++ b/qubes/mgmt.py @@ -22,8 +22,8 @@ Qubes OS Management API ''' +import asyncio import reprlib -import types import qubes.vm.qubesvm @@ -91,7 +91,7 @@ class QubesMgmt(object): 'no such attribute: {!r}'.format( untrusted_func_name)) - if not isinstance(untrusted_func, types.MethodType): + if not asyncio.iscoroutinefunction(untrusted_func): raise ProtocolError( 'no such method: {!r}'.format( untrusted_func_name)) @@ -128,6 +128,7 @@ class QubesMgmt(object): # ACTUAL RPC CALLS # + @asyncio.coroutine def vm_list(self, untrusted_payload): assert self.dest.name == 'dom0' assert not self.arg @@ -142,6 +143,7 @@ class QubesMgmt(object): vm.get_power_state()) for vm in sorted(domains)) + @asyncio.coroutine def vm_property_list(self, untrusted_payload): assert not self.arg assert not untrusted_payload @@ -151,6 +153,7 @@ class QubesMgmt(object): return ''.join('{}\n'.format(prop.__name__) for prop in properties) + @asyncio.coroutine def vm_property_get(self, untrusted_payload): assert self.arg in self.dest.property_list() assert not untrusted_payload @@ -167,6 +170,7 @@ class QubesMgmt(object): str(self.dest.property_is_default(self.arg)), self.repr(value)) + @asyncio.coroutine def vm_property_help(self, untrusted_payload): assert self.arg in self.dest.property_list() assert not untrusted_payload @@ -181,6 +185,7 @@ class QubesMgmt(object): return qubes.utils.format_doc(doc) + @asyncio.coroutine def vm_property_reset(self, untrusted_payload): assert self.arg in self.dest.property_list() assert not untrusted_payload diff --git a/qubes/tools/qubesd.py b/qubes/tools/qubesd.py index 4217a3af..2df5596b 100644 --- a/qubes/tools/qubesd.py +++ b/qubes/tools/qubesd.py @@ -59,9 +59,15 @@ class QubesDaemonProtocol(asyncio.Protocol): finally: self.untrusted_buffer.close() + asyncio.ensure_future(self.respond( + src, method, dest, arg, untrusted_payload=untrusted_payload)) + + @asyncio.coroutine + def respond(self, src, method, dest, arg, *, untrusted_payload): try: mgmt = qubes.mgmt.QubesMgmt(self.app, src, method, dest, arg) - response = mgmt.execute(untrusted_payload=untrusted_payload) + response = yield from mgmt.execute( + untrusted_payload=untrusted_payload) # except clauses will fall through to transport.abort() below