Skip to content

Commit

Permalink
q-dev: frontend_device -> attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Jun 1, 2024
1 parent ece1fd0 commit daac099
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions qubesusbproxy/core3ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
usb_connected_to_re = re.compile(br"^[a-zA-Z][a-zA-Z0-9_.-]*$")
usb_device_hw_ident_re = re.compile(r'^[0-9a-f]{4}:[0-9a-f]{4} ')

HWDATA_PATH = '/usr/share/hwdata'

class USBDevice(qubes.devices.DeviceInfo):
# pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -228,7 +229,7 @@ def _sanitize(
)

@property
def frontend_domain(self):
def attachment(self):
if not self.backend_domain.is_running():
return None
untrusted_connected_to = self.backend_domain.untrusted_qdb.read(
Expand Down Expand Up @@ -283,8 +284,8 @@ def _load_usb_known_devices() -> Dict[str, Dict[str, Tuple[str, str]]]:
# subclass subclass_name <-- single tab
# prog-if prog-if_name <-- two tabs
result = {}
with open('/usr/share/hwdata/usb.ids',
encoding='utf-8', errors='ignore') as usb_ids: # TODO debian etc.
with open(HWDATA_PATH + '/usb.ids',
encoding='utf-8', errors='ignore') as usb_ids:
for line in usb_ids.readlines():
line = line.rstrip()
if line.startswith('#'):
Expand Down Expand Up @@ -394,7 +395,7 @@ def on_domain_init_load(self, vm, event):
# avoid building a cache on domain-init, as it isn't fully set yet,
# and definitely isn't running yet
current_devices = {
dev.ident: dev.frontend_domain
dev.ident: dev.attachment
for dev in self.on_device_list_usb(vm, None)
}
self.devices_cache[vm.name] = current_devices
Expand All @@ -416,7 +417,7 @@ def on_qdb_change(self, vm, event, path):
"""A change in QubesDB means a change in device list."""
# pylint: disable=unused-argument,no-self-use
vm.fire_event('device-list-change:usb')
current_devices = dict((dev.ident, dev.frontend_domain)
current_devices = dict((dev.ident, dev.attachment)
for dev in self.on_device_list_usb(vm, None))

# send events about devices detached/attached outside by themselves
Expand Down Expand Up @@ -535,7 +536,7 @@ def on_device_list_attached(self, vm, event, **kwargs):
return

for dev in self.get_all_devices(vm.app):
if dev.frontend_domain == vm:
if dev.attachment == vm:
yield (dev, {'identity': dev.full_identity})

@qubes.ext.handler('device-pre-attach:usb')
Expand Down Expand Up @@ -567,10 +568,10 @@ async def on_device_attach_usb(self, vm, event, device, options):
# file=sys.stderr)
return

if device.frontend_domain:
if device.attachment:
raise qubes.devices.DeviceAlreadyAttached(
'Device {!s} already attached to {!s}'.format(
device, device.frontend_domain)
device, device.attachment)
)

stubdom_qrexec = (
Expand Down Expand Up @@ -622,7 +623,7 @@ async def on_device_detach_usb(self, vm, event, device):
if not isinstance(device, USBDevice):
return

connected_to = device.frontend_domain
connected_to = device.attachment
# detect race conditions; there is still race here, but much smaller
if connected_to is None or connected_to.qid != vm.qid:
raise QubesUSBException(
Expand Down

0 comments on commit daac099

Please sign in to comment.