diff mercurial/posix.py @ 30291:3239e2fdd2e2

chgserver: extract utility to bind unix domain socket to long path This is common problem of using sockaddr_un.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 21 May 2016 16:52:04 +0900 (2016-05-21)
parents c7129ed280b8
children 4b1af1c867fa 5f33116cd787
line wrap: on
line diff
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -598,3 +598,18 @@
         return ''.join(chunks)
     finally:
         fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags)
+
+def bindunixsocket(sock, path):
+    """Bind the UNIX domain socket to the specified path"""
+    # 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(path)
+    bakwdfd = None
+    if dirname:
+        bakwdfd = os.open('.', os.O_DIRECTORY)
+        os.chdir(dirname)
+    sock.bind(basename)
+    if bakwdfd:
+        os.fchdir(bakwdfd)
+        os.close(bakwdfd)