]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/posix/HostInfoPosix.cpp
Merge OpenSSL 1.0.2p.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / posix / HostInfoPosix.cpp
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 #if !defined(LLDB_DISABLE_PYTHON)
11 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
12 #endif
13
14 #include "lldb/Host/posix/HostInfoPosix.h"
15 #include "lldb/Utility/Log.h"
16
17 #include "clang/Basic/Version.h"
18 #include "clang/Config/config.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 #include <grp.h>
25 #include <limits.h>
26 #include <mutex>
27 #include <netdb.h>
28 #include <pwd.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33 using namespace lldb_private;
34
35 size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
36
37 bool HostInfoPosix::GetHostname(std::string &s) {
38   char hostname[PATH_MAX];
39   hostname[sizeof(hostname) - 1] = '\0';
40   if (::gethostname(hostname, sizeof(hostname) - 1) == 0) {
41     struct hostent *h = ::gethostbyname(hostname);
42     if (h)
43       s.assign(h->h_name);
44     else
45       s.assign(hostname);
46     return true;
47   }
48   return false;
49 }
50
51 #ifdef __ANDROID__
52 #include <android/api-level.h>
53 #endif
54 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
55 #define USE_GETPWUID
56 #endif
57
58 #ifdef USE_GETPWUID
59 static std::mutex s_getpwuid_lock;
60 #endif
61
62 const char *HostInfoPosix::LookupUserName(uint32_t uid,
63                                           std::string &user_name) {
64 #ifdef USE_GETPWUID
65   // getpwuid_r is missing from android-9
66   // make getpwuid thread safe with a mutex
67   std::lock_guard<std::mutex> lock(s_getpwuid_lock);
68   struct passwd *user_info_ptr = ::getpwuid(uid);
69   if (user_info_ptr) {
70     user_name.assign(user_info_ptr->pw_name);
71     return user_name.c_str();
72   }
73 #else
74   struct passwd user_info;
75   struct passwd *user_info_ptr = &user_info;
76   char user_buffer[PATH_MAX];
77   size_t user_buffer_size = sizeof(user_buffer);
78   if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size,
79                    &user_info_ptr) == 0) {
80     if (user_info_ptr) {
81       user_name.assign(user_info_ptr->pw_name);
82       return user_name.c_str();
83     }
84   }
85 #endif
86   user_name.clear();
87   return nullptr;
88 }
89
90 const char *HostInfoPosix::LookupGroupName(uint32_t gid,
91                                            std::string &group_name) {
92 #ifndef __ANDROID__
93   char group_buffer[PATH_MAX];
94   size_t group_buffer_size = sizeof(group_buffer);
95   struct group group_info;
96   struct group *group_info_ptr = &group_info;
97   // Try the threadsafe version first
98   if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size,
99                    &group_info_ptr) == 0) {
100     if (group_info_ptr) {
101       group_name.assign(group_info_ptr->gr_name);
102       return group_name.c_str();
103     }
104   } else {
105     // The threadsafe version isn't currently working for me on darwin, but the
106     // non-threadsafe version
107     // is, so I am calling it below.
108     group_info_ptr = ::getgrgid(gid);
109     if (group_info_ptr) {
110       group_name.assign(group_info_ptr->gr_name);
111       return group_name.c_str();
112     }
113   }
114   group_name.clear();
115 #else
116   assert(false && "getgrgid_r() not supported on Android");
117 #endif
118   return NULL;
119 }
120
121 uint32_t HostInfoPosix::GetUserID() { return getuid(); }
122
123 uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
124
125 uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
126
127 uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
128
129 FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh", false); }
130
131 bool HostInfoPosix::ComputePathRelativeToLibrary(FileSpec &file_spec,
132                                                  llvm::StringRef dir) {
133   Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
134
135   FileSpec lldb_file_spec;
136   if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
137     return false;
138
139   std::string raw_path = lldb_file_spec.GetPath();
140   // drop library directory
141   llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
142
143   // Most Posix systems (e.g. Linux/*BSD) will attempt to replace a */lib with
144   // */bin as the base directory for helper exe programs.  This will fail if the
145   // /lib and /bin directories are rooted in entirely different trees.
146   if (log)
147     log->Printf("HostInfoPosix::ComputePathRelativeToLibrary() attempting to "
148                 "derive the %s path from this path: %s",
149                 dir.data(), raw_path.c_str());
150
151   if (!parent_path.empty()) {
152     // Now write in bin in place of lib.
153     raw_path = (parent_path + dir).str();
154
155     if (log)
156       log->Printf("Host::%s() derived the bin path as: %s", __FUNCTION__,
157                   raw_path.c_str());
158   } else {
159     if (log)
160       log->Printf("Host::%s() failed to find /lib/liblldb within the shared "
161                   "lib path, bailing on bin path construction",
162                   __FUNCTION__);
163   }
164   file_spec.GetDirectory().SetString(raw_path);
165   return (bool)file_spec.GetDirectory();
166 }
167
168 bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
169   return ComputePathRelativeToLibrary(file_spec, "/bin");
170 }
171
172 bool HostInfoPosix::ComputeClangDirectory(FileSpec &file_spec) {
173   return ComputePathRelativeToLibrary(
174       file_spec, (llvm::Twine("/lib") + CLANG_LIBDIR_SUFFIX + "/clang/" +
175                   CLANG_VERSION_STRING)
176                      .str());
177 }
178
179 bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {
180   FileSpec temp_file("/opt/local/include/lldb", false);
181   file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
182   return true;
183 }
184
185 bool HostInfoPosix::ComputePythonDirectory(FileSpec &file_spec) {
186 #ifndef LLDB_DISABLE_PYTHON
187   FileSpec lldb_file_spec;
188   if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
189     return false;
190
191   char raw_path[PATH_MAX];
192   lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
193
194 #if defined(LLDB_PYTHON_RELATIVE_LIBDIR)
195   // Build the path by backing out of the lib dir, then building
196   // with whatever the real python interpreter uses.  (e.g. lib
197   // for most, lib64 on RHEL x86_64).
198   char python_path[PATH_MAX];
199   ::snprintf(python_path, sizeof(python_path), "%s/../%s", raw_path,
200              LLDB_PYTHON_RELATIVE_LIBDIR);
201
202   char final_path[PATH_MAX];
203   realpath(python_path, final_path);
204   file_spec.GetDirectory().SetCString(final_path);
205
206   return true;
207 #else
208   llvm::SmallString<256> python_version_dir;
209   llvm::raw_svector_ostream os(python_version_dir);
210   os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION
211      << "/site-packages";
212
213   // We may get our string truncated. Should we protect this with an assert?
214   ::strncat(raw_path, python_version_dir.c_str(),
215             sizeof(raw_path) - strlen(raw_path) - 1);
216
217   file_spec.GetDirectory().SetCString(raw_path);
218   return true;
219 #endif
220 #else
221   return false;
222 #endif
223 }
224
225 bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
226                                       std::string &var) {
227   if (const char *pvar = ::getenv(var_name.c_str())) {
228     var = std::string(pvar);
229     return true;
230   }
231   return false;
232 }