12#include "llvm/Support/Errno.h"
13#include "llvm/Support/FileSystem.h"
17#include <sys/socket.h>
27 (offsetof(struct sockaddr_un, sun_path) + strlen((ptr)->sun_path))
32static const int kType = SOCK_STREAM;
34static bool SetSockAddr(llvm::StringRef name,
const size_t name_offset,
35 sockaddr_un *saddr_un, socklen_t &saddr_un_len) {
36 if (name.size() + name_offset >
sizeof(saddr_un->sun_path))
39 memset(saddr_un, 0,
sizeof(*saddr_un));
42 memcpy(saddr_un->sun_path + name_offset, name.data(), name.size());
48 saddr_un_len = SUN_LEN(saddr_un);
51 offsetof(
struct sockaddr_un, sun_path) + name_offset + name.size();
53#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
55 saddr_un->sun_len = saddr_un_len;
65 :
Socket(ProtocolUnixDomain, should_close) {
70 :
Socket(protocol, true) {}
74 :
Socket(ProtocolUnixDomain, listen_socket.m_should_close_fd) {
80 socklen_t saddr_un_len;
89 (
struct sockaddr *)&saddr_un, saddr_un_len) < 0)
97 socklen_t saddr_un_len;
107 if (::bind(
GetNativeSocket(), (
struct sockaddr *)&saddr_un, saddr_un_len) ==
118 std::function<
void(std::unique_ptr<Socket> socket)> sock_cb) {
129 std::unique_ptr<DomainSocket> sock_up(
new DomainSocket(conn_fd, *
this));
130 sock_cb(std::move(sock_up));
134 std::vector<MainLoopBase::ReadHandleUP> handles;
137 return error.ToError();
144 llvm::sys::fs::remove(name);
151 struct sockaddr_un saddr_un;
152 saddr_un.sun_family = AF_UNIX;
153 socklen_t sock_addr_len =
sizeof(
struct sockaddr_un);
154 if (::getpeername(
m_socket, (
struct sockaddr *)&saddr_un, &sock_addr_len) !=
158 if (sock_addr_len <= offsetof(
struct sockaddr_un, sun_path))
162 sock_addr_len - offsetof(
struct sockaddr_un, sun_path) -
164 name = name.rtrim(
'\0');
174 return llvm::formatv(
176 GetNameOffset() == 0 ?
"unix-connect" :
"unix-abstract-connect", name);
183 struct sockaddr_un addr;
184 bzero(&addr,
sizeof(
struct sockaddr_un));
185 addr.sun_family = AF_UNIX;
186 socklen_t addr_len =
sizeof(
struct sockaddr_un);
187 if (::getsockname(
m_socket, (
struct sockaddr *)&addr, &addr_len) != 0)
190 return {llvm::formatv(
"unix-connect://{0}", addr.sun_path)};
static llvm::raw_ostream & error(Stream &strm)
static bool SetSockAddr(llvm::StringRef name, const size_t name_offset, sockaddr_un *saddr_un, socklen_t &saddr_un_len)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
DomainSocket(NativeSocket socket, bool should_close)
Status Listen(llvm::StringRef name, int backlog) override
llvm::Expected< std::vector< MainLoopBase::ReadHandleUP > > Accept(MainLoopBase &loop, std::function< void(std::unique_ptr< Socket > socket)> sock_cb) override
virtual size_t GetNameOffset() const
virtual void DeleteSocketFile(llvm::StringRef name)
Status Connect(llvm::StringRef name) override
std::string GetSocketName() const
std::string GetRemoteConnectionURI() const override
std::vector< std::string > GetListeningConnectionURI() const override
virtual ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp, const Callback &callback, Status &error)=0
NativeSocket GetNativeSocket() const
static const NativeSocket kInvalidSocketValue
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, Status &error)
static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, Status &error)
static void SetLastError(Status &error)
static Status FromErrorString(const char *str)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.