1 //===-- HostInfoPosix.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/posix/HostInfoPosix.h"
11 #include "lldb/Utility/Log.h"
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"
24 #include <sys/types.h>
27 using namespace lldb_private;
29 size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
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 struct hostent *h = ::gethostbyname(hostname);
46 #include <android/api-level.h>
48 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
53 static std::mutex s_getpwuid_lock;
56 const char *HostInfoPosix::LookupUserName(uint32_t uid,
57 std::string &user_name) {
59 // getpwuid_r is missing from android-9
60 // make getpwuid thread safe with a mutex
61 std::lock_guard<std::mutex> lock(s_getpwuid_lock);
62 struct passwd *user_info_ptr = ::getpwuid(uid);
64 user_name.assign(user_info_ptr->pw_name);
65 return user_name.c_str();
68 struct passwd user_info;
69 struct passwd *user_info_ptr = &user_info;
70 char user_buffer[PATH_MAX];
71 size_t user_buffer_size = sizeof(user_buffer);
72 if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size,
73 &user_info_ptr) == 0) {
75 user_name.assign(user_info_ptr->pw_name);
76 return user_name.c_str();
84 const char *HostInfoPosix::LookupGroupName(uint32_t gid,
85 std::string &group_name) {
87 char group_buffer[PATH_MAX];
88 size_t group_buffer_size = sizeof(group_buffer);
89 struct group group_info;
90 struct group *group_info_ptr = &group_info;
91 // Try the threadsafe version first
92 if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size,
93 &group_info_ptr) == 0) {
95 group_name.assign(group_info_ptr->gr_name);
96 return group_name.c_str();
99 // The threadsafe version isn't currently working for me on darwin, but the
100 // non-threadsafe version is, so I am calling it below.
101 group_info_ptr = ::getgrgid(gid);
102 if (group_info_ptr) {
103 group_name.assign(group_info_ptr->gr_name);
104 return group_name.c_str();
109 assert(false && "getgrgid_r() not supported on Android");
114 uint32_t HostInfoPosix::GetUserID() { return getuid(); }
116 uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
118 uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
120 uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
122 FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh"); }
124 bool HostInfoPosix::ComputePathRelativeToLibrary(FileSpec &file_spec,
125 llvm::StringRef dir) {
126 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
128 FileSpec lldb_file_spec = GetShlibDir();
132 std::string raw_path = lldb_file_spec.GetPath();
133 // drop library directory
134 llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
136 // Most Posix systems (e.g. Linux/*BSD) will attempt to replace a */lib with
137 // */bin as the base directory for helper exe programs. This will fail if
138 // the /lib and /bin directories are rooted in entirely different trees.
140 log->Printf("HostInfoPosix::ComputePathRelativeToLibrary() attempting to "
141 "derive the %s path from this path: %s",
142 dir.data(), raw_path.c_str());
144 if (!parent_path.empty()) {
145 // Now write in bin in place of lib.
146 raw_path = (parent_path + dir).str();
149 log->Printf("Host::%s() derived the bin path as: %s", __FUNCTION__,
153 log->Printf("Host::%s() failed to find /lib/liblldb within the shared "
154 "lib path, bailing on bin path construction",
157 file_spec.GetDirectory().SetString(raw_path);
158 return (bool)file_spec.GetDirectory();
161 bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
162 return ComputePathRelativeToLibrary(file_spec, "/bin");
165 bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {
166 FileSpec temp_file("/opt/local/include/lldb");
167 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
171 bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
173 if (const char *pvar = ::getenv(var_name.c_str())) {
174 var = std::string(pvar);