Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place Unix Domain (AF_UNIX) sockets' end points (files)?

Is there a convention where to place the 'files' representing the end points to Unix Domain Sockets?

I tend to put them to /tmp/some-application-specific-subdir-name/, but I wonder if there is a more common place.

The background is, that POSIX is not clear about the maximum path length to access such 'files':

The size of sun_path has intentionally been left undefined. This is because different implementations use different sizes. For example, 4.3 BSD uses a size of 108, and 4.4 BSD uses a size of 104. Since most implementations originate from BSD versions, the size is typically in the range 92 to 108.

Applications should not assume a particular length for sun_path or assume that it can hold {_POSIX_PATH_MAX} bytes (256).

So this "restriction" on the path's length should be kept out of the application's file/path name configurations.

like image 438
alk Avatar asked Sep 28 '11 08:09

alk


People also ask

Where do I put a domain socket in UNIX?

They are to be stored in /run/ according to the Filesystem Hierarchy Standard (FHS).

What is Unix domain socket path?

UNIX domain sockets are named with UNIX paths. For example, a socket might be named /tmp/foo. UNIX domain sockets communicate only between processes on a single host.

How are UNIX domain sockets implemented?

Unix sockets are created by socket sys call (while FIFO created by mkfifo). If you need client socket, you call connect, passing it server socket address. If you need server socket, you can bind to assign its address. While, for FIFO open call is used.

How do I connect to a Unix socket?

Create a socket with the socket() system call. Connect the socket to the address of the server using the connect() system call. Send and receive data. There are a number of ways to do this, but the simplest way is to use the read() and write() system calls.


2 Answers

The FHS says: /run

(It used to be /var/run.)

like image 53
JB. Avatar answered Sep 24 '22 09:09

JB.


On Ubuntu, the netstat command shows UNIX Domain Sockets in multiple different places. Some, however a few, in /var/run, as JB suggested; most of them in @/tmp/… (I believe the @ designate abstract names, which is Linux specific), and some others in various application's specific places. So in practice, the most common location seems to be in /tmp, at least on Ubuntu, which is a rather common platform. Note the /tmp location particularly makes sense here, as UDS has to be created by each bind and to be deleted afterwards (either when the socket is closed, or when the application exit, or when the application starts the next time and before its next invocation to bind).

like image 26
Hibou57 Avatar answered Sep 25 '22 09:09

Hibou57