Check if a path exceeds maximum for Unix domain socket

Operating systems limit the length of a path of a Unix domain socket. How can I check whether a particular path is within that limit?

Or, to put it another way, how can I check the maximum permitted length of a path of a Unix domain socket, on a Linux or Mac OS X system?

My use case here is for SSH multiplexing: if the ControlPath is too long, then SSH multiplexing won't work since it creates a unix domain sockets. I want to be able to check for a valid control path without having to actually start an ssh session and look for the error message.


Solution 1:

how can I check the maximum permitted length of a path of a Unix domain socket, on a Linux

On Linux, this length is usually defined as 108.

It is defined by the UNIX_PATH_MAX variable in the /usr/include/linux/un.h header file :

cat /usr/include/linux/un.h | grep "define UNIX_PATH_MAX"

#define UNIX_PATH_MAX   108

You could find further info here :

  • http://www.thomasstover.com/uds.html
  • http://man7.org/linux/man-pages/man7/unix.7.html

Solution 2:

On Mac OS X, According to the unix man page:

UNIX-domain addresses are variable-length filesystem pathnames of at most
104 characters.  The include file <sys/un.h> defines this address:

       struct sockaddr_un {
               u_char  sun_len;
               u_char  sun_family;
               char    sun_path[104];
       };

Here's a program that compiles on both Linux and OS X that will output the maximum length of the path of a unix domain socket.

#include <sys/un.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    struct sockaddr_un s;
    printf("%lu\n", sizeof(s.sun_path));
    return 0;
}

On Linux, it outputs 108, and on OS X, it outputs 104.