HostInfoPosix.cpp revision 360660
1//===-- HostInfoPosix.cpp ---------------------------------------*- C++ -*-===//
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
9#include "lldb/Host/posix/HostInfoPosix.h"
10#include "lldb/Utility/UserIDResolver.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
27using namespace lldb_private;
28
29size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
30
31bool 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
52namespace {
53class PosixUserIDResolver : public UserIDResolver {
54protected:
55  llvm::Optional<std::string> DoGetUserName(id_t uid) override;
56  llvm::Optional<std::string> DoGetGroupName(id_t gid) override;
57};
58} // namespace
59
60llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) {
61#ifdef USE_GETPWUID
62  // getpwuid_r is missing from android-9
63  // UserIDResolver provides some thread safety by making sure noone calls this
64  // function concurrently, but using getpwuid is ultimately not thread-safe as
65  // we don't know who else might be calling it.
66  struct passwd *user_info_ptr = ::getpwuid(uid);
67  if (user_info_ptr)
68    return std::string(user_info_ptr->pw_name);
69#else
70  struct passwd user_info;
71  struct passwd *user_info_ptr = &user_info;
72  char user_buffer[PATH_MAX];
73  size_t user_buffer_size = sizeof(user_buffer);
74  if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size,
75                   &user_info_ptr) == 0 &&
76      user_info_ptr) {
77    return std::string(user_info_ptr->pw_name);
78  }
79#endif
80  return llvm::None;
81}
82
83llvm::Optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) {
84#ifndef __ANDROID__
85  char group_buffer[PATH_MAX];
86  size_t group_buffer_size = sizeof(group_buffer);
87  struct group group_info;
88  struct group *group_info_ptr = &group_info;
89  // Try the threadsafe version first
90  if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size,
91                   &group_info_ptr) == 0) {
92    if (group_info_ptr)
93      return std::string(group_info_ptr->gr_name);
94  } else {
95    // The threadsafe version isn't currently working for me on darwin, but the
96    // non-threadsafe version is, so I am calling it below.
97    group_info_ptr = ::getgrgid(gid);
98    if (group_info_ptr)
99      return std::string(group_info_ptr->gr_name);
100  }
101#else
102  assert(false && "getgrgid_r() not supported on Android");
103#endif
104  return llvm::None;
105}
106
107static llvm::ManagedStatic<PosixUserIDResolver> g_user_id_resolver;
108
109UserIDResolver &HostInfoPosix::GetUserIDResolver() {
110  return *g_user_id_resolver;
111}
112
113uint32_t HostInfoPosix::GetUserID() { return getuid(); }
114
115uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
116
117uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
118
119uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
120
121FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh"); }
122
123bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
124  return ComputePathRelativeToLibrary(file_spec, "/bin");
125}
126
127bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {
128  FileSpec temp_file("/opt/local/include/lldb");
129  file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
130  return true;
131}
132
133bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
134                                      std::string &var) {
135  if (const char *pvar = ::getenv(var_name.c_str())) {
136    var = std::string(pvar);
137    return true;
138  }
139  return false;
140}
141