4

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.

2 Answers 2

2
+50

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.

2
  • Thanks for your answer. Yeah, I checked on Fedora and did not see that periodic clean-up implemented. The only problem is that for my application it is very easy to set the sticky bit on the folder, but harder to set on the files (I am not controlling how/when those files are created). If periodic clean-up is ambiguous in that respect, I will have no other option but to find a way to set all sticky bits in all socket files. I will wait until the bounty expires before accepting and awarding, in case someone else wants to add something new. :-)
    – Peque
    Apr 12, 2017 at 7:24
  • @Peque Well, the spec explicitly defines a minimum file access age of 6 hours before clean up, so if you can guarantee that the socket is connected to at least once every 6 hours, you'll be safe. (The connect() system call updates the access time.) Or you can run a cron job like find "$XDG_RUNTIME_DIR/your_subdir/" -type s -exec chmod +t '{}' '+' every few hours to add the sticky bit for all sockets that don't have one yet.
    – cg909
    Apr 16, 2017 at 0:55
0

As the sticky bit has no affect on a non-directory, I would assume that it is a matter of the program doing the clean up.

The sticky bit must be being used to tell this program to leave the files alone. Therefore if you can not tell from the documentation, you will have to:

  • Find which program is doing the clean up, and read its source code.
  • Do an experiment: put several files/sockets in the directory, some with sticky bit set, some with it not set, see what happens. (Use dummy files: ones that are not being used for anything.)
2
  • It looks like the files will be removed at logout, no matter what. Apr 11, 2017 at 17:56
  • 1
    The XDG documentation says "The lifetime of the directory MUST be bound to the user being logged in." So all the files will be removed at logout if you aren't logged in with the same user in another session (via SSH or on another VT)
    – cg909
    Apr 11, 2017 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .