Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/posix/HostInfoPosix.cpp
Line
Count
Source (jump to first uncovered line)
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
9
#include "lldb/Host/posix/HostInfoPosix.h"
10
#include "lldb/Utility/Log.h"
11
#include "lldb/Utility/UserIDResolver.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
1.86k
size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
30
31
2
bool HostInfoPosix::GetHostname(std::string &s) {
32
2
  char hostname[PATH_MAX];
33
2
  hostname[sizeof(hostname) - 1] = '\0';
34
2
  if (::gethostname(hostname, sizeof(hostname) - 1) == 0) {
35
2
    s.assign(hostname);
36
2
    return true;
37
2
  }
38
0
  return false;
39
2
}
40
41
17
llvm::Optional<std::string> HostInfoPosix::GetOSKernelDescription() {
42
17
  struct utsname un;
43
17
  if (uname(&un) < 0)
44
0
    return llvm::None;
45
46
17
  return std::string(un.version);
47
17
}
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 {
65
  std::string username;
66
  std::string shell;
67
};
68
69
1.40k
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
1.40k
  struct passwd user_info;
79
1.40k
  struct passwd *user_info_ptr = &user_info;
80
1.40k
  char user_buffer[PATH_MAX];
81
1.40k
  size_t user_buffer_size = sizeof(user_buffer);
82
1.40k
  if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size,
83
1.40k
                   &user_info_ptr) == 0 &&
84
1.40k
      user_info_ptr) {
85
1.40k
    return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell};
86
1.40k
  }
87
0
#endif
88
0
  return llvm::None;
89
1.40k
}
90
91
3
llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) {
92
3
  if (llvm::Optional<PasswdEntry> password = GetPassword(uid))
93
3
    return password->username;
94
0
  return llvm::None;
95
3
}
96
97
2
llvm::Optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) {
98
2
#ifndef __ANDROID__
99
2
  char group_buffer[PATH_MAX];
100
2
  size_t group_buffer_size = sizeof(group_buffer);
101
2
  struct group group_info;
102
2
  struct group *group_info_ptr = &group_info;
103
  // Try the threadsafe version first
104
2
  if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size,
105
2
                   &group_info_ptr) == 0) {
106
2
    if (group_info_ptr)
107
2
      return std::string(group_info_ptr->gr_name);
108
2
  } 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
0
    group_info_ptr = ::getgrgid(gid);
112
0
    if (group_info_ptr)
113
0
      return std::string(group_info_ptr->gr_name);
114
0
  }
115
0
#endif
116
0
  return llvm::None;
117
2
}
118
119
static llvm::ManagedStatic<PosixUserIDResolver> g_user_id_resolver;
120
121
161
UserIDResolver &HostInfoPosix::GetUserIDResolver() {
122
161
  return *g_user_id_resolver;
123
161
}
124
125
0
uint32_t HostInfoPosix::GetUserID() { return getuid(); }
126
127
0
uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
128
129
6.56k
uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
130
131
0
uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
132
133
1.40k
FileSpec HostInfoPosix::GetDefaultShell() {
134
1.40k
  if (const char *v = ::getenv("SHELL"))
135
4
    return FileSpec(v);
136
1.40k
  if (llvm::Optional<PasswdEntry> password = GetPassword(::geteuid()))
137
1.40k
    return FileSpec(password->shell);
138
0
  return FileSpec("/bin/sh");
139
1.40k
}
140
141
0
bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
142
0
  return ComputePathRelativeToLibrary(file_spec, "/bin");
143
0
}
144
145
0
bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {
146
0
  FileSpec temp_file("/opt/local/include/lldb");
147
0
  file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
148
0
  return true;
149
0
}
150
151
bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
152
0
                                      std::string &var) {
153
0
  if (const char *pvar = ::getenv(var_name.c_str())) {
154
0
    var = std::string(pvar);
155
0
    return true;
156
0
  }
157
0
  return false;
158
0
}