qvm_start_daemon.py 20 KB

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