12

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).

2
  • Directories usually always have the executable bits... They cannot be listed otherwise...
    – Macmade
    Commented Jan 9, 2013 at 14:15
  • 1
    Sorry, 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".)
    – Psychonaut
    Commented Jan 9, 2013 at 14:33

2 Answers 2

3

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 confstr with _CS_DARWIN_USER_TEMP_DIR, or the command getconf 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 TMPDIR on 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:

  1. DARWIN_USER_TEMP_DIR is an official solution which macOS itself automatically creates and uses (for example, the mktemp command uses it), while TemporaryItems isn't guaranteed to exist.

  2. A user's home directory might be network-mounted on MacOS servers/clusters (contrary to XDG_RUNTIME_DIR spec), while /private/var is always local.

8
  • 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_DIR in 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).
    – mtraceur
    Commented Jul 31, 2023 at 0:25
  • 1
    I would at least put it into a sub directory called runtime to decrease potential collisions, although it won't be safe, but at least safer. Also mkdir and chmod 0700 such dir to follow the guide lines from XDG. Commented Aug 16, 2023 at 0:53
  • 1
    On my macOS Ventura 13.6, /var is just a soft link to /private/var, so there's no distinction between system and per-user there. So /var/run technically 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
  • 1
    I don't have a TemporaryItems subdirectory in ~/Library/Caches as of macOS 15.0.01 Commented Oct 18, 2024 at 4:46
1

According to the spec:

$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.

3
  • 2
    Using "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.
    – Psychonaut
    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 ~/.run personally.
    – mattmc3
    Commented Nov 26, 2018 at 14:31
  • For context, in most Linux systems, XDG_RUNTIME_DIR is /run/user/$(id -u), where /run is a tmpfs (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).
    – mtraceur
    Commented Jul 30, 2023 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.