internal.py 5.0 KB

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