/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 <optional> |
23 | | #include <pwd.h> |
24 | | #include <sys/types.h> |
25 | | #include <sys/utsname.h> |
26 | | #include <unistd.h> |
27 | | |
28 | | using namespace lldb_private; |
29 | | |
30 | 1.20k | size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); } |
31 | | |
32 | 2 | bool HostInfoPosix::GetHostname(std::string &s) { |
33 | 2 | char hostname[PATH_MAX]; |
34 | 2 | hostname[sizeof(hostname) - 1] = '\0'; |
35 | 2 | if (::gethostname(hostname, sizeof(hostname) - 1) == 0) { |
36 | 2 | s.assign(hostname); |
37 | 2 | return true; |
38 | 2 | } |
39 | 0 | return false; |
40 | 2 | } |
41 | | |
42 | 13 | std::optional<std::string> HostInfoPosix::GetOSKernelDescription() { |
43 | 13 | struct utsname un; |
44 | 13 | if (uname(&un) < 0) |
45 | 0 | return std::nullopt; |
46 | | |
47 | 13 | return std::string(un.version); |
48 | 13 | } |
49 | | |
50 | | #ifdef __ANDROID__ |
51 | | #include <android/api-level.h> |
52 | | #endif |
53 | | #if defined(__ANDROID_API__) && __ANDROID_API__ < 21 |
54 | | #define USE_GETPWUID |
55 | | #endif |
56 | | |
57 | | namespace { |
58 | | class PosixUserIDResolver : public UserIDResolver { |
59 | | protected: |
60 | | std::optional<std::string> DoGetUserName(id_t uid) override; |
61 | | std::optional<std::string> DoGetGroupName(id_t gid) override; |
62 | | }; |
63 | | } // namespace |
64 | | |
65 | | struct PasswdEntry { |
66 | | std::string username; |
67 | | std::string shell; |
68 | | }; |
69 | | |
70 | 687 | static std::optional<PasswdEntry> GetPassword(id_t uid) { |
71 | | #ifdef USE_GETPWUID |
72 | | // getpwuid_r is missing from android-9 |
73 | | // The caller should provide some thread safety by making sure no one calls |
74 | | // this function concurrently, because using getpwuid is ultimately not |
75 | | // thread-safe as we don't know who else might be calling it. |
76 | | if (auto *user_info_ptr = ::getpwuid(uid)) |
77 | | return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell}; |
78 | | #else |
79 | 687 | struct passwd user_info; |
80 | 687 | struct passwd *user_info_ptr = &user_info; |
81 | 687 | char user_buffer[PATH_MAX]; |
82 | 687 | size_t user_buffer_size = sizeof(user_buffer); |
83 | 687 | if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size, |
84 | 687 | &user_info_ptr) == 0 && |
85 | 687 | user_info_ptr) { |
86 | 687 | return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell}; |
87 | 687 | } |
88 | 0 | #endif |
89 | 0 | return std::nullopt; |
90 | 687 | } |
91 | | |
92 | 2 | std::optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { |
93 | 2 | if (std::optional<PasswdEntry> password = GetPassword(uid)) |
94 | 2 | return password->username; |
95 | 0 | return std::nullopt; |
96 | 2 | } |
97 | | |
98 | 1 | std::optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) { |
99 | 1 | #ifndef __ANDROID__ |
100 | 1 | char group_buffer[PATH_MAX]; |
101 | 1 | size_t group_buffer_size = sizeof(group_buffer); |
102 | 1 | struct group group_info; |
103 | 1 | struct group *group_info_ptr = &group_info; |
104 | | // Try the threadsafe version first |
105 | 1 | if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size, |
106 | 1 | &group_info_ptr) == 0) { |
107 | 1 | if (group_info_ptr) |
108 | 1 | return std::string(group_info_ptr->gr_name); |
109 | 1 | } else { |
110 | | // The threadsafe version isn't currently working for me on darwin, but the |
111 | | // non-threadsafe version is, so I am calling it below. |
112 | 0 | group_info_ptr = ::getgrgid(gid); |
113 | 0 | if (group_info_ptr) |
114 | 0 | return std::string(group_info_ptr->gr_name); |
115 | 0 | } |
116 | 0 | #endif |
117 | 0 | return std::nullopt; |
118 | 1 | } |
119 | | |
120 | | static llvm::ManagedStatic<PosixUserIDResolver> g_user_id_resolver; |
121 | | |
122 | 150 | UserIDResolver &HostInfoPosix::GetUserIDResolver() { |
123 | 150 | return *g_user_id_resolver; |
124 | 150 | } |
125 | | |
126 | 0 | uint32_t HostInfoPosix::GetUserID() { return getuid(); } |
127 | | |
128 | 0 | uint32_t HostInfoPosix::GetGroupID() { return getgid(); } |
129 | | |
130 | 6.32k | uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); } |
131 | | |
132 | 0 | uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); } |
133 | | |
134 | 689 | FileSpec HostInfoPosix::GetDefaultShell() { |
135 | 689 | if (const char *v = ::getenv("SHELL")) |
136 | 4 | return FileSpec(v); |
137 | 685 | if (std::optional<PasswdEntry> password = GetPassword(::geteuid())) |
138 | 685 | return FileSpec(password->shell); |
139 | 0 | return FileSpec("/bin/sh"); |
140 | 685 | } |
141 | | |
142 | 0 | bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) { |
143 | 0 | return ComputePathRelativeToLibrary(file_spec, "/bin"); |
144 | 0 | } |
145 | | |
146 | 0 | bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) { |
147 | 0 | FileSpec temp_file("/opt/local/include/lldb"); |
148 | 0 | file_spec.SetDirectory(temp_file.GetPath()); |
149 | 0 | return true; |
150 | 0 | } |
151 | | |
152 | | bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name, |
153 | 0 | std::string &var) { |
154 | 0 | if (const char *pvar = ::getenv(var_name.c_str())) { |
155 | 0 | var = std::string(pvar); |
156 | 0 | return true; |
157 | 0 | } |
158 | 0 | return false; |
159 | 0 | } |