16 class Error(Exception): pass
19 def __init__(d, pid, debug=None):
22 d._stderr = tempfile.TemporaryFile(mode='w+')
25 d._sp = subprocess.Popen(
26 preexec_fn = _shuffle_fd3,
27 stdin = subprocess.PIPE,
28 stdout = subprocess.PIPE,
31 args = ['gdb', '-p', str(pid), '-batch', '-ex',
32 'python import fishdescriptor.indonor as id;'+
33 ' id.DonorImplementation().eval_loop()'
37 def _eval_integer(d, expr):
39 l = d._sp.stdout.readline()
40 if not len(l): raise Error('gdb process donor python repl quit')
41 if l != b'!\n': raise RuntimeError("indonor said %s" % repr(l))
42 d._sp.stdin.write(expr.encode('utf-8') + b'\n')
44 l = d._sp.stdout.readline().rstrip(b'\n')
46 except Exception as e:
47 if d._stderr is not None:
49 shutil.copyfileobj(d._stderr, sys.stderr)
54 def _eval_success(d, expr):
55 r = d._eval_integer(expr)
56 if r != 1: raise RuntimeError("eval of %s gave %d" % (expr, r))
59 return d._eval_integer('di.geteuid()')
61 def _ancilmsg(d, fds):
66 my $fds = pack "i*", @ARGV;
67 my $m = Socket::MsgHdr::pack_cmsghdr SOL_SOCKET, SCM_RIGHTS, $fds;
68 print join ", ", unpack "C*", $m;
70 ap = subprocess.Popen(
71 stdin = subprocess.DEVNULL,
72 stdout = subprocess.PIPE,
73 args = ['perl','-we',perl_script] + [str(x) for x in fds]
75 (output, dummy) = ap.communicate()
76 return output.decode('utf-8')
78 def donate(d, path, fds):
79 ancil = d._ancilmsg(fds)
80 d._eval_success('di.donate(%s, [ %s ])'
81 % (repr(path), ancil))
82 return len(ancil.split(','))
85 d._eval_integer('di.mkdir(%s)'
93 if oe.errno != os.errno.ENOENT: raise oe
96 def _sock_dir(d, target_euid):
97 run_dir = '/run/user/%d' % target_euid
98 if d._exists(run_dir):
99 return run_dir + '/fishdescriptor'
102 pw = pwd.getpwuid(target_euid)
103 return pw.pw_dir + '/.fishdescriptor'
108 'cannot find good socket path - no /run/user/UID nor pw entry for target process euid %d'
113 # -> list of fds in our process
116 sockdir = d._sock_dir(euid)
119 sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d.pid)
121 target_root = '/proc/%d/root' % d.pid
122 if not d._exists(target_root):
125 our_sockname = target_root + sockname
131 try: os.remove(our_sockname)
132 except FileNotFoundError: pass
134 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
138 ancil_len = d.donate(our_sockname, fds)
139 (s2, dummy) = s.accept()
140 (msg, ancil, flags, sender) = s2.recvmsg(1, ancil_len)
143 unpack_fmt = '%di' % len(fds)
145 for clvl, ctype, cdata in ancil:
146 if clvl == socket.SOL_SOCKET and ctype == socket.SCM_RIGHTS:
147 assert(got_fds is None)
148 got_fds = struct.unpack_from(unpack_fmt, cdata)
151 if s is not None: s.close()
152 if s2 is not None: s2.close()
154 try: os.remove(our_sockname)
155 except FileNotFoundError: pass