Mercurial > public > mercurial-scm > hg
comparison mercurial/pure/osutil.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 | 642e31cb55f0 |
children | 18c8c18993f0 |
comparison
equal
deleted
inserted
replaced
49273:34020d1f1635 | 49275:c6a3243567b6 |
---|---|
7 | 7 |
8 | 8 |
9 import ctypes | 9 import ctypes |
10 import ctypes.util | 10 import ctypes.util |
11 import os | 11 import os |
12 import socket | |
13 import stat as statmod | 12 import stat as statmod |
14 | 13 |
15 from ..pycompat import getattr | 14 from ..pycompat import getattr |
16 from .. import ( | 15 from .. import ( |
17 encoding, | 16 encoding, |
69 | 68 |
70 | 69 |
71 if not pycompat.iswindows: | 70 if not pycompat.iswindows: |
72 posixfile = open | 71 posixfile = open |
73 | 72 |
74 _SCM_RIGHTS = 0x01 | |
75 _socklen_t = ctypes.c_uint | |
76 | |
77 if pycompat.sysplatform.startswith(b'linux'): | |
78 # socket.h says "the type should be socklen_t but the definition of | |
79 # the kernel is incompatible with this." | |
80 _cmsg_len_t = ctypes.c_size_t | |
81 _msg_controllen_t = ctypes.c_size_t | |
82 _msg_iovlen_t = ctypes.c_size_t | |
83 else: | |
84 _cmsg_len_t = _socklen_t | |
85 _msg_controllen_t = _socklen_t | |
86 _msg_iovlen_t = ctypes.c_int | |
87 | |
88 class _iovec(ctypes.Structure): | |
89 _fields_ = [ | |
90 (u'iov_base', ctypes.c_void_p), | |
91 (u'iov_len', ctypes.c_size_t), | |
92 ] | |
93 | |
94 class _msghdr(ctypes.Structure): | |
95 _fields_ = [ | |
96 (u'msg_name', ctypes.c_void_p), | |
97 (u'msg_namelen', _socklen_t), | |
98 (u'msg_iov', ctypes.POINTER(_iovec)), | |
99 (u'msg_iovlen', _msg_iovlen_t), | |
100 (u'msg_control', ctypes.c_void_p), | |
101 (u'msg_controllen', _msg_controllen_t), | |
102 (u'msg_flags', ctypes.c_int), | |
103 ] | |
104 | |
105 class _cmsghdr(ctypes.Structure): | |
106 _fields_ = [ | |
107 (u'cmsg_len', _cmsg_len_t), | |
108 (u'cmsg_level', ctypes.c_int), | |
109 (u'cmsg_type', ctypes.c_int), | |
110 (u'cmsg_data', ctypes.c_ubyte * 0), | |
111 ] | |
112 | |
113 _libc = ctypes.CDLL(ctypes.util.find_library(u'c'), use_errno=True) | |
114 _recvmsg = getattr(_libc, 'recvmsg', None) | |
115 if _recvmsg: | |
116 _recvmsg.restype = getattr(ctypes, 'c_ssize_t', ctypes.c_long) | |
117 _recvmsg.argtypes = ( | |
118 ctypes.c_int, | |
119 ctypes.POINTER(_msghdr), | |
120 ctypes.c_int, | |
121 ) | |
122 else: | |
123 # recvmsg isn't always provided by libc; such systems are unsupported | |
124 def _recvmsg(sockfd, msg, flags): | |
125 raise NotImplementedError(b'unsupported platform') | |
126 | |
127 def _CMSG_FIRSTHDR(msgh): | |
128 if msgh.msg_controllen < ctypes.sizeof(_cmsghdr): | |
129 return | |
130 cmsgptr = ctypes.cast(msgh.msg_control, ctypes.POINTER(_cmsghdr)) | |
131 return cmsgptr.contents | |
132 | |
133 # The pure version is less portable than the native version because the | |
134 # handling of socket ancillary data heavily depends on C preprocessor. | |
135 # Also, some length fields are wrongly typed in Linux kernel. | |
136 def recvfds(sockfd): | |
137 """receive list of file descriptors via socket""" | |
138 dummy = (ctypes.c_ubyte * 1)() | |
139 iov = _iovec(ctypes.cast(dummy, ctypes.c_void_p), ctypes.sizeof(dummy)) | |
140 cbuf = ctypes.create_string_buffer(256) | |
141 msgh = _msghdr( | |
142 None, | |
143 0, | |
144 ctypes.pointer(iov), | |
145 1, | |
146 ctypes.cast(cbuf, ctypes.c_void_p), | |
147 ctypes.sizeof(cbuf), | |
148 0, | |
149 ) | |
150 r = _recvmsg(sockfd, ctypes.byref(msgh), 0) | |
151 if r < 0: | |
152 e = ctypes.get_errno() | |
153 raise OSError(e, os.strerror(e)) | |
154 # assumes that the first cmsg has fds because it isn't easy to write | |
155 # portable CMSG_NXTHDR() with ctypes. | |
156 cmsg = _CMSG_FIRSTHDR(msgh) | |
157 if not cmsg: | |
158 return [] | |
159 if ( | |
160 cmsg.cmsg_level != socket.SOL_SOCKET | |
161 or cmsg.cmsg_type != _SCM_RIGHTS | |
162 ): | |
163 return [] | |
164 rfds = ctypes.cast(cmsg.cmsg_data, ctypes.POINTER(ctypes.c_int)) | |
165 rfdscount = ( | |
166 cmsg.cmsg_len - _cmsghdr.cmsg_data.offset | |
167 ) // ctypes.sizeof(ctypes.c_int) | |
168 return [rfds[i] for i in pycompat.xrange(rfdscount)] | |
169 | |
170 | 73 |
171 else: | 74 else: |
172 import msvcrt | 75 import msvcrt |
173 | 76 |
174 _kernel32 = ctypes.windll.kernel32 # pytype: disable=module-attr | 77 _kernel32 = ctypes.windll.kernel32 # pytype: disable=module-attr |