diff --git a/src/main.cc b/src/main.cc index cbbc9beeb..15ece3320 100644 --- a/src/main.cc +++ b/src/main.cc @@ -956,7 +956,7 @@ int main(int argc, char* argv[]) const bool clear_sessions = (bool)parser.get_switch("clear"); if (list_sessions or clear_sessions) { - const String username = get_user_name(geteuid()); + const String username = get_user_name(); const StringView tmp_dir = tmpdir(); for (auto& session : list_files(format("{}/kakoune/{}/", tmp_dir, username))) diff --git a/src/remote.cc b/src/remote.cc index da441bd87..fafda9d5a 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -519,9 +519,12 @@ void RemoteUI::exit(int status) m_socket_watcher.events() |= FdEvents::Write; } -String get_user_name(int uid) +String get_user_name() { - return getpwuid(uid)->pw_name; + auto pw = getpwuid(geteuid()); + if (pw) + return pw->pw_name; + return getenv("USER"); } static sockaddr_un session_addr(StringView session) @@ -535,7 +538,7 @@ static sockaddr_un session_addr(StringView session) format_to(addr.sun_path, "{}/kakoune/{}", tmpdir(), session); else format_to(addr.sun_path, "{}/kakoune/{}/{}", tmpdir(), - get_user_name(geteuid()), session); + get_user_name(), session); return addr; } @@ -815,9 +818,9 @@ bool Server::rename_session(StringView name) throw runtime_error{format("invalid session name: '{}'", name)}; String old_socket_file = format("{}/kakoune/{}/{}", tmpdir(), - get_user_name(geteuid()), m_session); + get_user_name(), m_session); String new_socket_file = format("{}/kakoune/{}/{}", tmpdir(), - get_user_name(geteuid()), name); + get_user_name(), name); if (rename(old_socket_file.c_str(), new_socket_file.c_str()) != 0) return false; @@ -831,7 +834,7 @@ void Server::close_session(bool do_unlink) if (do_unlink) { String socket_file = format("{}/kakoune/{}/{}", tmpdir(), - get_user_name(geteuid()), m_session); + get_user_name(), m_session); unlink(socket_file.c_str()); } m_listener->close_fd(); diff --git a/src/remote.hh b/src/remote.hh index 19eb00713..fa04b006f 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -44,7 +44,7 @@ private: }; void send_command(StringView session, StringView command); -String get_user_name(int uid); +String get_user_name(); struct Server : public Singleton {