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