10#include "lldb/Host/Config.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/Support/Path.h"
18#include "llvm/Support/raw_ostream.h"
29#include <sys/utsname.h>
35struct HostInfoPosixFields {
36 llvm::once_flag m_os_version_once_flag;
37 llvm::VersionTuple m_os_version;
42 static HostInfoPosixFields *
g_fields =
new HostInfoPosixFields();
43 assert(
g_fields &&
"Missing call to Initialize?");
44 llvm::call_once(
g_fields->m_os_version_once_flag, []() {
49 llvm::StringRef release = un.release;
52 release = release.substr(0, release.find_first_not_of(
"0123456789."));
53 g_fields->m_os_version.tryParse(release);
63 hostname[
sizeof(hostname) - 1] =
'\0';
64 if (::gethostname(hostname,
sizeof(hostname) - 1) == 0) {
76 return std::string(un.version);
81 ::memset(&un, 0,
sizeof(utsname));
86 return std::string(un.release);
90#include <android/api-level.h>
92#if defined(__ANDROID_API__) && __ANDROID_API__ < 21
99 std::optional<std::string> DoGetUserName(id_t uid)
override;
100 std::optional<std::string> DoGetGroupName(id_t gid)
override;
115 if (
auto *user_info_ptr = ::getpwuid(uid))
116 return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell};
118 struct passwd user_info;
119 struct passwd *user_info_ptr = &user_info;
121 size_t user_buffer_size =
sizeof(user_buffer);
122 if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size,
123 &user_info_ptr) == 0 &&
125 return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell};
131std::optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) {
132 if (std::optional<PasswdEntry> password =
GetPassword(uid))
137std::optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) {
140 size_t group_buffer_size =
sizeof(group_buffer);
141 struct group group_info;
142 struct group *group_info_ptr = &group_info;
144 if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size,
145 &group_info_ptr) == 0) {
147 return std::string(group_info_ptr->gr_name);
151 group_info_ptr = ::getgrgid(gid);
153 return std::string(group_info_ptr->gr_name);
174 if (
const char *v = ::getenv(
"SHELL"))
176 if (std::optional<PasswdEntry> password =
GetPassword(::geteuid()))
185 file_spec.
SetDirectory(HostInfo::GetProgramFileSpec().GetDirectory());
190 FileSpec temp_file(
"/usr/" LLDB_INSTALL_LIBDIR_BASENAME
"/lldb/plugins");
200 const char *xdg_data_home = getenv(
"XDG_DATA_HOME");
201 if (xdg_data_home && xdg_data_home[0]) {
202 std::string user_plugin_dir(xdg_data_home);
203 user_plugin_dir +=
"/lldb";
211 FileSpec temp_file(
"/opt/local/include/lldb");
218 if (
const char *pvar = ::getenv(var_name.c_str())) {
219 var = std::string(pvar);
static HostInfoBaseFields * g_fields
static llvm::ManagedStatic< PosixUserIDResolver > g_user_id_resolver
static std::optional< PasswdEntry > GetPassword(id_t uid)
bool IsEmpty() const
Test for empty string.
void SetDirectory(ConstString directory)
Directory string set accessor.
const ConstString & GetDirectory() const
Directory string const get accessor.
bool IsAbsolute() const
Returns true if the filespec represents an absolute path.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
static bool ComputePathRelativeToLibrary(FileSpec &file_spec, llvm::StringRef dir)
static uint32_t GetUserID()
static std::optional< std::string > GetOSBuildString()
static bool ComputeSupportExeDirectory(FileSpec &file_spec)
static bool GetHostname(std::string &s)
static bool ComputeSystemPluginsDirectory(FileSpec &file_spec)
static std::optional< std::string > GetOSKernelDescription()
static FileSpec GetDefaultShell()
static uint32_t GetEffectiveUserID()
static llvm::VersionTuple GetOSVersion()
static bool GetEnvironmentVar(const std::string &var_name, std::string &var)
static size_t GetPageSize()
static uint32_t GetGroupID()
static bool ComputeUserPluginsDirectory(FileSpec &file_spec)
static uint32_t GetEffectiveGroupID()
static UserIDResolver & GetUserIDResolver()
static bool ComputeHeaderDirectory(FileSpec &file_spec)
An abstract interface for things that know how to map numeric user/group IDs into names.
A class that represents a running process on the host machine.