Creating an XDG_RUNTIME_DIR

2026-04-02 #kiss #linux #unix

Many applications rely on a runtime directory: a dedicated directory which serves as a rendezvous point for different components and for runtime files.

The full documentation for it is in the XDG base directory specification.

Prior art

Requirements

Concurrent sessions for the same user should share the same runtime directory. For example, when logging in via a physical seat on a host, and then starting an SSH session, they both need to use the same runtime directory path.

A native approach might use /tmp/run-$USER, but this is vulnerable to another user account creating a directory in that location, inadvertently controlling ownership of sensitive files. Likewise, another user simply creating a file there, blocking creating of the runtime dir. Using a deterministic location in a world-writeable location is insecure.

Approaches

All possible approaches have their caveats and compromises. We can only pick which compromise we prefer.

Additionally, PAM, setuid or a dedicated daemon all require root involvement in configuring or initialising a user session after user login. This kind of initialisation should be handled inside the user session: the root user should only be involved if strictly necessary.

Randomised location in /tmp

Of all the above approaches, I opted for using a non-deterministic (i.e., unpredictable) location in /tmp.

A “name file” somewhere in $HOME keeps the location of the current runtime directory. Upon login, a helper called via ~/.zprofile checks this file. If it contains the location of a directory owned by the current user with the right permission, it is used as the current $XDG_RUNTIME_DIR. Otherwise, a lock is taken, a new directory is created, and the name file is replaced. The lock ensures that two concurrent executions don’t overstep each other when replacing the name file.

This is simple enough, and can be implemented in a small, simple binary.

Have comments or want to discuss this topic?
Send an email to my public inbox: ~whynothugo/public-inbox@lists.sr.ht.
Reply privately by email: hugo@whynothugo.nl.

— § —