qvm_start_gui.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. ''' GUI daemon launcher tool'''
  21. import os
  22. import signal
  23. import subprocess
  24. import asyncio
  25. import re
  26. import xcffib
  27. import xcffib.xproto # pylint: disable=unused-import
  28. import daemon.pidfile
  29. import qubesadmin
  30. import qubesadmin.tools
  31. import qubesadmin.vm
  32. have_events = False
  33. try:
  34. # pylint: disable=wrong-import-position
  35. import qubesadmin.events
  36. have_events = True
  37. except ImportError:
  38. pass
  39. GUI_DAEMON_PATH = '/usr/bin/qubes-guid'
  40. QUBES_ICON_DIR = '/usr/share/icons/hicolor/128x128/devices'
  41. # "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm"
  42. REGEX_OUTPUT = re.compile(r'''
  43. (?x) # ignore whitespace
  44. ^ # start of string
  45. (?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc
  46. (?P<connect>(dis)?connected) # dis/connected
  47. ([ ]
  48. (?P<primary>(primary)?)[ ]?
  49. (( # a group
  50. (?P<width>\d+)x # either 1024x768+0+0
  51. (?P<height>\d+)[+]
  52. (?P<x>\d+)[+]
  53. (?P<y>\d+)
  54. )|[\D]) # or not a digit
  55. ([ ]\(.*\))?[ ]? # ignore options
  56. ( # 304mm x 228mm
  57. (?P<width_mm>\d+)mm[ ]x[ ]
  58. (?P<height_mm>\d+)mm
  59. )?
  60. .* # ignore rest of line
  61. )? # everything after (dis)connect is optional
  62. ''')
  63. def get_monitor_layout():
  64. '''Get list of monitors and their size/position'''
  65. outputs = []
  66. for line in subprocess.Popen(
  67. ['xrandr', '-q'], stdout=subprocess.PIPE).stdout:
  68. line = line.decode()
  69. if not line.startswith("Screen") and not line.startswith(" "):
  70. output_params = REGEX_OUTPUT.match(line).groupdict()
  71. if output_params['width']:
  72. phys_size = ""
  73. if output_params['width_mm'] and int(output_params['width_mm']):
  74. # don't provide real values for privacy reasons - see
  75. # #1951 for details
  76. dpi = (int(output_params['width']) * 254 //
  77. int(output_params['width_mm']) // 10)
  78. if dpi > 300:
  79. dpi = 300
  80. elif dpi > 200:
  81. dpi = 200
  82. elif dpi > 150:
  83. dpi = 150
  84. else:
  85. # if lower, don't provide this info to the VM at all
  86. dpi = 0
  87. if dpi:
  88. # now calculate dimensions based on approximate DPI
  89. phys_size = " {} {}".format(
  90. int(output_params['width']) * 254 // dpi // 10,
  91. int(output_params['height']) * 254 // dpi // 10,
  92. )
  93. outputs.append("%s %s %s %s%s\n" % (
  94. output_params['width'],
  95. output_params['height'],
  96. output_params['x'],
  97. output_params['y'],
  98. phys_size,
  99. ))
  100. return outputs
  101. class GUILauncher(object):
  102. '''Launch GUI daemon for VMs'''
  103. def __init__(self, app: qubesadmin.app.QubesBase):
  104. ''' Initialize GUILauncher.
  105. :param app: :py:class:`qubesadmin.Qubes` instance
  106. '''
  107. self.app = app
  108. self.started_processes = {}
  109. @staticmethod
  110. def kde_guid_args(vm):
  111. '''Return KDE-specific arguments for gui-daemon, if applicable'''
  112. guid_cmd = []
  113. # Avoid using environment variables for checking the current session,
  114. # because this script may be called with cleared env (like with sudo).
  115. if subprocess.check_output(
  116. ['xprop', '-root', '-notype', 'KWIN_RUNNING']) == \
  117. b'KWIN_RUNNING = 0x1\n':
  118. # native decoration plugins is used, so adjust window properties
  119. # accordingly
  120. guid_cmd += ['-T'] # prefix window titles with VM name
  121. # get owner of X11 session
  122. session_owner = None
  123. for line in subprocess.check_output(['xhost']).splitlines():
  124. if line == b'SI:localuser:root':
  125. pass
  126. elif line.startswith(b'SI:localuser:'):
  127. session_owner = line.split(b':')[2].decode()
  128. if session_owner is not None:
  129. data_dir = os.path.expanduser(
  130. '~{}/.local/share'.format(session_owner))
  131. else:
  132. # fallback to current user
  133. data_dir = os.path.expanduser('~/.local/share')
  134. guid_cmd += ['-p',
  135. '_KDE_NET_WM_COLOR_SCHEME=s:{}'.format(
  136. os.path.join(data_dir,
  137. 'qubes-kde', vm.label.name + '.colors'))]
  138. return guid_cmd
  139. def common_guid_args(self, vm):
  140. '''Common qubes-guid arguments for PV(H), HVM and Stubdomain'''
  141. guid_cmd = [GUI_DAEMON_PATH,
  142. '-N', vm.name,
  143. '-c', vm.label.color,
  144. '-i', os.path.join(QUBES_ICON_DIR, vm.label.icon) + '.png',
  145. '-l', str(vm.label.index)]
  146. if vm.debug:
  147. guid_cmd += ['-v', '-v']
  148. # elif not verbose:
  149. else:
  150. guid_cmd += ['-q']
  151. guid_cmd += self.kde_guid_args(vm)
  152. return guid_cmd
  153. @staticmethod
  154. def guid_pidfile(xid):
  155. '''Helper function to construct a pidfile path'''
  156. return '/var/run/qubes/guid-running.{}'.format(xid)
  157. @asyncio.coroutine
  158. def start_gui_for_vm(self, vm, monitor_layout=None):
  159. '''Start GUI daemon (qubes-guid) connected directly to a VM
  160. This function is a coroutine.
  161. :param vm: VM for which start GUI daemon
  162. :param monitor_layout: monitor layout to send; if None, fetch it from
  163. local X server.
  164. '''
  165. guid_cmd = self.common_guid_args(vm)
  166. guid_cmd.extend(['-d', str(vm.xid)])
  167. if vm.virt_mode == 'hvm':
  168. guid_cmd.extend(['-n'])
  169. if vm.features.check_with_template('rpc-clipboard', False):
  170. guid_cmd.extend(['-Q'])
  171. stubdom_guid_pidfile = self.guid_pidfile(vm.stubdom_xid)
  172. if not vm.debug and os.path.exists(stubdom_guid_pidfile):
  173. # Terminate stubdom guid once "real" gui agent connects
  174. with open(stubdom_guid_pidfile, 'r') as pidfile:
  175. stubdom_guid_pid = pidfile.read().strip()
  176. guid_cmd += ['-K', stubdom_guid_pid]
  177. vm.log.info('Starting GUI')
  178. yield from asyncio.create_subprocess_exec(*guid_cmd)
  179. yield from self.send_monitor_layout(vm, layout=monitor_layout,
  180. startup=True)
  181. @asyncio.coroutine
  182. def start_gui_for_stubdomain(self, vm, force=False):
  183. '''Start GUI daemon (qubes-guid) connected to a stubdomain
  184. This function is a coroutine.
  185. '''
  186. want_stubdom = force
  187. # if no 'gui' feature set at all, assume no gui agent installed
  188. if not want_stubdom and \
  189. vm.features.check_with_template('gui', None) is None:
  190. want_stubdom = True
  191. if not want_stubdom and vm.debug:
  192. want_stubdom = True
  193. if not want_stubdom:
  194. return
  195. if os.path.exists(self.guid_pidfile(vm.stubdom_xid)):
  196. return
  197. vm.log.info('Starting GUI (stubdomain)')
  198. guid_cmd = self.common_guid_args(vm)
  199. guid_cmd.extend(['-d', str(vm.stubdom_xid), '-t', str(vm.xid)])
  200. yield from asyncio.create_subprocess_exec(*guid_cmd)
  201. @asyncio.coroutine
  202. def start_gui(self, vm, force_stubdom=False, monitor_layout=None):
  203. '''Start GUI daemon regardless of start event.
  204. This function is a coroutine.
  205. :param vm: VM for which GUI daemon should be started
  206. :param force_stubdom: Force GUI daemon for stubdomain, even if the
  207. one for target AppVM is running.
  208. '''
  209. if not vm.features.check_with_template('gui', True):
  210. return
  211. if vm.virt_mode == 'hvm':
  212. yield from self.start_gui_for_stubdomain(vm,
  213. force=force_stubdom)
  214. if not os.path.exists(self.guid_pidfile(vm.xid)):
  215. yield from self.start_gui_for_vm(vm, monitor_layout=monitor_layout)
  216. @asyncio.coroutine
  217. def send_monitor_layout(self, vm, layout=None, startup=False):
  218. '''Send monitor layout to a given VM
  219. This function is a coroutine.
  220. :param vm: VM to which send monitor layout
  221. :param layout: monitor layout to send; if None, fetch it from
  222. local X server.
  223. :param startup:
  224. :return: None
  225. '''
  226. # pylint: disable=no-self-use
  227. if vm.features.check_with_template('no-monitor-layout', False) \
  228. or not vm.is_running():
  229. return
  230. if layout is None:
  231. layout = get_monitor_layout()
  232. if not layout:
  233. return
  234. vm.log.info('Sending monitor layout')
  235. if not startup:
  236. with open(self.guid_pidfile(vm.xid)) as pidfile:
  237. pid = int(pidfile.read())
  238. os.kill(pid, signal.SIGHUP)
  239. try:
  240. with open(self.guid_pidfile(vm.stubdom_xid)) as pidfile:
  241. pid = int(pidfile.read())
  242. os.kill(pid, signal.SIGHUP)
  243. except FileNotFoundError:
  244. pass
  245. try:
  246. yield from asyncio.get_event_loop().run_in_executor(None,
  247. vm.run_service_for_stdio, 'qubes.SetMonitorLayout',
  248. ''.join(layout).encode())
  249. except subprocess.CalledProcessError as e:
  250. vm.log.warning('Failed to send monitor layout: %s', e.stderr)
  251. def send_monitor_layout_all(self):
  252. '''Send monitor layout to all (running) VMs'''
  253. monitor_layout = get_monitor_layout()
  254. for vm in self.app.domains:
  255. if vm.klass == 'AdminVM':
  256. continue
  257. if vm.is_running():
  258. if not vm.features.check_with_template('gui', True):
  259. continue
  260. asyncio.ensure_future(self.send_monitor_layout(vm,
  261. monitor_layout))
  262. def on_domain_spawn(self, vm, _event, **kwargs):
  263. '''Handler of 'domain-spawn' event, starts GUI daemon for stubdomain'''
  264. if not vm.features.check_with_template('gui', True):
  265. return
  266. if vm.virt_mode == 'hvm' and kwargs.get('start_guid', 'True') == 'True':
  267. asyncio.ensure_future(self.start_gui_for_stubdomain(vm))
  268. def on_domain_start(self, vm, _event, **kwargs):
  269. '''Handler of 'domain-start' event, starts GUI daemon for actual VM'''
  270. if not vm.features.check_with_template('gui', True):
  271. return
  272. if kwargs.get('start_guid', 'True') == 'True':
  273. asyncio.ensure_future(self.start_gui_for_vm(vm))
  274. def on_connection_established(self, _subject, _event, **_kwargs):
  275. '''Handler of 'connection-established' event, used to launch GUI
  276. daemon for domains started before this tool. '''
  277. monitor_layout = get_monitor_layout()
  278. self.app.domains.clear_cache()
  279. for vm in self.app.domains:
  280. if vm.klass == 'AdminVM':
  281. continue
  282. if not vm.features.check_with_template('gui', True):
  283. continue
  284. power_state = vm.get_power_state()
  285. if power_state == 'Running':
  286. asyncio.ensure_future(self.start_gui(vm,
  287. monitor_layout=monitor_layout))
  288. elif power_state == 'Transient':
  289. # it is still starting, we'll get 'domain-start' event when
  290. # fully started
  291. if vm.virt_mode == 'hvm':
  292. asyncio.ensure_future(self.start_gui_for_stubdomain(vm))
  293. def register_events(self, events):
  294. '''Register domain startup events in app.events dispatcher'''
  295. events.add_handler('domain-spawn', self.on_domain_spawn)
  296. events.add_handler('domain-start', self.on_domain_start)
  297. events.add_handler('connection-established',
  298. self.on_connection_established)
  299. def x_reader(conn, callback):
  300. '''Try reading something from X connection to check if it's still alive.
  301. In case it isn't, call *callback*.
  302. '''
  303. try:
  304. conn.poll_for_event()
  305. except xcffib.ConnectionException:
  306. callback()
  307. if 'XDG_RUNTIME_DIR' in os.environ:
  308. pidfile_path = os.path.join(os.environ['XDG_RUNTIME_DIR'],
  309. 'qvm-start-gui.pid')
  310. else:
  311. pidfile_path = os.path.join(os.environ.get('HOME', '/'),
  312. '.qvm-start-gui.pid')
  313. parser = qubesadmin.tools.QubesArgumentParser(
  314. description='start GUI for qube(s)', vmname_nargs='*')
  315. parser.add_argument('--watch', action='store_true',
  316. help='Keep watching for further domains startups, must be used with --all')
  317. parser.add_argument('--force-stubdomain', action='store_true',
  318. help='Start GUI to stubdomain-emulated VGA, even if gui-agent is running '
  319. 'in the VM')
  320. parser.add_argument('--pidfile', action='store', default=pidfile_path,
  321. help='Pidfile path to create in --watch mode')
  322. parser.add_argument('--notify-monitor-layout', action='store_true',
  323. help='Notify running instance in --watch mode about changed monitor layout')
  324. def main(args=None):
  325. ''' Main function of qvm-start-gui tool'''
  326. args = parser.parse_args(args)
  327. if args.watch and not args.all_domains:
  328. parser.error('--watch option must be used with --all')
  329. if args.watch and args.notify_monitor_layout:
  330. parser.error('--watch cannot be used with --notify-monitor-layout')
  331. launcher = GUILauncher(args.app)
  332. if args.watch:
  333. if not have_events:
  334. parser.error('--watch option require Python >= 3.5')
  335. with daemon.pidfile.TimeoutPIDLockFile(args.pidfile):
  336. loop = asyncio.get_event_loop()
  337. # pylint: disable=no-member
  338. events = qubesadmin.events.EventsDispatcher(args.app)
  339. # pylint: enable=no-member
  340. launcher.register_events(events)
  341. events_listener = asyncio.ensure_future(events.listen_for_events())
  342. for signame in ('SIGINT', 'SIGTERM'):
  343. loop.add_signal_handler(getattr(signal, signame),
  344. events_listener.cancel) # pylint: disable=no-member
  345. loop.add_signal_handler(signal.SIGHUP,
  346. launcher.send_monitor_layout_all)
  347. conn = xcffib.connect()
  348. x_fd = conn.get_file_descriptor()
  349. loop.add_reader(x_fd, x_reader, conn, events_listener.cancel)
  350. try:
  351. loop.run_until_complete(events_listener)
  352. except asyncio.CancelledError:
  353. pass
  354. loop.remove_reader(x_fd)
  355. loop.stop()
  356. loop.run_forever()
  357. loop.close()
  358. elif args.notify_monitor_layout:
  359. try:
  360. with open(pidfile_path, 'r') as pidfile:
  361. pid = int(pidfile.read().strip())
  362. os.kill(pid, signal.SIGHUP)
  363. except (FileNotFoundError, ValueError) as e:
  364. parser.error('Cannot open pidfile {}: {}'.format(pidfile_path,
  365. str(e)))
  366. else:
  367. loop = asyncio.get_event_loop()
  368. tasks = []
  369. for vm in args.domains:
  370. if vm.is_running():
  371. tasks.append(asyncio.ensure_future(launcher.start_gui(
  372. vm, force_stubdom=args.force_stubdomain)))
  373. if tasks:
  374. loop.run_until_complete(asyncio.wait(tasks))
  375. loop.stop()
  376. loop.run_forever()
  377. loop.close()
  378. if __name__ == '__main__':
  379. main()