Does Mac OS X implement the XDG Base Directory Specification? If not, what's the equivalent of $XDG_RUNTIME_DIR? An application I help maintain needs a temporary directory in which binary (i.e., executable) files can be placed and executed. So this directory should be preferably unique to the user and must be guaranteed to allow files to have the executable bit set (if such a thing exists on the file systems used by Mac OS X).
-
Directories usually always have the executable bits... They cannot be listed otherwise...– MacmadeCommented Jan 9, 2013 at 14:15
-
1Sorry, I meant to say that the directory should allow files in it to have the exec bit set. In the *nix world many people mount their $TMPDIR as noexec as a security measure. (I believe that the XDG Base Directory Specification vaguely prohibits this practice for $XDG_RUNTIME_DIR, which states that that directory must be "fully-featured by the standards of the operating system", including "proper permissions".)– PsychonautCommented Jan 9, 2013 at 14:33
2 Answers
The closest macOS has to XDG_RUNTIME_DIR appears to be DARWIN_USER_TEMP_DIR.
⚠️ CAUTION
This is not an environment variable. This is a system "configuration variable": you'd use the function
confstrwith_CS_DARWIN_USER_TEMP_DIR, or the commandgetconf DARWIN_USER_TEMP_DIR, to get the value.
That will point to a private per-user temporary directory, which is cleared on every reboot. (Currently, this will be a path like /private/var/folders/.../T, but that's an implementation detail.)
💡 Tip
The default value of
TMPDIRon macOS points to the same directory.
From man confstr on a Mac:
_CS_DARWIN_USER_TEMP_DIR
Provides the path to a user's temporary items directory. The directory will be created it if does
not already exist. This directory is created with access permissions of 0700 and restricted by the
umask(2) of the calling process and is a good location for temporary files.
By default, files in this location may be cleaned (removed) by the system if they are not accessed
in 3 days.
This is almost the same as what the spec defines for XDG_RUNTIME_DIR.
📝 Note
Files in
XDG_RUNTIME_DIR"MAY be subjected to periodic clean-up" as well.
So the only difference is that Mac doesn't guarantee cleanup on log out. (For example, if you're putting a Unix domain socket in your runtime directory, when being portable to Mac you can't rely on that socket being automatically deleted for you if the user logs out but doesn't turn off their computer.)
This answer previously recommended using ~/Library/Caches/TemporaryItems, which seems to be an older convention. But:
DARWIN_USER_TEMP_DIRis an official solution which macOS itself automatically creates and uses (for example, themktempcommand uses it), whileTemporaryItemsisn't guaranteed to exist.A user's home directory might be network-mounted on MacOS servers/clusters (contrary to
XDG_RUNTIME_DIRspec), while/private/varis always local.
-
While it is true that MacOS does not follow XDG individual apps are free to do so. For example, the Elvish shell does so. Commented Jul 31, 2023 at 0:11
-
@KurtisRader In the case of the runtime directory, my answer is (the best approximation of) choosing to follow XDG on MacOS (or you can strictly follow the spec by acting as if there is no runtime directory in the absence of
XDG_RUNTIME_DIRin the environment, since last I checked XDG does not specify a default value for the runtime directory like it does for many of the others, but since MacOS doesn't put that in the environment, that just means you either always fail, always use a fallback, or just push this question of what the runtime directory should be rootwards in the call tree).– mtraceurCommented Jul 31, 2023 at 0:25 -
1I would at least put it into a sub directory called
runtimeto decrease potential collisions, although it won't be safe, but at least safer. Alsomkdirandchmod 0700such dir to follow the guide lines from XDG. Commented Aug 16, 2023 at 0:53 -
1On my macOS Ventura 13.6,
/varis just a soft link to/private/var, so there's no distinction between system and per-user there. So/var/runtechnically violates the standard's requirement that it be owned by the user and 700. I'm going to go with it anyway, because that's where everything else seems to store those kinds of files. If I run into any trouble, I'll switch it to/var/run/user/$USER. Commented Oct 25, 2023 at 20:56 -
1I don't have a TemporaryItems subdirectory in ~/Library/Caches as of macOS 15.0.01 Commented Oct 18, 2024 at 4:46
$XDG_RUNTIME_DIR defines the base directory relative to which user-specific non-essential runtime files and other file objects (such as sockets, named pipes, ...) should be stored. The directory MUST be owned by the user, and he MUST be the only one having read and write access to it. Its Unix access mode MUST be 0700.
There is not much other guidance given. I believe this to mean that you can put this in any folder in your $HOME. I posit that there's not much reason why this couldn't be the same as your $XDG_CACHE_HOME if the permissions are right and the data is cache-like.
-
2Using "any folder in your
$HOME" defeats the purpose of using specific, standardized locations such as those of the XDG Base Directory Specification. (The idea is to prevent the filesystem from being littered with hundreds of different cache, config, and temp folders, each used by a different application.) The question is about whether the designers of Mac OS X (now macOS) explicitly specified a particular location for temp executables, not whether it's safe to use one of your choosing. Commented Nov 26, 2018 at 7:29 -
Seeing as how it's not specified in the spec, it's apparently a free for all. Don't shoot the messenger. I don't like it either. I use
~/.runpersonally.– mattmc3Commented Nov 26, 2018 at 14:31 -
For context, in most Linux systems,
XDG_RUNTIME_DIRis/run/user/$(id -u), where/runis atmpfs(in-memory virtual file-system, which is a huge hint as to the intended purpose: your runtime dir is basically working memory or scratch space that just happens to be on the file system instead of in your process - it's not expected to survive between system restarts, user logins, and (although this is less obvious) even executions of your program).– mtraceurCommented Jul 30, 2023 at 19:03