Handle termination signals to exit gracefully and prevent resource leak

Make sure we free the reserved resources and kill launched subprocesses
before stopping. Before this patch it was not the case for instance if we
received a SIGTREM signal from kill.

Change-Id: I039e4d1908a04bf606b101ddc6a186ba67e6178e
diff --git a/src/osmo-gsm-tester.py b/src/osmo-gsm-tester.py
index 504c6a9..766f356 100755
--- a/src/osmo-gsm-tester.py
+++ b/src/osmo-gsm-tester.py
@@ -68,11 +68,23 @@
 
 import sys
 import argparse
+from signal import *
 from osmo_gsm_tester import __version__
 from osmo_gsm_tester import trial, suite, log, config
 
+def sig_handler_cleanup(signum, frame):
+    print("killed by signal %d" % signum)
+    # This sys.exit() will raise a SystemExit base exception at the current
+    # point of execution. Code must be prepared to clean system-wide resources
+    # by using the "finally" section. This allows at the end 'atexit' hooks to
+    # be called before exiting.
+    sys.exit(1)
+
 def main():
 
+    for sig in (SIGINT, SIGTERM, SIGQUIT, SIGPIPE, SIGHUP):
+        signal(sig, sig_handler_cleanup)
+
     parser = argparse.ArgumentParser(epilog=__doc__, formatter_class=argparse.RawTextHelpFormatter)
     # Note: since we're using RawTextHelpFormatter to keep nicely separate
     # paragraphs in the long help text, we unfortunately also need to take care
@@ -184,7 +196,8 @@
                 if status == trial.Trial.FAIL:
                     any_failed = True
                 trials_run.append(current_trial)
-        except:
+        except Exception:
+            # Do not catch here subclasses of BaseException such as SystemExit, let them finish the program
             current_trial.log_exn()
 
     sys.stderr.flush()