summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2021-05-01 15:29:50 +1000
committerMaxime Coste <mawww@kakoune.org>2021-05-01 15:29:50 +1000
commitdb9ef82398a08bdf985ff26bfb230fb0cd1221a5 (patch)
treedb638792e6bcea6457ef7c092d9b4fbfe7a29856 /src
parent7090be59df3f91f844f96db8b5ebd83154dccc72 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/remote.cc36
-rw-r--r--src/remote.hh2
2 files changed, 15 insertions, 23 deletions
diff --git a/src/remote.cc b/src/remote.cc
index 1b02251e..e51fda89 100644
--- a/src/remote.cc
+++ b/src/remote.cc
@@ -587,28 +587,20 @@ String get_user_name()
return getenv("USER");
}
-String session_directory()
+const String& session_directory()
{
- StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
- if (xdg_runtime_dir.empty())
- return format("{}/kakoune/{}", tmpdir(), get_user_name());
- else
- return format("{}/kakoune", xdg_runtime_dir);
-}
-
-void make_session_directory()
-{
- 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);
+ static String session_dir = [] {
+ StringView xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
+ if (not xdg_runtime_dir.empty())
+ {
+ if (struct stat st; stat(xdg_runtime_dir.zstr(), &st) == 0 && st.st_uid == geteuid())
+ 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());
+ }();
+ return session_dir;
}
String session_path(StringView session)
@@ -863,7 +855,7 @@ Server::Server(String session_name, bool is_daemon)
fcntl(listen_sock, F_SETFD, FD_CLOEXEC);
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
auto old_mask = umask(0077);
diff --git a/src/remote.hh b/src/remote.hh
index 661053a8..903e03f1 100644
--- a/src/remote.hh
+++ b/src/remote.hh
@@ -45,7 +45,7 @@ private:
void send_command(StringView session, StringView command);
String get_user_name();
-String session_directory();
+const String& session_directory();
String session_path(StringView session);
struct Server : public Singleton<Server>