]> arthur.barton.de Git - bup.git/blobdiff - main.py
main: even more fixes for signal handling.
[bup.git] / main.py
diff --git a/main.py b/main.py
index 3dcff697faf231d5079627298a84c05d22978ff5..941242ef4bbb9f6962c930346f916a1ec8bcfd01 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -93,24 +93,27 @@ def force_tty():
     if fix_stdout or fix_stderr:
         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):
-    pass
+    def __init__(self, signum):
+        self.signum = signum
+        Exception.__init__(self, 'signal %d received' % signum)
 def handler(signum, frame):
-    raise SigException('signal %d received' % signum)
+    raise SigException(signum)
 
 signal.signal(signal.SIGTERM, handler)
 signal.signal(signal.SIGINT, handler)
@@ -121,12 +124,19 @@ try:
     try:
         p = subprocess.Popen([subpath(subcmd)] + argv[2:],
                              stdout=outf, stderr=errf, preexec_fn=force_tty)
-        ret = p.wait()
+        while 1:
+            # if we get a signal while waiting, we have to keep waiting, just
+            # in case our child doesn't die.
+            try:
+                ret = p.wait()
+                break
+            except SigException, e:
+                log('\nbup: %s\n' % e)
+                os.kill(p.pid, e.signum)
+                ret = 94
     except OSError, e:
         log('%s: %s\n' % (subpath(subcmd), e))
         ret = 98
-    except SigException, e:
-        ret = 94
 finally:
     if p and p.poll() == None:
         os.kill(p.pid, signal.SIGTERM)