LibCore: Default to a temp directory for runtime directory on non-Linux

/run/user/$uid is a systemd-ism, and is not present on other platforms.
This commit is contained in:
Andrew Kaster 2023-12-28 11:29:14 +01:00 committed by Andreas Kling
parent 053e4d5e64
commit 0c025c7d7e
Notes: sideshowbarker 2024-07-17 10:05:47 +09:00

View file

@ -12,6 +12,7 @@
#include <AK/StringBuilder.h>
#include <LibCore/SessionManagement.h>
#include <LibCore/StandardPaths.h>
#include <LibCore/System.h>
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
@ -109,9 +110,22 @@ ErrorOr<ByteString> StandardPaths::runtime_directory()
builder.append("/Library/Application Support"sv);
#elif defined(AK_OS_HAIKU)
builder.append("/boot/system/var/shared_memory"sv);
#else
#elif defined(AK_OS_LINUX)
auto uid = getuid();
builder.appendff("/run/user/{}", uid);
#else
// Just create a directory in /tmp that's owned by us with 0700
auto uid = getuid();
builder.appendff("/tmp/runtime_{}", uid);
auto error_or_stat = System::stat(builder.string_view());
if (error_or_stat.is_error()) {
MUST(System::mkdir(builder.string_view(), 0700));
} else {
auto stat = error_or_stat.release_value();
VERIFY(S_ISDIR(stat.st_mode));
if ((stat.st_mode & 0777) != 0700)
warnln("{} has unexpected mode flags {}", builder.string_view(), stat.st_mode);
}
#endif
return LexicalPath::canonicalized_path(builder.to_byte_string());