diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 20:51:52 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 20:51:52 +0000 |
commit | 5f29bb8a675e8f96452b632e7129113f7dec850e (patch) | |
tree | 3d3f2a0d3ad10872a4dcaba8ec8d1d20c87ab147 /source/Host/posix | |
parent | 88c643b6fec27eec436c8d138fee6346e92337d6 (diff) | |
download | src-5f29bb8a675e8f96452b632e7129113f7dec850e.tar.gz src-5f29bb8a675e8f96452b632e7129113f7dec850e.zip |
Vendor import of stripped lldb trunk r366426 (just before the release_90
branch point):
https://llvm.org/svn/llvm-project/lldb/trunk@366426
Notes
Notes:
svn path=/vendor/lldb/dist/; revision=351290
Diffstat (limited to 'source/Host/posix')
-rw-r--r-- | source/Host/posix/ConnectionFileDescriptorPosix.cpp | 32 | ||||
-rw-r--r-- | source/Host/posix/DomainSocket.cpp | 40 | ||||
-rw-r--r-- | source/Host/posix/FileSystem.cpp | 12 | ||||
-rw-r--r-- | source/Host/posix/HostInfoPosix.cpp | 104 | ||||
-rw-r--r-- | source/Host/posix/HostProcessPosix.cpp | 9 | ||||
-rw-r--r-- | source/Host/posix/HostThreadPosix.cpp | 9 | ||||
-rw-r--r-- | source/Host/posix/LockFilePosix.cpp | 11 | ||||
-rw-r--r-- | source/Host/posix/PipePosix.cpp | 16 | ||||
-rw-r--r-- | source/Host/posix/ProcessLauncherPosixFork.cpp | 16 |
9 files changed, 118 insertions, 131 deletions
diff --git a/source/Host/posix/ConnectionFileDescriptorPosix.cpp b/source/Host/posix/ConnectionFileDescriptorPosix.cpp index deac3844d4a2..067e85972eca 100644 --- a/source/Host/posix/ConnectionFileDescriptorPosix.cpp +++ b/source/Host/posix/ConnectionFileDescriptorPosix.cpp @@ -1,9 +1,8 @@ //===-- ConnectionFileDescriptorPosix.cpp -----------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -32,6 +31,7 @@ #include <unistd.h> #endif +#include <memory> #include <sstream> #include "llvm/Support/Errno.h" @@ -87,8 +87,8 @@ ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit) 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.reset(new File(fd, owns_fd)); - m_read_sp.reset(new File(fd, false)); + m_write_sp = std::make_shared<File>(fd, owns_fd); + m_read_sp = std::make_shared<File>(fd, false); Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT)); @@ -111,7 +111,7 @@ ConnectionFileDescriptor::~ConnectionFileDescriptor() { if (log) log->Printf("%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()", static_cast<void *>(this)); - Disconnect(NULL); + Disconnect(nullptr); CloseCommandPipe(); } @@ -222,8 +222,8 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path, m_read_sp = std::move(tcp_socket); m_write_sp = m_read_sp; } else { - m_read_sp.reset(new File(fd, false)); - m_write_sp.reset(new File(fd, false)); + m_read_sp = std::make_shared<File>(fd, false); + m_write_sp = std::make_shared<File>(fd, false); } m_uri = *addr; return eConnectionStatusSuccess; @@ -262,7 +262,7 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path, options.c_cc[VMIN] = 1; options.c_cc[VTIME] = 0; - ::tcsetattr(fd, TCSANOW, &options); + llvm::sys::RetryAfterSignal(-1, ::tcsetattr, fd, TCSANOW, &options); } int flags = ::fcntl(fd, F_GETFL, 0); @@ -272,8 +272,8 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path, ::fcntl(fd, F_SETFL, flags); } } - m_read_sp.reset(new File(fd, true)); - m_write_sp.reset(new File(fd, false)); + m_read_sp = std::make_shared<File>(fd, true); + m_write_sp = std::make_shared<File>(fd, false); return eConnectionStatusSuccess; } #endif @@ -758,13 +758,7 @@ void ConnectionFileDescriptor::SetChildProcessesInherit( } void ConnectionFileDescriptor::InitializeSocket(Socket *socket) { - assert(socket->GetSocketProtocol() == Socket::ProtocolTcp); - TCPSocket *tcp_socket = static_cast<TCPSocket *>(socket); - m_write_sp.reset(socket); m_read_sp = m_write_sp; - StreamString strm; - strm.Printf("connect://%s:%u", tcp_socket->GetRemoteIPAddress().c_str(), - tcp_socket->GetRemotePortNumber()); - m_uri = strm.GetString(); + m_uri = socket->GetRemoteConnectionURI(); } diff --git a/source/Host/posix/DomainSocket.cpp b/source/Host/posix/DomainSocket.cpp index 3e3abadc2e5a..27872f48129c 100644 --- a/source/Host/posix/DomainSocket.cpp +++ b/source/Host/posix/DomainSocket.cpp @@ -1,14 +1,14 @@ //===-- DomainSocket.cpp ----------------------------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "lldb/Host/posix/DomainSocket.h" +#include "llvm/Support/Errno.h" #include "llvm/Support/FileSystem.h" #include <stddef.h> @@ -82,8 +82,8 @@ Status DomainSocket::Connect(llvm::StringRef name) { m_socket = CreateSocket(kDomain, kType, 0, m_child_processes_inherit, error); if (error.Fail()) return error; - if (::connect(GetNativeSocket(), (struct sockaddr *)&saddr_un, saddr_un_len) < - 0) + if (llvm::sys::RetryAfterSignal(-1, ::connect, GetNativeSocket(), + (struct sockaddr *)&saddr_un, saddr_un_len) < 0) SetLastError(error); return error; @@ -125,3 +125,31 @@ size_t DomainSocket::GetNameOffset() const { return 0; } void DomainSocket::DeleteSocketFile(llvm::StringRef name) { llvm::sys::fs::remove(name); } + +std::string DomainSocket::GetSocketName() const { + if (m_socket != kInvalidSocketValue) { + struct sockaddr_un saddr_un; + saddr_un.sun_family = AF_UNIX; + socklen_t sock_addr_len = sizeof(struct sockaddr_un); + if (::getpeername(m_socket, (struct sockaddr *)&saddr_un, &sock_addr_len) == + 0) { + std::string name(saddr_un.sun_path + GetNameOffset(), + sock_addr_len - + offsetof(struct sockaddr_un, sun_path) - + GetNameOffset()); + if (name.back() == '\0') name.pop_back(); + return name; + } + } + return ""; +} + +std::string DomainSocket::GetRemoteConnectionURI() const { + if (m_socket != kInvalidSocketValue) { + return llvm::formatv("{0}://{1}", + GetNameOffset() == 0 ? "unix-connect" + : "unix-abstract-connect", + GetSocketName()); + } + return ""; +} diff --git a/source/Host/posix/FileSystem.cpp b/source/Host/posix/FileSystem.cpp index d7045ff99919..32fae68abb4d 100644 --- a/source/Host/posix/FileSystem.cpp +++ b/source/Host/posix/FileSystem.cpp @@ -1,9 +1,8 @@ //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -26,6 +25,7 @@ #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" +#include "llvm/Support/Errno.h" #include "llvm/Support/FileSystem.h" using namespace lldb; @@ -72,9 +72,9 @@ Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) { } FILE *FileSystem::Fopen(const char *path, const char *mode) { - return ::fopen(path, mode); + return llvm::sys::RetryAfterSignal(nullptr, ::fopen, path, mode); } int FileSystem::Open(const char *path, int flags, int mode) { - return ::open(path, flags, mode); + return llvm::sys::RetryAfterSignal(-1, ::open, path, flags, mode); } diff --git a/source/Host/posix/HostInfoPosix.cpp b/source/Host/posix/HostInfoPosix.cpp index 4763ebc9b9d4..f300e22e9e5c 100644 --- a/source/Host/posix/HostInfoPosix.cpp +++ b/source/Host/posix/HostInfoPosix.cpp @@ -1,13 +1,13 @@ //===-- HostInfoPosix.cpp ---------------------------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "lldb/Host/posix/HostInfoPosix.h" +#include "lldb/Utility/UserIDResolver.h" #include "lldb/Utility/Log.h" #include "llvm/ADT/SmallString.h" @@ -49,40 +49,38 @@ bool HostInfoPosix::GetHostname(std::string &s) { #define USE_GETPWUID #endif -#ifdef USE_GETPWUID -static std::mutex s_getpwuid_lock; -#endif +namespace { +class PosixUserIDResolver : public UserIDResolver { +protected: + llvm::Optional<std::string> DoGetUserName(id_t uid) override; + llvm::Optional<std::string> DoGetGroupName(id_t gid) override; +}; +} // namespace -const char *HostInfoPosix::LookupUserName(uint32_t uid, - std::string &user_name) { +llvm::Optional<std::string> PosixUserIDResolver::DoGetUserName(id_t uid) { #ifdef USE_GETPWUID // getpwuid_r is missing from android-9 - // make getpwuid thread safe with a mutex - std::lock_guard<std::mutex> lock(s_getpwuid_lock); + // 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) { - user_name.assign(user_info_ptr->pw_name); - return user_name.c_str(); - } + if (user_info_ptr) + return std::string(user_info_ptr->pw_name); #else 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_info_ptr) == 0 && + user_info_ptr) { + return std::string(user_info_ptr->pw_name); } #endif - user_name.clear(); - return nullptr; + return llvm::None; } -const char *HostInfoPosix::LookupGroupName(uint32_t gid, - std::string &group_name) { +llvm::Optional<std::string> PosixUserIDResolver::DoGetGroupName(id_t gid) { #ifndef __ANDROID__ char group_buffer[PATH_MAX]; size_t group_buffer_size = sizeof(group_buffer); @@ -91,24 +89,25 @@ const char *HostInfoPosix::LookupGroupName(uint32_t gid, // 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(); - } + if (group_info_ptr) + return std::string(group_info_ptr->gr_name); } 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(); - } + if (group_info_ptr) + return std::string(group_info_ptr->gr_name); } - group_name.clear(); #else assert(false && "getgrgid_r() not supported on Android"); #endif - return NULL; + return llvm::None; +} + +static llvm::ManagedStatic<PosixUserIDResolver> g_user_id_resolver; + +UserIDResolver &HostInfoPosix::GetUserIDResolver() { + return *g_user_id_resolver; } uint32_t HostInfoPosix::GetUserID() { return getuid(); } @@ -121,43 +120,6 @@ uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); } FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh"); } -bool HostInfoPosix::ComputePathRelativeToLibrary(FileSpec &file_spec, - llvm::StringRef dir) { - Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); - - FileSpec lldb_file_spec = GetShlibDir(); - if (!lldb_file_spec) - return false; - - std::string raw_path = lldb_file_spec.GetPath(); - // drop library directory - llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path); - - // Most Posix systems (e.g. Linux/*BSD) will attempt to replace a */lib with - // */bin as the base directory for helper exe programs. This will fail if - // the /lib and /bin directories are rooted in entirely different trees. - if (log) - log->Printf("HostInfoPosix::ComputePathRelativeToLibrary() attempting to " - "derive the %s path from this path: %s", - dir.data(), raw_path.c_str()); - - if (!parent_path.empty()) { - // Now write in bin in place of lib. - raw_path = (parent_path + dir).str(); - - if (log) - log->Printf("Host::%s() derived the bin path as: %s", __FUNCTION__, - raw_path.c_str()); - } else { - if (log) - log->Printf("Host::%s() failed to find /lib/liblldb within the shared " - "lib path, bailing on bin path construction", - __FUNCTION__); - } - file_spec.GetDirectory().SetString(raw_path); - return (bool)file_spec.GetDirectory(); -} - bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) { return ComputePathRelativeToLibrary(file_spec, "/bin"); } diff --git a/source/Host/posix/HostProcessPosix.cpp b/source/Host/posix/HostProcessPosix.cpp index f431e0c72de1..cc187d442468 100644 --- a/source/Host/posix/HostProcessPosix.cpp +++ b/source/Host/posix/HostProcessPosix.cpp @@ -1,9 +1,8 @@ //===-- HostProcessPosix.cpp ------------------------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -88,7 +87,7 @@ bool HostProcessPosix::IsRunning() const { return error.Success(); } -HostThread HostProcessPosix::StartMonitoring( +llvm::Expected<HostThread> HostProcessPosix::StartMonitoring( const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { return Host::StartMonitoringChildProcess(callback, m_process, monitor_signals); diff --git a/source/Host/posix/HostThreadPosix.cpp b/source/Host/posix/HostThreadPosix.cpp index 13de42f763ec..d78bba517f69 100644 --- a/source/Host/posix/HostThreadPosix.cpp +++ b/source/Host/posix/HostThreadPosix.cpp @@ -1,9 +1,8 @@ //===-- HostThreadPosix.cpp -------------------------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -30,7 +29,7 @@ Status HostThreadPosix::Join(lldb::thread_result_t *result) { error.SetError(err, lldb::eErrorTypePOSIX); } else { if (result) - *result = NULL; + *result = nullptr; error.SetError(EINVAL, eErrorTypePOSIX); } diff --git a/source/Host/posix/LockFilePosix.cpp b/source/Host/posix/LockFilePosix.cpp index 05423062bd44..a6eae95c333b 100644 --- a/source/Host/posix/LockFilePosix.cpp +++ b/source/Host/posix/LockFilePosix.cpp @@ -1,14 +1,15 @@ //===-- LockFilePosix.cpp ---------------------------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "lldb/Host/posix/LockFilePosix.h" +#include "llvm/Support/Errno.h" + #include <fcntl.h> #include <unistd.h> @@ -28,7 +29,7 @@ Status fileLock(int fd, int cmd, int lock_type, const uint64_t start, fl.l_pid = ::getpid(); Status error; - if (::fcntl(fd, cmd, &fl) == -1) + if (llvm::sys::RetryAfterSignal(-1, ::fcntl, fd, cmd, &fl) == -1) error.SetErrorToErrno(); return error; diff --git a/source/Host/posix/PipePosix.cpp b/source/Host/posix/PipePosix.cpp index 866a9897ee43..efdc151e3763 100644 --- a/source/Host/posix/PipePosix.cpp +++ b/source/Host/posix/PipePosix.cpp @@ -1,9 +1,8 @@ //===-- PipePosix.cpp -------------------------------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -11,6 +10,7 @@ #include "lldb/Host/HostInfo.h" #include "lldb/Utility/SelectHelper.h" #include "llvm/ADT/SmallString.h" +#include "llvm/Support/Errno.h" #include "llvm/Support/FileSystem.h" #if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)) @@ -158,7 +158,7 @@ Status PipePosix::OpenAsReader(llvm::StringRef name, flags |= O_CLOEXEC; Status error; - int fd = ::open(name.data(), flags); + int fd = llvm::sys::RetryAfterSignal(-1, ::open, name.data(), flags); if (fd != -1) m_fds[READ] = fd; else @@ -193,7 +193,7 @@ PipePosix::OpenAsWriterWithTimeout(llvm::StringRef name, if (fd == -1) { const auto errno_copy = errno; // We may get ENXIO if a reader side of the pipe hasn't opened yet. - if (errno_copy != ENXIO) + if (errno_copy != ENXIO && errno_copy != EINTR) return Status(errno_copy, eErrorTypePOSIX); std::this_thread::sleep_for( @@ -276,6 +276,8 @@ Status PipePosix::ReadWithTimeout(void *buf, size_t size, bytes_read += result; if (bytes_read == size || result == 0) break; + } else if (errno == EINTR) { + continue; } else { error.SetErrorToErrno(); break; @@ -306,6 +308,8 @@ Status PipePosix::Write(const void *buf, size_t size, size_t &bytes_written) { bytes_written += result; if (bytes_written == size) break; + } else if (errno == EINTR) { + continue; } else { error.SetErrorToErrno(); } diff --git a/source/Host/posix/ProcessLauncherPosixFork.cpp b/source/Host/posix/ProcessLauncherPosixFork.cpp index 6bf78463d060..185c7f0fe248 100644 --- a/source/Host/posix/ProcessLauncherPosixFork.cpp +++ b/source/Host/posix/ProcessLauncherPosixFork.cpp @@ -1,9 +1,8 @@ -//===-- ProcessLauncherLinux.cpp --------------------------------*- C++ -*-===// +//===-- ProcessLauncherPosixFork.cpp ----------------------------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -11,7 +10,7 @@ #include "lldb/Host/Host.h" #include "lldb/Host/HostProcess.h" #include "lldb/Host/Pipe.h" -#include "lldb/Target/ProcessLaunchInfo.h" +#include "lldb/Host/ProcessLaunchInfo.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" #include "llvm/Support/Errno.h" @@ -73,7 +72,8 @@ static void DisableASLRIfRequested(int error_fd, const ProcessLaunchInfo &info) static void DupDescriptor(int error_fd, const FileSpec &file_spec, int fd, int flags) { - int target_fd = ::open(file_spec.GetCString(), flags, 0666); + int target_fd = llvm::sys::RetryAfterSignal(-1, ::open, + file_spec.GetCString(), flags, 0666); if (target_fd == -1) ExitWithError(error_fd, "DupDescriptor-open"); @@ -212,7 +212,7 @@ ProcessLauncherPosixFork::LaunchProcess(const ProcessLaunchInfo &launch_info, error.SetErrorString(buf); - waitpid(pid, nullptr, 0); + llvm::sys::RetryAfterSignal(-1, waitpid, pid, nullptr, 0); return HostProcess(); } |