[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- DomainSocket.cpp --------------------------------------------------===//
|
2015-10-16 07:54:09 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2015-10-16 07:54:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Host/posix/DomainSocket.h"
|
|
|
|
|
2019-03-22 03:35:55 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2017-03-21 13:47:57 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-10-16 07:54:09 +08:00
|
|
|
|
2015-11-03 04:04:18 +08:00
|
|
|
#include <stddef.h>
|
2015-10-16 07:54:09 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2015-11-03 04:04:18 +08:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
// Android does not have SUN_LEN
|
|
|
|
#ifndef SUN_LEN
|
2016-03-16 22:03:20 +08:00
|
|
|
#define SUN_LEN(ptr) \
|
|
|
|
(offsetof(struct sockaddr_un, sun_path) + strlen((ptr)->sun_path))
|
2015-11-03 04:04:18 +08:00
|
|
|
#endif
|
|
|
|
#endif // #ifdef __ANDROID__
|
|
|
|
|
2015-10-16 07:54:09 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kDomain = AF_UNIX;
|
|
|
|
const int kType = SOCK_STREAM;
|
|
|
|
|
2015-11-03 04:04:18 +08:00
|
|
|
bool SetSockAddr(llvm::StringRef name, const size_t name_offset,
|
|
|
|
sockaddr_un *saddr_un, socklen_t &saddr_un_len) {
|
2015-10-23 01:50:33 +08:00
|
|
|
if (name.size() + name_offset > sizeof(saddr_un->sun_path))
|
|
|
|
return false;
|
|
|
|
|
2015-11-03 04:04:18 +08:00
|
|
|
memset(saddr_un, 0, sizeof(*saddr_un));
|
2015-10-16 07:54:09 +08:00
|
|
|
saddr_un->sun_family = kDomain;
|
2015-10-23 01:50:33 +08:00
|
|
|
|
2015-11-03 04:04:18 +08:00
|
|
|
memcpy(saddr_un->sun_path + name_offset, name.data(), name.size());
|
|
|
|
|
|
|
|
// For domain sockets we can use SUN_LEN in order to calculate size of
|
|
|
|
// sockaddr_un, but for abstract sockets we have to calculate size manually
|
|
|
|
// because of leading null symbol.
|
|
|
|
if (name_offset == 0)
|
|
|
|
saddr_un_len = SUN_LEN(saddr_un);
|
|
|
|
else
|
|
|
|
saddr_un_len =
|
|
|
|
offsetof(struct sockaddr_un, sun_path) + name_offset + name.size();
|
|
|
|
|
2015-10-16 07:54:09 +08:00
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
2015-11-03 04:04:18 +08:00
|
|
|
saddr_un->sun_len = saddr_un_len;
|
2015-10-16 07:54:09 +08:00
|
|
|
#endif
|
2015-11-03 04:04:18 +08:00
|
|
|
|
2015-10-23 01:50:33 +08:00
|
|
|
return true;
|
2015-10-16 07:54:09 +08:00
|
|
|
}
|
2017-04-27 07:17:20 +08:00
|
|
|
} // namespace
|
2015-10-16 07:54:09 +08:00
|
|
|
|
2017-04-27 07:17:20 +08:00
|
|
|
DomainSocket::DomainSocket(bool should_close, bool child_processes_inherit)
|
|
|
|
: Socket(ProtocolUnixDomain, should_close, child_processes_inherit) {}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-23 01:50:33 +08:00
|
|
|
DomainSocket::DomainSocket(SocketProtocol protocol,
|
2017-04-27 07:17:20 +08:00
|
|
|
bool child_processes_inherit)
|
|
|
|
: Socket(protocol, true, child_processes_inherit) {}
|
|
|
|
|
|
|
|
DomainSocket::DomainSocket(NativeSocket socket,
|
|
|
|
const DomainSocket &listen_socket)
|
|
|
|
: Socket(ProtocolUnixDomain, listen_socket.m_should_close_fd,
|
|
|
|
listen_socket.m_child_processes_inherit) {
|
|
|
|
m_socket = socket;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status DomainSocket::Connect(llvm::StringRef name) {
|
2015-10-16 07:54:09 +08:00
|
|
|
sockaddr_un saddr_un;
|
2015-11-03 04:04:18 +08:00
|
|
|
socklen_t saddr_un_len;
|
|
|
|
if (!SetSockAddr(name, GetNameOffset(), &saddr_un, saddr_un_len))
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("Failed to set socket address");
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2017-04-27 07:17:20 +08:00
|
|
|
m_socket = CreateSocket(kDomain, kType, 0, m_child_processes_inherit, error);
|
|
|
|
if (error.Fail())
|
|
|
|
return error;
|
2019-03-22 03:35:55 +08:00
|
|
|
if (llvm::sys::RetryAfterSignal(-1, ::connect, GetNativeSocket(),
|
|
|
|
(struct sockaddr *)&saddr_un, saddr_un_len) < 0)
|
2015-10-16 07:54:09 +08:00
|
|
|
SetLastError(error);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status DomainSocket::Listen(llvm::StringRef name, int backlog) {
|
2015-10-16 07:54:09 +08:00
|
|
|
sockaddr_un saddr_un;
|
2015-11-03 04:04:18 +08:00
|
|
|
socklen_t saddr_un_len;
|
|
|
|
if (!SetSockAddr(name, GetNameOffset(), &saddr_un, saddr_un_len))
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("Failed to set socket address");
|
2015-10-16 07:54:09 +08:00
|
|
|
|
2015-10-23 01:50:33 +08:00
|
|
|
DeleteSocketFile(name);
|
2015-10-16 07:54:09 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2017-04-27 07:17:20 +08:00
|
|
|
m_socket = CreateSocket(kDomain, kType, 0, m_child_processes_inherit, error);
|
|
|
|
if (error.Fail())
|
|
|
|
return error;
|
2015-11-03 04:04:18 +08:00
|
|
|
if (::bind(GetNativeSocket(), (struct sockaddr *)&saddr_un, saddr_un_len) ==
|
|
|
|
0)
|
2015-10-16 07:54:09 +08:00
|
|
|
if (::listen(GetNativeSocket(), backlog) == 0)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
SetLastError(error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status DomainSocket::Accept(Socket *&socket) {
|
|
|
|
Status error;
|
2015-10-16 07:54:09 +08:00
|
|
|
auto conn_fd = AcceptSocket(GetNativeSocket(), nullptr, nullptr,
|
2017-04-27 07:17:20 +08:00
|
|
|
m_child_processes_inherit, error);
|
2015-10-16 07:54:09 +08:00
|
|
|
if (error.Success())
|
2017-04-27 07:17:20 +08:00
|
|
|
socket = new DomainSocket(conn_fd, *this);
|
2015-10-16 07:54:09 +08:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
2015-10-23 01:50:33 +08:00
|
|
|
|
|
|
|
size_t DomainSocket::GetNameOffset() const { return 0; }
|
|
|
|
|
|
|
|
void DomainSocket::DeleteSocketFile(llvm::StringRef name) {
|
2017-03-21 13:47:57 +08:00
|
|
|
llvm::sys::fs::remove(name);
|
2015-10-23 01:50:33 +08:00
|
|
|
}
|
2019-05-31 07:30:35 +08:00
|
|
|
|
|
|
|
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) ==
|
2019-05-31 13:55:07 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-05-31 07:30:35 +08:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DomainSocket::GetRemoteConnectionURI() const {
|
|
|
|
if (m_socket != kInvalidSocketValue) {
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(llvm::formatv(
|
|
|
|
"{0}://{1}",
|
|
|
|
GetNameOffset() == 0 ? "unix-connect" : "unix-abstract-connect",
|
|
|
|
GetSocketName()));
|
2019-05-31 07:30:35 +08:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|