devices.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import re
  24. import qubes
  25. class DeviceCollection(object):
  26. '''Bag for devices.
  27. Used as default value for :py:meth:`DeviceManager.__missing__` factory.
  28. :param vm: VM for which we manage devices
  29. :param class_: device class
  30. '''
  31. def __init__(self, vm, class_):
  32. self._vm = vm
  33. self._class = class_
  34. self._set = set()
  35. def attach(self, device):
  36. '''Attach (add) device to domain.
  37. :param str device: device identifier (format is class-dependent)
  38. '''
  39. try:
  40. devclass = qubes.get_entry_point_one('qubes.devices', self._class)
  41. except KeyError:
  42. devclass = str
  43. if not isinstance(device, devclass):
  44. device = devclass(device)
  45. if device in self:
  46. raise KeyError(
  47. 'device {!r} of class {} already attached to {!r}'.format(
  48. device, self._class, self._vm))
  49. self._vm.fire_event_pre('device-pre-attach:{}'.format(self._class),
  50. device)
  51. self._set.add(device)
  52. self._vm.fire_event('device-attach:{}'.format(self._class), device)
  53. def detach(self, device):
  54. '''Detach (remove) device from domain.
  55. :param str device: device identifier (format is class-dependent)
  56. '''
  57. if device not in self:
  58. raise KeyError(
  59. 'device {!r} of class {} not attached to {!r}'.format(
  60. device, self._class, self._vm))
  61. self._vm.fire_event_pre('device-pre-detach:{}'.format(self._class),
  62. device)
  63. self._set.remove(device)
  64. self._vm.fire_event('device-detach:{}'.format(self._class), device)
  65. def __iter__(self):
  66. return iter(self._set)
  67. def __contains__(self, item):
  68. return item in self._set
  69. def __len__(self):
  70. return len(self._set)
  71. class DeviceManager(dict):
  72. '''Device manager that hold all devices by their classess.
  73. :param vm: VM for which we manage devices
  74. '''
  75. def __init__(self, vm):
  76. super(DeviceManager, self).__init__()
  77. self._vm = vm
  78. def __missing__(self, key):
  79. self[key] = DeviceCollection(self._vm, key)
  80. return self[key]
  81. class RegexDevice(str):
  82. def __init__(self, *args, **kwargs):
  83. super(RegexDevice, self).__init__(*args, **kwargs)
  84. dev_match = self.regex.match(self)
  85. if not dev_match:
  86. raise ValueError('Invalid device identifier: {!r}'.format(self))
  87. for group in self.regex.groupindex:
  88. setattr(self, group, dev_match.group(group))
  89. class PCIDevice(RegexDevice):
  90. regex = re.compile(
  91. r'^(?P<bus>[0-9a-f]+):(?P<device>[0-9a-f]+)\.(?P<function>[0-9a-f]+)$')