3 # This file is part of chiark-utils, a collection of useful programs
4 # used on chiark.greenend.org.uk.
7 # Copyright 2018 Citrix Systems Ltd
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3, or (at your option) any later version.
13 # This is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, consult the Free Software Foundation's
20 # website at www.fsf.org, or the GNU Project website at www.gnu.org.
37 class Error(Exception): pass
40 def __init__(d, pid, debug=None):
43 d._stderr = tempfile.TemporaryFile(mode='w+')
46 d._sp = subprocess.Popen(
47 preexec_fn = _shuffle_fd3,
48 stdin = subprocess.PIPE,
49 stdout = subprocess.PIPE,
52 args = ['gdb', '-p', str(pid), '-batch', '-ex',
53 'python import fishdescriptor.indonor as id;'+
54 ' id.DonorImplementation().eval_loop()'
58 def _eval_integer(d, expr):
60 l = d._sp.stdout.readline()
61 if not len(l): raise Error('gdb process donor python repl quit')
62 if l != b'!\n': raise RuntimeError("indonor said %s" % repr(l))
63 d._sp.stdin.write(expr.encode('utf-8') + b'\n')
65 l = d._sp.stdout.readline().rstrip(b'\n')
67 except Exception as e:
68 if d._stderr is not None:
70 shutil.copyfileobj(d._stderr, sys.stderr)
75 def _eval_success(d, expr):
76 r = d._eval_integer(expr)
77 if r != 1: raise RuntimeError("eval of %s gave %d" % (expr, r))
80 return d._eval_integer('di.geteuid()')
82 def _ancilmsg(d, fds):
87 my $fds = pack "i*", @ARGV;
88 my $m = Socket::MsgHdr::pack_cmsghdr SOL_SOCKET, SCM_RIGHTS, $fds;
89 print join ", ", unpack "C*", $m;
91 ap = subprocess.Popen(
92 stdin = subprocess.DEVNULL,
93 stdout = subprocess.PIPE,
94 args = ['perl','-we',perl_script] + [str(x) for x in fds]
96 (output, dummy) = ap.communicate()
97 return output.decode('utf-8')
99 def donate(d, path, fds):
100 ancil = d._ancilmsg(fds)
101 d._eval_success('di.donate(%s, [ %s ])'
102 % (repr(path), ancil))
103 return len(ancil.split(','))
106 d._eval_integer('di.mkdir(%s)'
109 def _exists(d, path):
113 except OSError as oe:
114 if oe.errno != os.errno.ENOENT: raise oe
117 def _sock_dir(d, target_euid, target_root):
118 run_dir = '/run/user/%d' % target_euid
119 if d._exists(target_root + run_dir):
120 return run_dir + '/fishdescriptor'
123 pw = pwd.getpwuid(target_euid)
124 return pw.pw_dir + '/.fishdescriptor'
129 'cannot find good socket path - no /run/user/UID nor pw entry for target process euid %d'
134 # -> list of fds in our process
136 target_root = '/proc/%d/root' % d.pid
137 if not d._exists(target_root):
141 sockdir = d._sock_dir(euid, target_root)
144 sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d.pid)
146 our_sockname = target_root + sockname
152 try: os.remove(our_sockname)
153 except FileNotFoundError: pass
155 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
157 os.chmod(our_sockname, 666)
160 ancil_len = d.donate(sockname, fds)
161 (s2, dummy) = s.accept()
162 (msg, ancil, flags, sender) = s2.recvmsg(1, ancil_len)
165 unpack_fmt = '%di' % len(fds)
167 for clvl, ctype, cdata in ancil:
168 if clvl == socket.SOL_SOCKET and ctype == socket.SCM_RIGHTS:
169 assert(got_fds is None)
170 got_fds = struct.unpack_from(unpack_fmt, cdata)
173 if s is not None: s.close()
174 if s2 is not None: s2.close()
176 try: os.remove(our_sockname)
177 except FileNotFoundError: pass