11#include "llvm/Support/Errno.h"
12#include "llvm/Support/FileSystem.h"
15#include <sys/socket.h>
25 (offsetof(struct sockaddr_un, sun_path) + strlen((ptr)->sun_path))
30static const int kType = SOCK_STREAM;
32static bool SetSockAddr(llvm::StringRef name,
const size_t name_offset,
33 sockaddr_un *saddr_un, socklen_t &saddr_un_len) {
34 if (name.size() + name_offset >
sizeof(saddr_un->sun_path))
37 memset(saddr_un, 0,
sizeof(*saddr_un));
40 memcpy(saddr_un->sun_path + name_offset, name.data(), name.size());
46 saddr_un_len = SUN_LEN(saddr_un);
49 offsetof(
struct sockaddr_un, sun_path) + name_offset + name.size();
51#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
53 saddr_un->sun_len = saddr_un_len;
60 :
Socket(ProtocolUnixDomain, should_close, child_processes_inherit) {}
63 bool child_processes_inherit)
64 :
Socket(protocol, true, child_processes_inherit) {}
68 :
Socket(ProtocolUnixDomain, listen_socket.m_should_close_fd,
69 listen_socket.m_child_processes_inherit) {
75 socklen_t saddr_un_len;
77 return Status(
"Failed to set socket address");
84 (
struct sockaddr *)&saddr_un, saddr_un_len) < 0)
92 socklen_t saddr_un_len;
94 return Status(
"Failed to set socket address");
102 if (::bind(
GetNativeSocket(), (
struct sockaddr *)&saddr_un, saddr_un_len) ==
124 llvm::sys::fs::remove(name);
131 struct sockaddr_un saddr_un;
132 saddr_un.sun_family = AF_UNIX;
133 socklen_t sock_addr_len =
sizeof(
struct sockaddr_un);
134 if (::getpeername(
m_socket, (
struct sockaddr *)&saddr_un, &sock_addr_len) !=
138 if (sock_addr_len <= offsetof(
struct sockaddr_un, sun_path))
142 sock_addr_len - offsetof(
struct sockaddr_un, sun_path) -
144 name = name.rtrim(
'\0');
154 return llvm::formatv(
156 GetNameOffset() == 0 ?
"unix-connect" :
"unix-abstract-connect", name);
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)
Status Listen(llvm::StringRef name, int backlog) override
Status Accept(Socket *&socket) 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
DomainSocket(bool should_close, bool child_processes_inherit)
NativeSocket GetNativeSocket() const
static const NativeSocket kInvalidSocketValue
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit, Status &error)
bool m_child_processes_inherit
static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, bool child_processes_inherit, Status &error)
static void SetLastError(Status &error)
A class that represents a running process on the host machine.