@@ -276,7 +276,7 @@ async def create_unix_server(
276276 sock = None , backlog = 100 , ssl = None ,
277277 ssl_handshake_timeout = None ,
278278 ssl_shutdown_timeout = None ,
279- start_serving = True , cleanup_socket = True ):
279+ start_serving = True , cleanup_socket = True , mode = None ):
280280 if isinstance (ssl , bool ):
281281 raise TypeError ('ssl argument must be an SSLContext or None' )
282282
@@ -294,6 +294,9 @@ async def create_unix_server(
294294 'path and sock can not be specified at the same time' )
295295
296296 path = os .fspath (path )
297+ if mode is not None and path and path [0 ] in (0 , '\x00 ' ):
298+ raise ValueError (
299+ 'mode is not supported for abstract sockets' )
297300 sock = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
298301
299302 # Check for abstract socket. `str` and `bytes` paths are supported.
@@ -322,11 +325,26 @@ async def create_unix_server(
322325 except :
323326 sock .close ()
324327 raise
328+
329+ if mode is not None :
330+ # The socket cannot accept connections until listen() is
331+ # called, which happens later in Server._start_serving(),
332+ # so no connection can be accepted while the socket still
333+ # has the default permissions.
334+ try :
335+ os .chmod (path , mode )
336+ except :
337+ sock .close ()
338+ raise
325339 else :
326340 if sock is None :
327341 raise ValueError (
328342 'path was not specified, and no sock specified' )
329343
344+ if mode is not None :
345+ raise ValueError (
346+ 'mode is only meaningful with path' )
347+
330348 if (sock .family != socket .AF_UNIX or
331349 sock .type != socket .SOCK_STREAM ):
332350 raise ValueError (
@@ -658,19 +676,19 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
658676 # On AIX, the reader trick (to be notified when the read end of the
659677 # socket is closed) only works for sockets. On other platforms it
660678 # works for pipes and sockets. (Exception: OS X 10.4? Issue #19294.)
661- # On macOS, the trick misfires for named FIFOs (but not for pipes
662- # created with os.pipe(), which have st_nlink == 0): the write end
663- # polls as readable whenever unread data sits in the FIFO, and no
679+ # On macOS and Solaris , the trick misfires for named FIFOs (but not for
680+ # pipes created with os.pipe(), which have st_nlink == 0): the write
681+ # end polls as readable whenever unread data sits in the FIFO, and no
664682 # event is delivered when the read end is closed, so it can only
665- # ever report a false disconnection (gh-145030). The same xnu
683+ # ever report a false disconnection (gh-145030). The same XNU
666684 # behaviour applies on iOS/tvOS/watchOS (sys.platform is not
667685 # "darwin" there).
668- is_named_fifo_on_apple = (
669- sys .platform in {"darwin" , "ios" , "tvos" , "watchos" }
686+ is_named_fifo_without_close_event = (
687+ sys .platform in {"darwin" , "ios" , "tvos" , "watchos" , "sunos5" }
670688 and is_fifo and pipe_stat .st_nlink > 0 )
671689 if is_socket or (is_fifo
672690 and not sys .platform .startswith ("aix" )
673- and not is_named_fifo_on_apple ):
691+ and not is_named_fifo_without_close_event ):
674692 # only start reading when connection_made() has been called
675693 self ._loop .call_soon (self ._loop ._add_reader ,
676694 self ._fileno , self ._read_ready )
0 commit comments