1 //===-- HostInfoPosix.cpp ---------------------------------------*- C++ -*-===//
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/UserIDResolver.h"
11 #include "lldb/Utility/Log.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>
27 using namespace lldb_private;
29 size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
31 bool 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);
46 #include <android/api-level.h>
48 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
53 class PosixUserIDResolver : public UserIDResolver {
55 llvm::Optional<std::string> DoGetUserName(id_t uid) override;
56 llvm::Optional<std::string> DoGetGroupName(id_t gid) override;
60 llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) {
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);
68 return std::string(user_info_ptr->pw_name);
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 &&
77 return std::string(user_info_ptr->pw_name);
83 llvm::Optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) {
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) {
93 return std::string(group_info_ptr->gr_name);
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);
99 return std::string(group_info_ptr->gr_name);
102 assert(false && "getgrgid_r() not supported on Android");
107 static llvm::ManagedStatic<PosixUserIDResolver> g_user_id_resolver;
109 UserIDResolver &HostInfoPosix::GetUserIDResolver() {
110 return *g_user_id_resolver;
113 uint32_t HostInfoPosix::GetUserID() { return getuid(); }
115 uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
117 uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
119 uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
121 FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh"); }
123 bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
124 return ComputePathRelativeToLibrary(file_spec, "/bin");
127 bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {
128 FileSpec temp_file("/opt/local/include/lldb");
129 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
133 bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
135 if (const char *pvar = ::getenv(var_name.c_str())) {
136 var = std::string(pvar);