Rework session directory logic

Do not use a shared kakoune/ directory for all users to avoid the
complexity of having to set the sticky bit on that dir, resolve the
session directoy only once by using a static variable and an
immediately evaluated lambda.

This fixes an annoyance whenver using `su` and having Kakoune refuse
to start due to XDG_RUNTIME_DIR still being set.
This commit is contained in:
Maxime Coste 2021-05-01 15:29:50 +10:00
parent 7090be59df
commit db9ef82398
2 changed files with 15 additions and 23 deletions

View File

@ -587,28 +587,20 @@ String get_user_name()
return getenv("USER"); return getenv("USER");
} }
String session_directory() const String& session_directory()
{ {
static String session_dir = [] {
StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"); StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
if (xdg_runtime_dir.empty()) if (not xdg_runtime_dir.empty())
return format("{}/kakoune/{}", tmpdir(), get_user_name()); {
else if (struct stat st; stat(xdg_runtime_dir.zstr(), &st) == 0 && st.st_uid == geteuid())
return format("{}/kakoune", xdg_runtime_dir); return format("{}/kakoune", xdg_runtime_dir);
else
write_to_debug_buffer("XDG_RUNTIME_DIR does not exist or not owned by current user, using tmpdir");
} }
return format("{}/kakoune-{}", tmpdir(), get_user_name());
void make_session_directory() }();
{ return session_dir;
StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
if (xdg_runtime_dir.empty())
{
// set sticky bit on the shared kakoune directory
make_directory(format("{}/kakoune", tmpdir()), 01777);
}
else if (struct stat st;
stat(xdg_runtime_dir.zstr(), &st) == 0 && st.st_uid != geteuid())
throw runtime_error("XDG_RUNTIME_DIR is not owned by current user");
make_directory(session_directory(), 0711);
} }
String session_path(StringView session) String session_path(StringView session)
@ -863,7 +855,7 @@ Server::Server(String session_name, bool is_daemon)
fcntl(listen_sock, F_SETFD, FD_CLOEXEC); fcntl(listen_sock, F_SETFD, FD_CLOEXEC);
sockaddr_un addr = session_addr(m_session); sockaddr_un addr = session_addr(m_session);
make_session_directory(); make_directory(session_directory(), 0711);
// Do not give any access to the socket to other users by default // Do not give any access to the socket to other users by default
auto old_mask = umask(0077); auto old_mask = umask(0077);

View File

@ -45,7 +45,7 @@ private:
void send_command(StringView session, StringView command); void send_command(StringView session, StringView command);
String get_user_name(); String get_user_name();
String session_directory(); const String& session_directory();
String session_path(StringView session); String session_path(StringView session);
struct Server : public Singleton<Server> struct Server : public Singleton<Server>