2017-04-14 13:28:45 +02:00
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-10-20 13:24:56 +02:00
|
|
|
""" GUI daemon launcher tool"""
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import subprocess
|
|
|
|
import asyncio
|
2017-04-15 20:15:06 +02:00
|
|
|
import re
|
2019-09-21 04:51:43 +02:00
|
|
|
|
|
|
|
import functools
|
2018-02-26 22:11:25 +01:00
|
|
|
import xcffib
|
|
|
|
import xcffib.xproto # pylint: disable=unused-import
|
2017-04-15 20:15:06 +02:00
|
|
|
|
2017-04-21 15:05:20 +02:00
|
|
|
import daemon.pidfile
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin
|
2018-07-14 01:49:55 +02:00
|
|
|
import qubesadmin.exc
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.tools
|
|
|
|
import qubesadmin.vm
|
2019-10-20 13:24:56 +02:00
|
|
|
|
2017-05-11 23:40:49 +02:00
|
|
|
have_events = False
|
|
|
|
try:
|
2017-05-17 13:20:59 +02:00
|
|
|
# pylint: disable=wrong-import-position
|
2017-05-11 23:40:49 +02:00
|
|
|
import qubesadmin.events
|
2019-10-20 13:24:56 +02:00
|
|
|
|
2017-05-11 23:40:49 +02:00
|
|
|
have_events = True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
GUI_DAEMON_PATH = '/usr/bin/qubes-guid'
|
|
|
|
QUBES_ICON_DIR = '/usr/share/icons/hicolor/128x128/devices'
|
|
|
|
|
2017-04-15 20:15:06 +02:00
|
|
|
# "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm"
|
2019-10-20 13:24:56 +02:00
|
|
|
REGEX_OUTPUT = re.compile(r"""
|
2017-04-15 20:15:06 +02:00
|
|
|
(?x) # ignore whitespace
|
|
|
|
^ # start of string
|
|
|
|
(?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc
|
|
|
|
(?P<connect>(dis)?connected) # dis/connected
|
|
|
|
([ ]
|
|
|
|
(?P<primary>(primary)?)[ ]?
|
|
|
|
(( # a group
|
|
|
|
(?P<width>\d+)x # either 1024x768+0+0
|
|
|
|
(?P<height>\d+)[+]
|
|
|
|
(?P<x>\d+)[+]
|
|
|
|
(?P<y>\d+)
|
|
|
|
)|[\D]) # or not a digit
|
|
|
|
([ ]\(.*\))?[ ]? # ignore options
|
|
|
|
( # 304mm x 228mm
|
|
|
|
(?P<width_mm>\d+)mm[ ]x[ ]
|
|
|
|
(?P<height_mm>\d+)mm
|
|
|
|
)?
|
|
|
|
.* # ignore rest of line
|
|
|
|
)? # everything after (dis)connect is optional
|
2019-10-20 13:24:56 +02:00
|
|
|
""")
|
2017-04-15 20:15:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_monitor_layout():
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Get list of monitors and their size/position"""
|
2017-04-15 20:15:06 +02:00
|
|
|
outputs = []
|
|
|
|
|
|
|
|
for line in subprocess.Popen(
|
|
|
|
['xrandr', '-q'], stdout=subprocess.PIPE).stdout:
|
|
|
|
line = line.decode()
|
|
|
|
if not line.startswith("Screen") and not line.startswith(" "):
|
|
|
|
output_params = REGEX_OUTPUT.match(line).groupdict()
|
|
|
|
if output_params['width']:
|
|
|
|
phys_size = ""
|
|
|
|
if output_params['width_mm'] and int(output_params['width_mm']):
|
|
|
|
# don't provide real values for privacy reasons - see
|
|
|
|
# #1951 for details
|
|
|
|
dpi = (int(output_params['width']) * 254 //
|
2019-10-20 13:24:56 +02:00
|
|
|
int(output_params['width_mm']) // 10)
|
2017-04-15 20:15:06 +02:00
|
|
|
if dpi > 300:
|
|
|
|
dpi = 300
|
|
|
|
elif dpi > 200:
|
|
|
|
dpi = 200
|
|
|
|
elif dpi > 150:
|
|
|
|
dpi = 150
|
|
|
|
else:
|
|
|
|
# if lower, don't provide this info to the VM at all
|
|
|
|
dpi = 0
|
|
|
|
if dpi:
|
|
|
|
# now calculate dimensions based on approximate DPI
|
|
|
|
phys_size = " {} {}".format(
|
|
|
|
int(output_params['width']) * 254 // dpi // 10,
|
|
|
|
int(output_params['height']) * 254 // dpi // 10,
|
|
|
|
)
|
|
|
|
outputs.append("%s %s %s %s%s\n" % (
|
2019-10-20 13:24:56 +02:00
|
|
|
output_params['width'],
|
|
|
|
output_params['height'],
|
|
|
|
output_params['x'],
|
|
|
|
output_params['y'],
|
|
|
|
phys_size,
|
2017-04-15 20:15:06 +02:00
|
|
|
))
|
|
|
|
return outputs
|
|
|
|
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
class GUILauncher(object):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Launch GUI daemon for VMs"""
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
def __init__(self, app: qubesadmin.app.QubesBase):
|
2019-10-20 13:24:56 +02:00
|
|
|
""" Initialize GUILauncher.
|
2017-04-14 13:28:45 +02:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
:param app: :py:class:`qubesadmin.Qubes` instance
|
2019-10-20 13:24:56 +02:00
|
|
|
"""
|
2017-04-14 13:28:45 +02:00
|
|
|
self.app = app
|
|
|
|
self.started_processes = {}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def kde_guid_args(vm):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Return KDE-specific arguments for gui-daemon, if applicable"""
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
guid_cmd = []
|
|
|
|
# Avoid using environment variables for checking the current session,
|
|
|
|
# because this script may be called with cleared env (like with sudo).
|
|
|
|
if subprocess.check_output(
|
|
|
|
['xprop', '-root', '-notype', 'KWIN_RUNNING']) == \
|
|
|
|
b'KWIN_RUNNING = 0x1\n':
|
|
|
|
# native decoration plugins is used, so adjust window properties
|
|
|
|
# accordingly
|
|
|
|
guid_cmd += ['-T'] # prefix window titles with VM name
|
|
|
|
# get owner of X11 session
|
|
|
|
session_owner = None
|
|
|
|
for line in subprocess.check_output(['xhost']).splitlines():
|
|
|
|
if line == b'SI:localuser:root':
|
|
|
|
pass
|
|
|
|
elif line.startswith(b'SI:localuser:'):
|
|
|
|
session_owner = line.split(b':')[2].decode()
|
|
|
|
if session_owner is not None:
|
|
|
|
data_dir = os.path.expanduser(
|
|
|
|
'~{}/.local/share'.format(session_owner))
|
|
|
|
else:
|
|
|
|
# fallback to current user
|
|
|
|
data_dir = os.path.expanduser('~/.local/share')
|
|
|
|
|
|
|
|
guid_cmd += ['-p',
|
2019-10-20 13:24:56 +02:00
|
|
|
'_KDE_NET_WM_COLOR_SCHEME=s:{}'.format(
|
|
|
|
os.path.join(data_dir,
|
|
|
|
'qubes-kde',
|
|
|
|
vm.label.name + '.colors'))]
|
2017-04-14 13:28:45 +02:00
|
|
|
return guid_cmd
|
|
|
|
|
|
|
|
def common_guid_args(self, vm):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Common qubes-guid arguments for PV(H), HVM and Stubdomain"""
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
guid_cmd = [GUI_DAEMON_PATH,
|
2019-10-20 13:24:56 +02:00
|
|
|
'-N', vm.name,
|
|
|
|
'-c', vm.label.color,
|
|
|
|
'-i', os.path.join(QUBES_ICON_DIR, vm.label.icon) + '.png',
|
|
|
|
'-l', str(vm.label.index)]
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
if vm.debug:
|
|
|
|
guid_cmd += ['-v', '-v']
|
|
|
|
# elif not verbose:
|
|
|
|
else:
|
|
|
|
guid_cmd += ['-q']
|
|
|
|
|
2018-07-09 19:46:01 +02:00
|
|
|
if vm.features.check_with_template('rpc-clipboard', False):
|
|
|
|
guid_cmd.extend(['-Q'])
|
|
|
|
|
2017-04-14 13:28:45 +02:00
|
|
|
guid_cmd += self.kde_guid_args(vm)
|
|
|
|
return guid_cmd
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def guid_pidfile(xid):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Helper function to construct a pidfile path"""
|
2017-04-14 13:28:45 +02:00
|
|
|
return '/var/run/qubes/guid-running.{}'.format(xid)
|
|
|
|
|
2017-06-21 05:06:16 +02:00
|
|
|
@asyncio.coroutine
|
|
|
|
def start_gui_for_vm(self, vm, monitor_layout=None):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Start GUI daemon (qubes-guid) connected directly to a VM
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
This function is a coroutine.
|
2017-06-21 05:06:16 +02:00
|
|
|
|
|
|
|
:param vm: VM for which start GUI daemon
|
|
|
|
:param monitor_layout: monitor layout to send; if None, fetch it from
|
2018-12-08 23:53:55 +01:00
|
|
|
local X server.
|
2019-10-20 13:24:56 +02:00
|
|
|
"""
|
2017-04-14 13:28:45 +02:00
|
|
|
guid_cmd = self.common_guid_args(vm)
|
|
|
|
guid_cmd.extend(['-d', str(vm.xid)])
|
|
|
|
|
2017-07-18 04:22:34 +02:00
|
|
|
if vm.virt_mode == 'hvm':
|
2017-04-14 13:28:45 +02:00
|
|
|
guid_cmd.extend(['-n'])
|
|
|
|
|
|
|
|
stubdom_guid_pidfile = self.guid_pidfile(vm.stubdom_xid)
|
|
|
|
if not vm.debug and os.path.exists(stubdom_guid_pidfile):
|
|
|
|
# Terminate stubdom guid once "real" gui agent connects
|
|
|
|
with open(stubdom_guid_pidfile, 'r') as pidfile:
|
|
|
|
stubdom_guid_pid = pidfile.read().strip()
|
|
|
|
guid_cmd += ['-K', stubdom_guid_pid]
|
|
|
|
|
2017-06-21 05:06:16 +02:00
|
|
|
vm.log.info('Starting GUI')
|
|
|
|
|
|
|
|
yield from asyncio.create_subprocess_exec(*guid_cmd)
|
|
|
|
|
|
|
|
yield from self.send_monitor_layout(vm, layout=monitor_layout,
|
2019-10-20 13:24:56 +02:00
|
|
|
startup=True)
|
2017-04-14 13:28:45 +02:00
|
|
|
|
2017-07-30 18:54:08 +02:00
|
|
|
@asyncio.coroutine
|
|
|
|
def start_gui_for_stubdomain(self, vm, force=False):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Start GUI daemon (qubes-guid) connected to a stubdomain
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
This function is a coroutine.
|
2019-10-20 13:24:56 +02:00
|
|
|
"""
|
2017-07-30 18:54:08 +02:00
|
|
|
want_stubdom = force
|
|
|
|
if not want_stubdom and \
|
2018-07-09 20:00:05 +02:00
|
|
|
vm.features.check_with_template('gui-emulated', False):
|
|
|
|
want_stubdom = True
|
|
|
|
# if no 'gui' or 'gui-emulated' feature set at all, use emulated GUI
|
|
|
|
if not want_stubdom and \
|
|
|
|
vm.features.check_with_template('gui', None) is None and \
|
|
|
|
vm.features.check_with_template('gui-emulated', None) is None:
|
2017-07-30 18:54:08 +02:00
|
|
|
want_stubdom = True
|
|
|
|
if not want_stubdom and vm.debug:
|
|
|
|
want_stubdom = True
|
|
|
|
if not want_stubdom:
|
|
|
|
return
|
|
|
|
if os.path.exists(self.guid_pidfile(vm.stubdom_xid)):
|
|
|
|
return
|
2017-06-21 05:06:16 +02:00
|
|
|
vm.log.info('Starting GUI (stubdomain)')
|
2017-04-14 13:28:45 +02:00
|
|
|
guid_cmd = self.common_guid_args(vm)
|
|
|
|
guid_cmd.extend(['-d', str(vm.stubdom_xid), '-t', str(vm.xid)])
|
|
|
|
|
2017-07-30 18:54:08 +02:00
|
|
|
yield from asyncio.create_subprocess_exec(*guid_cmd)
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-04-15 20:15:06 +02:00
|
|
|
def start_gui(self, vm, force_stubdom=False, monitor_layout=None):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Start GUI daemon regardless of start event.
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
This function is a coroutine.
|
|
|
|
|
|
|
|
:param vm: VM for which GUI daemon should be started
|
|
|
|
:param force_stubdom: Force GUI daemon for stubdomain, even if the
|
2018-12-08 23:53:55 +01:00
|
|
|
one for target AppVM is running.
|
2019-10-20 13:24:56 +02:00
|
|
|
:param monitor_layout: monitor layout configuration
|
|
|
|
"""
|
2019-10-20 15:53:32 +02:00
|
|
|
guivm = getattr(vm, 'guivm', None)
|
|
|
|
if guivm != vm.app.get_local_name():
|
|
|
|
vm.log.info('GUI connected to {}. Skipping.'.format(guivm))
|
2019-10-20 13:31:57 +02:00
|
|
|
return
|
|
|
|
|
2017-07-18 04:22:34 +02:00
|
|
|
if vm.virt_mode == 'hvm':
|
2019-10-20 13:24:56 +02:00
|
|
|
yield from self.start_gui_for_stubdomain(vm, force=force_stubdom)
|
2017-04-14 13:28:45 +02:00
|
|
|
|
2018-07-09 20:00:05 +02:00
|
|
|
if not vm.features.check_with_template('gui', True):
|
|
|
|
return
|
|
|
|
|
2017-04-14 13:28:45 +02:00
|
|
|
if not os.path.exists(self.guid_pidfile(vm.xid)):
|
2017-06-21 05:06:16 +02:00
|
|
|
yield from self.start_gui_for_vm(vm, monitor_layout=monitor_layout)
|
2017-04-15 20:15:06 +02:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def send_monitor_layout(self, vm, layout=None, startup=False):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Send monitor layout to a given VM
|
2017-04-15 20:15:06 +02:00
|
|
|
|
|
|
|
This function is a coroutine.
|
|
|
|
|
|
|
|
:param vm: VM to which send monitor layout
|
|
|
|
:param layout: monitor layout to send; if None, fetch it from
|
2018-12-08 23:53:55 +01:00
|
|
|
local X server.
|
2017-04-15 20:15:06 +02:00
|
|
|
:param startup:
|
|
|
|
:return: None
|
2019-10-20 13:24:56 +02:00
|
|
|
"""
|
2017-04-15 20:15:06 +02:00
|
|
|
# pylint: disable=no-self-use
|
|
|
|
if vm.features.check_with_template('no-monitor-layout', False) \
|
|
|
|
or not vm.is_running():
|
|
|
|
return
|
|
|
|
|
|
|
|
if layout is None:
|
|
|
|
layout = get_monitor_layout()
|
|
|
|
if not layout:
|
|
|
|
return
|
|
|
|
|
|
|
|
vm.log.info('Sending monitor layout')
|
|
|
|
|
|
|
|
if not startup:
|
|
|
|
with open(self.guid_pidfile(vm.xid)) as pidfile:
|
|
|
|
pid = int(pidfile.read())
|
|
|
|
os.kill(pid, signal.SIGHUP)
|
|
|
|
try:
|
|
|
|
with open(self.guid_pidfile(vm.stubdom_xid)) as pidfile:
|
|
|
|
pid = int(pidfile.read())
|
|
|
|
os.kill(pid, signal.SIGHUP)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
2017-06-21 05:06:16 +02:00
|
|
|
try:
|
2019-10-20 13:24:56 +02:00
|
|
|
yield from asyncio.get_event_loop(). \
|
|
|
|
run_in_executor(None,
|
|
|
|
functools.partial(
|
|
|
|
vm.run_service_for_stdio,
|
|
|
|
'qubes.SetMonitorLayout',
|
|
|
|
input=''.join(layout).encode(),
|
|
|
|
autostart=False))
|
2017-06-21 05:06:16 +02:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
vm.log.warning('Failed to send monitor layout: %s', e.stderr)
|
2017-04-15 20:15:06 +02:00
|
|
|
|
|
|
|
def send_monitor_layout_all(self):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Send monitor layout to all (running) VMs"""
|
2017-04-15 20:15:06 +02:00
|
|
|
monitor_layout = get_monitor_layout()
|
|
|
|
for vm in self.app.domains:
|
2019-10-20 15:11:24 +02:00
|
|
|
if getattr(vm, 'guivm', None) != vm.app.get_local_name():
|
2019-10-20 13:31:57 +02:00
|
|
|
continue
|
2017-10-02 20:43:01 +02:00
|
|
|
if vm.klass == 'AdminVM':
|
2017-04-15 20:15:06 +02:00
|
|
|
continue
|
|
|
|
if vm.is_running():
|
|
|
|
if not vm.features.check_with_template('gui', True):
|
|
|
|
continue
|
|
|
|
asyncio.ensure_future(self.send_monitor_layout(vm,
|
2019-10-20 13:24:56 +02:00
|
|
|
monitor_layout))
|
2017-04-15 20:15:06 +02:00
|
|
|
|
2017-04-14 13:28:45 +02:00
|
|
|
def on_domain_spawn(self, vm, _event, **kwargs):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Handler of 'domain-spawn' event, starts GUI daemon for stubdomain"""
|
2018-07-14 01:49:55 +02:00
|
|
|
try:
|
2019-10-20 15:11:24 +02:00
|
|
|
if getattr(vm, 'guivm', None) != vm.app.get_local_name():
|
2019-10-20 13:31:57 +02:00
|
|
|
return
|
2018-07-14 01:49:55 +02:00
|
|
|
if not vm.features.check_with_template('gui', True):
|
|
|
|
return
|
|
|
|
if vm.virt_mode == 'hvm' and \
|
|
|
|
kwargs.get('start_guid', 'True') == 'True':
|
|
|
|
asyncio.ensure_future(self.start_gui_for_stubdomain(vm))
|
|
|
|
except qubesadmin.exc.QubesException as e:
|
|
|
|
vm.log.warning('Failed to start GUI for %s: %s', vm.name, str(e))
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
def on_domain_start(self, vm, _event, **kwargs):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Handler of 'domain-start' event, starts GUI daemon for actual VM"""
|
2018-07-14 01:49:55 +02:00
|
|
|
try:
|
2019-10-20 15:11:24 +02:00
|
|
|
if getattr(vm, 'guivm', None) != vm.app.get_local_name():
|
2019-10-20 13:31:57 +02:00
|
|
|
return
|
2018-07-14 01:49:55 +02:00
|
|
|
if not vm.features.check_with_template('gui', True):
|
|
|
|
return
|
|
|
|
if kwargs.get('start_guid', 'True') == 'True':
|
|
|
|
asyncio.ensure_future(self.start_gui_for_vm(vm))
|
|
|
|
except qubesadmin.exc.QubesException as e:
|
|
|
|
vm.log.warning('Failed to start GUI for %s: %s', vm.name, str(e))
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
def on_connection_established(self, _subject, _event, **_kwargs):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Handler of 'connection-established' event, used to launch GUI
|
|
|
|
daemon for domains started before this tool. """
|
2017-04-15 20:15:06 +02:00
|
|
|
|
|
|
|
monitor_layout = get_monitor_layout()
|
2017-06-21 05:06:16 +02:00
|
|
|
self.app.domains.clear_cache()
|
2017-04-14 13:28:45 +02:00
|
|
|
for vm in self.app.domains:
|
2017-10-02 20:43:01 +02:00
|
|
|
if vm.klass == 'AdminVM':
|
2017-04-14 13:28:45 +02:00
|
|
|
continue
|
2019-10-20 15:11:24 +02:00
|
|
|
if getattr(vm, 'guivm', None) != vm.app.get_local_name():
|
2019-10-20 13:31:57 +02:00
|
|
|
continue
|
2017-06-21 05:06:16 +02:00
|
|
|
if not vm.features.check_with_template('gui', True):
|
|
|
|
continue
|
|
|
|
power_state = vm.get_power_state()
|
|
|
|
if power_state == 'Running':
|
2019-10-20 13:24:56 +02:00
|
|
|
asyncio.ensure_future(
|
|
|
|
self.start_gui(vm, monitor_layout=monitor_layout))
|
2017-06-21 05:06:16 +02:00
|
|
|
elif power_state == 'Transient':
|
|
|
|
# it is still starting, we'll get 'domain-start' event when
|
|
|
|
# fully started
|
2017-07-18 04:22:34 +02:00
|
|
|
if vm.virt_mode == 'hvm':
|
2017-06-21 05:06:16 +02:00
|
|
|
asyncio.ensure_future(self.start_gui_for_stubdomain(vm))
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
def register_events(self, events):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Register domain startup events in app.events dispatcher"""
|
2017-04-14 13:28:45 +02:00
|
|
|
events.add_handler('domain-spawn', self.on_domain_spawn)
|
|
|
|
events.add_handler('domain-start', self.on_domain_start)
|
|
|
|
events.add_handler('connection-established',
|
2019-10-20 13:24:56 +02:00
|
|
|
self.on_connection_established)
|
|
|
|
|
2017-04-14 13:28:45 +02:00
|
|
|
|
2018-02-26 22:11:25 +01:00
|
|
|
def x_reader(conn, callback):
|
2019-10-20 13:24:56 +02:00
|
|
|
"""Try reading something from X connection to check if it's still alive.
|
2018-02-26 22:11:25 +01:00
|
|
|
In case it isn't, call *callback*.
|
2019-10-20 13:24:56 +02:00
|
|
|
"""
|
2018-02-26 22:11:25 +01:00
|
|
|
try:
|
|
|
|
conn.poll_for_event()
|
|
|
|
except xcffib.ConnectionException:
|
|
|
|
callback()
|
2017-04-14 13:28:45 +02:00
|
|
|
|
2019-10-20 13:24:56 +02:00
|
|
|
|
2017-04-21 15:05:20 +02:00
|
|
|
if 'XDG_RUNTIME_DIR' in os.environ:
|
|
|
|
pidfile_path = os.path.join(os.environ['XDG_RUNTIME_DIR'],
|
2019-10-20 13:24:56 +02:00
|
|
|
'qvm-start-gui.pid')
|
2017-04-21 15:05:20 +02:00
|
|
|
else:
|
|
|
|
pidfile_path = os.path.join(os.environ.get('HOME', '/'),
|
2019-10-20 13:24:56 +02:00
|
|
|
'.qvm-start-gui.pid')
|
2017-04-21 15:05:20 +02:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
parser = qubesadmin.tools.QubesArgumentParser(
|
2017-05-11 20:20:15 +02:00
|
|
|
description='start GUI for qube(s)', vmname_nargs='*')
|
2017-04-14 13:28:45 +02:00
|
|
|
parser.add_argument('--watch', action='store_true',
|
2019-10-20 13:24:56 +02:00
|
|
|
help='Keep watching for further domains'
|
|
|
|
' startups, must be used with --all')
|
2017-07-27 19:04:36 +02:00
|
|
|
parser.add_argument('--force-stubdomain', action='store_true',
|
2019-10-20 13:24:56 +02:00
|
|
|
help='Start GUI to stubdomain-emulated VGA,'
|
|
|
|
' even if gui-agent is running in the VM')
|
2017-04-21 15:05:20 +02:00
|
|
|
parser.add_argument('--pidfile', action='store', default=pidfile_path,
|
2019-10-20 13:24:56 +02:00
|
|
|
help='Pidfile path to create in --watch mode')
|
2017-05-12 19:43:59 +02:00
|
|
|
parser.add_argument('--notify-monitor-layout', action='store_true',
|
2019-10-20 13:24:56 +02:00
|
|
|
help='Notify running instance in --watch mode'
|
|
|
|
' about changed monitor layout')
|
2017-04-14 13:28:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main(args=None):
|
2019-10-20 13:24:56 +02:00
|
|
|
""" Main function of qvm-start-gui tool"""
|
2017-04-14 13:28:45 +02:00
|
|
|
args = parser.parse_args(args)
|
|
|
|
if args.watch and not args.all_domains:
|
|
|
|
parser.error('--watch option must be used with --all')
|
2017-04-21 15:05:20 +02:00
|
|
|
if args.watch and args.notify_monitor_layout:
|
|
|
|
parser.error('--watch cannot be used with --notify-monitor-layout')
|
2017-04-14 13:28:45 +02:00
|
|
|
launcher = GUILauncher(args.app)
|
|
|
|
if args.watch:
|
2017-05-11 23:40:49 +02:00
|
|
|
if not have_events:
|
|
|
|
parser.error('--watch option require Python >= 3.5')
|
2017-04-21 15:05:20 +02:00
|
|
|
with daemon.pidfile.TimeoutPIDLockFile(args.pidfile):
|
|
|
|
loop = asyncio.get_event_loop()
|
2017-05-11 23:40:49 +02:00
|
|
|
# pylint: disable=no-member
|
2017-05-11 23:21:04 +02:00
|
|
|
events = qubesadmin.events.EventsDispatcher(args.app)
|
2017-05-11 23:40:49 +02:00
|
|
|
# pylint: enable=no-member
|
2017-04-21 15:05:20 +02:00
|
|
|
launcher.register_events(events)
|
2017-04-14 13:28:45 +02:00
|
|
|
|
2017-04-21 15:05:20 +02:00
|
|
|
events_listener = asyncio.ensure_future(events.listen_for_events())
|
2017-04-14 13:28:45 +02:00
|
|
|
|
2017-04-21 15:05:20 +02:00
|
|
|
for signame in ('SIGINT', 'SIGTERM'):
|
|
|
|
loop.add_signal_handler(getattr(signal, signame),
|
2019-10-20 13:24:56 +02:00
|
|
|
events_listener.cancel) # pylint: disable=no-member
|
2017-04-14 13:28:45 +02:00
|
|
|
|
2017-04-21 15:05:20 +02:00
|
|
|
loop.add_signal_handler(signal.SIGHUP,
|
2019-10-20 13:24:56 +02:00
|
|
|
launcher.send_monitor_layout_all)
|
2017-04-15 20:15:06 +02:00
|
|
|
|
2018-02-26 22:11:25 +01:00
|
|
|
conn = xcffib.connect()
|
|
|
|
x_fd = conn.get_file_descriptor()
|
|
|
|
loop.add_reader(x_fd, x_reader, conn, events_listener.cancel)
|
|
|
|
|
2017-04-21 15:05:20 +02:00
|
|
|
try:
|
|
|
|
loop.run_until_complete(events_listener)
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
pass
|
2018-02-26 22:11:25 +01:00
|
|
|
loop.remove_reader(x_fd)
|
2017-04-21 15:05:20 +02:00
|
|
|
loop.stop()
|
|
|
|
loop.run_forever()
|
|
|
|
loop.close()
|
|
|
|
elif args.notify_monitor_layout:
|
2017-04-15 20:12:42 +02:00
|
|
|
try:
|
2017-04-21 15:05:20 +02:00
|
|
|
with open(pidfile_path, 'r') as pidfile:
|
|
|
|
pid = int(pidfile.read().strip())
|
|
|
|
os.kill(pid, signal.SIGHUP)
|
|
|
|
except (FileNotFoundError, ValueError) as e:
|
|
|
|
parser.error('Cannot open pidfile {}: {}'.format(pidfile_path,
|
2019-10-20 13:24:56 +02:00
|
|
|
str(e)))
|
2017-04-14 13:28:45 +02:00
|
|
|
else:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
tasks = []
|
|
|
|
for vm in args.domains:
|
|
|
|
if vm.is_running():
|
2017-07-27 19:04:36 +02:00
|
|
|
tasks.append(asyncio.ensure_future(launcher.start_gui(
|
|
|
|
vm, force_stubdom=args.force_stubdomain)))
|
2017-04-14 13:28:45 +02:00
|
|
|
if tasks:
|
|
|
|
loop.run_until_complete(asyncio.wait(tasks))
|
|
|
|
loop.stop()
|
|
|
|
loop.run_forever()
|
|
|
|
loop.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|