chiark / gitweb /
863cf83660de3e63525c21ce833ad0b69420830c
[chiark-utils.git] / fishdescriptor / fishdescriptor
1 #!/usr/bin/python3
2
3 import sys
4 import fishdescriptor.fish
5 import optparse
6 import re
7
8 donor = None
9
10 usage = '''fishdescriptor [-p|--pid] <pid> <action>... [-p|--pid <pid> <action>...]
11
12 <action>s
13   [<here-0fd>=]<there-fd>
14           fish the openfile referenced by descriptor <there-fd> in
15           (the most recent) <pid> and keep a descriptor onto it;
16           and, optionally, give it the number <here-fd> for exec
17   exec <program> [<arg>...]
18           execute a process with each specified <here>
19           as an actual fd
20   sockinfo
21           calls getsockname/getpeername on the most recent
22           <there-fd>
23
24   -p|-pid <pid>
25           now attach to <pid>, detaching from previous pid
26 '''
27
28 pending = []
29 # list of (nominal, there) where nominal might be None
30
31 fdmap = { }
32 # fdmap[nominal] = (actual, Donor, there)
33
34 last_nominal = None
35
36 def implement_pending():
37     actuals = donor.fish([pend[1] for pend in pending])
38     assert(len(actuals) == len(pending))
39     for (nominal, there), actual in zip(pending, actuals)
40         overwriting_info = fdmap.get(nominal)
41         if overwriting_info is not None: os.close(overwriting_info[0])
42         fdmap[nominal] = (actual, Donor, there)
43         last_nominal = nominal
44
45 def implmement_sockinfo(nominal):
46     (actual, tdonor, there) = fdmap[nominal]
47     # socket.fromfd requires the AF.  But of course we don't know the AF.
48     # There isn't a sane way to get it in Python:
49     #  https://utcc.utoronto.ca/~cks/space/blog/python/SocketFromFdMistake
50     # Rejected options:
51     #  https://github.com/tiran/socketfromfd
52     #   adds a dependency, not portable due to reliance on SO_DOMAIN
53     #  call getsockname using ctypes
54     #   no sane way to discover how to unpack sa_family_t
55     perl_script = '''
56         use strict;
57         use Socket;
58         use POSIX;
59         my $sa = getsockname STDIN;
60         exit 0 if !defined $sa and $!==ENOTSOCK;
61         my $family = sockaddr_family $sa;
62         print $family, "\n" or die $!;
63     '''
64     famp = subprocess.Popen(
65         stdin = actual,
66         stdout = subprocess.PIPE,
67         args = ['perl','-we',perl_script]
68     )
69     (output, dummy) = famp.communicate()
70     family = int(output)
71
72     sock = socket.fromfd(fd, family, 0)
73
74     print("[%s] %d sockinfo" % (tdonor.pid, there), end='')
75     for f in (lambda: socket.AddressFamily(family).name,
76               lambda: repr(sock.getsockname()),
77               lambda: repr(sock.getpeername())):
78         try: info = f()
79         except Exception as e: info = repr(e)
80         print("\t", info, sep='', end='')
81     print("")
82
83     sock.close()
84
85 def permute_fds_for_exec():
86     actual2intended = { info[0]: nominal for nominal, info in fdmap.items }
87     # invariant at the start of each loop iteration:
88     #     for each intended (aka `nominal') we have processed:
89     #         relevant open-file is only held in fd intended
90     #         (unless `nominal' is None in which case it is closed)
91     #     for each intended (aka `nominal') we have NOT processed:
92     #         relevant open-file is only held in actual
93     #         where  actual = fdmap[nominal][0]
94     #         and where  actual2intended[actual] = nominal
95     # we can rely on processing each intended only once,
96     #  since they're hash keys
97     # the post-condition is not really a valid state (fdmap
98     #  is nonsense) but we call this function just before exec
99     for intended, (actual, tdonor, there) in fdmap.items():
100         if intended == actual:
101             continue
102         if intended is not None:
103             inway_intended = actual2intended.get(intended)
104             if inway_intended is not None:
105                 inway_moved = os.dup(intended)
106                 actual2intended[inway_moved] = inway_intended
107                 fdmap[inway_intented][0] = inway_moved
108             os.dup2(actual, intended)
109         os.close(actual)
110
111 def implement_exec(argl):
112     if donor is not None: donor.detach()
113     sys.stdout.flush()
114     permut_fds_for_exec()
115     os.execvp(argl[0], argl)
116
117 def set_donor(pid):
118     global donor
119     if donor is not None: donor.detach()
120     donor = fishdescriptor.fish.Donor(pid)
121
122 def ocb_set_donor(option, opt, value, parser):
123     set_donor(value)
124
125 ov = optparse.Values()
126
127 def process_args():
128     def arg_matches(regexp):
129         nonlocal m
130         m = re.search(regexp, arg)
131         return m
132
133     op = optparse.OptionParser(usage=usage)
134
135     op.disable_interspersed_args()
136     op.add_option('-p','--pid', type='int', action='callback',
137                   callback='ocb_set_donor')
138
139     args = sys.argv
140
141     while True:
142         (ov, args) = op.parse_args(args=args, values=ov)
143         if not len(args): break
144
145         arg = args.pop(0)
146
147         if donor is not None:
148             set_donor(int(arg))
149         elif arg_matches(r'^(?:(\d+)=)?(\d+)?$'):
150             (nominal, there) = m.groups()
151             nominal = None if nominal is None else int(nominal)
152             there = int(there)
153             pending.append = (nominal,there)
154         elif arg = 'exec':
155             if not len(args):
156                 op.error("exec needs command to run")
157             implement_pending()
158             implement_exec(args)
159         elif arg = 'sockinfo':
160             if last_nominal is None:
161                 op.error('sockinfo needs a prior fd spec')
162             implement_pending()
163             implement_sockinfo(last_nominal)
164         else:
165             op.error("unknown argument/option `%s'" % arg)
166
167     implement_pending()
168     
169
170             there = int(m.group[1])
171             nominal = None if m.group
172
173             ,nominal) = map(int, m.groups())
174             
175
176 pid = int(sys.argv[1])
177 fds = [int(x) for x in sys.argv[2:]]
178
179 d = fishdescriptor.fish.Donor(pid)
180 r = d.fish(fds)
181 print(repr(r))