chiark / gitweb /
fishdescriptor: sort out debugging output
[chiark-utils.git] / fishdescriptor / py / fishdescriptor / fish.py
1 # python 3 only
2
3 import socket
4 import subprocess
5 import os
6 import pwd
7 import struct
8 import tempfile
9 import shutil
10 import sys
11
12 def _shuffle_fd3():
13     os.dup2(1,3)
14     os.dup2(2,1)
15
16 class Donor():
17     def __init__(d, pid, debug=None):
18         d.pid = pid
19         if debug is None:
20             d._stderr = tempfile.TemporaryFile(mode='w+')
21         else:
22             d._stderr = None
23         d._sp = subprocess.Popen(
24             preexec_fn = _shuffle_fd3,
25             stdin = subprocess.PIPE,
26             stdout = subprocess.PIPE,
27             stderr = d._stderr,
28             close_fds = False,
29             args = ['gdb', '-p', str(pid), '-batch', '-ex',
30                     'python import fishdescriptor.indonor as id;'+
31                     ' id.DonorImplementation().eval_loop()'
32                 ]
33         )            
34
35     def _eval_integer(d, expr):
36         try:
37             l = d._sp.stdout.readline()
38             if l != b'!\n': raise RuntimeError("indonor said %s" % repr(l))
39             d._sp.stdin.write(expr.encode('utf-8') + b'\n')
40             d._sp.stdin.flush()
41             l = d._sp.stdout.readline().rstrip(b'\n')
42             return int(l)
43         except Exception as e:
44             if d._stderr is not None:
45                 d._stderr.seek(0)
46                 shutil.copyfileobj(d._stderr, sys.stderr)
47                 d._stderr.seek(0)
48                 d._stderr.truncate()
49             raise e
50
51     def _eval_success(d, expr):
52         r = d._eval_integer(expr)
53         if r != 1: raise RuntimeError("eval of %s gave %d" % (expr, r))
54
55     def _geteuid(d):
56         return d._eval_integer('di.geteuid()')
57
58     def _ancilmsg(d, fds):
59         perl_script = '''
60             use strict;
61             use Socket;
62             use Socket::MsgHdr;
63             my $fds = pack "i*", @ARGV;
64             my $m = Socket::MsgHdr::pack_cmsghdr SOL_SOCKET, SCM_RIGHTS, $fds;
65             print join ", ", unpack "C*", $m;
66         '''
67         ap = subprocess.Popen(
68             stdin = subprocess.DEVNULL,
69             stdout = subprocess.PIPE,
70             args = ['perl','-we',perl_script] + [str(x) for x in fds]
71         )
72         (output, dummy) = ap.communicate()
73         return output.decode('utf-8')
74
75     def donate(d, path, fds):
76         ancil = d._ancilmsg(fds)
77         d._eval_success('di.donate(%s, [ %s ])'
78                         % (repr(path), ancil))
79         return len(ancil.split(','))
80
81     def mkdir(d, path):
82         d._eval_integer('di.mkdir(%s)'
83                         % (repr(path)))
84
85     def _exists(d, path):
86         try:
87             os.stat(path)
88             return True
89         except OSError as oe:
90             if oe.errno != os.errno.ENOENT: raise oe
91             return False
92
93     def _sock_dir(d, target_euid):
94         run_dir = '/run/user/%d' % target_euid
95         if d._exists(run_dir):
96             return run_dir + '/fishdescriptor'
97
98         try:
99             pw = pwd.getpwuid(target_euid)
100             return pw.pw_dir + '/.fishdescriptor'
101         except KeyError:
102             pass
103
104         raise RuntimeError(
105  'cannot find good socket path - no /run/user/UID nor pw entry for target process euid %d'
106             % target_euid
107         )
108
109     def fish(d, fds):
110         # -> list of fds in our process
111
112         euid = d._geteuid()
113         sockdir = d._sock_dir(euid)
114         d.mkdir(sockdir)
115
116         sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d.pid)
117
118         target_root = '/proc/%d/root' % d.pid
119         if not d._exists(target_root):
120             target_root = ''
121
122         our_sockname = target_root + sockname
123
124         s = None
125         s2 = None
126
127         try:
128             try: os.remove(our_sockname)
129             except FileNotFoundError: pass
130
131             s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
132             s.bind(our_sockname)
133             s.listen(1)
134
135             ancil_len = d.donate(our_sockname, fds)
136             (s2, dummy) = s.accept()
137             (msg, ancil, flags, sender) = s2.recvmsg(1, ancil_len)
138
139             got_fds = None
140             unpack_fmt = '%di' % len(fds)
141
142             for clvl, ctype, cdata in ancil:
143                 if clvl == socket.SOL_SOCKET and ctype == socket.SCM_RIGHTS:
144                     assert(got_fds is None)
145                     got_fds = struct.unpack_from(unpack_fmt, cdata)
146
147         finally:
148             if s is not None: s.close()
149             if s2 is not None: s2.close()
150
151             try: os.remove(our_sockname)
152             except FileNotFoundError: pass
153
154         return list(got_fds)
155
156     def detach(d):
157         d._sp.stdin.close()