Mercurial > hg-stable
changeset 49286: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 | 34020d1f1635 |
children | 127d33e63d1a |
files | mercurial/cext/osutil.c mercurial/cext/osutil.pyi mercurial/chgserver.py mercurial/pure/osutil.py mercurial/util.py |
diffstat | 5 files changed, 11 insertions(+), 177 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/cext/osutil.c Mon Jun 06 13:58:32 2022 +0400 +++ b/mercurial/cext/osutil.c Thu Jun 02 23:57:56 2022 +0200 @@ -685,75 +685,6 @@ return NULL; } -/* - * recvfds() simply does not release GIL during blocking io operation because - * command server is known to be single-threaded. - * - * Old systems such as Solaris don't provide CMSG_LEN, msg_control, etc. - * Currently, recvfds() is not supported on these platforms. - */ -#ifdef CMSG_LEN - -static ssize_t recvfdstobuf(int sockfd, int **rfds, void *cbuf, size_t cbufsize) -{ - char dummy[1]; - struct iovec iov = {dummy, sizeof(dummy)}; - struct msghdr msgh = {0}; - struct cmsghdr *cmsg; - - msgh.msg_iov = &iov; - msgh.msg_iovlen = 1; - msgh.msg_control = cbuf; - msgh.msg_controllen = (socklen_t)cbufsize; - if (recvmsg(sockfd, &msgh, 0) < 0) - return -1; - - for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg; - cmsg = CMSG_NXTHDR(&msgh, cmsg)) { - if (cmsg->cmsg_level != SOL_SOCKET || - cmsg->cmsg_type != SCM_RIGHTS) - continue; - *rfds = (int *)CMSG_DATA(cmsg); - return (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); - } - - *rfds = cbuf; - return 0; -} - -static PyObject *recvfds(PyObject *self, PyObject *args) -{ - int sockfd; - int *rfds = NULL; - ssize_t rfdscount, i; - char cbuf[256]; - PyObject *rfdslist = NULL; - - if (!PyArg_ParseTuple(args, "i", &sockfd)) - return NULL; - - rfdscount = recvfdstobuf(sockfd, &rfds, cbuf, sizeof(cbuf)); - if (rfdscount < 0) - return PyErr_SetFromErrno(PyExc_OSError); - - rfdslist = PyList_New(rfdscount); - if (!rfdslist) - goto bail; - for (i = 0; i < rfdscount; i++) { - PyObject *obj = PyLong_FromLong(rfds[i]); - if (!obj) - goto bail; - PyList_SET_ITEM(rfdslist, i, obj); - } - return rfdslist; - -bail: - Py_XDECREF(rfdslist); - return NULL; -} - -#endif /* CMSG_LEN */ - /* allow disabling setprocname via compiler flags */ #ifndef SETPROCNAME_USE_NONE #if defined(HAVE_SETPROCTITLE) @@ -1285,10 +1216,6 @@ {"statfiles", (PyCFunction)statfiles, METH_VARARGS | METH_KEYWORDS, "stat a series of files or symlinks\n" "Returns None for non-existent entries and entries of other types.\n"}, -#ifdef CMSG_LEN - {"recvfds", (PyCFunction)recvfds, METH_VARARGS, - "receive list of file descriptors via socket\n"}, -#endif #ifndef SETPROCNAME_USE_NONE {"setprocname", (PyCFunction)setprocname, METH_VARARGS, "set process title (best-effort)\n"},
--- a/mercurial/cext/osutil.pyi Mon Jun 06 13:58:32 2022 +0400 +++ b/mercurial/cext/osutil.pyi Thu Jun 02 23:57:56 2022 +0200 @@ -18,7 +18,6 @@ def listdir(path: bytes, st: bool, skip: bool) -> List[stat]: ... def posixfile(name: AnyStr, mode: bytes, buffering: int) -> IO: ... def statfiles(names: Sequence[bytes]) -> List[stat]: ... -def recvfds(sockfd: int) -> List[int]: ... def setprocname(name: bytes) -> None: ... def getfstype(path: bytes) -> bytes: ... def getfsmountpoint(path: bytes) -> bytes: ...
--- a/mercurial/chgserver.py Mon Jun 06 13:58:32 2022 +0400 +++ b/mercurial/chgserver.py Thu Jun 02 23:57:56 2022 +0200 @@ -389,7 +389,17 @@ # tell client to sendmsg() with 1-byte payload, which makes it # distinctive from "attachio\n" command consumed by client.read() self.clientsock.sendall(struct.pack(b'>cI', b'I', 1)) - clientfds = util.recvfds(self.clientsock.fileno()) + + data, ancdata, msg_flags, address = self.clientsock.recvmsg(1, 256) + assert len(ancdata) == 1 + cmsg_level, cmsg_type, cmsg_data = ancdata[0] + assert cmsg_level == socket.SOL_SOCKET + assert cmsg_type == socket.SCM_RIGHTS + # memoryview.cast() was added in typeshed 61600d68772a, but pytype + # still complains + # pytype: disable=attribute-error + clientfds = memoryview(cmsg_data).cast('i').tolist() + # pytype: enable=attribute-error self.ui.log(b'chgserver', b'received fds: %r\n', clientfds) ui = self.ui
--- a/mercurial/pure/osutil.py Mon Jun 06 13:58:32 2022 +0400 +++ b/mercurial/pure/osutil.py Thu Jun 02 23:57:56 2022 +0200 @@ -9,7 +9,6 @@ import ctypes import ctypes.util import os -import socket import stat as statmod from ..pycompat import getattr @@ -71,102 +70,6 @@ if not pycompat.iswindows: posixfile = open - _SCM_RIGHTS = 0x01 - _socklen_t = ctypes.c_uint - - if pycompat.sysplatform.startswith(b'linux'): - # socket.h says "the type should be socklen_t but the definition of - # the kernel is incompatible with this." - _cmsg_len_t = ctypes.c_size_t - _msg_controllen_t = ctypes.c_size_t - _msg_iovlen_t = ctypes.c_size_t - else: - _cmsg_len_t = _socklen_t - _msg_controllen_t = _socklen_t - _msg_iovlen_t = ctypes.c_int - - class _iovec(ctypes.Structure): - _fields_ = [ - (u'iov_base', ctypes.c_void_p), - (u'iov_len', ctypes.c_size_t), - ] - - class _msghdr(ctypes.Structure): - _fields_ = [ - (u'msg_name', ctypes.c_void_p), - (u'msg_namelen', _socklen_t), - (u'msg_iov', ctypes.POINTER(_iovec)), - (u'msg_iovlen', _msg_iovlen_t), - (u'msg_control', ctypes.c_void_p), - (u'msg_controllen', _msg_controllen_t), - (u'msg_flags', ctypes.c_int), - ] - - class _cmsghdr(ctypes.Structure): - _fields_ = [ - (u'cmsg_len', _cmsg_len_t), - (u'cmsg_level', ctypes.c_int), - (u'cmsg_type', ctypes.c_int), - (u'cmsg_data', ctypes.c_ubyte * 0), - ] - - _libc = ctypes.CDLL(ctypes.util.find_library(u'c'), use_errno=True) - _recvmsg = getattr(_libc, 'recvmsg', None) - if _recvmsg: - _recvmsg.restype = getattr(ctypes, 'c_ssize_t', ctypes.c_long) - _recvmsg.argtypes = ( - ctypes.c_int, - ctypes.POINTER(_msghdr), - ctypes.c_int, - ) - else: - # recvmsg isn't always provided by libc; such systems are unsupported - def _recvmsg(sockfd, msg, flags): - raise NotImplementedError(b'unsupported platform') - - def _CMSG_FIRSTHDR(msgh): - if msgh.msg_controllen < ctypes.sizeof(_cmsghdr): - return - cmsgptr = ctypes.cast(msgh.msg_control, ctypes.POINTER(_cmsghdr)) - return cmsgptr.contents - - # The pure version is less portable than the native version because the - # handling of socket ancillary data heavily depends on C preprocessor. - # Also, some length fields are wrongly typed in Linux kernel. - def recvfds(sockfd): - """receive list of file descriptors via socket""" - dummy = (ctypes.c_ubyte * 1)() - iov = _iovec(ctypes.cast(dummy, ctypes.c_void_p), ctypes.sizeof(dummy)) - cbuf = ctypes.create_string_buffer(256) - msgh = _msghdr( - None, - 0, - ctypes.pointer(iov), - 1, - ctypes.cast(cbuf, ctypes.c_void_p), - ctypes.sizeof(cbuf), - 0, - ) - r = _recvmsg(sockfd, ctypes.byref(msgh), 0) - if r < 0: - e = ctypes.get_errno() - raise OSError(e, os.strerror(e)) - # assumes that the first cmsg has fds because it isn't easy to write - # portable CMSG_NXTHDR() with ctypes. - cmsg = _CMSG_FIRSTHDR(msgh) - if not cmsg: - return [] - if ( - cmsg.cmsg_level != socket.SOL_SOCKET - or cmsg.cmsg_type != _SCM_RIGHTS - ): - return [] - rfds = ctypes.cast(cmsg.cmsg_data, ctypes.POINTER(ctypes.c_int)) - rfdscount = ( - cmsg.cmsg_len - _cmsghdr.cmsg_data.offset - ) // ctypes.sizeof(ctypes.c_int) - return [rfds[i] for i in pycompat.xrange(rfdscount)] - else: import msvcrt
--- a/mercurial/util.py Mon Jun 06 13:58:32 2022 +0400 +++ b/mercurial/util.py Thu Jun 02 23:57:56 2022 +0200 @@ -156,11 +156,6 @@ SERVERROLE = compression.SERVERROLE CLIENTROLE = compression.CLIENTROLE -try: - recvfds = osutil.recvfds -except AttributeError: - pass - # Python compatibility _notset = object()