15 d._sp = subprocess.Popen(
16 preexec_fn = _shuffle_fd3,
17 stdin = subprocess.PIPE,
18 stdout = subprocess.PIPE,
20 args = ['gdb', '-p', str(pid), '-batch', '-ex',
21 'python import fishdescriptor.indonor as id;'+
22 ' id.DonorImplementation().eval_loop()'
26 def _eval_integer(d, expr):
27 l = d._sp.stdin.readline()
28 if l != '!\n': raise RuntimeError("indonor said %s" % repr(l))
29 d._sp.stdout.write(expr + '\n')
31 l = d._sp.stdin.readline().rstrip('\n')
34 def _eval_success(d, expr):
35 r = d._eval_integer(expr)
36 if r != 1: raise RuntimeError("eval of %s gave %d" % (expr, r))
39 return d._eval_integer('di.geteuid()')
41 def _ancilmsg(d, fds):
46 my $fds = pack "i*", @ARGV;
47 my $m = Socket::MsgHdr::pack_cmsghdr SOL_SOCKET, SCM_RIGHTS, $fds;
48 print join ", ", unpack "C*", $m;
50 ap = subprocess.Popen(
51 stdin = subprocess.DEVNULL,
52 stdout = subprocess.PIPE,
53 args = ['perl','-we',perl_script] + fds
55 (output, dummy) = ap.communicate()
58 def donate(d, path, fds):
59 ancil = d._ancilmsg(fds)
60 d._eval_success('di.donate(%s, %s)'
61 % (repr(path), ancil))
62 return len(ancil.split(','))
65 d._eval_integer('di.mkdir(%s)'
73 if oe.errno != os.errno.ENOENT: raise oe
76 def _sock_dir(d, target_euid):
77 run_dir = '/run/user/%d' % target_euid
78 if d._exists(run_dir):
79 return run_dir + 'fishdescriptor'
82 pw = pwd.getpwuid(target_euid)
83 return pw.pw_dir + '.fishdescriptor'
88 'cannot find good socket path - no /run/user/UID nor pw entry for target process euid %d'
93 # -> list of fds in our process
96 sockdir = d._sock_dir(euid)
99 sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d._pid)
101 target_root = '/proc/%d/root/' % d._pid
102 if not d._exists(target_root):
105 our_sockname = target_root + sockname
111 try: os.remove(our_sockname)
112 except FileNotFoundError: pass
114 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
118 ancil_len = d.donate(our_sockname, fds)
120 (msg, ancil, flags, sender) = s2.recvmsg(1, ancil_len)
124 for clvl, ctype, cdata in ancil:
125 if clvl == socket.SOL_SOCKET and ctype == socket.SCM_RIGHTS:
126 got_fds += cdata # need to trim any surplus, and unpack
129 if s is not None: s.close()
130 if s2 is not None: s2.close()
132 try: os.remove(our_sockname)
133 except FileNotFoundError: pass