services.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. '''Extension responsible for qvm-service framework'''
  21. import qubes.ext
  22. class ServicesExtension(qubes.ext.Extension):
  23. '''This extension export features with 'service.' prefix to QubesDB in
  24. /qubes-service/ tree.
  25. '''
  26. # pylint: disable=no-self-use
  27. @qubes.ext.handler('domain-qdb-create')
  28. def on_domain_qdb_create(self, vm, event):
  29. '''Actually export features'''
  30. # pylint: disable=unused-argument
  31. for feature, value in vm.features.items():
  32. if not feature.startswith('service.'):
  33. continue
  34. service = feature[len('service.'):]
  35. # forcefully convert to '0' or '1'
  36. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  37. str(int(bool(value))))
  38. # always set meminfo-writer according to maxmem
  39. vm.untrusted_qdb.write('/qubes-service/meminfo-writer',
  40. '1' if vm.maxmem > 0 else '0')
  41. @qubes.ext.handler('domain-feature-set:*')
  42. def on_domain_feature_set(self, vm, event, feature, value, oldvalue=None):
  43. '''Update /qubes-service/ QubesDB tree in runtime'''
  44. # pylint: disable=unused-argument
  45. # TODO: remove this compatibility hack in Qubes 4.1
  46. if feature == 'service.meminfo-writer':
  47. # if someone try to enable meminfo-writer ...
  48. if value:
  49. # ... reset maxmem to default
  50. vm.maxmem = qubes.property.DEFAULT
  51. else:
  52. # otherwise, set to 0
  53. vm.maxmem = 0
  54. # in any case, remove the entry, as it does not indicate memory
  55. # balancing state anymore
  56. del vm.features['service.meminfo-writer']
  57. if not vm.is_running():
  58. return
  59. if not feature.startswith('service.'):
  60. return
  61. service = feature[len('service.'):]
  62. # forcefully convert to '0' or '1'
  63. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  64. str(int(bool(value))))
  65. @qubes.ext.handler('domain-feature-delete:*')
  66. def on_domain_feature_delete(self, vm, event, feature):
  67. '''Update /qubes-service/ QubesDB tree in runtime'''
  68. # pylint: disable=unused-argument
  69. if not vm.is_running():
  70. return
  71. if not feature.startswith('service.'):
  72. return
  73. service = feature[len('service.'):]
  74. # this one is excluded from user control
  75. if service == 'meminfo-writer':
  76. return
  77. vm.untrusted_qdb.rm('/qubes-service/{}'.format(service))
  78. @qubes.ext.handler('domain-load')
  79. def on_domain_load(self, vm, event):
  80. '''Migrate meminfo-writer service into maxmem'''
  81. # pylint: disable=no-self-use,unused-argument
  82. if 'service.meminfo-writer' in vm.features:
  83. # if was set to false, force maxmem=0
  84. # otherwise, simply ignore as the default is fine
  85. if not vm.features['service.meminfo-writer']:
  86. vm.maxmem = 0
  87. del vm.features['service.meminfo-writer']
  88. @qubes.ext.handler('features-request')
  89. def supported_services(self, vm, event, untrusted_features):
  90. '''Handle advertisement of supported services'''
  91. # pylint: disable=no-self-use,unused-argument
  92. if getattr(vm, 'template', None):
  93. vm.log.warning(
  94. 'Ignoring qubes.FeaturesRequest from template-based VM')
  95. return
  96. new_supported_services = set()
  97. for requested_service in untrusted_features:
  98. if not requested_service.startswith('supported-service.'):
  99. continue
  100. if untrusted_features[requested_service] == '1':
  101. # only allow to advertise service as supported, lack of entry
  102. # means service is not supported
  103. new_supported_services.add(requested_service)
  104. del untrusted_features
  105. # if no service is supported, ignore the whole thing - do not clear
  106. # all services in case of empty request (manual or such)
  107. if not new_supported_services:
  108. return
  109. old_supported_services = set(
  110. feat for feat in vm.features
  111. if feat.startswith('supported-service.') and vm.features[feat])
  112. for feature in new_supported_services.difference(
  113. old_supported_services):
  114. vm.features[feature] = True
  115. for feature in old_supported_services.difference(
  116. new_supported_services):
  117. del vm.features[feature]