|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "lldb/Host/posix/DomainSocket.h" |
| 10 | +#include "lldb/Utility/LLDBLog.h" |
10 | 11 |
|
11 | 12 | #include "llvm/Support/Errno.h" |
12 | 13 | #include "llvm/Support/FileSystem.h" |
13 | 14 |
|
14 | 15 | #include <cstddef> |
| 16 | +#include <memory> |
15 | 17 | #include <sys/socket.h> |
16 | 18 | #include <sys/un.h> |
17 | 19 |
|
@@ -57,7 +59,14 @@ static bool SetSockAddr(llvm::StringRef name, const size_t name_offset, |
57 | 59 | } |
58 | 60 |
|
59 | 61 | DomainSocket::DomainSocket(bool should_close, bool child_processes_inherit) |
60 | | - : Socket(ProtocolUnixDomain, should_close, child_processes_inherit) {} |
| 62 | + : DomainSocket(kInvalidSocketValue, should_close, child_processes_inherit) { |
| 63 | +} |
| 64 | + |
| 65 | +DomainSocket::DomainSocket(NativeSocket socket, bool should_close, |
| 66 | + bool child_processes_inherit) |
| 67 | + : Socket(ProtocolUnixDomain, should_close, child_processes_inherit) { |
| 68 | + m_socket = socket; |
| 69 | +} |
61 | 70 |
|
62 | 71 | DomainSocket::DomainSocket(SocketProtocol protocol, |
63 | 72 | bool child_processes_inherit) |
@@ -108,14 +117,31 @@ Status DomainSocket::Listen(llvm::StringRef name, int backlog) { |
108 | 117 | return error; |
109 | 118 | } |
110 | 119 |
|
111 | | -Status DomainSocket::Accept(Socket *&socket) { |
112 | | - Status error; |
113 | | - auto conn_fd = AcceptSocket(GetNativeSocket(), nullptr, nullptr, |
114 | | - m_child_processes_inherit, error); |
115 | | - if (error.Success()) |
116 | | - socket = new DomainSocket(conn_fd, *this); |
| 120 | +llvm::Expected<std::vector<MainLoopBase::ReadHandleUP>> DomainSocket::Accept( |
| 121 | + MainLoopBase &loop, |
| 122 | + std::function<void(std::unique_ptr<Socket> socket)> sock_cb) { |
| 123 | + // TODO: Refactor MainLoop to avoid the shared_ptr requirement. |
| 124 | + auto io_sp = std::make_shared<DomainSocket>(GetNativeSocket(), false, |
| 125 | + m_child_processes_inherit); |
| 126 | + auto cb = [this, sock_cb](MainLoopBase &loop) { |
| 127 | + Log *log = GetLog(LLDBLog::Host); |
| 128 | + Status error; |
| 129 | + auto conn_fd = AcceptSocket(GetNativeSocket(), nullptr, nullptr, |
| 130 | + m_child_processes_inherit, error); |
| 131 | + if (error.Fail()) { |
| 132 | + LLDB_LOG(log, "AcceptSocket({0}): {1}", GetNativeSocket(), error); |
| 133 | + return; |
| 134 | + } |
| 135 | + std::unique_ptr<DomainSocket> sock_up(new DomainSocket(conn_fd, *this)); |
| 136 | + sock_cb(std::move(sock_up)); |
| 137 | + }; |
117 | 138 |
|
118 | | - return error; |
| 139 | + Status error; |
| 140 | + std::vector<MainLoopBase::ReadHandleUP> handles; |
| 141 | + handles.emplace_back(loop.RegisterReadObject(io_sp, cb, error)); |
| 142 | + if (error.Fail()) |
| 143 | + return error.ToError(); |
| 144 | + return handles; |
119 | 145 | } |
120 | 146 |
|
121 | 147 | size_t DomainSocket::GetNameOffset() const { return 0; } |
|
0 commit comments