chiark / gitweb /
fishdescriptor: before redo option parsing
authorIan Jackson <ian.jackson@eu.citrix.com>
Thu, 19 Oct 2017 18:59:50 +0000 (19:59 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Thu, 19 Oct 2017 18:59:50 +0000 (19:59 +0100)
Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
fishdescriptor/fishdescriptor
fishdescriptor/py/fishdescriptor/fish.py

index 7150df2..b8c732a 100755 (executable)
@@ -2,6 +2,66 @@
 
 import sys
 import fishdescriptor.fish
+import re
+
+donor = None
+
+usage =
+'''
+fishdescriptor <pid> <action>... [-p|--pid <pid> <action>...]
+
+<action>s
+  <there-fd>[=<here>]
+          fish the openfile referenced by descriptor <there-fd> in
+          (the most recent) <pid> and keep a descriptor onto it;
+          and, optionally, give it the number <here> for exec
+  exec <program> [<arg>...]
+          execute a process with each specified numeric <here>
+          as an actual fd
+  sockinfo
+          calls getsockname/getpeername on the most recent
+          <there-fd>
+
+  -p|-pid <pid>
+          now attach to <pid>, detaching from previous pid
+'''
+
+def set_donor(pid_arg):
+    pid = int(pid_arg)
+    if donor is not None: donor.detach()
+    donor = fishdescriptor.fish.Donor(pid)
+
+def process_args():
+    def arg_matches(regexp):
+        nonlocal m
+        m = re.search(regexp, arg)
+        return m
+
+    arg_ix = 0
+    def next_arg():
+        sys.argv[arg_ix] = 
+
+    while arg_ix < len(sys.argv):
+        arg = next_arg()
+        if arg_matches(r'^-p(\d+)') or
+           arg_matches(r'^--pid=(\d+)$')
+            set_donor(m.groups(1)[0])
+            
+            pid = 
+(?:-p|^--pid(?:=|$))(\d*)$'):
+            
+            pid = int(m.groups(1)[0])
+        elif arg_matches(r'^(?:-p|^--pid=)(\d+)$'):
+            
+        if expecting_pid:
+            set_donor(arg)
+        el
+        elif arg_matches(r'\d+
+            
+    parser = OptionParser(usage=usage, )
+
+    parser.add_option("-p", "--pid", type='int', callback=set_donor)
+    parser.add_option(
 
 pid = int(sys.argv[1])
 fds = [int(x) for x in sys.argv[2:]]
index 763684b..ff77fa9 100644 (file)
@@ -136,3 +136,6 @@ class Donor():
             except FileNotFoundError: pass
 
         return list(got_fds)
+
+    def detach(d):
+        d._sp.stdin.close()