chiark / gitweb /
Merge remote-tracking branch 'mariner/fishdescriptor'
[chiark-utils.git] / fishdescriptor / py / fishdescriptor / fish.py
1 # fish.py
2
3 # This file is part of chiark-utils, a collection of useful programs
4 # used on chiark.greenend.org.uk.
5 #
6 # This file is:
7 #  Copyright 2018 Citrix Systems Ltd
8 #
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.
12 #
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
16 # details.
17 #
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.
21
22 # python 3 only
23
24 import socket
25 import subprocess
26 import os
27 import pwd
28 import struct
29 import tempfile
30 import shutil
31 import sys
32
33 def _shuffle_fd3():
34     os.dup2(1,3)
35     os.dup2(2,1)
36
37 class Error(Exception): pass
38
39 class Donor():
40     def __init__(d, pid, debug=None):
41         d.pid = pid
42         if debug is None:
43             d._stderr = tempfile.TemporaryFile(mode='w+')
44         else:
45             d._stderr = None
46         d._sp = subprocess.Popen(
47             preexec_fn = _shuffle_fd3,
48             stdin = subprocess.PIPE,
49             stdout = subprocess.PIPE,
50             stderr = d._stderr,
51             close_fds = False,
52             args = ['gdb', '-p', str(pid), '-batch', '-ex',
53                     'python import fishdescriptor.indonor as id;'+
54                     ' id.DonorImplementation().eval_loop()'
55                 ]
56         )            
57
58     def _eval_integer(d, expr):
59         try:
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')
64             d._sp.stdin.flush()
65             l = d._sp.stdout.readline().rstrip(b'\n')
66             return int(l)
67         except Exception as e:
68             if d._stderr is not None:
69                 d._stderr.seek(0)
70                 shutil.copyfileobj(d._stderr, sys.stderr)
71                 d._stderr.seek(0)
72                 d._stderr.truncate()
73             raise e
74
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))
78
79     def _geteuid(d):
80         return d._eval_integer('di.geteuid()')
81
82     def _ancilmsg(d, fds):
83         perl_script = '''
84             use strict;
85             use Socket;
86             use Socket::MsgHdr;
87             my $fds = pack "i*", @ARGV;
88             my $m = Socket::MsgHdr::pack_cmsghdr SOL_SOCKET, SCM_RIGHTS, $fds;
89             print join ", ", unpack "C*", $m;
90         '''
91         ap = subprocess.Popen(
92             stdin = subprocess.DEVNULL,
93             stdout = subprocess.PIPE,
94             args = ['perl','-we',perl_script] + [str(x) for x in fds]
95         )
96         (output, dummy) = ap.communicate()
97         return output.decode('utf-8')
98
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(','))
104
105     def mkdir(d, path):
106         d._eval_integer('di.mkdir(%s)'
107                         % (repr(path)))
108
109     def _exists(d, path):
110         try:
111             os.stat(path)
112             return True
113         except OSError as oe:
114             if oe.errno != os.errno.ENOENT: raise oe
115             return False
116
117     def _sock_dir(d, target_euid):
118         run_dir = '/run/user/%d' % target_euid
119         if d._exists(run_dir):
120             return run_dir + '/fishdescriptor'
121
122         try:
123             pw = pwd.getpwuid(target_euid)
124             return pw.pw_dir + '/.fishdescriptor'
125         except KeyError:
126             pass
127
128         raise RuntimeError(
129  'cannot find good socket path - no /run/user/UID nor pw entry for target process euid %d'
130             % target_euid
131         )
132
133     def fish(d, fds):
134         # -> list of fds in our process
135
136         euid = d._geteuid()
137         sockdir = d._sock_dir(euid)
138         d.mkdir(sockdir)
139
140         sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d.pid)
141
142         target_root = '/proc/%d/root' % d.pid
143         if not d._exists(target_root):
144             target_root = ''
145
146         our_sockname = target_root + sockname
147
148         s = None
149         s2 = None
150
151         try:
152             try: os.remove(our_sockname)
153             except FileNotFoundError: pass
154
155             s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
156             s.bind(our_sockname)
157             s.listen(1)
158
159             ancil_len = d.donate(our_sockname, fds)
160             (s2, dummy) = s.accept()
161             (msg, ancil, flags, sender) = s2.recvmsg(1, ancil_len)
162
163             got_fds = None
164             unpack_fmt = '%di' % len(fds)
165
166             for clvl, ctype, cdata in ancil:
167                 if clvl == socket.SOL_SOCKET and ctype == socket.SCM_RIGHTS:
168                     assert(got_fds is None)
169                     got_fds = struct.unpack_from(unpack_fmt, cdata)
170
171         finally:
172             if s is not None: s.close()
173             if s2 is not None: s2.close()
174
175             try: os.remove(our_sockname)
176             except FileNotFoundError: pass
177
178         return list(got_fds)
179
180     def detach(d):
181         d._sp.stdin.close()