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