diff options
Diffstat (limited to 'source/Host/posix/HostInfoPosix.cpp')
-rw-r--r-- | source/Host/posix/HostInfoPosix.cpp | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/source/Host/posix/HostInfoPosix.cpp b/source/Host/posix/HostInfoPosix.cpp index f300e22e9e5c..63cc5dc65e00 100644 --- a/source/Host/posix/HostInfoPosix.cpp +++ b/source/Host/posix/HostInfoPosix.cpp @@ -7,8 +7,8 @@ //===----------------------------------------------------------------------===// #include "lldb/Host/posix/HostInfoPosix.h" -#include "lldb/Utility/UserIDResolver.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/UserIDResolver.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" @@ -18,7 +18,6 @@ #include <grp.h> #include <limits.h> #include <mutex> -#include <netdb.h> #include <pwd.h> #include <stdlib.h> #include <sys/types.h> @@ -32,11 +31,7 @@ bool HostInfoPosix::GetHostname(std::string &s) { char hostname[PATH_MAX]; hostname[sizeof(hostname) - 1] = '\0'; if (::gethostname(hostname, sizeof(hostname) - 1) == 0) { - struct hostent *h = ::gethostbyname(hostname); - if (h) - s.assign(h->h_name); - else - s.assign(hostname); + s.assign(hostname); return true; } return false; @@ -57,15 +52,19 @@ protected: }; } // namespace -llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { +struct PasswdEntry { + std::string username; + std::string shell; +}; + +static llvm::Optional<PasswdEntry> GetPassword(id_t uid) { #ifdef USE_GETPWUID // getpwuid_r is missing from android-9 - // UserIDResolver provides some thread safety by making sure noone calls this - // function concurrently, but using getpwuid is ultimately not thread-safe as - // we don't know who else might be calling it. - struct passwd *user_info_ptr = ::getpwuid(uid); - if (user_info_ptr) - return std::string(user_info_ptr->pw_name); + // The caller should provide some thread safety by making sure no one calls + // this function concurrently, because using getpwuid is ultimately not + // thread-safe as we don't know who else might be calling it. + if (auto *user_info_ptr = ::getpwuid(uid)) + return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell}; #else struct passwd user_info; struct passwd *user_info_ptr = &user_info; @@ -74,12 +73,18 @@ llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size, &user_info_ptr) == 0 && user_info_ptr) { - return std::string(user_info_ptr->pw_name); + return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell}; } #endif return llvm::None; } +llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { + if (llvm::Optional<PasswdEntry> password = GetPassword(uid)) + return password->username; + return llvm::None; +} + llvm::Optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) { #ifndef __ANDROID__ char group_buffer[PATH_MAX]; @@ -98,8 +103,6 @@ llvm::Optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) { if (group_info_ptr) return std::string(group_info_ptr->gr_name); } -#else - assert(false && "getgrgid_r() not supported on Android"); #endif return llvm::None; } @@ -118,7 +121,13 @@ uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); } uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); } -FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh"); } +FileSpec HostInfoPosix::GetDefaultShell() { + if (const char *v = ::getenv("SHELL")) + return FileSpec(v); + if (llvm::Optional<PasswdEntry> password = GetPassword(::geteuid())) + return FileSpec(password->shell); + return FileSpec("/bin/sh"); +} bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) { return ComputePathRelativeToLibrary(file_spec, "/bin"); |