diff options
author | Sascha Silbe <sascha-pgp@silbe.org> | 2010-02-07 13:10:35 (GMT) |
---|---|---|
committer | Sascha Silbe <sascha-pgp@silbe.org> | 2010-02-07 13:10:35 (GMT) |
commit | 374fcd0ec733e964eb0a84305e15919780f61678 (patch) | |
tree | 71d455e04003b44db361cfc361f314041f20617f | |
parent | c13e2e642656cc4f79121264924399f10d9d5e53 (diff) |
kill server on viewer exit to emulate previous behaviour
-rw-r--r-- | src/jarabe/util/emulator.py | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/jarabe/util/emulator.py b/src/jarabe/util/emulator.py index 4b3b931..8934f5e 100644 --- a/src/jarabe/util/emulator.py +++ b/src/jarabe/util/emulator.py @@ -14,6 +14,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import errno import os import signal import subprocess @@ -27,6 +28,8 @@ from sugar import env DEFAULT_DIMENSIONS = (800, 600) _DEV_NULL = open(os.devnull, 'w') +server = None + def _run_pipe(command, stdin=None): """Run a program with optional input and return output. @@ -103,13 +106,18 @@ def _run_server(display, dpi, dimensions, fullscreen): def _kill_pipe(pipe): - """Terminate and wait for child process.""" + """Terminate and wait for child process (if any).""" try: os.kill(pipe.pid, signal.SIGTERM) - except OSError: - pass + except OSError, exception: + if exception.errno != errno.ESRCH: + raise - pipe.wait() + try: + pipe.wait() + except OSError, exception: + if exception.errno != errno.ECHILD: + raise def _start_server(dpi, dimensions, fullscreen): @@ -185,8 +193,24 @@ def _parse_args(): return parser.parse_args() +def _sigchld_handler(number_, frame_): + """Kill server when any (direct) child exits. + + So closing the viewer will close the server as well.""" + if server.returncode is not None: + return + + signal.signal(signal.SIGCHLD, signal.SIG_DFL) + + print 'sugar-emulator: Child exited, shutting down server' + _kill_pipe(server) + return + + def main(): """Script-level operations""" + global server + options, args = _parse_args() display, server = _start_server(options.dpi, options.dimensions, options.fullscreen) @@ -201,6 +225,8 @@ def main(): _start_window_manager() command += args + signal.signal(signal.SIGCHLD, _sigchld_handler) subprocess.call(command, close_fds=True) + _kill_pipe(viewer) _kill_pipe(server) |