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