From d0a8426272eeb905ea2ac2aaadaf74b01fb47d48 Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Fri, 24 Aug 2018 10:47:11 -0400 Subject: [PATCH] Use $USER if getpwuid fails --- src/main.cc | 2 +- src/remote.cc | 15 +++++++++------ src/remote.hh | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.cc b/src/main.cc index cbbc9bee..15ece332 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 da441bd8..fafda9d5 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 19eb0071..fa04b006 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 {