Sticky bit and socket files in XDG_RUNTIME_DIR

permissionssocket

I am storing some Unix socket files under a sub-directory beneath $XDG_RUNTIME_DIR. On the documentation, I can read:

Files in this directory MAY be subjected to periodic clean-up. To ensure that your files are not removed, they should have their access time timestamp modified at least once every 6 hours of monotonic time or the 'sticky' bit should be set on the file.

I wonder if it is necessary to set the sticky bit on each socket file in order to avoid periodic clean-up or if it is sufficient to set the sticky bit in the sub-directory in which I am storing all the socket files.

Best Answer

The part of the XDG Base Directory specification you cited speaks about setting the sticky bit on files:

Files in this directory MAY be subjected to periodic clean-up. To ensure that your files are not removed, they should have their access time timestamp modified at least once every 6 hours of monotonic time or the 'sticky' bit should be set on the file.

The specification is somewhat ambiguous here as file can mean every filesystem entity, non-directory filesystem entity or regular file depending on context. But the sticky bit on directories has a special effect on Linux and is even named different when used on directories in the chmod(1) manpage:

The restricted deletion flag or sticky bit is a single bit, whose interpretation depends on the file type. For directories, it prevents unprivileged users from removing or renaming a file in the directory unless they own the file or the directory; this is called the restricted deletion flag for the directory, and is commonly found on world-writable directories like /tmp.

Because of this it's reasonable to assume that file in the XDG documentation in this context means non-directory filesystem entity.

But as the specification is not completely unambiguous it will depend on the implementation of the clean-up mechanism of your distribution. It looks like there is currently no such periodic clean-up at least on Fedora and on Linux Mint, but as this may change in the future and there is no telling how the distributions will interpret this part of the spec, it's safer to set it on every file/socket you want to exclude from periodic cleanups.

EDIT: For systemd based distributions, pam_systemd is responsible for managing $XDG_RUNTIME_DIR. It currently only performs creation on the first login and deletion on the last logout. Also systemd creates sockets in subdirectories of $XDG_RUNTIME_DIR and doesn't set the sticky bit on anything. This strongly suggests that at least no systemd based distribution implements periodic clean-up yet.

Related Question