internal.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- encoding: utf8 -*-
  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. ''' Internal interface for dom0 components to communicate with qubesd. '''
  21. import asyncio
  22. import json
  23. import subprocess
  24. import qubes.api
  25. import qubes.api.admin
  26. import qubes.vm.adminvm
  27. import qubes.vm.dispvm
  28. class QubesInternalAPI(qubes.api.AbstractQubesAPI):
  29. ''' Communication interface for dom0 components,
  30. by design the input here is trusted.'''
  31. SOCKNAME = '/var/run/qubesd.internal.sock'
  32. @qubes.api.method('internal.GetSystemInfo', no_payload=True)
  33. @asyncio.coroutine
  34. def getsysteminfo(self):
  35. self.enforce(self.dest.name == 'dom0')
  36. self.enforce(not self.arg)
  37. system_info = {'domains': {
  38. domain.name: {
  39. 'tags': list(domain.tags),
  40. 'type': domain.__class__.__name__,
  41. 'template_for_dispvms':
  42. getattr(domain, 'template_for_dispvms', False),
  43. 'default_dispvm': (str(domain.default_dispvm) if
  44. getattr(domain, 'default_dispvm', None) else None),
  45. 'icon': str(domain.label.icon),
  46. } for domain in self.app.domains
  47. }}
  48. return json.dumps(system_info)
  49. @qubes.api.method('internal.vm.volume.ImportEnd')
  50. @asyncio.coroutine
  51. def vm_volume_import_end(self, untrusted_payload):
  52. '''
  53. This is second half of admin.vm.volume.Import handling. It is called
  54. when actual import is finished. Response from this method is sent do
  55. the client (as a response for admin.vm.volume.Import call).
  56. '''
  57. self.enforce(self.arg in self.dest.volumes.keys())
  58. success = untrusted_payload == b'ok'
  59. try:
  60. yield from self.dest.storage.import_data_end(self.arg,
  61. success=success)
  62. except:
  63. self.dest.fire_event('domain-volume-import-end', volume=self.arg,
  64. success=False)
  65. raise
  66. self.dest.fire_event('domain-volume-import-end', volume=self.arg,
  67. success=success)
  68. if not success:
  69. raise qubes.exc.QubesException('Data import failed')
  70. @qubes.api.method('internal.SuspendPre', no_payload=True)
  71. @asyncio.coroutine
  72. def suspend_pre(self):
  73. '''
  74. Method called before host system goes to sleep.
  75. :return:
  76. '''
  77. # first notify all VMs
  78. processes = []
  79. for vm in self.app.domains:
  80. if isinstance(vm, qubes.vm.adminvm.AdminVM):
  81. continue
  82. if not vm.is_running():
  83. continue
  84. if not vm.features.check_with_template('qrexec', False):
  85. continue
  86. try:
  87. proc = yield from vm.run_service(
  88. 'qubes.SuspendPreAll', user='root',
  89. stdin=subprocess.DEVNULL,
  90. stdout=subprocess.DEVNULL,
  91. stderr=subprocess.DEVNULL)
  92. processes.append(proc)
  93. except qubes.exc.QubesException as e:
  94. vm.log.warning('Failed to run qubes.SuspendPreAll: %s', str(e))
  95. # FIXME: some timeout?
  96. if processes:
  97. yield from asyncio.wait([p.wait() for p in processes])
  98. coros = []
  99. # then suspend/pause VMs
  100. for vm in self.app.domains:
  101. if isinstance(vm, qubes.vm.adminvm.AdminVM):
  102. continue
  103. if vm.is_running():
  104. coros.append(vm.suspend())
  105. if coros:
  106. yield from asyncio.wait(coros)
  107. @qubes.api.method('internal.SuspendPost', no_payload=True)
  108. @asyncio.coroutine
  109. def suspend_post(self):
  110. '''
  111. Method called after host system wake up from sleep.
  112. :return:
  113. '''
  114. coros = []
  115. # first resume/unpause VMs
  116. for vm in self.app.domains:
  117. if isinstance(vm, qubes.vm.adminvm.AdminVM):
  118. continue
  119. if vm.get_power_state() in ["Paused", "Suspended"]:
  120. coros.append(vm.resume())
  121. if coros:
  122. yield from asyncio.wait(coros)
  123. # then notify all VMs
  124. processes = []
  125. for vm in self.app.domains:
  126. if isinstance(vm, qubes.vm.adminvm.AdminVM):
  127. continue
  128. if not vm.is_running():
  129. continue
  130. if not vm.features.check_with_template('qrexec', False):
  131. continue
  132. try:
  133. proc = yield from vm.run_service(
  134. 'qubes.SuspendPostAll', user='root',
  135. stdin=subprocess.DEVNULL,
  136. stdout=subprocess.DEVNULL,
  137. stderr=subprocess.DEVNULL)
  138. processes.append(proc)
  139. except qubes.exc.QubesException as e:
  140. vm.log.warning('Failed to run qubes.SuspendPostAll: %s', str(e))
  141. # FIXME: some timeout?
  142. if processes:
  143. yield from asyncio.wait([p.wait() for p in processes])