1 //===-- HostInfoPosix.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Host/posix/HostInfoPosix.h" 10 #include "lldb/Utility/Log.h" 11 #include "lldb/Utility/UserIDResolver.h" 12 13 #include "llvm/ADT/SmallString.h" 14 #include "llvm/ADT/Twine.h" 15 #include "llvm/Support/Path.h" 16 #include "llvm/Support/raw_ostream.h" 17 18 #include <climits> 19 #include <cstdlib> 20 #include <grp.h> 21 #include <mutex> 22 #include <optional> 23 #include <pwd.h> 24 #include <sys/types.h> 25 #include <sys/utsname.h> 26 #include <unistd.h> 27 28 using namespace lldb_private; 29 30 size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); } 31 32 bool HostInfoPosix::GetHostname(std::string &s) { 33 char hostname[PATH_MAX]; 34 hostname[sizeof(hostname) - 1] = '\0'; 35 if (::gethostname(hostname, sizeof(hostname) - 1) == 0) { 36 s.assign(hostname); 37 return true; 38 } 39 return false; 40 } 41 42 std::optional<std::string> HostInfoPosix::GetOSKernelDescription() { 43 struct utsname un; 44 if (uname(&un) < 0) 45 return std::nullopt; 46 47 return std::string(un.version); 48 } 49 50 #ifdef __ANDROID__ 51 #include <android/api-level.h> 52 #endif 53 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21 54 #define USE_GETPWUID 55 #endif 56 57 namespace { 58 class PosixUserIDResolver : public UserIDResolver { 59 protected: 60 std::optional<std::string> DoGetUserName(id_t uid) override; 61 std::optional<std::string> DoGetGroupName(id_t gid) override; 62 }; 63 } // namespace 64 65 struct PasswdEntry { 66 std::string username; 67 std::string shell; 68 }; 69 70 static std::optional<PasswdEntry> GetPassword(id_t uid) { 71 #ifdef USE_GETPWUID 72 // getpwuid_r is missing from android-9 73 // The caller should provide some thread safety by making sure no one calls 74 // this function concurrently, because using getpwuid is ultimately not 75 // thread-safe as we don't know who else might be calling it. 76 if (auto *user_info_ptr = ::getpwuid(uid)) 77 return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell}; 78 #else 79 struct passwd user_info; 80 struct passwd *user_info_ptr = &user_info; 81 char user_buffer[PATH_MAX]; 82 size_t user_buffer_size = sizeof(user_buffer); 83 if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size, 84 &user_info_ptr) == 0 && 85 user_info_ptr) { 86 return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell}; 87 } 88 #endif 89 return std::nullopt; 90 } 91 92 std::optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { 93 if (std::optional<PasswdEntry> password = GetPassword(uid)) 94 return password->username; 95 return std::nullopt; 96 } 97 98 std::optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) { 99 #ifndef __ANDROID__ 100 char group_buffer[PATH_MAX]; 101 size_t group_buffer_size = sizeof(group_buffer); 102 struct group group_info; 103 struct group *group_info_ptr = &group_info; 104 // Try the threadsafe version first 105 if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size, 106 &group_info_ptr) == 0) { 107 if (group_info_ptr) 108 return std::string(group_info_ptr->gr_name); 109 } else { 110 // The threadsafe version isn't currently working for me on darwin, but the 111 // non-threadsafe version is, so I am calling it below. 112 group_info_ptr = ::getgrgid(gid); 113 if (group_info_ptr) 114 return std::string(group_info_ptr->gr_name); 115 } 116 #endif 117 return std::nullopt; 118 } 119 120 static llvm::ManagedStatic<PosixUserIDResolver> g_user_id_resolver; 121 122 UserIDResolver &HostInfoPosix::GetUserIDResolver() { 123 return *g_user_id_resolver; 124 } 125 126 uint32_t HostInfoPosix::GetUserID() { return getuid(); } 127 128 uint32_t HostInfoPosix::GetGroupID() { return getgid(); } 129 130 uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); } 131 132 uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); } 133 134 FileSpec HostInfoPosix::GetDefaultShell() { 135 if (const char *v = ::getenv("SHELL")) 136 return FileSpec(v); 137 if (std::optional<PasswdEntry> password = GetPassword(::geteuid())) 138 return FileSpec(password->shell); 139 return FileSpec("/bin/sh"); 140 } 141 142 bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) { 143 return ComputePathRelativeToLibrary(file_spec, "/bin"); 144 } 145 146 bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) { 147 FileSpec temp_file("/opt/local/include/lldb"); 148 file_spec.SetDirectory(temp_file.GetPath()); 149 return true; 150 } 151 152 bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name, 153 std::string &var) { 154 if (const char *pvar = ::getenv(var_name.c_str())) { 155 var = std::string(pvar); 156 return true; 157 } 158 return false; 159 } 160