1 //===-- HostInfoPosix.cpp ---------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/posix/HostInfoPosix.h"
11 #include "lldb/Utility/Log.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 <grp.h>
19 #include <limits.h>
20 #include <mutex>
21 #include <netdb.h>
22 #include <pwd.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 using namespace lldb_private;
28
GetPageSize()29 size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
30
GetHostname(std::string & s)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);
36 if (h)
37 s.assign(h->h_name);
38 else
39 s.assign(hostname);
40 return true;
41 }
42 return false;
43 }
44
45 #ifdef __ANDROID__
46 #include <android/api-level.h>
47 #endif
48 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
49 #define USE_GETPWUID
50 #endif
51
52 #ifdef USE_GETPWUID
53 static std::mutex s_getpwuid_lock;
54 #endif
55
LookupUserName(uint32_t uid,std::string & user_name)56 const char *HostInfoPosix::LookupUserName(uint32_t uid,
57 std::string &user_name) {
58 #ifdef USE_GETPWUID
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);
63 if (user_info_ptr) {
64 user_name.assign(user_info_ptr->pw_name);
65 return user_name.c_str();
66 }
67 #else
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) {
74 if (user_info_ptr) {
75 user_name.assign(user_info_ptr->pw_name);
76 return user_name.c_str();
77 }
78 }
79 #endif
80 user_name.clear();
81 return nullptr;
82 }
83
LookupGroupName(uint32_t gid,std::string & group_name)84 const char *HostInfoPosix::LookupGroupName(uint32_t gid,
85 std::string &group_name) {
86 #ifndef __ANDROID__
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) {
94 if (group_info_ptr) {
95 group_name.assign(group_info_ptr->gr_name);
96 return group_name.c_str();
97 }
98 } else {
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();
105 }
106 }
107 group_name.clear();
108 #else
109 assert(false && "getgrgid_r() not supported on Android");
110 #endif
111 return NULL;
112 }
113
GetUserID()114 uint32_t HostInfoPosix::GetUserID() { return getuid(); }
115
GetGroupID()116 uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
117
GetEffectiveUserID()118 uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
119
GetEffectiveGroupID()120 uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
121
GetDefaultShell()122 FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh"); }
123
ComputePathRelativeToLibrary(FileSpec & file_spec,llvm::StringRef dir)124 bool HostInfoPosix::ComputePathRelativeToLibrary(FileSpec &file_spec,
125 llvm::StringRef dir) {
126 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
127
128 FileSpec lldb_file_spec = GetShlibDir();
129 if (!lldb_file_spec)
130 return false;
131
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);
135
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.
139 if (log)
140 log->Printf("HostInfoPosix::ComputePathRelativeToLibrary() attempting to "
141 "derive the %s path from this path: %s",
142 dir.data(), raw_path.c_str());
143
144 if (!parent_path.empty()) {
145 // Now write in bin in place of lib.
146 raw_path = (parent_path + dir).str();
147
148 if (log)
149 log->Printf("Host::%s() derived the bin path as: %s", __FUNCTION__,
150 raw_path.c_str());
151 } else {
152 if (log)
153 log->Printf("Host::%s() failed to find /lib/liblldb within the shared "
154 "lib path, bailing on bin path construction",
155 __FUNCTION__);
156 }
157 file_spec.GetDirectory().SetString(raw_path);
158 return (bool)file_spec.GetDirectory();
159 }
160
ComputeSupportExeDirectory(FileSpec & file_spec)161 bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
162 return ComputePathRelativeToLibrary(file_spec, "/bin");
163 }
164
ComputeHeaderDirectory(FileSpec & file_spec)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());
168 return true;
169 }
170
GetEnvironmentVar(const std::string & var_name,std::string & var)171 bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
172 std::string &var) {
173 if (const char *pvar = ::getenv(var_name.c_str())) {
174 var = std::string(pvar);
175 return true;
176 }
177 return false;
178 }
179