services.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, 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. @qubes.ext.handler('domain-feature-set')
  39. def on_domain_feature_set(self, vm, event, feature, value, oldvalue=None):
  40. '''Update /qubes-service/ QubesDB tree in runtime'''
  41. # pylint: disable=unused-argument
  42. if not vm.is_running():
  43. return
  44. if not feature.startswith('service.'):
  45. return
  46. service = feature[len('service.'):]
  47. # forcefully convert to '0' or '1'
  48. vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
  49. str(int(bool(value))))
  50. @qubes.ext.handler('domain-feature-delete')
  51. def on_domain_feature_delete(self, vm, event, feature):
  52. '''Update /qubes-service/ QubesDB tree in runtime'''
  53. # pylint: disable=unused-argument
  54. if not vm.is_running():
  55. return
  56. if not feature.startswith('service.'):
  57. return
  58. service = feature[len('service.'):]
  59. vm.untrusted_qdb.rm('/qubes-service/{}'.format(service))