Use $USER if getpwuid fails

This commit is contained in:
Matt Peterson 2018-08-24 10:47:11 -04:00
parent a91fc83bfe
commit d0a8426272
3 changed files with 11 additions and 8 deletions

View File

@ -956,7 +956,7 @@ int main(int argc, char* argv[])
const bool clear_sessions = (bool)parser.get_switch("clear"); const bool clear_sessions = (bool)parser.get_switch("clear");
if (list_sessions or clear_sessions) if (list_sessions or clear_sessions)
{ {
const String username = get_user_name(geteuid()); const String username = get_user_name();
const StringView tmp_dir = tmpdir(); const StringView tmp_dir = tmpdir();
for (auto& session : list_files(format("{}/kakoune/{}/", tmp_dir, for (auto& session : list_files(format("{}/kakoune/{}/", tmp_dir,
username))) username)))

View File

@ -519,9 +519,12 @@ void RemoteUI::exit(int status)
m_socket_watcher.events() |= FdEvents::Write; 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) 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); format_to(addr.sun_path, "{}/kakoune/{}", tmpdir(), session);
else else
format_to(addr.sun_path, "{}/kakoune/{}/{}", tmpdir(), format_to(addr.sun_path, "{}/kakoune/{}/{}", tmpdir(),
get_user_name(geteuid()), session); get_user_name(), session);
return addr; return addr;
} }
@ -815,9 +818,9 @@ bool Server::rename_session(StringView name)
throw runtime_error{format("invalid session name: '{}'", name)}; throw runtime_error{format("invalid session name: '{}'", name)};
String old_socket_file = format("{}/kakoune/{}/{}", tmpdir(), 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(), 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) if (rename(old_socket_file.c_str(), new_socket_file.c_str()) != 0)
return false; return false;
@ -831,7 +834,7 @@ void Server::close_session(bool do_unlink)
if (do_unlink) if (do_unlink)
{ {
String socket_file = format("{}/kakoune/{}/{}", tmpdir(), String socket_file = format("{}/kakoune/{}/{}", tmpdir(),
get_user_name(geteuid()), m_session); get_user_name(), m_session);
unlink(socket_file.c_str()); unlink(socket_file.c_str());
} }
m_listener->close_fd(); m_listener->close_fd();

View File

@ -44,7 +44,7 @@ private:
}; };
void send_command(StringView session, StringView command); void send_command(StringView session, StringView command);
String get_user_name(int uid); String get_user_name();
struct Server : public Singleton<Server> struct Server : public Singleton<Server>
{ {