LLDB  mainline
HostInfoPosix.cpp
Go to the documentation of this file.
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 
10 #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 <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 
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 {
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 
122  return *g_user_id_resolver;
123 }
124 
125 uint32_t HostInfoPosix::GetUserID() { return getuid(); }
126 
127 uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
128 
130 
132 
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 
142  return ComputePathRelativeToLibrary(file_spec, "/bin");
143 }
144 
146  FileSpec temp_file("/opt/local/include/lldb");
147  file_spec.SetDirectory(temp_file.GetPath());
148  return true;
149 }
150 
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 }
HostInfoPosix.h
lldb_private::HostInfoPosix::GetEnvironmentVar
static bool GetEnvironmentVar(const std::string &var_name, std::string &var)
Definition: HostInfoPosix.cpp:151
lldb_private::HostInfoPosix::ComputeSupportExeDirectory
static bool ComputeSupportExeDirectory(FileSpec &file_spec)
Definition: HostInfoPosix.cpp:141
lldb_private::HostInfoPosix::ComputeHeaderDirectory
static bool ComputeHeaderDirectory(FileSpec &file_spec)
Definition: HostInfoPosix.cpp:145
lldb_private::HostInfoPosix::GetOSKernelDescription
static llvm::Optional< std::string > GetOSKernelDescription()
Definition: HostInfoPosix.cpp:41
lldb_private::HostInfoPosix::GetHostname
static bool GetHostname(std::string &s)
Definition: HostInfoPosix.cpp:31
lldb_private::HostInfoPosix::GetEffectiveGroupID
static uint32_t GetEffectiveGroupID()
Definition: HostInfoPosix.cpp:131
lldb_private::FileSpec
Definition: FileSpec.h:55
PasswdEntry::shell
std::string shell
Definition: HostInfoPosix.cpp:66
Log.h
lldb_private::HostInfoPosix::GetEffectiveUserID
static uint32_t GetEffectiveUserID()
Definition: HostInfoPosix.cpp:129
g_user_id_resolver
static llvm::ManagedStatic< PosixUserIDResolver > g_user_id_resolver
Definition: HostInfoPosix.cpp:119
lldb_private::HostInfoPosix::GetDefaultShell
static FileSpec GetDefaultShell()
Definition: HostInfoPosix.cpp:133
lldb_private::HostInfoBase::ComputePathRelativeToLibrary
static bool ComputePathRelativeToLibrary(FileSpec &file_spec, llvm::StringRef dir)
Definition: HostInfoBase.cpp:218
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
UserIDResolver.h
lldb_private::UserIDResolver
An abstract interface for things that know how to map numeric user/group IDs into names.
Definition: UserIDResolver.h:23
lldb_private::HostInfoPosix::GetGroupID
static uint32_t GetGroupID()
Definition: HostInfoPosix.cpp:127
lldb_private::HostInfoPosix::GetUserID
static uint32_t GetUserID()
Definition: HostInfoPosix.cpp:125
uint32_t
PasswdEntry::username
std::string username
Definition: HostInfoPosix.cpp:65
PasswdEntry
Definition: HostInfoPosix.cpp:64
lldb_private::FileSpec::SetDirectory
void SetDirectory(ConstString directory)
Directory string set accessor.
Definition: FileSpec.cpp:332
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::HostInfoPosix::GetUserIDResolver
static UserIDResolver & GetUserIDResolver()
Definition: HostInfoPosix.cpp:121
lldb_private::HostInfoPosix::GetPageSize
static size_t GetPageSize()
Definition: HostInfoPosix.cpp:29
GetPassword
static llvm::Optional< PasswdEntry > GetPassword(id_t uid)
Definition: HostInfoPosix.cpp:69
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:364