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