services.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program 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
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://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):
  29. '''Actually export features'''
  30. for feature, value in vm.features.items():
  31. if not feature.startswith('service.'):
  32. continue
  33. service = feature[len('service.'):]
  34. # forcefully convert to '0' or '1'
  35. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  36. str(int(bool(value))))
  37. @qubes.ext.handler('domain-feature-set')
  38. def on_domain_feature_set(self, vm, feature, value, oldvalue=None):
  39. '''Update /qubes-service/ QubesDB tree in runtime'''
  40. # pylint: disable=unused-argument
  41. if not vm.is_running():
  42. return
  43. if not feature.startswith('service.'):
  44. return
  45. service = feature[len('service.'):]
  46. # forcefully convert to '0' or '1'
  47. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  48. str(int(bool(value))))
  49. @qubes.ext.handler('domain-feature-delete')
  50. def on_domain_feature_delete(self, vm, feature):
  51. '''Update /qubes-service/ QubesDB tree in runtime'''
  52. if not vm.is_running():
  53. return
  54. if not feature.startswith('service.'):
  55. return
  56. service = feature[len('service.'):]
  57. vm.untrusted_qdb.rm('/qubes-service/{}'.format(service))