#!/usr/bin/env python
import sys, getopt, socket, subprocess
from bup import options, path
from bup.helpers import *

optspec = """
bup daemon [options...] -- [bup-server options...]
--
l,listen  ip address to listen on, defaults to *
p,port    port to listen on, defaults to 1982
"""
o = options.Options(optspec, optfunc=getopt.getopt)
(opt, flags, extra) = o.parse(sys.argv[1:])

host = opt.listen
port = opt.port and int(opt.port) or 1982

import socket
import sys

socks = []
e = None
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
                              socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except socket.error, e:
        continue
    try:
        if af == socket.AF_INET6:
            log("bup daemon: listening on [%s]:%s\n" % sa[:2])
        else:
            log("bup daemon: listening on %s:%s\n" % sa[:2])
        s.bind(sa)
        s.listen(1)
    except socket.error, e:
        s.close()
        continue
    socks.append(s)

if not socks:
    log('bup daemon: listen socket: %s\n' % e.args[1])
    sys.exit(1)

try:
    while True:
        [rl,wl,xl] = select.select(socks, [], [], 60)
        for l in rl:
            s, src = l.accept()
            log("Socket accepted connection from %s\n" % (src,))
            sp = subprocess.Popen([path.exe(), 'mux', '--', 'server'] + extra,
                                  stdin=os.dup(s.fileno()), stdout=os.dup(s.fileno()))
            s.close()
finally:
    for l in socks:
        l.shutdown(socket.SHUT_RDWR)
        l.close()

debug1("bup daemon: done")

@@ -934,32 +934,96 @@
 %25s%5Cn%22 %25 sa%5B:2%5D)%0A
+        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)%0A
         s.bind(s

use SO_REUSEADDR.