MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
UDSBackend.cpp
Go to the documentation of this file.
1#include "UDSBackend.hpp"
2
3#include "StreamBackend.inl"
4
6
7namespace MayaFlux::Core {
8
9namespace {
10
11 bool socket_answers(const std::filesystem::path& path)
12 {
13 using Stream = asio::local::stream_protocol;
14 try {
15 asio::io_context context;
16 Stream::socket probe(context);
17 probe.open(Stream());
18 probe.non_blocking(true);
19
20 asio::error_code ec;
21 (void)probe.connect(Stream::endpoint(path.string()), ec);
22
23 return !(ec == asio::error::connection_refused
24 || ec == std::errc::no_such_file_or_directory);
25 } catch (const std::exception&) {
26 return true;
27 }
28 }
29
30 Platform::UnixSocketSource source_of(const std::string& text)
31 {
32 if (text.find_first_of("/\\") != std::string::npos) {
33 return Platform::UnixSocketFromPath { .path = text };
34 }
35 return Platform::UnixSocketFromName { .file_name = text };
36 }
37
38 std::string refusal(const Platform::AcquireFailure& failure)
39 {
41
42 std::string text;
43 switch (failure.reason) {
44 case Reason::EmptyRequest:
45 text = "empty request";
46 break;
47 case Reason::InvalidName:
48 text = "invalid name";
49 break;
50 case Reason::Missing:
51 text = "not found";
52 break;
53 case Reason::NotDirectory:
54 text = "not a directory";
55 break;
56 case Reason::NotRegularFile:
57 text = "not a regular file";
58 break;
59 case Reason::MarkerMissing:
60 text = "marker file missing";
61 break;
62 case Reason::TooLong:
63 text = "path too long";
64 break;
65 case Reason::Unsafe:
66 text = "unsafe location";
67 break;
68 case Reason::NotSocket:
69 text = "not a socket";
70 break;
71 case Reason::InUse:
72 text = "already in use";
73 break;
74 case Reason::StaleNotRemoved:
75 text = "stale socket could not be removed";
76 break;
77 }
78 if (!failure.request.empty()) {
79 text += ": " + failure.request;
80 }
81 return "unix socket refused, " + text;
82 }
83
84} // namespace
85
87{
88 return info.remote_path.empty() && !info.local_path.empty();
89}
90
91std::expected<UdsTransport::Bound, std::string> UdsTransport::bind(
92 Protocol::acceptor& acceptor, const EndpointInfo& info)
93{
94 const auto acquired = Platform::acquire_unix_socket(source_of(info.local_path), socket_answers);
95 if (!acquired) {
96 return std::unexpected(refusal(acquired.error()));
97 }
98
99 Bound bound { .path = acquired->path };
100 const Protocol::endpoint endpoint(bound.path.string());
101 asio::error_code ec;
102
103 if (acceptor.open(endpoint.protocol(), ec)) {
104 return std::unexpected("failed to open acceptor: " + ec.message());
105 }
106 if (acceptor.bind(endpoint, ec)) {
107 return std::unexpected("failed to bind " + bound.path.string() + ": " + ec.message());
108 }
109
110 const auto fail = [&](const std::string& message) -> std::unexpected<std::string> {
111 asio::error_code close_ec;
112 (void)acceptor.close(close_ec);
113 release(bound);
114 return std::unexpected(message);
115 };
116
117 if (acceptor.listen(asio::socket_base::max_listen_connections, ec)) {
118 return fail("failed to listen on " + bound.path.string() + ": " + ec.message());
119 }
120 if (const auto restricted = Platform::restrict_unix_socket_to_owner(bound.path); !restricted) {
121 return fail(refusal(restricted.error()));
122 }
123
124 return bound;
125}
126
127void UdsTransport::release(const Bound& bound)
128{
129 if (bound.path.empty()) {
130 return;
131 }
132 std::error_code ec;
133 std::filesystem::remove(bound.path, ec);
134}
135
136std::expected<UdsTransport::Protocol::endpoint, std::string> UdsTransport::connect_target(const EndpointInfo& info)
137{
138 const auto resolved = Platform::resolve_unix_socket_path(source_of(info.remote_path));
139 if (!resolved) {
140 return std::unexpected(refusal(resolved.error()));
141 }
142 if (resolved->u8string().size() > Platform::unix_socket_path_limit) {
143 return std::unexpected("socket path too long: " + resolved->string());
144 }
145 return Protocol::endpoint(resolved->string());
146}
147
148void UdsTransport::describe_accepted(const Protocol::socket&,
149 const EndpointInfo& listener, EndpointInfo& accepted)
150{
151 accepted.local_path = listener.local_path;
152}
153
154std::string UdsTransport::describe(const EndpointInfo& info)
155{
156 return info.remote_path.empty() ? info.local_path : info.remote_path;
157}
158
160{
161 return describe(info);
162}
163
164template class MAYAFLUX_API StreamBackend<UdsTransport>;
165
166} // namespace MayaFlux::Core
Connection-oriented reliable message transport over an Asio stream protocol.
std::variant< UnixSocketFromPath, UnixSocketFromName > UnixSocketSource
constexpr size_t unix_socket_path_limit
Acquired< std::filesystem::path > resolve_unix_socket_path(const UnixSocketSource &source)
Resolve a Unix socket request to a path, for a client that connects to an existing socket.
Acquired< UnixSocket > acquire_unix_socket(const UnixSocketSource &source, Probe &&is_alive)
Check that a Unix socket path can be bound, removing a stale socket file.
Acquired< void > restrict_unix_socket_to_owner(const std::filesystem::path &path)
Make a bound socket file usable by its owner only.
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.
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)