gounix-socket

Is there any restrictions on unix socket path length


The application written in golang and is ran within Debian bookworm based docker container fails to listen for the unix socket with the error message listen unix [socket-path]: bind: invalid argument

And it looks the issue is reproduced only if the socket path is big enough (something bigger than about 100 chars). And it is not clear does it matter the full socket length or the base name it self as trying different combination looks to fail at different critical length values.

The name being used for the socket is a regular base64 string (without = marks) on some seed data

E.g it works well for

/var/lib/R9D5kgn3JfYgcpo3RX2nSzkQ2BIXXNSnJHpP4gR9D5kgn3JfYgcpo3RX2nSzkQ2BIXXNSnJHpP4g.sock

yet fails on

listen unix /var/lib/R9D5kgn3JfYgcpo3RX2nSzkQ2BIXXNSnJHpP4gR9D5kgn3JfYgcpo3RX2nSzkQ2BIXXNSnJHpP4gR9D5kgn3JfYgcpo3RX2nSzkQ2BIXXNSnJHpP4g.sock: bind: invalid argument

So


Solution

  • The maximum size for the name of a UNIX domain socket is restricted by the definition in the sockaddr_un structure. On Linux it is 108, which fits your observation of "something bigger than about 100 chars". From man unix:

    A UNIX domain socket address is represented in the following
    structure:
    
        struct sockaddr_un {
            sa_family_t sun_family;               /* AF_UNIX */
            char        sun_path[108];            /* Pathname */
        };
    
    The sun_family field always contains AF_UNIX.  On Linux, sun_path
    is 108 bytes in size ...