]> arthur.barton.de Git - bup.git/commitdiff
cmd/daemon: FD_CLOEXEC the listener socket and don't leak fd for the connection.
authorAvery Pennarun <apenwarr@gmail.com>
Sun, 13 Feb 2011 12:10:08 +0000 (04:10 -0800)
committerAvery Pennarun <apenwarr@gmail.com>
Sun, 13 Feb 2011 13:02:21 +0000 (05:02 -0800)
Otherwise the listener gets inherited by all the child processes (mostly
harmless) and subprograms run by bup-server inherit an extra fd for the
connection socket (problematic since we want the connection to close as soon
as bup-server closes).

Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
cmd/daemon-cmd.py

index c46f411c25c1956e2ac75de6cc901a998552c32d..b92716fdbb3057a986e30a48f15a1b835fe479a5 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-import sys, getopt, socket, subprocess
+import sys, getopt, socket, subprocess, fcntl
 from bup import options, path
 from bup.helpers import *
 
@@ -35,6 +35,7 @@ for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         s.bind(sa)
         s.listen(1)
+        fcntl.fcntl(s.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
     except socket.error, e:
         s.close()
         continue
@@ -53,10 +54,10 @@ try:
                 log("Socket accepted connection from %s\n" % (src,))
                 fd1 = os.dup(s.fileno())
                 fd2 = os.dup(s.fileno())
+                s.close()
                 sp = subprocess.Popen([path.exe(), 'mux', '--', 'server']
                                       + extra, stdin=fd1, stdout=fd2)
             finally:
-                s.close()
                 os.close(fd1)
                 os.close(fd2)
 finally: