]> arthur.barton.de Git - bup.git/blobdiff - main.py
Correctly pass along SIGINT to child processes.
[bup.git] / main.py
diff --git a/main.py b/main.py
index 8cd22937bda207fd5a7afe859a9b7b9a502e999e..50eb24454154b216287563e1ad5c773d27bb24b9 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-import sys, os, subprocess
+import sys, os, subprocess, signal
 
 argv = sys.argv
 exe = argv[0]
@@ -10,6 +10,7 @@ libpath = os.path.join(exepath, 'lib')
 cmdpath = os.path.join(exepath, 'cmd')
 sys.path[:0] = [libpath]
 os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '')
+os.environ['BUP_MAIN_EXE'] = os.path.abspath(exe)
 
 from bup.helpers import *
 
@@ -37,8 +38,9 @@ def usage():
         ftp = 'Browse backup sets using an ftp-like client',
         fsck = 'Check backup sets for damage and add redundancy information',
         fuse = 'Mount your backup sets as a filesystem',
+        help = 'Print detailed help for the given command',
         index = 'Create or display the index of files to back up',
-        join = 'The reverse operation to "bup split"',
+        join = 'Retrieve a file backed up using "bup split"',
         ls = 'Browse the files in your backup sets',
         midx = 'Index objects to speed up future backups',
         save = 'Save files into a backup set (note: run "bup index" first)',
@@ -89,21 +91,36 @@ fix_stderr = not already_fixed and os.isatty(2)
 
 def force_tty():
     if fix_stdout or fix_stderr:
-        os.environ['BUP_FORCE_TTY'] = '1'
+        amt = (fix_stdout and 1 or 0) + (fix_stderr and 2 or 0)
+        os.environ['BUP_FORCE_TTY'] = str(amt)
+    os.setsid()  # make sure ctrl-c is sent just to us, not to child too
 
 if fix_stdout or fix_stderr:
     realf = fix_stderr and 2 or 1
     n = subprocess.Popen([subpath('newliner')],
                          stdin=subprocess.PIPE, stdout=os.dup(realf),
                          close_fds=True, preexec_fn=force_tty)
-    outf = fix_stdout and n.stdin.fileno() or 1
-    errf = fix_stderr and n.stdin.fileno() or 2
+    outf = fix_stdout and n.stdin.fileno() or None
+    errf = fix_stderr and n.stdin.fileno() or None
 else:
     n = None
-    outf = 1
-    errf = 2
+    outf = None
+    errf = None
+
+
+class SigException(Exception):
+    def __init__(self, signum):
+        self.signum = signum
+        Exception.__init__(self, 'signal %d received' % signum)
+def handler(signum, frame):
+    raise SigException(signum)
+
+signal.signal(signal.SIGTERM, handler)
+signal.signal(signal.SIGINT, handler)
 
 ret = 95
+p = None
+killsig = signal.SIGTERM
 try:
     try:
         p = subprocess.Popen([subpath(subcmd)] + argv[2:],
@@ -112,9 +129,14 @@ try:
     except OSError, e:
         log('%s: %s\n' % (subpath(subcmd), e))
         ret = 98
-    except KeyboardInterrupt, e:
+    except SigException, e:
+        log('\nbup: %s\n' % e)
+        killsig = e.signum
         ret = 94
 finally:
+    if p and p.poll() == None:
+        os.kill(p.pid, killsig)
+        p.wait()
     if n:
         n.stdin.close()
         try: