1 //===-- HostInfoPosix.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/posix/HostInfoPosix.h"
10 #include "lldb/Utility/Log.h"
11 #include "lldb/Utility/UserIDResolver.h"
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"
24 #include <sys/types.h>
25 #include <sys/utsname.h>
28 using namespace lldb_private
;
30 size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
32 bool HostInfoPosix::GetHostname(std::string
&s
) {
33 char hostname
[PATH_MAX
];
34 hostname
[sizeof(hostname
) - 1] = '\0';
35 if (::gethostname(hostname
, sizeof(hostname
) - 1) == 0) {
42 std::optional
<std::string
> HostInfoPosix::GetOSKernelDescription() {
47 return std::string(un
.version
);
51 #include <android/api-level.h>
53 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
58 class PosixUserIDResolver
: public UserIDResolver
{
60 std::optional
<std::string
> DoGetUserName(id_t uid
) override
;
61 std::optional
<std::string
> DoGetGroupName(id_t gid
) override
;
70 static std::optional
<PasswdEntry
> GetPassword(id_t uid
) {
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
};
79 struct passwd user_info
;
80 struct passwd
*user_info_ptr
= &user_info
;
81 char user_buffer
[PATH_MAX
];
82 size_t user_buffer_size
= sizeof(user_buffer
);
83 if (::getpwuid_r(uid
, &user_info
, user_buffer
, user_buffer_size
,
84 &user_info_ptr
) == 0 &&
86 return PasswdEntry
{user_info_ptr
->pw_name
, user_info_ptr
->pw_shell
};
92 std::optional
<std::string
> PosixUserIDResolver::DoGetUserName(id_t uid
) {
93 if (std::optional
<PasswdEntry
> password
= GetPassword(uid
))
94 return password
->username
;
98 std::optional
<std::string
> PosixUserIDResolver::DoGetGroupName(id_t gid
) {
100 char group_buffer
[PATH_MAX
];
101 size_t group_buffer_size
= sizeof(group_buffer
);
102 struct group group_info
;
103 struct group
*group_info_ptr
= &group_info
;
104 // Try the threadsafe version first
105 if (::getgrgid_r(gid
, &group_info
, group_buffer
, group_buffer_size
,
106 &group_info_ptr
) == 0) {
108 return std::string(group_info_ptr
->gr_name
);
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 group_info_ptr
= ::getgrgid(gid
);
114 return std::string(group_info_ptr
->gr_name
);
120 static llvm::ManagedStatic
<PosixUserIDResolver
> g_user_id_resolver
;
122 UserIDResolver
&HostInfoPosix::GetUserIDResolver() {
123 return *g_user_id_resolver
;
126 uint32_t HostInfoPosix::GetUserID() { return getuid(); }
128 uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
130 uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
132 uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
134 FileSpec
HostInfoPosix::GetDefaultShell() {
135 if (const char *v
= ::getenv("SHELL"))
137 if (std::optional
<PasswdEntry
> password
= GetPassword(::geteuid()))
138 return FileSpec(password
->shell
);
139 return FileSpec("/bin/sh");
142 bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec
&file_spec
) {
143 return ComputePathRelativeToLibrary(file_spec
, "/bin");
146 bool HostInfoPosix::ComputeHeaderDirectory(FileSpec
&file_spec
) {
147 FileSpec
temp_file("/opt/local/include/lldb");
148 file_spec
.SetDirectory(temp_file
.GetPath());
152 bool HostInfoPosix::GetEnvironmentVar(const std::string
&var_name
,
154 if (const char *pvar
= ::getenv(var_name
.c_str())) {
155 var
= std::string(pvar
);