Mercurial > hg
comparison mercurial/chgserver.py @ 49275:c6a3243567b6
chg: replace mercurial.util.recvfds() by simpler pure Python implementation
On Python 3, we have socket.socket.recvmsg(). This makes it possible to receive
FDs in pure Python code. The new code behaves like the previous
implementations, except that it’s more strict about the format of the ancillary
data. This works because we know in which format the FDs are passed.
Because the code is (and always has been) specific to chg (payload is 1 byte,
number of passed FDs is limited) and we now have only one implementation and
the code is very short, I decided to stop exposing a function in
mercurial.util.
Note on terminology: The SCM_RIGHTS mechanism is used to share open file
descriptions to another process over a socket. The sending side passes an array
of file descriptors and the receiving side receives an array of file
descriptors. The file descriptors are different in general on both sides but
refer to the same open file descriptions. The two terms are often conflated,
even in the official documentation. That’s why I used “FD” above, which could
mean both “file descriptor” and “file description”.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Thu, 02 Jun 2022 23:57:56 +0200 |
parents | cd51d4957b28 |
children | 55886050a583 |
comparison
equal
deleted
inserted
replaced
49273:34020d1f1635 | 49275:c6a3243567b6 |
---|---|
387 channels except cresult will no longer be used | 387 channels except cresult will no longer be used |
388 """ | 388 """ |
389 # tell client to sendmsg() with 1-byte payload, which makes it | 389 # tell client to sendmsg() with 1-byte payload, which makes it |
390 # distinctive from "attachio\n" command consumed by client.read() | 390 # distinctive from "attachio\n" command consumed by client.read() |
391 self.clientsock.sendall(struct.pack(b'>cI', b'I', 1)) | 391 self.clientsock.sendall(struct.pack(b'>cI', b'I', 1)) |
392 clientfds = util.recvfds(self.clientsock.fileno()) | 392 |
393 data, ancdata, msg_flags, address = self.clientsock.recvmsg(1, 256) | |
394 assert len(ancdata) == 1 | |
395 cmsg_level, cmsg_type, cmsg_data = ancdata[0] | |
396 assert cmsg_level == socket.SOL_SOCKET | |
397 assert cmsg_type == socket.SCM_RIGHTS | |
398 # memoryview.cast() was added in typeshed 61600d68772a, but pytype | |
399 # still complains | |
400 # pytype: disable=attribute-error | |
401 clientfds = memoryview(cmsg_data).cast('i').tolist() | |
402 # pytype: enable=attribute-error | |
393 self.ui.log(b'chgserver', b'received fds: %r\n', clientfds) | 403 self.ui.log(b'chgserver', b'received fds: %r\n', clientfds) |
394 | 404 |
395 ui = self.ui | 405 ui = self.ui |
396 ui.flush() | 406 ui.flush() |
397 self._saveio() | 407 self._saveio() |