chiark / gitweb /
fishdescriptor: When looking for run_dir, look in target root
[chiark-utils.git] / fishdescriptor / py / fishdescriptor / fish.py
index a506938964f71671c757b57decdceacb9039151a..dcb7c32ac817a3bcbfb2cc50a803933938c6e69a 100644 (file)
@@ -1,3 +1,24 @@
+# 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
@@ -13,6 +34,8 @@ def _shuffle_fd3():
     os.dup2(1,3)
     os.dup2(2,1)
 
+class Error(Exception): pass
+
 class Donor():
     def __init__(d, pid, debug=None):
         d.pid = pid
@@ -35,6 +58,7 @@ class Donor():
     def _eval_integer(d, expr):
         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()
@@ -90,9 +114,9 @@ class Donor():
             if oe.errno != os.errno.ENOENT: raise oe
             return False
 
-    def _sock_dir(d, target_euid):
+    def _sock_dir(d, target_euid, target_root):
         run_dir = '/run/user/%d' % target_euid
-        if d._exists(run_dir):
+        if d._exists(target_root + run_dir):
             return run_dir + '/fishdescriptor'
 
         try:
@@ -109,16 +133,16 @@ class Donor():
     def fish(d, fds):
         # -> list of fds in our process
 
+        target_root = '/proc/%d/root' % d.pid
+        if not d._exists(target_root):
+            target_root = ''
+
         euid = d._geteuid()
-        sockdir = d._sock_dir(euid)
+        sockdir = d._sock_dir(euid, target_root)
         d.mkdir(sockdir)
 
         sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d.pid)
 
-        target_root = '/proc/%d/root' % d.pid
-        if not d._exists(target_root):
-            target_root = ''
-
         our_sockname = target_root + sockname
 
         s = None
@@ -130,9 +154,10 @@ class Donor():
 
             s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
             s.bind(our_sockname)
+            os.chmod(our_sockname, 666)
             s.listen(1)
 
-            ancil_len = d.donate(our_sockname, fds)
+            ancil_len = d.donate(sockname, fds)
             (s2, dummy) = s.accept()
             (msg, ancil, flags, sender) = s2.recvmsg(1, ancil_len)