Skip to content

Commit

Permalink
q-dev: events
Browse files Browse the repository at this point in the history
device-added:usb
device-removed:usb
  • Loading branch information
piotrbartman committed Jun 1, 2024
1 parent d985cf1 commit ab08892
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions qubesusbproxy/core3ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import re
import string
import subprocess
import sys

import tempfile
from enum import Enum
Expand All @@ -48,7 +49,6 @@
class USBDevice(qubes.devices.DeviceInfo):
# pylint: disable=too-few-public-methods
def __init__(self, backend_domain, ident):
# super(USBDevice, self).__init__(backend_domain, ident, None)
super(USBDevice, self).__init__(
backend_domain=backend_domain, ident=ident, devclass="usb")

Expand Down Expand Up @@ -137,8 +137,7 @@ def interfaces(self) -> List[qubes.devices.DeviceInterface]:
Every device should have at least one interface.
"""
if (len(self._interfaces) == 1
and self._interfaces[0] == qubes.devices.DeviceInterface.Other):
if self._interfaces is None:
result = self._load_interfaces_from_qubesdb()
else:
result = self._interfaces
Expand All @@ -162,7 +161,7 @@ def parent_device(self) -> Optional[qubes.devices.DeviceInfo]:

def _load_interfaces_from_qubesdb(self) \
-> List[qubes.devices.DeviceInterface]:
result = []
result = [qubes.devices.DeviceInterface.Other]
if not self.backend_domain.is_running():
# don't cache this value
return result
Expand Down Expand Up @@ -319,6 +318,7 @@ def _load_usb_known_devices() -> Dict[str, Dict[str, Tuple[str, str]]]:

return result


class USBProxyNotInstalled(qubes.exc.QubesException):
pass

Expand Down Expand Up @@ -397,9 +397,12 @@ def on_domain_init_load(self, vm, event):
if event == 'domain-load':
# avoid building a cache on domain-init, as it isn't fully set yet,
# and definitely isn't running yet
current_devices = dict((dev.ident, dev.frontend_domain)
for dev in self.on_device_list_usb(vm, None))
current_devices = {
dev.ident: dev.frontend_domain
for dev in self.on_device_list_usb(vm, None)
}
self.devices_cache[vm.name] = current_devices
# TODO: fire device-added
else:
self.devices_cache[vm.name] = {}

Expand Down Expand Up @@ -427,26 +430,32 @@ def on_qdb_change(self, vm, event, path):
connected_devices = dict()
disconnected_devices = dict()
devices_cache_for_vm = self.devices_cache[vm.name]
for dev, connected_to in current_devices.items():
if dev not in devices_cache_for_vm:
new_devices.add(dev)
elif devices_cache_for_vm[dev] != current_devices[dev]:
if devices_cache_for_vm[dev] is not None:
disconnected_devices[dev] = devices_cache_for_vm[dev]
if current_devices[dev] is not None:
connected_devices[dev] = current_devices[dev]
for dev_id, connected_to in current_devices.items():
if dev_id not in devices_cache_for_vm:
new_devices.add(dev_id)
device = USBDevice(vm, dev_id)
vm.fire_event('device-added:usb', device=device)
elif devices_cache_for_vm[dev_id] != current_devices[dev_id]:
if devices_cache_for_vm[dev_id] is not None:
disconnected_devices[dev_id] = devices_cache_for_vm[dev_id]
if current_devices[dev_id] is not None:
connected_devices[dev_id] = current_devices[dev_id]
for dev_id, connected_to in devices_cache_for_vm.items():
if dev_id not in current_devices:
device = USBDevice(vm, dev_id)
vm.fire_event('device-removed:usb', device=device)

self.devices_cache[vm.name] = current_devices
# send events about devices detached/attached outside by themselves
# (like device pulled out or manual qubes.USB qrexec call)
for dev_ident, front_vm in disconnected_devices.items():
dev = USBDevice(vm, dev_ident)
dev_id = USBDevice(vm, dev_ident)
asyncio.ensure_future(front_vm.fire_event_async('device-detach:usb',
device=dev))
device=dev_id))
for dev_ident, front_vm in connected_devices.items():
dev = USBDevice(vm, dev_ident)
dev_id = USBDevice(vm, dev_ident)
asyncio.ensure_future(front_vm.fire_event_async('device-attach:usb',
device=dev,
device=dev_id,
options={}))
for front_vm in vm.app.domains:
if not front_vm.is_running():
Expand Down

0 comments on commit ab08892

Please sign in to comment.