3#include "StreamBackend.inl"
11 bool socket_answers(
const std::filesystem::path& path)
13 using Stream = asio::local::stream_protocol;
15 asio::io_context context;
16 Stream::socket probe(context);
18 probe.non_blocking(
true);
21 (void)probe.connect(Stream::endpoint(path.string()), ec);
23 return !(ec == asio::error::connection_refused
24 || ec == std::errc::no_such_file_or_directory);
25 }
catch (
const std::exception&) {
32 if (text.find_first_of(
"/\\") != std::string::npos) {
33 return Platform::UnixSocketFromPath { .path = text };
35 return Platform::UnixSocketFromName { .file_name = text };
38 std::string refusal(
const Platform::AcquireFailure& failure)
43 switch (failure.reason) {
44 case Reason::EmptyRequest:
45 text =
"empty request";
47 case Reason::InvalidName:
48 text =
"invalid name";
53 case Reason::NotDirectory:
54 text =
"not a directory";
56 case Reason::NotRegularFile:
57 text =
"not a regular file";
59 case Reason::MarkerMissing:
60 text =
"marker file missing";
63 text =
"path too long";
66 text =
"unsafe location";
68 case Reason::NotSocket:
69 text =
"not a socket";
72 text =
"already in use";
74 case Reason::StaleNotRemoved:
75 text =
"stale socket could not be removed";
78 if (!failure.request.empty()) {
79 text +=
": " + failure.request;
81 return "unix socket refused, " + text;
96 return std::unexpected(refusal(acquired.error()));
99 Bound bound { .path = acquired->path };
100 const Protocol::endpoint endpoint(bound.
path.string());
103 if (acceptor.open(endpoint.protocol(), ec)) {
104 return std::unexpected(
"failed to open acceptor: " + ec.message());
106 if (acceptor.bind(endpoint, ec)) {
107 return std::unexpected(
"failed to bind " + bound.
path.string() +
": " + ec.message());
110 const auto fail = [&](
const std::string& message) -> std::unexpected<std::string> {
111 asio::error_code close_ec;
112 (void)acceptor.close(close_ec);
114 return std::unexpected(message);
117 if (acceptor.listen(asio::socket_base::max_listen_connections, ec)) {
118 return fail(
"failed to listen on " + bound.
path.string() +
": " + ec.message());
121 return fail(refusal(restricted.error()));
129 if (bound.
path.empty()) {
133 std::filesystem::remove(bound.
path, ec);
140 return std::unexpected(refusal(resolved.error()));
143 return std::unexpected(
"socket path too long: " + resolved->string());
145 return Protocol::endpoint(resolved->string());
Connection-oriented reliable message transport over an Asio stream protocol.
std::string remote_path
UDS: socket to connect to, given the same way as local_path.
std::string local_path
UDS: socket to listen on. A bare file name is placed in the user's runtime directory.
Describes one logical send/receive endpoint managed by a backend.
std::filesystem::path path
static std::string describe(const EndpointInfo &info)
static std::expected< Bound, std::string > bind(Protocol::acceptor &acceptor, const EndpointInfo &info)
static std::expected< Protocol::endpoint, std::string > connect_target(const EndpointInfo &info)
static bool is_listener(const EndpointInfo &info)
static void release(const Bound &bound)
static void describe_accepted(const Protocol::socket &socket, const EndpointInfo &listener, EndpointInfo &accepted)
static std::string peer_label(const EndpointInfo &info)