Skip to content

Commit

Permalink
Rework session directory logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mawww committed May 1, 2021
1 parent 7090be5 commit db9ef82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
36 changes: 14 additions & 22 deletions src/remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/remote.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit db9ef82

Please sign in to comment.