Move GetUsername and GetGroupname to HostInfoPosix

llvm-svn: 216210
This commit is contained in:
Zachary Turner 2014-08-21 20:02:17 +00:00
parent 5ed17dad95
commit b245ecac79
6 changed files with 68 additions and 94 deletions

View File

@ -22,6 +22,8 @@ class HostInfoPosix : public HostInfoBase
public:
static size_t GetPageSize();
static bool GetHostname(std::string &s);
static bool LookupUserName(uint32_t uid, std::string &user_name);
static bool LookupGroupName(uint32_t gid, std::string &group_name);
protected:
static bool ComputeSupportExeDirectory(FileSpec &file_spec);

View File

@ -867,65 +867,6 @@ Host::GetModuleFileSpecForHostAddress (const void *host_addr)
#ifndef _WIN32
const char *
Host::GetUserName (uint32_t uid, std::string &user_name)
{
struct passwd user_info;
struct passwd *user_info_ptr = &user_info;
char user_buffer[PATH_MAX];
size_t user_buffer_size = sizeof(user_buffer);
if (::getpwuid_r (uid,
&user_info,
user_buffer,
user_buffer_size,
&user_info_ptr) == 0)
{
if (user_info_ptr)
{
user_name.assign (user_info_ptr->pw_name);
return user_name.c_str();
}
}
user_name.clear();
return NULL;
}
const char *
Host::GetGroupName (uint32_t gid, std::string &group_name)
{
char group_buffer[PATH_MAX];
size_t group_buffer_size = sizeof(group_buffer);
struct group group_info;
struct group *group_info_ptr = &group_info;
// Try the threadsafe version first
if (::getgrgid_r (gid,
&group_info,
group_buffer,
group_buffer_size,
&group_info_ptr) == 0)
{
if (group_info_ptr)
{
group_name.assign (group_info_ptr->gr_name);
return group_name.c_str();
}
}
else
{
// The threadsafe version isn't currently working
// for me on darwin, but the non-threadsafe version
// is, so I am calling it below.
group_info_ptr = ::getgrgid (gid);
if (group_info_ptr)
{
group_name.assign (group_info_ptr->gr_name);
return group_name.c_str();
}
}
group_name.clear();
return NULL;
}
uint32_t
Host::GetUserID ()
{
@ -952,23 +893,6 @@ Host::GetEffectiveGroupID ()
#endif
#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) \
&& !defined(__linux__) && !defined(_WIN32)
uint32_t
Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
{
process_infos.Clear();
return process_infos.GetSize();
}
bool
Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
{
process_info.Clear();
return false;
}
#endif
#if !defined(__linux__)
bool
Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)

View File

@ -15,8 +15,11 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
#include <netdb.h>
#include <grp.h>
#include <limits.h>
#include <netdb.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
using namespace lldb_private;
@ -44,6 +47,56 @@ HostInfoPosix::GetHostname(std::string &s)
return false;
}
bool
HostInfoPosix::LookupUserName(uint32_t uid, std::string &user_name)
{
struct passwd user_info;
struct passwd *user_info_ptr = &user_info;
char user_buffer[PATH_MAX];
size_t user_buffer_size = sizeof(user_buffer);
if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size, &user_info_ptr) == 0)
{
if (user_info_ptr)
{
user_name.assign(user_info_ptr->pw_name);
return user_name.c_str();
}
}
user_name.clear();
return NULL;
}
bool
HostInfoPosix::LookupGroupName(uint32_t gid, std::string &group_name)
{
char group_buffer[PATH_MAX];
size_t group_buffer_size = sizeof(group_buffer);
struct group group_info;
struct group *group_info_ptr = &group_info;
// Try the threadsafe version first
if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size, &group_info_ptr) == 0)
{
if (group_info_ptr)
{
group_name.assign(group_info_ptr->gr_name);
return group_name.c_str();
}
}
else
{
// The threadsafe version isn't currently working for me on darwin, but the non-threadsafe version
// is, so I am calling it below.
group_info_ptr = ::getgrgid(gid);
if (group_info_ptr)
{
group_name.assign(group_info_ptr->gr_name);
return group_name.c_str();
}
}
group_name.clear();
return NULL;
}
bool
HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec)
{

View File

@ -235,19 +235,6 @@ Host::DynamicLibraryGetSymbol(void *opaque, const char *symbol_name, Error &erro
return NULL;
}
const char *
Host::GetUserName (uint32_t uid, std::string &user_name)
{
return NULL;
}
const char *
Host::GetGroupName (uint32_t gid, std::string &group_name)
{
llvm_unreachable("Windows does not support group name");
return NULL;
}
uint32_t
Host::GetUserID ()
{

View File

@ -1538,19 +1538,21 @@ GDBRemoteCommunicationServer::Handle_qsProcessInfo (StringExtractorGDBRemote &pa
GDBRemoteCommunication::PacketResult
GDBRemoteCommunicationServer::Handle_qUserName (StringExtractorGDBRemote &packet)
{
#if !defined(LLDB_DISABLE_POSIX)
// Packet format: "qUserName:%i" where %i is the uid
packet.SetFilePos(::strlen ("qUserName:"));
uint32_t uid = packet.GetU32 (UINT32_MAX);
if (uid != UINT32_MAX)
{
std::string name;
if (Host::GetUserName (uid, name))
if (HostInfo::LookupUserName(uid, name))
{
StreamString response;
response.PutCStringAsRawHex8 (name.c_str());
return SendPacketNoLock (response.GetData(), response.GetSize());
}
}
#endif
return SendErrorResponse (5);
}
@ -1558,19 +1560,21 @@ GDBRemoteCommunicationServer::Handle_qUserName (StringExtractorGDBRemote &packet
GDBRemoteCommunication::PacketResult
GDBRemoteCommunicationServer::Handle_qGroupName (StringExtractorGDBRemote &packet)
{
#if !defined(LLDB_DISABLE_POSIX)
// Packet format: "qGroupName:%i" where %i is the gid
packet.SetFilePos(::strlen ("qGroupName:"));
uint32_t gid = packet.GetU32 (UINT32_MAX);
if (gid != UINT32_MAX)
{
std::string name;
if (Host::GetGroupName (gid, name))
if (HostInfo::LookupGroupName(gid, name))
{
StreamString response;
response.PutCStringAsRawHex8 (name.c_str());
return SendPacketNoLock (response.GetData(), response.GetSize());
}
}
#endif
return SendErrorResponse (6);
}

View File

@ -776,30 +776,34 @@ Platform::SetRemoteWorkingDirectory(const ConstString &path)
const char *
Platform::GetUserName (uint32_t uid)
{
#if !defined(LLDB_DISABLE_POSIX)
const char *user_name = GetCachedUserName(uid);
if (user_name)
return user_name;
if (IsHost())
{
std::string name;
if (Host::GetUserName(uid, name))
if (HostInfo::LookupUserName(uid, name))
return SetCachedUserName (uid, name.c_str(), name.size());
}
#endif
return NULL;
}
const char *
Platform::GetGroupName (uint32_t gid)
{
#if !defined(LLDB_DISABLE_POSIX)
const char *group_name = GetCachedGroupName(gid);
if (group_name)
return group_name;
if (IsHost())
{
std::string name;
if (Host::GetGroupName(gid, name))
if (HostInfo::LookupGroupName(gid, name))
return SetCachedGroupName (gid, name.c_str(), name.size());
}
#endif
return NULL;
}