internal.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 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. ''' 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. assert self.dest.name == 'dom0'
  36. assert not self.arg
  37. system_info = {'domains': {
  38. domain.name: {
  39. 'tags': list(domain.tags),
  40. 'type': domain.__class__.__name__,
  41. 'dispvm_allowed': getattr(domain, 'dispvm_allowed', False),
  42. 'default_dispvm': (str(domain.default_dispvm) if
  43. getattr(domain, 'default_dispvm', None) else None),
  44. 'icon': str(domain.label.icon),
  45. } for domain in self.app.domains
  46. }}
  47. return json.dumps(system_info)
  48. @qubes.api.method('internal.vm.Start', no_payload=True)
  49. @asyncio.coroutine
  50. def start(self):
  51. assert not self.arg
  52. if self.dest.name == 'dom0':
  53. return
  54. yield from self.dest.start()
  55. @qubes.api.method('internal.vm.Create.DispVM', no_payload=True)
  56. @asyncio.coroutine
  57. def create_dispvm(self):
  58. assert not self.arg
  59. # TODO convert to coroutine
  60. dispvm = qubes.vm.dispvm.DispVM.from_appvm(self.dest)
  61. return dispvm.name
  62. @qubes.api.method('internal.vm.CleanupDispVM', no_payload=True)
  63. @asyncio.coroutine
  64. def cleanup_dispvm(self):
  65. assert not self.arg
  66. # TODO convert to coroutine
  67. self.dest.cleanup()
  68. @qubes.api.method('internal.vm.volume.ImportEnd')
  69. @asyncio.coroutine
  70. def vm_volume_import_end(self, untrusted_payload):
  71. '''
  72. This is second half of admin.vm.volume.Import handling. It is called
  73. when actual import is finished. Response from this method is sent do
  74. the client (as a response for admin.vm.volume.Import call).
  75. '''
  76. assert self.arg in self.dest.volumes.keys()
  77. success = untrusted_payload == b'ok'
  78. try:
  79. self.dest.storage.import_data_end(self.arg, success=success)
  80. except:
  81. self.dest.fire_event('domain-volume-import-end', volume=self.arg,
  82. succeess=False)
  83. raise
  84. self.dest.fire_event('domain-volume-import-end', volume=self.arg,
  85. succeess=success)
  86. if not success:
  87. raise qubes.exc.QubesException('Data import failed')
  88. @qubes.api.method('internal.SuspendPre', no_payload=True)
  89. @asyncio.coroutine
  90. def suspend_pre(self):
  91. '''
  92. Method called before host system goes to sleep.
  93. :return:
  94. '''
  95. # first notify all VMs
  96. processes = []
  97. for vm in self.app.domains:
  98. if isinstance(vm, qubes.vm.adminvm.AdminVM):
  99. continue
  100. if vm.is_running():
  101. proc = yield from vm.run_service(
  102. 'qubes.SuspendPreAll', user='root',
  103. stdin=subprocess.DEVNULL,
  104. stdout=subprocess.DEVNULL,
  105. stderr=subprocess.DEVNULL)
  106. processes.append(proc)
  107. # FIXME: some timeout?
  108. if processes:
  109. yield from asyncio.wait([p.wait() for p in processes])
  110. coros = []
  111. # then suspend/pause VMs
  112. for vm in self.app.domains:
  113. if isinstance(vm, qubes.vm.adminvm.AdminVM):
  114. continue
  115. if vm.is_running():
  116. coros.append(vm.suspend())
  117. if coros:
  118. yield from asyncio.wait(coros)
  119. @qubes.api.method('internal.SuspendPost', no_payload=True)
  120. @asyncio.coroutine
  121. def suspend_post(self):
  122. '''
  123. Method called after host system wake up from sleep.
  124. :return:
  125. '''
  126. coros = []
  127. # first resume/unpause VMs
  128. for vm in self.app.domains:
  129. if isinstance(vm, qubes.vm.adminvm.AdminVM):
  130. continue
  131. if vm.get_power_state() in ["Paused", "Suspended"]:
  132. coros.append(vm.resume())
  133. if coros:
  134. yield from asyncio.wait(coros)
  135. # then notify all VMs
  136. processes = []
  137. for vm in self.app.domains:
  138. if isinstance(vm, qubes.vm.adminvm.AdminVM):
  139. continue
  140. if vm.is_running():
  141. proc = yield from vm.run_service(
  142. 'qubes.SuspendPostAll', user='root',
  143. stdin=subprocess.DEVNULL,
  144. stdout=subprocess.DEVNULL,
  145. stderr=subprocess.DEVNULL)
  146. processes.append(proc)
  147. # FIXME: some timeout?
  148. if processes:
  149. yield from asyncio.wait([p.wait() for p in processes])