You are not logged in.

#1 2011-10-12 18:43:27

Franek
Member
Registered: 2010-05-16
Posts: 100

Unix domain sockets: How to name them? And where to put them?

I am using unix sockets for inter-process communication in a C++/gtkmm project.

Are there naming conventions and conventions about where to place sockets in the filesystem? In all example code I found, dummies like /home/user/mysocket were being used.

If there are not any conventions: Would it be sensible to put the sockets in /tmp/ and compose the names of the name of the program which uses the concerning socket, and its PID? (The latter also depends on my use case, of course; from that point of view, it seems sensible.)

Offline

#2 2011-10-12 18:54:34

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: Unix domain sockets: How to name them? And where to put them?

@Franek:  KDE applications place their sockets (through KDE API, of course) in "/tmp/ksocket-foo/bar__0", where "foo" is the user name, "bar" the application name and "0" an increasing counter (in case an application creates more than one socket).  Emacs places its server socket in a similar manner "/tmp/emacs1000/server", where "1000" is the UID.

I'd use emacs' name scheme, e.g. "/tmp/bar1000/what_the_socket_is_for", where "bar" is the application name again. 

However, do not place sockets directly in "/tmp/". For one thing, it just clutters this global directory, but more importantly it's unsafe, as every other user can inspect these sockets.  Create a directory within "/tmp/" and restrict its access rights to the owner (mode 700).  Thus other users can't inspect the sockets within that directory.  Of course, the access rights of the sockets themselves must also be limited.

Last edited by lunar (2011-10-12 18:59:32)

Offline

#3 2011-10-12 20:16:52

Franek
Member
Registered: 2010-05-16
Posts: 100

Re: Unix domain sockets: How to name them? And where to put them?

lunar, thanks for the input! I will make sure to use user-specific subdirectories of /tmp with appropriate permissions. The Emacs naming scheme looks fine, although I will have to add PIDs or something similar to the "what_the_socket_is_for" part, as each running instance of my app needs its own socket.

(Not marking as "solved" yet, as someone might still come up with a real standard.)

Offline

#4 2011-10-12 20:26:40

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: Unix domain sockets: How to name them? And where to put them?

@Franek:  If every instance of your application has its own socket, what is the point in having sockets at all?!

Offline

#5 2011-10-12 20:49:09

Franek
Member
Registered: 2010-05-16
Posts: 100

Re: Unix domain sockets: How to name them? And where to put them?

It is a suite of three different applications which need to communicate.

Last edited by Franek (2011-10-12 20:49:26)

Offline

#6 2011-10-12 22:23:11

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Unix domain sockets: How to name them? And where to put them?

/tmp should not be used at all for sockets. Create a directory under /run/$progname and put them there.

Offline

#7 2011-10-12 22:52:38

Franek
Member
Registered: 2010-05-16
Posts: 100

Re: Unix domain sockets: How to name them? And where to put them?

falconindy, why should sockets not go in /tmp? And why do you recommend /run rather than /var/run?

Offline

#8 2011-10-12 23:27:13

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Unix domain sockets: How to name them? And where to put them?

- Data in /tmp has zero guarantee of persistence. Never use it for files that you would care about disappearing.
- /run replaces /var/run, as per FHS. Eventually in Arch, /var/run will only be a symlink to /run. Everyone who uses systemd in Arch currently has /run bind mounted onto /var/run.

Last edited by falconindy (2011-10-12 23:27:58)

Offline

#9 2011-10-13 08:58:44

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: Unix domain sockets: How to name them? And where to put them?

@falconindy:  A normal process can't create files in "/run/".

Offline

#10 2011-10-13 09:57:53

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Unix domain sockets: How to name them? And where to put them?

Well, not under sysvinit (systemd creates /run/user/$USER for this sort of thing.)... Of course, you have the tmpfiles.d framework at your disposal so that you can create arbitrary directories ownered to whomever you like.

Honestly, I don't see the problem with leaving the socket in /home/$USER (probably not right in the user's home).

Last edited by falconindy (2011-10-13 10:00:37)

Offline

#11 2011-10-13 10:06:35

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: Unix domain sockets: How to name them? And where to put them?

@falconindy:  I don't see, how tmpfiles could help here.  It is not known beforehand which users might possibly run the application, thus you can't create tmpfiles entries for its socket directory during installation.  And creating tmpfiles entries on the fly would require root privileges.

And after all, "/tmp/" does normally not just vanish in a puff of smoke while the system is running.  Or if it does, you've got other problems than missing sockets.  Surely, "/tmp/" isn't the right place for sockets of system services, but I don't see a problem with sockets of ordinary applications.

When creating sockets in "/home/", dangling sockets (caused e.g. by a crash of the application) are kept across reboots, and the application might fail to create the socket until the user manually removes the file.  This can't happen in "/tmp/", which is cleaned at boot.

Last edited by lunar (2011-10-13 10:15:05)

Offline

#12 2011-10-13 20:14:48

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: Unix domain sockets: How to name them? And where to put them?

you could also use $XDG_CACHE_HOME, i.e. ~/.local/share/<yourapp>/socketfile
See http://standards.freedesktop.org/basedi … atest.html


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#13 2011-10-14 16:24:52

Franek
Member
Registered: 2010-05-16
Posts: 100

Re: Unix domain sockets: How to name them? And where to put them?

Wow, I did not expect this to be so controversial. How can something so common and so old as sockets not have a standard place in the filesystem?

The idea of using $XDG_CACHE_HOME came to me as well, but it did not seem right to me: First because it is not usually cleaned automatically (at reboot), as lunar has pointed out; second because the standard says to use it for "user specific non-essential data files", which sockets are not: neither data files, nor necessarily non-essential.

Offline

#14 2011-10-14 16:31:26

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: Unix domain sockets: How to name them? And where to put them?

Franek wrote:

The idea of using $XDG_CACHE_HOME came to me as well, but it did not seem right to me: First because it is not usually cleaned automatically (at reboot), as lunar has pointed out; second because the standard says to use it for "user specific non-essential data files", which sockets are not: neither data files, nor necessarily non-essential.

That makes sense.  I never thought of it like that, while I should have.  Carry on.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

Board footer

Powered by FluxBB