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