Skip to content

Commit 1267f3f

Browse files
committed
q-dev: async confirmation
1 parent 1db8f8f commit 1267f3f

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

qubes/ext/block.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import qubes.device_protocol
3333
import qubes.devices
3434
import qubes.ext
35-
from qubes.ext.utils import device_list_change, confirm_device_attachment
35+
from qubes.ext import utils
3636
from qubes.storage import Storage
3737

3838
name_re = re.compile(r"\A[a-z0-9-]{1,12}\Z")
@@ -324,7 +324,7 @@ def on_qdb_change(self, vm, event, path):
324324
current_devices = dict(
325325
(dev.port_id, device_attachments.get(dev.port_id, None))
326326
for dev in self.on_device_list_block(vm, None))
327-
device_list_change(self, current_devices, vm, path, BlockDevice)
327+
utils.device_list_change(self, current_devices, vm, path, BlockDevice)
328328

329329
@staticmethod
330330
def get_device_attachments(vm_):
@@ -569,7 +569,10 @@ async def attach_and_notify(self, vm, assignment):
569569
# bypass DeviceCollection logic preventing double attach
570570
device = assignment.device
571571
if assignment.mode.value == "ask-to-attach":
572-
if vm.name != confirm_device_attachment(device, {vm: assignment}):
572+
allowed = await utils.confirm_device_attachment(
573+
device, {vm: assignment})
574+
allowed = allowed.strip()
575+
if vm.name != allowed:
573576
return
574577
self.on_device_pre_attached_block(
575578
vm, 'device-pre-attach:block', device, assignment.options)

qubes/ext/utils.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def device_list_change(
8686
if len(frontends) > 1:
8787
# unique
8888
device = tuple(frontends.values())[0].device
89-
target_name = confirm_device_attachment(device, frontends)
89+
target_name = asyncio.ensure_future(
90+
confirm_device_attachment(device, frontends)).result()
9091
for front in frontends:
9192
if front.name == target_name:
9293
target = front
@@ -140,17 +141,21 @@ def compare_device_cache(vm, devices_cache, current_devices):
140141
return added, attached, detached, removed
141142

142143

143-
def confirm_device_attachment(device, frontends) -> str:
144+
async def confirm_device_attachment(device, frontends) -> str:
144145
try:
146+
front_names = [f.name for f in frontends.keys()]
145147
# pylint: disable=consider-using-with
146-
proc = subprocess.Popen(
147-
["attach-confirm", device.backend_domain.name,
148-
device.port_id, device.description,
149-
*[f.name for f in frontends.keys()]],
150-
stdout=subprocess.PIPE, stderr=subprocess.PIPE
148+
# vm names are safe to just join by spaces
149+
proc = await asyncio.create_subprocess_shell(
150+
" ".join(["attach-confirm", device.backend_domain.name,
151+
device.port_id, "'" + device.description + "'", *front_names]),
152+
stdout=asyncio.subprocess.PIPE
151153
)
152-
(target_name, _) = proc.communicate()
153-
return target_name.decode()
154+
(target_name, _) = await proc.communicate()
155+
target_name = target_name.decode()
156+
if target_name in front_names:
157+
return target_name
158+
return ""
154159
except Exception as exc:
155160
print("attach-confirm", exc, file=sys.stderr)
156161
return ""

0 commit comments

Comments
 (0)