chiark / gitweb /
fishdescriptor: Provide copyright notices and licence statements
[chiark-utils.git] / fishdescriptor / py / fishdescriptor / fish.py
index 78bb2d86a8725466c47f2a9c2e38d55694c9ca2f..0f180bc11c8d59c11bb07caaaa22c81500ef35c4 100644 (file)
@@ -1,21 +1,53 @@
+# fish.py
+
+# This file is part of chiark-utils, a collection of useful programs
+# used on chiark.greenend.org.uk.
+#
+# This file is:
+#  Copyright 2018 Citrix Systems Ltd
+#
+# This is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3, or (at your option) any later version.
+#
+# This is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, consult the Free Software Foundation's
+# website at www.fsf.org, or the GNU Project website at www.gnu.org.
+
 # python 3 only
 
 import socket
 import subprocess
 import os
 import pwd
+import struct
+import tempfile
+import shutil
+import sys
 
 def _shuffle_fd3():
     os.dup2(1,3)
     os.dup2(2,1)
 
+class Error(Exception): pass
+
 class Donor():
-    def __init__(d, pid):
-        d._pid = pid
+    def __init__(d, pid, debug=None):
+        d.pid = pid
+        if debug is None:
+            d._stderr = tempfile.TemporaryFile(mode='w+')
+        else:
+            d._stderr = None
         d._sp = subprocess.Popen(
             preexec_fn = _shuffle_fd3,
             stdin = subprocess.PIPE,
             stdout = subprocess.PIPE,
+            stderr = d._stderr,
             close_fds = False,
             args = ['gdb', '-p', str(pid), '-batch', '-ex',
                     'python import fishdescriptor.indonor as id;'+
@@ -24,12 +56,21 @@ class Donor():
         )            
 
     def _eval_integer(d, expr):
-        l = d._sp.stdout.readline()
-        if l != b'!\n': raise RuntimeError("indonor said %s" % repr(l))
-        d._sp.stdin.write(expr.encode('utf-8') + b'\n')
-        d._sp.stdin.flush()
-        l = d._sp.stdout.readline().rstrip(b'\n')
-        return int(l)
+        try:
+            l = d._sp.stdout.readline()
+            if not len(l): raise Error('gdb process donor python repl quit')
+            if l != b'!\n': raise RuntimeError("indonor said %s" % repr(l))
+            d._sp.stdin.write(expr.encode('utf-8') + b'\n')
+            d._sp.stdin.flush()
+            l = d._sp.stdout.readline().rstrip(b'\n')
+            return int(l)
+        except Exception as e:
+            if d._stderr is not None:
+                d._stderr.seek(0)
+                shutil.copyfileobj(d._stderr, sys.stderr)
+                d._stderr.seek(0)
+                d._stderr.truncate()
+            raise e
 
     def _eval_success(d, expr):
         r = d._eval_integer(expr)
@@ -53,11 +94,11 @@ class Donor():
             args = ['perl','-we',perl_script] + [str(x) for x in fds]
         )
         (output, dummy) = ap.communicate()
-        return output
+        return output.decode('utf-8')
 
     def donate(d, path, fds):
         ancil = d._ancilmsg(fds)
-        d._eval_success('di.donate(%s, %s)'
+        d._eval_success('di.donate(%s, [ %s ])'
                         % (repr(path), ancil))
         return len(ancil.split(','))
 
@@ -96,9 +137,9 @@ class Donor():
         sockdir = d._sock_dir(euid)
         d.mkdir(sockdir)
 
-        sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d._pid)
+        sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d.pid)
 
-        target_root = '/proc/%d/root' % d._pid
+        target_root = '/proc/%d/root' % d.pid
         if not d._exists(target_root):
             target_root = ''
 
@@ -116,14 +157,16 @@ class Donor():
             s.listen(1)
 
             ancil_len = d.donate(our_sockname, fds)
-            s2 = s.accept()
+            (s2, dummy) = s.accept()
             (msg, ancil, flags, sender) = s2.recvmsg(1, ancil_len)
 
-            got_fds = [ ]
+            got_fds = None
+            unpack_fmt = '%di' % len(fds)
 
             for clvl, ctype, cdata in ancil:
                 if clvl == socket.SOL_SOCKET and ctype == socket.SCM_RIGHTS:
-                    got_fds += cdata # need to trim any surplus, and unpack
+                    assert(got_fds is None)
+                    got_fds = struct.unpack_from(unpack_fmt, cdata)
 
         finally:
             if s is not None: s.close()
@@ -131,3 +174,8 @@ class Donor():
 
             try: os.remove(our_sockname)
             except FileNotFoundError: pass
+
+        return list(got_fds)
+
+    def detach(d):
+        d._sp.stdin.close()