devices.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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:' + self._class, device)
  50. self._set.add(device)
  51. self._vm.fire_event('device-attach:' + self._class, device)
  52. def detach(self, device):
  53. '''Detach (remove) device from domain.
  54. :param str device: device identifier (format is class-dependent)
  55. '''
  56. if device not in self:
  57. raise KeyError(
  58. 'device {!r} of class {} not attached to {!r}'.format(
  59. device, self._class, self._vm))
  60. self._vm.fire_event_pre('device-pre-detach:' + self._class, device)
  61. self._set.remove(device)
  62. self._vm.fire_event('device-detach:' + self._class, device)
  63. def __iter__(self):
  64. return iter(self._set)
  65. def __contains__(self, item):
  66. return item in self._set
  67. def __len__(self):
  68. return len(self._set)
  69. class DeviceManager(dict):
  70. '''Device manager that hold all devices by their classess.
  71. :param vm: VM for which we manage devices
  72. '''
  73. def __init__(self, vm):
  74. super(DeviceManager, self).__init__()
  75. self._vm = vm
  76. def __missing__(self, key):
  77. self[key] = DeviceCollection(self._vm, key)
  78. return self[key]
  79. class RegexDevice(str):
  80. def __init__(self, *args, **kwargs):
  81. super(RegexDevice, self).__init__(*args, **kwargs)
  82. dev_match = self.regex.match(self)
  83. if not dev_match:
  84. raise ValueError('Invalid device identifier: {!r}'.format(self))
  85. for group in self.regex.groupindex:
  86. setattr(self, group, dev_match.group(group))
  87. class PCIDevice(RegexDevice):
  88. regex = re.compile(
  89. r'^(?P<bus>[0-9a-f]+):(?P<device>[0-9a-f]+)\.(?P<function>[0-9a-f]+)$')