gui.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 subprocess
  27. import qubes.config
  28. import qubes.ext
  29. class GUI(qubes.ext.Extension):
  30. @qubes.ext.handler('domain-start', 'domain-cmd-pre-run')
  31. def start_guid(self, vm, event, preparing_dvm=False, start_guid=True,
  32. extra_guid_args=None, **kwargs):
  33. '''Launch gui daemon.
  34. GUI daemon securely displays windows from domain.
  35. ''' # pylint: disable=no-self-use,unused-argument
  36. if not start_guid or preparing_dvm \
  37. or not os.path.exists('/var/run/shm.id'):
  38. return
  39. if not vm.features.check_with_template('gui', not vm.hvm):
  40. vm.log.debug('Not starting gui daemon, disabled by features')
  41. return
  42. if not os.getenv('DISPLAY'):
  43. vm.log.error('Not starting gui daemon, no DISPLAY set')
  44. return
  45. vm.log.info('Starting gui daemon')
  46. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  47. '-d', str(vm.xid), '-N', vm.name,
  48. '-c', vm.label.color,
  49. '-i', vm.label.icon_path,
  50. '-l', str(vm.label.index)]
  51. if extra_guid_args is not None:
  52. guid_cmd += extra_guid_args
  53. if vm.debug:
  54. guid_cmd += ['-v', '-v']
  55. # elif not verbose:
  56. else:
  57. guid_cmd += ['-q']
  58. if vm.hvm:
  59. guid_cmd += ['-Q', '-n']
  60. stubdom_guid_pidfile = \
  61. '/var/run/qubes/guid-running.{}'.format(self.get_stubdom_xid(vm))
  62. if not vm.debug and os.path.exists(stubdom_guid_pidfile):
  63. # Terminate stubdom guid once "real" gui agent connects
  64. stubdom_guid_pid = open(stubdom_guid_pidfile, 'r').read().strip()
  65. guid_cmd += ['-K', stubdom_guid_pid]
  66. try:
  67. subprocess.check_call(guid_cmd)
  68. except subprocess.CalledProcessError:
  69. raise qubes.exc.QubesVMError(vm,
  70. 'Cannot start qubes-guid for domain {!r}'.format(vm.name))
  71. vm.notify_monitor_layout()
  72. vm.wait_for_session()
  73. @staticmethod
  74. def get_stubdom_xid(vm):
  75. if vm.xid < 0:
  76. return -1
  77. if vm.app.vmm.xs is None:
  78. return -1
  79. stubdom_xid_str = vm.app.vmm.xs.read('',
  80. '/local/domain/{}/image/device-model-domid'.format(vm.xid))
  81. if stubdom_xid_str is None or not stubdom_xid_str.isdigit():
  82. return -1
  83. return int(stubdom_xid_str)
  84. @staticmethod
  85. def send_gui_mode(vm):
  86. vm.run_service('qubes.SetGuiMode',
  87. input=('SEAMLESS'
  88. if vm.features.get('gui-seamless', False)
  89. else 'FULLSCREEN'))
  90. @qubes.ext.handler('domain-spawn')
  91. def on_domain_spawn(self, vm, event, start_guid=True, **kwargs):
  92. if not start_guid:
  93. return
  94. if not vm.hvm:
  95. return
  96. if not os.getenv('DISPLAY'):
  97. vm.log.error('Not starting gui daemon, no DISPLAY set')
  98. return
  99. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  100. '-d', str(self.get_stubdom_xid(vm)),
  101. '-t', str(vm.xid),
  102. '-N', vm.name,
  103. '-c', vm.label.color,
  104. '-i', vm.label.icon_path,
  105. '-l', str(vm.label.index),
  106. ]
  107. if vm.debug:
  108. guid_cmd += ['-v', '-v']
  109. else:
  110. guid_cmd += ['-q']
  111. try:
  112. subprocess.check_call(guid_cmd)
  113. except subprocess.CalledProcesException:
  114. raise qubes.exc.QubesVMError(vm, 'Cannot start gui daemon')
  115. @qubes.ext.handler('monitor-layout-change')
  116. def on_monitor_layout_change(self, vm, event, monitor_layout):
  117. # pylint: disable=no-self-use
  118. if vm.features.check_with_template('no-monitor-layout', False) \
  119. or not vm.is_running():
  120. return
  121. pipe = vm.run('QUBESRPC qubes.SetMonitorLayout dom0',
  122. passio_popen=True, wait=True)
  123. pipe.stdin.write(''.join(monitor_layout))
  124. pipe.stdin.close()
  125. pipe.wait()