services.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if not vm.is_running():
  46. return
  47. if not feature.startswith('service.'):
  48. return
  49. service = feature[len('service.'):]
  50. # forcefully convert to '0' or '1'
  51. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  52. str(int(bool(value))))
  53. @qubes.ext.handler('domain-feature-delete:*')
  54. def on_domain_feature_delete(self, vm, event, feature):
  55. '''Update /qubes-service/ QubesDB tree in runtime'''
  56. # pylint: disable=unused-argument
  57. if not vm.is_running():
  58. return
  59. if not feature.startswith('service.'):
  60. return
  61. service = feature[len('service.'):]
  62. vm.untrusted_qdb.rm('/qubes-service/{}'.format(service))
  63. @qubes.ext.handler('features-request')
  64. def supported_services(self, vm, event, untrusted_features):
  65. '''Handle advertisement of supported services'''
  66. # pylint: disable=no-self-use,unused-argument
  67. if getattr(vm, 'template', None):
  68. vm.log.warning(
  69. 'Ignoring qubes.FeaturesRequest from template-based VM')
  70. return
  71. new_supported_services = set()
  72. for requested_service in untrusted_features:
  73. if not requested_service.startswith('supported-service.'):
  74. continue
  75. if untrusted_features[requested_service] == '1':
  76. # only allow to advertise service as supported, lack of entry
  77. # means service is not supported
  78. new_supported_services.add(requested_service)
  79. del untrusted_features
  80. # if no service is supported, ignore the whole thing - do not clear
  81. # all services in case of empty request (manual or such)
  82. if not new_supported_services:
  83. return
  84. old_supported_services = set(
  85. feat for feat in vm.features
  86. if feat.startswith('supported-service.') and vm.features[feat])
  87. for feature in new_supported_services.difference(
  88. old_supported_services):
  89. vm.features[feature] = True
  90. for feature in old_supported_services.difference(
  91. new_supported_services):
  92. del vm.features[feature]