[tracker/sam/sandbox-ctrl+c] trackertestutils: Ignore CTRL+C in interactive shells



commit 66752331032068cb605cfa1c036f656fd3f0df20
Author: Sam Thursfield <sam afuera me uk>
Date:   Tue Sep 17 00:40:22 2019 +0200

    trackertestutils: Ignore CTRL+C in interactive shells
    
    This fixes a problem in the run-uninstalled script where CTRL+C would
    kill the D-Bus daemon associated with an interactive shell, breaking
    everything.

 utils/trackertestutils/__main__.py   | 40 ++++++++++++++++++++++++------------
 utils/trackertestutils/dbusdaemon.py |  5 +++--
 utils/trackertestutils/helpers.py    |  4 ++--
 3 files changed, 32 insertions(+), 17 deletions(-)
---
diff --git a/utils/trackertestutils/__main__.py b/utils/trackertestutils/__main__.py
index b67b23d12..0b7b3daba 100644
--- a/utils/trackertestutils/__main__.py
+++ b/utils/trackertestutils/__main__.py
@@ -26,6 +26,7 @@
 import argparse
 import collections
 import configparser
+import contextlib
 import locale
 import logging
 import os
@@ -114,7 +115,8 @@ def environment_set_and_add_path(env, var, prefix, suffix):
     env[var] = full
 
 
-def create_sandbox(index_location, prefix=None, verbosity=0, dbus_config=None):
+def create_sandbox(index_location, prefix=None, verbosity=0, dbus_config=None,
+                   interactive=False):
     assert prefix is None or dbus_config is None
 
     extra_env = {}
@@ -142,7 +144,7 @@ def create_sandbox(index_location, prefix=None, verbosity=0, dbus_config=None):
     log.debug('Using index location "%s"' % index_location)
 
     sandbox = helpers.TrackerDBusSandbox(dbus_config, extra_env=extra_env)
-    sandbox.start()
+    sandbox.start(new_session=(interactive == True))
 
     # Update our own environment, so when we launch a subprocess it has the
     # same settings as the Tracker daemons.
@@ -371,6 +373,13 @@ def wait_for_miners(watches):
             time.sleep(0.1)
 
 
+@contextlib.contextmanager
+def ignore_sigint():
+    handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
+    yield
+    signal.signal(signal.SIGINT, handler)
+
+
 def main():
     locale.setlocale(locale.LC_ALL, '')
 
@@ -417,8 +426,12 @@ def main():
     else:
         index_location = args.index_location
 
+    interactive = not (args.command)
+
     # Set up environment variables and foo needed to get started.
-    sandbox = create_sandbox(index_location, args.prefix, verbosity, dbus_config=args.dbus_config)
+    sandbox = create_sandbox(index_location, args.prefix, verbosity,
+                             dbus_config=args.dbus_config,
+                             interactive=interactive)
     config_set()
 
     link_to_mime_data()
@@ -430,7 +443,17 @@ def main():
         miner_watches[miner] = watch
 
     try:
-        if args.command:
+        if interactive:
+            if args.dbus_config:
+                print(f"Using Tracker daemons from build tree with D-Bus config {args.dbus_config}")
+            else:
+                print(f"Using Tracker daemons from prefix {args.prefix}")
+            print("Starting interactive Tracker sandbox shell... (type 'exit' to finish)")
+            print()
+
+            with ignore_sigint():
+                subprocess.run(shell)
+        else:
             command = [shell, '-c', ' '.join(shlex.quote(c) for c in args.command)]
 
             log.debug("Running: %s", command)
@@ -441,15 +464,6 @@ def main():
 
             log.debug("Process finished with returncode %i", result.returncode)
             sys.exit(result.returncode)
-        else:
-            if args.dbus_config:
-                print(f"Using Tracker daemons from build tree with D-Bus config {args.dbus_config}")
-            else:
-                print(f"Using Tracker daemons from prefix {args.prefix}")
-            print("Starting interactive Tracker sandbox shell... (type 'exit' to finish)")
-            print()
-
-            os.system(shell)
     finally:
         sandbox.stop()
         if index_tmpdir:
diff --git a/utils/trackertestutils/dbusdaemon.py b/utils/trackertestutils/dbusdaemon.py
index 641889c11..71f8a31fa 100644
--- a/utils/trackertestutils/dbusdaemon.py
+++ b/utils/trackertestutils/dbusdaemon.py
@@ -58,7 +58,7 @@ class DBusDaemon:
             raise DaemonNotStartedError()
         return self._gdbus_connection
 
-    def start(self, config_file=None, env=None):
+    def start(self, config_file=None, env=None, new_session=False):
         dbus_command = ['dbus-daemon', '--print-address=1', '--print-pid=1']
         if config_file:
             dbus_command += ['--config-file=' + config_file]
@@ -66,7 +66,8 @@ class DBusDaemon:
             dbus_command += ['--session']
         log.debug("Running: %s", dbus_command)
         self.process = subprocess.Popen(
-            dbus_command, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+            dbus_command, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+            start_new_session=new_session)
 
         self._previous_sigterm_handler = signal.signal(
             signal.SIGTERM, self._sigterm_handler)
diff --git a/utils/trackertestutils/helpers.py b/utils/trackertestutils/helpers.py
index 92fd32949..a80d3266a 100644
--- a/utils/trackertestutils/helpers.py
+++ b/utils/trackertestutils/helpers.py
@@ -448,7 +448,7 @@ class TrackerDBusSandbox:
 
         self.daemon = dbusdaemon.DBusDaemon()
 
-    def start(self):
+    def start(self, new_session=False):
         env = os.environ
         env.update(self.extra_env)
         env['G_MESSAGES_PREFIXED'] = 'all'
@@ -463,7 +463,7 @@ class TrackerDBusSandbox:
 
         log.info("Starting D-Bus daemon for sandbox.")
         log.debug("Added environment variables: %s", self.extra_env)
-        self.daemon.start(self.dbus_daemon_config_file, env=env)
+        self.daemon.start(self.dbus_daemon_config_file, env=env, new_session=new_session)
 
     def stop(self):
         tracker_processes = []


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]