Is there a safe way to automatically remove a stray unix-socket files before it’s related services are run?
Solution 1:
Use tmpfs
, e.g. create the file inside /dev/shm
.
It is intended to appear as a mounted file system, but stored in volatile memory instead of a persistent storage device.
[emphasis mine]
In my Debian /run
is also tmpfs
and I can see few tools have created their sockets there. According to FHS /run
is a good place for a socket created by a system-wide service.
Run-time variable data: Information about the running system since last boot, e.g., currently logged-in users and running daemons. Files under this directory must be either removed or truncated at the beginning of the boot process; but this is not necessary on systems that provide this directory as a temporary filesystem (tmpfs).
In my Debian /run
belongs to root and its mode bits (permissions) are rwxr-xr-x
. Normal users cannot benefit from it.
On the other hand /dev/shm
is rwxrwxrwt
, anyone can use it. But since it's a "common land" (like /tmp
), few problems arise. Possibility of name conflicts is one of them. Two users may disturb each other even if their intentions are perfectly harmless.
Then there is /run/user/$uid
, also as tmpfs
:
used for storing files used by running processes for that user. […]
This directory is local to the system and only accessible by the target user. So applications looking to store their files locally no longer have to worry about access control.