diff options
| author | Maxime Coste <mawww@kakoune.org> | 2017-12-04 14:27:54 +0800 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2017-12-04 15:19:57 +0800 |
| commit | 274367116a57c5788cc4b30861dcd94f54fba60e (patch) | |
| tree | 0a0f611802cfc3ea50f2590640256f85467742c1 /src/remote.cc | |
| parent | 7d32b3fc3689219d47fc236eefa4749f2e7c3c61 (diff) | |
Replace uses of getpwuid which is incompatible with static linking
Introduce a get_user_name function which parses '/etc/passwd' to find
the username associated with a user id.
Diffstat (limited to 'src/remote.cc')
| -rw-r--r-- | src/remote.cc | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/remote.cc b/src/remote.cc index ebd92f72..8cfbdc5b 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -528,6 +528,23 @@ void RemoteUI::exit(int status) m_socket_watcher.events() |= FdEvents::Write; } +String get_user_name(int uid) +{ + struct invalid_index : runtime_error + { + invalid_index(size_t i) : runtime_error{format("invalid index '{}'", i)} {} + }; + + MappedFile passwd{"/etc/passwd"}; + for (auto entry : (StringView)passwd | split<StringView>('\n')) + { + auto name_and_id = entry | split<StringView>(':') | elements<invalid_index, 0, 2>(); + if (str_to_int(name_and_id[1]) == uid) + return name_and_id[0].str(); + } + throw runtime_error(format("Cannot find user name for uid '{}'", uid)); +} + static sockaddr_un session_addr(StringView session) { sockaddr_un addr; @@ -539,7 +556,7 @@ static sockaddr_un session_addr(StringView session) format_to(addr.sun_path, "{}/kakoune/{}", tmpdir(), session); else format_to(addr.sun_path, "{}/kakoune/{}/{}", tmpdir(), - getpwuid(geteuid())->pw_name, session); + get_user_name(geteuid()), session); return addr; } @@ -813,9 +830,9 @@ bool Server::rename_session(StringView name) throw runtime_error{"Cannot create sessions with '/' in their name"}; String old_socket_file = format("{}/kakoune/{}/{}", tmpdir(), - getpwuid(geteuid())->pw_name, m_session); + get_user_name(geteuid()), m_session); String new_socket_file = format("{}/kakoune/{}/{}", tmpdir(), - getpwuid(geteuid())->pw_name, name); + get_user_name(geteuid()), name); if (rename(old_socket_file.c_str(), new_socket_file.c_str()) != 0) return false; @@ -829,7 +846,7 @@ void Server::close_session(bool do_unlink) if (do_unlink) { String socket_file = format("{}/kakoune/{}/{}", tmpdir(), - getpwuid(geteuid())->pw_name, m_session); + get_user_name(geteuid()), m_session); unlink(socket_file.c_str()); } m_listener->close_fd(); |
