/usr/share/pyshared/x2go/guardian.py is in python-x2go 0.1.1.8-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | # -*- coding: utf-8 -*-
# Copyright (C) 2010-2011 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# Python X2go is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Python X2go 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
"""\
X2goSessionGuardian class - a guardian thread that controls X2go session threads
and their sub-threads (like reverse forwarding tunnels, Paramiko transport threads,
etc.).
"""
__NAME__ = 'x2goguardian-pylib'
# modules
import gevent
import threading
import copy
# Python X2go modules
from cleanup import x2go_cleanup
import x2go_exceptions
import log
_sigterm_received = False
def _sigterm_handle(s, f):
global _sigterm_received
_sigterm_received = True
return 0
class X2goSessionGuardian(threading.Thread):
"""\
L{X2goSessionGuardian} thread controls X2go session threads and their sub-threads (like
reverse forwarding tunnels, Paramiko transport threads, etc.). Its main function is
to tidy up once a session gets interrupted (SIGTERM, SIGINT).
There is one L{X2goSessionGuardian} for each L{X2goClient} instance (thus: for normal
setups there should be _one_ L{X2goClient} and _one_ L{X2goSessionGuardian} in use).
"""
def __init__(self, client_instance,
auto_update_listsessions_cache=False,
auto_update_listdesktops_cache=False,
auto_update_sessionregistry=False,
auto_register_sessions=False,
refresh_interval=5,
logger=None, loglevel=log.loglevel_DEFAULT):
"""\
@param auto_update_listsessions_cache: let L{X2goSessionGuardian} refresh the session list cache for all L{X2goSession} objects
@type auto_update_listsessions_cache: C{bool}
@param auto_update_listdesktops_cache: let L{X2goSessionGuardian} refresh desktop lists in the session list cache for all L{X2goSession} objects
@type auto_update_listdesktops_cache: C{bool}
@param auto_update_sessionregistry: if set to C{True} the session status will be updated in regular intervals
@type auto_update_sessionregistry: C{bool}
@param auto_register_sessions: register new sessions automatically once they appear in the X2go session (e.g.
instantiated by another client that is connected to the same X2go server under same user ID)
@type auto_register_sessions: C{bool}
@param refresh_interval: refresh cache and session registry every <refresh_interval> seconds
@type refresh_interval: C{int}
@param logger: you can pass an L{X2goLogger} object to the L{X2goSessionGuardian} constructor
@type logger: C{instance}
@param loglevel: if no L{X2goLogger} object has been supplied a new one will be
constructed with the given loglevel
@type loglevel: C{int}
"""
if logger is None:
self.logger = log.X2goLogger(loglevel=loglevel)
else:
self.logger = copy.deepcopy(logger)
self.logger.tag = __NAME__
self.client_instance = client_instance
self.auto_update_listsessions_cache = auto_update_listsessions_cache
self.auto_update_listdesktops_cache = auto_update_listdesktops_cache
self.auto_update_sessionregistry = auto_update_sessionregistry
self.auto_register_sessions = auto_register_sessions
self.refresh_interval = refresh_interval
threading.Thread.__init__(self, target=self.guardian)
self.daemon = True
self.start()
def guardian(self):
"""\
The handler of this L{X2goSessionGuardian} thread.
"""
global _sigterm_received
seconds = 0
self._keepalive = True
while not _sigterm_received and self._keepalive:
gevent.sleep(1)
seconds += 1
if seconds % self.refresh_interval == 0:
self.logger('Entering X2go Guardian client management loop...', loglevel=log.loglevel_DEBUG)
if self.auto_update_listsessions_cache:
self.client_instance.update_cache_all_profiles(update_sessions=self.auto_update_listsessions_cache,
update_desktops=self.auto_update_listdesktops_cache,
)
if self.auto_update_sessionregistry and not self.auto_register_sessions:
self.client_instance.update_sessionregistry_status_all_profiles()
# session auto-registration will automatically trigger an update of the session registry status
if self.auto_register_sessions:
self.client_instance.register_available_server_sessions_all_profiles()
self.logger('X2go session guardian thread waking up after %s seconds' % seconds, loglevel=log.loglevel_DEBUG)
for session_uuid in self.client_instance.session_registry.keys():
session_summary = self.client_instance.get_session_summary(session_uuid)
self.logger('calling session cleanup on profile %s for terminal session: %s' % (session_summary['profile_name'], session_summary['session_name']), loglevel=log.loglevel_DEBUG)
x2go_cleanup(threads=session_summary['active_threads'])
def stop_thread(self):
"""\
Stop this L{X2goSessionGuardian} thread.
"""
self._keepalive = False
|