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

3

41

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.

Shirline answered 28/9, 2011 at 8:27 Comment(1)
related: Idiomatic location for file based sockets on Debian systems on Unix & Linux StackExchangeChangeup
V
48

The FHS says: /run

(It used to be /var/run.)

Villosity answered 28/9, 2011 at 8:43 Comment(7)
Thanks for the reference to FHS, as I missed it. Anyway writing to /var/run normaly isn't possible for ordinary users, as it's not for the application in question. So, is there an alternative to /var/run?Shirline
@alk, I tend to just put them along with the application's dynamic data. Which might happen to be in /tmp.Villosity
A note from 2019: many distros are now migrating /var/run to /run.Bullock
Please @Villosity update the answer: from 2015 FHS 3.0: "These functions have been moved to /run; this directory exists to ensure compatibility with systems and software using an older version of this specification." Further infoCivilize
Updated to FHS 3.0. @caligari: you know you're allowed to edit on your own, right?Villosity
touch /run/hi gives permission denied error, so I wonder how my user-mode app would create a domain socket there.Manille
@AndrewArnott Look at /run/user/$(id -u)/ instead of /run/. If you use systemd that directory should exist and be owned by your login’s user:group. :)Doloroso
F
8

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).

Foliose answered 30/3, 2013 at 9:32 Comment(1)
Thanks for your suggestions. The design decision was taken to make the base path for the UDS files configurable and have it default to /tmp/, so the files would go to /tmp/<app-specific-dir>/. The platforms in use were Red-Hat-Linux, Suse-Linux and z/Linux.Shirline
R
-3

In Android you can place it in /dev/socket/

Rolo answered 16/3, 2016 at 11:11 Comment(2)
Please add some explanation. Your answer is currently flagged "low quality" and might eventually be removed.Ala
While this may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.Derk

© 2022 - 2024 — McMap. All rights reserved.