Search code examples
linuxsocketsunix-socket

How to use unix domain socket without creating a socket file


In Linux, is there any example of using AF_LOCAL (unix domain sockets) to communicate between processes (IPC) without using a file? (on a read only filesystem)

I must use a Unix Domain socket, but I don't have file create/write access on the system.

Thank you in advance.


Solution

  • You can create a unix domain socket with an "abstract socket address". Simply make the first character of the sun_path string in the sockaddr_un you pass to bind be '\0'. After this initial NUL, write a string to the remainder of sun_path and pad it out to UNIX_PATH_MAX with NULs (or anything else).

    Sockets created this way will not have any filesystem entry, but instead will be placed into an invisible system-wide socket namespace. The socket name is not a null-terminated string; it's a UNIX_PATH_MAX length string starting with a NUL, and any other NULs have no special significance. So it's vitally important to pad out that name, or you'll put extra uninitialized memory garbage into that name, with unexpected results. By convention, this is generally done with NUL pads, but it's up to you.

    For more information, consult unix(7), and specifically the part on abstract socket addresses. A fully worked example can also be found here.