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.

alk
  • 69,737
  • 10
  • 105
  • 255
  • related: [Idiomatic location for file based sockets on Debian systems](https://unix.stackexchange.com/q/88083/366064) on Unix & Linux StackExchange – FObersteiner Jun 28 '23 at 11:32

3 Answers3

48

The FHS says: /run

(It used to be /var/run.)

JB.
  • 40,344
  • 12
  • 79
  • 106
  • 5
    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? – alk Sep 28 '11 at 09:13
  • 1
    @alk, I tend to just put them along with the application's dynamic data. Which might happen to be in `/tmp`. – JB. Sep 29 '11 at 07:27
  • 3
    A note from 2019: many distros are now migrating `/var/run` to `/run`. – 张实唯 Jun 01 '19 at 07:43
  • Please @JB. 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 info](https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s13.html) – caligari Aug 06 '19 at 08:47
  • Updated to FHS 3.0. @caligari: you know you're allowed to edit on your own, right? – JB. Sep 20 '19 at 10:05
  • `touch /run/hi` gives permission denied error, so I wonder how my user-mode app would create a domain socket there. – Andrew Arnott Jan 23 '23 at 22:39
  • @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. :) – Freso Mar 23 '23 at 09:29
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).

Hibou57
  • 6,870
  • 6
  • 52
  • 56
  • 3
    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//`. The platforms in use were Red-Hat-Linux, Suse-Linux and z/Linux. – alk Mar 31 '13 at 11:11
-3

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

  • 1
    Please add some explanation. Your answer is currently flagged "low quality" and might eventually be removed. – Adam B Mar 16 '16 at 17:13
  • 1
    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. – Toby Speight Mar 16 '16 at 18:21