diff options
Diffstat (limited to 'source/Host/posix')
-rw-r--r-- | source/Host/posix/ConnectionFileDescriptorPosix.cpp | 159 | ||||
-rw-r--r-- | source/Host/posix/HostInfoPosix.cpp | 45 |
2 files changed, 110 insertions, 94 deletions
diff --git a/source/Host/posix/ConnectionFileDescriptorPosix.cpp b/source/Host/posix/ConnectionFileDescriptorPosix.cpp index 067e85972eca..325d854921e3 100644 --- a/source/Host/posix/ConnectionFileDescriptorPosix.cpp +++ b/source/Host/posix/ConnectionFileDescriptorPosix.cpp @@ -79,23 +79,22 @@ ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit) m_child_processes_inherit(child_processes_inherit) { Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT)); - if (log) - log->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor ()", - static_cast<void *>(this)); + LLDB_LOGF(log, "%p ConnectionFileDescriptor::ConnectionFileDescriptor ()", + static_cast<void *>(this)); } ConnectionFileDescriptor::ConnectionFileDescriptor(int fd, bool owns_fd) : Connection(), m_pipe(), m_mutex(), m_shutting_down(false), m_waiting_for_accept(false), m_child_processes_inherit(false) { - m_write_sp = std::make_shared<File>(fd, owns_fd); - m_read_sp = std::make_shared<File>(fd, false); + m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, owns_fd); + m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false); Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT)); - if (log) - log->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = " - "%i, owns_fd = %i)", - static_cast<void *>(this), fd, owns_fd); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = " + "%i, owns_fd = %i)", + static_cast<void *>(this), fd, owns_fd); OpenCommandPipe(); } @@ -108,9 +107,8 @@ ConnectionFileDescriptor::ConnectionFileDescriptor(Socket *socket) ConnectionFileDescriptor::~ConnectionFileDescriptor() { Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT)); - if (log) - log->Printf("%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()", - static_cast<void *>(this)); + LLDB_LOGF(log, "%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()", + static_cast<void *>(this)); Disconnect(nullptr); CloseCommandPipe(); } @@ -122,24 +120,23 @@ void ConnectionFileDescriptor::OpenCommandPipe() { // Make the command file descriptor here: Status result = m_pipe.CreateNew(m_child_processes_inherit); if (!result.Success()) { - if (log) - log->Printf("%p ConnectionFileDescriptor::OpenCommandPipe () - could not " - "make pipe: %s", - static_cast<void *>(this), result.AsCString()); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::OpenCommandPipe () - could not " + "make pipe: %s", + static_cast<void *>(this), result.AsCString()); } else { - if (log) - log->Printf("%p ConnectionFileDescriptor::OpenCommandPipe() - success " - "readfd=%d writefd=%d", - static_cast<void *>(this), m_pipe.GetReadFileDescriptor(), - m_pipe.GetWriteFileDescriptor()); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::OpenCommandPipe() - success " + "readfd=%d writefd=%d", + static_cast<void *>(this), m_pipe.GetReadFileDescriptor(), + m_pipe.GetWriteFileDescriptor()); } } void ConnectionFileDescriptor::CloseCommandPipe() { Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION)); - if (log) - log->Printf("%p ConnectionFileDescriptor::CloseCommandPipe()", - static_cast<void *>(this)); + LLDB_LOGF(log, "%p ConnectionFileDescriptor::CloseCommandPipe()", + static_cast<void *>(this)); m_pipe.Close(); } @@ -153,9 +150,8 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path, Status *error_ptr) { std::lock_guard<std::recursive_mutex> guard(m_mutex); Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION)); - if (log) - log->Printf("%p ConnectionFileDescriptor::Connect (url = '%s')", - static_cast<void *>(this), path.str().c_str()); + LLDB_LOGF(log, "%p ConnectionFileDescriptor::Connect (url = '%s')", + static_cast<void *>(this), path.str().c_str()); OpenCommandPipe(); @@ -222,8 +218,10 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path, m_read_sp = std::move(tcp_socket); m_write_sp = m_read_sp; } else { - m_read_sp = std::make_shared<File>(fd, false); - m_write_sp = std::make_shared<File>(fd, false); + m_read_sp = + std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false); + m_write_sp = + std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false); } m_uri = *addr; return eConnectionStatusSuccess; @@ -272,8 +270,8 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path, ::fcntl(fd, F_SETFL, flags); } } - m_read_sp = std::make_shared<File>(fd, true); - m_write_sp = std::make_shared<File>(fd, false); + m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, true); + m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false); return eConnectionStatusSuccess; } #endif @@ -295,17 +293,15 @@ bool ConnectionFileDescriptor::InterruptRead() { ConnectionStatus ConnectionFileDescriptor::Disconnect(Status *error_ptr) { Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION)); - if (log) - log->Printf("%p ConnectionFileDescriptor::Disconnect ()", - static_cast<void *>(this)); + LLDB_LOGF(log, "%p ConnectionFileDescriptor::Disconnect ()", + static_cast<void *>(this)); ConnectionStatus status = eConnectionStatusSuccess; if (!IsConnected()) { - if (log) - log->Printf( - "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect", - static_cast<void *>(this)); + LLDB_LOGF( + log, "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect", + static_cast<void *>(this)); return eConnectionStatusSuccess; } @@ -318,27 +314,28 @@ ConnectionStatus ConnectionFileDescriptor::Disconnect(Status *error_ptr) { // descriptor. If that's the case, then send the "q" char to the command // file channel so the read will wake up and the connection will then know to // shut down. - - m_shutting_down = true; - std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock); if (!locker.try_lock()) { if (m_pipe.CanWrite()) { size_t bytes_written = 0; Status result = m_pipe.Write("q", 1, bytes_written); - if (log) - log->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get " - "the lock, sent 'q' to %d, error = '%s'.", - static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(), - result.AsCString()); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::Disconnect(): Couldn't get " + "the lock, sent 'q' to %d, error = '%s'.", + static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(), + result.AsCString()); } else if (log) { - log->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the " - "lock, but no command pipe is available.", - static_cast<void *>(this)); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::Disconnect(): Couldn't get the " + "lock, but no command pipe is available.", + static_cast<void *>(this)); } locker.lock(); } + // Prevents reads and writes during shutdown. + m_shutting_down = true; + Status error = m_read_sp->Close(); Status error2 = m_write_sp->Close(); if (error.Fail() || error2.Fail()) @@ -362,10 +359,10 @@ size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len, std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock); if (!locker.try_lock()) { - if (log) - log->Printf("%p ConnectionFileDescriptor::Read () failed to get the " - "connection lock.", - static_cast<void *>(this)); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::Read () failed to get the " + "connection lock.", + static_cast<void *>(this)); if (error_ptr) error_ptr->SetErrorString("failed to get the connection lock for read."); @@ -374,6 +371,8 @@ size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len, } if (m_shutting_down) { + if (error_ptr) + error_ptr->SetErrorString("shutting down"); status = eConnectionStatusError; return 0; } @@ -387,12 +386,13 @@ size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len, error = m_read_sp->Read(dst, bytes_read); if (log) { - log->Printf("%p ConnectionFileDescriptor::Read() fd = %" PRIu64 - ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s", - static_cast<void *>(this), - static_cast<uint64_t>(m_read_sp->GetWaitableHandle()), - static_cast<void *>(dst), static_cast<uint64_t>(dst_len), - static_cast<uint64_t>(bytes_read), error.AsCString()); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::Read() fd = %" PRIu64 + ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s", + static_cast<void *>(this), + static_cast<uint64_t>(m_read_sp->GetWaitableHandle()), + static_cast<void *>(dst), static_cast<uint64_t>(dst_len), + static_cast<uint64_t>(bytes_read), error.AsCString()); } if (bytes_read == 0) { @@ -464,11 +464,11 @@ size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len, ConnectionStatus &status, Status *error_ptr) { Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION)); - if (log) - log->Printf( - "%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64 ")", - static_cast<void *>(this), static_cast<const void *>(src), - static_cast<uint64_t>(src_len)); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64 + ")", + static_cast<void *>(this), static_cast<const void *>(src), + static_cast<uint64_t>(src_len)); if (!IsConnected()) { if (error_ptr) @@ -477,19 +477,26 @@ size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len, return 0; } + if (m_shutting_down) { + if (error_ptr) + error_ptr->SetErrorString("shutting down"); + status = eConnectionStatusError; + return 0; + } + Status error; size_t bytes_sent = src_len; error = m_write_sp->Write(src, bytes_sent); if (log) { - log->Printf("%p ConnectionFileDescriptor::Write(fd = %" PRIu64 - ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 - " (error = %s)", - static_cast<void *>(this), - static_cast<uint64_t>(m_write_sp->GetWaitableHandle()), - static_cast<const void *>(src), static_cast<uint64_t>(src_len), - static_cast<uint64_t>(bytes_sent), error.AsCString()); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::Write(fd = %" PRIu64 + ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)", + static_cast<void *>(this), + static_cast<uint64_t>(m_write_sp->GetWaitableHandle()), + static_cast<const void *>(src), static_cast<uint64_t>(src_len), + static_cast<uint64_t>(bytes_sent), error.AsCString()); } if (error_ptr) @@ -559,7 +566,7 @@ ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout, select_helper.SetTimeout(*timeout); select_helper.FDSetRead(handle); -#if defined(_MSC_VER) +#if defined(_WIN32) // select() won't accept pipes on Windows. The entire Windows codepath // needs to be converted over to using WaitForMultipleObjects and event // HANDLEs, but for now at least this will allow ::select() to not return @@ -613,10 +620,10 @@ ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout, (void)bytes_read; switch (c) { case 'q': - if (log) - log->Printf("%p ConnectionFileDescriptor::BytesAvailable() " - "got data: %c from the command channel.", - static_cast<void *>(this), c); + LLDB_LOGF(log, + "%p ConnectionFileDescriptor::BytesAvailable() " + "got data: %c from the command channel.", + static_cast<void *>(this), c); return eConnectionStatusEndOfFile; case 'i': // Interrupt the current read diff --git a/source/Host/posix/HostInfoPosix.cpp b/source/Host/posix/HostInfoPosix.cpp index f300e22e9e5c..63cc5dc65e00 100644 --- a/source/Host/posix/HostInfoPosix.cpp +++ b/source/Host/posix/HostInfoPosix.cpp @@ -7,8 +7,8 @@ //===----------------------------------------------------------------------===// #include "lldb/Host/posix/HostInfoPosix.h" -#include "lldb/Utility/UserIDResolver.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/UserIDResolver.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" @@ -18,7 +18,6 @@ #include <grp.h> #include <limits.h> #include <mutex> -#include <netdb.h> #include <pwd.h> #include <stdlib.h> #include <sys/types.h> @@ -32,11 +31,7 @@ bool HostInfoPosix::GetHostname(std::string &s) { char hostname[PATH_MAX]; hostname[sizeof(hostname) - 1] = '\0'; if (::gethostname(hostname, sizeof(hostname) - 1) == 0) { - struct hostent *h = ::gethostbyname(hostname); - if (h) - s.assign(h->h_name); - else - s.assign(hostname); + s.assign(hostname); return true; } return false; @@ -57,15 +52,19 @@ protected: }; } // namespace -llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { +struct PasswdEntry { + std::string username; + std::string shell; +}; + +static llvm::Optional<PasswdEntry> GetPassword(id_t uid) { #ifdef USE_GETPWUID // getpwuid_r is missing from android-9 - // UserIDResolver provides some thread safety by making sure noone calls this - // function concurrently, but using getpwuid is ultimately not thread-safe as - // we don't know who else might be calling it. - struct passwd *user_info_ptr = ::getpwuid(uid); - if (user_info_ptr) - return std::string(user_info_ptr->pw_name); + // The caller should provide some thread safety by making sure no one calls + // this function concurrently, because using getpwuid is ultimately not + // thread-safe as we don't know who else might be calling it. + if (auto *user_info_ptr = ::getpwuid(uid)) + return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell}; #else struct passwd user_info; struct passwd *user_info_ptr = &user_info; @@ -74,12 +73,18 @@ llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size, &user_info_ptr) == 0 && user_info_ptr) { - return std::string(user_info_ptr->pw_name); + return PasswdEntry{user_info_ptr->pw_name, user_info_ptr->pw_shell}; } #endif return llvm::None; } +llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { + if (llvm::Optional<PasswdEntry> password = GetPassword(uid)) + return password->username; + return llvm::None; +} + llvm::Optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) { #ifndef __ANDROID__ char group_buffer[PATH_MAX]; @@ -98,8 +103,6 @@ llvm::Optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) { if (group_info_ptr) return std::string(group_info_ptr->gr_name); } -#else - assert(false && "getgrgid_r() not supported on Android"); #endif return llvm::None; } @@ -118,7 +121,13 @@ uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); } uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); } -FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh"); } +FileSpec HostInfoPosix::GetDefaultShell() { + if (const char *v = ::getenv("SHELL")) + return FileSpec(v); + if (llvm::Optional<PasswdEntry> password = GetPassword(::geteuid())) + return FileSpec(password->shell); + return FileSpec("/bin/sh"); +} bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) { return ComputePathRelativeToLibrary(file_spec, "/bin"); |