gui.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/env python
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013-2016 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. # Copyright (C) 2014-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. import os
  26. import re
  27. import subprocess
  28. import qubes.config
  29. import qubes.ext
  30. # "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm"
  31. REGEX_OUTPUT = re.compile(r'''
  32. (?x) # ignore whitespace
  33. ^ # start of string
  34. (?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc
  35. (?P<connect>(dis)?connected)[ ]# dis/connected
  36. (?P<primary>(primary)?)[ ]?
  37. (( # a group
  38. (?P<width>\d+)x # either 1024x768+0+0
  39. (?P<height>\d+)[+]
  40. (?P<x>\d+)[+]
  41. (?P<y>\d+)
  42. )|[\D]) # or not a digit
  43. .* # ignore rest of line
  44. ''')
  45. def get_monitor_layout():
  46. outputs = []
  47. for line in subprocess.Popen(
  48. ['xrandr', '-q'], stdout=subprocess.PIPE).stdout:
  49. if not line.startswith("Screen") and not line.startswith(" "):
  50. output_params = REGEX_OUTPUT.match(line).groupdict()
  51. if output_params['width']:
  52. outputs.append("%s %s %s %s\n" % (
  53. output_params['width'],
  54. output_params['height'],
  55. output_params['x'],
  56. output_params['y']))
  57. return outputs
  58. class GUI(qubes.ext.Extension):
  59. @qubes.ext.handler('domain-start', 'domain-cmd-pre-run')
  60. def start_guid(self, vm, event, preparing_dvm=False, start_guid=True,
  61. extra_guid_args=None, **kwargs):
  62. '''Launch gui daemon.
  63. GUI daemon securely displays windows from domain.
  64. ''' # pylint: disable=no-self-use,unused-argument
  65. if not start_guid or preparing_dvm \
  66. or not os.path.exists('/var/run/shm.id'):
  67. return
  68. if self.is_guid_running(vm):
  69. return
  70. if not vm.features.check_with_template('gui', not vm.hvm):
  71. vm.log.debug('Not starting gui daemon, disabled by features')
  72. return
  73. if not os.getenv('DISPLAY'):
  74. vm.log.error('Not starting gui daemon, no DISPLAY set')
  75. return
  76. vm.log.info('Starting gui daemon')
  77. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  78. '-d', str(vm.xid), '-N', vm.name,
  79. '-c', vm.label.color,
  80. '-i', vm.label.icon_path,
  81. '-l', str(vm.label.index)]
  82. if extra_guid_args is not None:
  83. guid_cmd += extra_guid_args
  84. if vm.debug:
  85. guid_cmd += ['-v', '-v']
  86. # elif not verbose:
  87. else:
  88. guid_cmd += ['-q']
  89. if vm.hvm:
  90. guid_cmd += ['-Q', '-n']
  91. stubdom_guid_pidfile = '/var/run/qubes/guid-running.{}'.format(
  92. self.get_stubdom_xid(vm))
  93. if not vm.debug and os.path.exists(stubdom_guid_pidfile):
  94. # Terminate stubdom guid once "real" gui agent connects
  95. stubdom_guid_pid = \
  96. open(stubdom_guid_pidfile, 'r').read().strip()
  97. guid_cmd += ['-K', stubdom_guid_pid]
  98. try:
  99. subprocess.check_call(guid_cmd)
  100. except subprocess.CalledProcessError:
  101. raise qubes.exc.QubesVMError(vm,
  102. 'Cannot start qubes-guid for domain {!r}'.format(vm.name))
  103. vm.fire_event('monitor-layout-change')
  104. vm.wait_for_session()
  105. @staticmethod
  106. def get_stubdom_xid(vm):
  107. if vm.xid < 0:
  108. return -1
  109. if vm.app.vmm.xs is None:
  110. return -1
  111. stubdom_xid_str = vm.app.vmm.xs.read('',
  112. '/local/domain/{}/image/device-model-domid'.format(vm.xid))
  113. if stubdom_xid_str is None or not stubdom_xid_str.isdigit():
  114. return -1
  115. return int(stubdom_xid_str)
  116. @staticmethod
  117. def send_gui_mode(vm):
  118. vm.run_service('qubes.SetGuiMode',
  119. input=('SEAMLESS'
  120. if vm.features.get('gui-seamless', False)
  121. else 'FULLSCREEN'))
  122. @qubes.ext.handler('domain-spawn')
  123. def on_domain_spawn(self, vm, event, start_guid=True, **kwargs):
  124. # pylint: disable=unused-argument
  125. if not start_guid:
  126. return
  127. if not vm.hvm:
  128. return
  129. if not os.getenv('DISPLAY'):
  130. vm.log.error('Not starting gui daemon, no DISPLAY set')
  131. return
  132. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  133. '-d', str(self.get_stubdom_xid(vm)),
  134. '-t', str(vm.xid),
  135. '-N', vm.name,
  136. '-c', vm.label.color,
  137. '-i', vm.label.icon_path,
  138. '-l', str(vm.label.index),
  139. ]
  140. if vm.debug:
  141. guid_cmd += ['-v', '-v']
  142. else:
  143. guid_cmd += ['-q']
  144. try:
  145. subprocess.check_call(guid_cmd)
  146. except subprocess.CalledProcessError:
  147. raise qubes.exc.QubesVMError(vm, 'Cannot start gui daemon')
  148. @qubes.ext.handler('monitor-layout-change')
  149. def on_monitor_layout_change(self, vm, event, monitor_layout=None):
  150. # pylint: disable=no-self-use,unused-argument
  151. if vm.features.check_with_template('no-monitor-layout', False) \
  152. or not vm.is_running():
  153. return
  154. if monitor_layout is None:
  155. monitor_layout = get_monitor_layout()
  156. if not monitor_layout:
  157. return
  158. pipe = vm.run('QUBESRPC qubes.SetMonitorLayout dom0',
  159. passio_popen=True, wait=True)
  160. pipe.stdin.write(''.join(monitor_layout))
  161. pipe.stdin.close()
  162. pipe.wait()
  163. @staticmethod
  164. def is_guid_running(vm):
  165. '''Check whether gui daemon for this domain is available.
  166. :returns: :py:obj:`True` if guid is running, \
  167. :py:obj:`False` otherwise.
  168. :rtype: bool
  169. '''
  170. xid = vm.xid
  171. if xid < 0:
  172. return False
  173. if not os.path.exists('/var/run/qubes/guid-running.{}'.format(xid)):
  174. return False
  175. return True
  176. @qubes.ext.handler('domain-is-fully-usable')
  177. def on_domain_is_fully_usable(self, vm, event):
  178. # pylint: disable=unused-argument
  179. if not self.is_guid_running(vm):
  180. yield False