-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwr.py
executable file
·199 lines (143 loc) · 5.7 KB
/
wr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
import array
import os
import select
import signal
import socket
import threading
from concurrent.futures import ThreadPoolExecutor
EXECUTOR = ThreadPoolExecutor(
max_workers=10,
thread_name_prefix='ClientHandler',
)
SHUTDOWN_EVENT = threading.Event()
UNIX_SOCKET_FILENAME = 'fd-pass.sock'
def signal_handler(signum, frame):
frame.f_globals['sig_recv'] = signum
def client_handler(sock, addr, port):
thread_name = threading.current_thread().name
quit = False
print(f'Handling connection from ({addr}:{port}) in ({thread_name})')
poller = select.poll()
poller.register(sock, select.POLLIN)
while not quit:
for _, flags in poller.poll(0.1):
if flags & select.POLLNVAL:
print(f'Socket invalid for ({addr}:{port}) in ({thread_name})')
# Return here to avoid calling `close()` on a socket
# that is invalid.
return
elif flags & select.POLLERR:
print(f'Socket error for ({addr}:{port}) in ({thread_name})')
quit = True
elif flags & select.POLLHUP:
print(f'Socket hangup for ({addr}:{port}) in ({thread_name})')
quit = True
else:
data = sock.recv(1024)
if not data:
quit = True
break
sock.sendall(data)
if SHUTDOWN_EVENT.is_set():
print(f'Received shutdown event in ({thread_name})')
break
print(f'Closing connection from ({addr}:{port}) in ({thread_name})')
sock.close()
# NOTE(awiddersheim): Ripped this straight from the Python docs[1].
# Turns out Python 3.9 added this to the core socket library as
# `recv_fds()` as well as an accompanying `send_fds()`. There was also
# functionality built into the multiprocessing.reduction module in
# Python 3.4[3]. It is not very well documented however.
# [1]: https://docs.python.org/3/library/socket.html#socket.socket.recvmsg
# [2]: https://bugs.python.org/issue28724
# [3]: https://github.com/python/cpython/commit/84ed9a68bd9a13252b376b21a9167dabae254325
def recv_fds(sock, msglen, maxfds):
fds = array.array('i')
msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize))
for cmsg_level, cmsg_type, cmsg_data in ancdata:
if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS:
# Append data, ignoring any truncated integers at the end.
fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
return msg, list(fds)
qb_sock = None
quit = 0
sig_recv = 0
signal.signal(signal.SIGQUIT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
print(f'Starting Wide Receiver on PID ({os.getpid()})')
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.unlink(UNIX_SOCKET_FILENAME)
except FileNotFoundError:
pass
sock.bind(UNIX_SOCKET_FILENAME)
sock.listen(128)
print(f'Listening for messages on ({UNIX_SOCKET_FILENAME})')
poller = select.poll()
poller.register(sock, select.POLLIN)
while quit != 1:
if sig_recv != 0:
print(f'Processing signal ({signal.Signals(sig_recv).name})')
if sig_recv in [signal.SIGINT, signal.SIGQUIT, signal.SIGTERM]:
quit = 1
continue
for fd, flags in poller.poll(0.1):
if fd == sock.fileno():
if flags & (select.POLLNVAL | select.POLLERR | select.POLLHUP):
raise Exception(f'Unrecoverable error with ({UNIX_SOCKET_FILENAME}), error was ({flags})')
# TODO: Allow more than one connection here?
if qb_sock:
print(f'Already handling connection on ({UNIX_SOCKET_FILENAME})')
continue
qb_sock, _ = sock.accept()
print(f'Handling connection from ({UNIX_SOCKET_FILENAME})')
poller.register(qb_sock, select.POLLIN)
elif fd == qb_sock.fileno():
msg = None
fds = None
skip_close = False
if flags & select.POLLNVAL:
print(f'Socket invalid for QB on ({UNIX_SOCKET_FILENAME})')
skip_close = True
elif flags & select.POLLERR:
print(f'Socket error for QB on ({UNIX_SOCKET_FILENAME})')
elif flags & select.POLLHUP:
print(f'Socket hangup for QB on ({UNIX_SOCKET_FILENAME})')
else:
msg, fds = recv_fds(
sock=qb_sock,
msglen=1,
maxfds=1,
)
if not msg and not fds:
print(f'Connection for QB on ({UNIX_SOCKET_FILENAME}) closed')
poller.unregister(qb_sock)
if not skip_close:
qb_sock.close()
qb_sock = None
continue
if len(fds) != msg[0]:
print(f'Expected ({msg[0]}) file descriptors but got ({len(fds)})')
client_sock = socket.socket(
family=socket.AF_INET,
type=socket.SOCK_STREAM,
fileno=fds[0],
)
addr, port = client_sock.getpeername()
print(f'Handling connection from ({addr}:{port})')
client_sock.sendall(f'Hello from Wide Receiver on PID ({os.getpid()})!\n'.encode())
print(f'Starting client thread for ({addr}:{port})')
EXECUTOR.submit(
client_handler,
sock=client_sock,
addr=addr,
port=port,
)
print('Shutting down')
if qb_sock:
qb_sock.close()
sock.close()
SHUTDOWN_EVENT.set()
EXECUTOR.shutdown()