]> arthur.barton.de Git - bup.git/blob - cmd/on--server-cmd.py
cdfd44113c352e3560a521e97d7e915344b7ee07
[bup.git] / cmd / on--server-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7 import sys, os, struct
8 from bup import options, helpers
9
10 optspec = """
11 bup on--server
12 --
13     This command is run automatically by 'bup on'
14 """
15 o = options.Options(optspec)
16 (opt, flags, extra) = o.parse(sys.argv[1:])
17 if extra:
18     o.fatal('no arguments expected')
19
20 # get the subcommand's argv.
21 # Normally we could just pass this on the command line, but since we'll often
22 # be getting called on the other end of an ssh pipe, which tends to mangle
23 # argv (by sending it via the shell), this way is much safer.
24 buf = sys.stdin.read(4)
25 sz = struct.unpack('!I', buf)[0]
26 assert(sz > 0)
27 assert(sz < 1000000)
28 buf = sys.stdin.read(sz)
29 assert(len(buf) == sz)
30 argv = buf.split('\0')
31 argv = [argv[0], 'mux', '--'] + argv
32
33 # stdin/stdout are supposedly connected to 'bup server' that the caller
34 # started for us (often on the other end of an ssh tunnel), so we don't want
35 # to misuse them.  Move them out of the way, then replace stdout with
36 # a pointer to stderr in case our subcommand wants to do something with it.
37 #
38 # It might be nice to do the same with stdin, but my experiments showed that
39 # ssh seems to make its child's stderr a readable-but-never-reads-anything
40 # socket.  They really should have used shutdown(SHUT_WR) on the other end
41 # of it, but probably didn't.  Anyway, it's too messy, so let's just make sure
42 # anyone reading from stdin is disappointed.
43 #
44 # (You can't just leave stdin/stdout "not open" by closing the file
45 # descriptors.  Then the next file that opens is automatically assigned 0 or 1,
46 # and people *trying* to read/write stdin/stdout get screwed.)
47 os.dup2(0, 3)
48 os.dup2(1, 4)
49 os.dup2(2, 1)
50 fd = os.open('/dev/null', os.O_RDONLY)
51 os.dup2(fd, 0)
52 os.close(fd)
53
54 os.environ['BUP_SERVER_REVERSE'] = helpers.hostname()
55 os.execvp(argv[0], argv)
56 sys.exit(99)