chgserver: use relative path at socket.bind

Before this patch, if the server address is long, the server will fail to
listen and throw the error:

  socket.error: AF_UNIX path too long

It is because AF_UNIX path usually has a very short length limit (107 chars on
common platforms, see sys/un.h).

This patch addresses the issue by using relative path instead. Therefore the
directory length does not matter. It helps run tests with chg using a long
$TMPDIR.
This commit is contained in:
Jun Wu 2016-04-04 03:17:59 +01:00
parent c99168df8c
commit abecf92520

View File

@ -611,11 +611,22 @@ class AutoExitMixIn: # use old-style to comply with SocketServer design
# use a unique temp address so we can stat the file and do ownership
# check later
tempaddress = _tempaddress(self.server_address)
self.socket.bind(tempaddress)
self._socketstat = os.stat(tempaddress)
# use relative path instead of full path at bind() if possible, since
# AF_UNIX path has very small length limit (107 chars) on common
# platforms (see sys/un.h)
dirname, basename = os.path.split(tempaddress)
bakwdfd = None
if dirname:
bakwdfd = os.open('.', os.O_DIRECTORY)
os.chdir(dirname)
self.socket.bind(basename)
self._socketstat = os.stat(basename)
# rename will replace the old socket file if exists atomically. the
# old server will detect ownership change and exit.
util.rename(tempaddress, self.server_address)
util.rename(basename, self.server_address)
if bakwdfd:
os.fchdir(bakwdfd)
os.close(bakwdfd)
def issocketowner(self):
try: