internal.py 5.7 KB

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