MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ acquire_unix_socket()

template<std::predicate< const std::filesystem::path & > Probe>
Acquired< UnixSocket > MayaFlux::Platform::acquire_unix_socket ( const UnixSocketSource & source,
Probe && is_alive )
nodiscard

Check that a Unix socket path can be bound, removing a stale socket file.

Refuses a path that is too long, whose directory is missing or writable by others, or that holds something other than a socket. A socket file that is_alive says answers is refused as in use. One that does not is removed. Nothing is created, and bind can still fail, since another process may take the path first.

Parameters
sourceThe request.
is_aliveCalled with an existing socket's path, returns true if a server answers on it.
Returns
The socket path, or the reason it was refused and the paths checked.

Definition at line 228 of file UnixSocket.hpp.

229{
230 auto resolved = detail::resolve_socket_path(source);
231 if (!resolved) {
232 return std::unexpected(resolved.error());
233 }
234 const std::filesystem::path path = std::move(*resolved);
235
236 if (path.u8string().size() > unix_socket_path_limit) {
237 return std::unexpected(detail::make_failure(AcquireFailure::Reason::TooLong, path.string(), { path }));
238 }
239
240 const std::filesystem::path directory = path.parent_path();
241 if (!detail::path_exists(directory)) {
242 return std::unexpected(detail::make_failure(AcquireFailure::Reason::Missing, path.string(), { directory }));
243 }
244 if (!detail::path_is_directory(directory)) {
245 return std::unexpected(detail::make_failure(AcquireFailure::Reason::NotDirectory, path.string(), { directory }));
246 }
247 if (!detail::socket_directory_is_safe(directory)) {
248 return std::unexpected(detail::make_failure(AcquireFailure::Reason::Unsafe, path.string(), { directory }));
249 }
250
251 std::error_code ec;
252 const auto entry = std::filesystem::symlink_status(path, ec);
253 if (entry.type() == std::filesystem::file_type::not_found) {
254 return UnixSocket { .path = path, .replaced_stale = false };
255 }
256 if (ec) {
257 return std::unexpected(detail::make_failure(AcquireFailure::Reason::Unsafe, path.string(), { path }));
258 }
259 if (!detail::entry_is_socket(entry, path)) {
260 return std::unexpected(detail::make_failure(AcquireFailure::Reason::NotSocket, path.string(), { path }));
261 }
262
263 if (is_alive(path)) {
264 return std::unexpected(detail::make_failure(AcquireFailure::Reason::InUse, path.string(), { path }));
265 }
266
267 std::filesystem::remove(path, ec);
268 if (ec) {
269 return std::unexpected(detail::make_failure(AcquireFailure::Reason::StaleNotRemoved, path.string(), { path }));
270 }
271 return UnixSocket { .path = path, .replaced_stale = true };
272}
bool path_exists(const std::filesystem::path &path)
bool path_is_directory(const std::filesystem::path &path)
bool socket_directory_is_safe(const std::filesystem::path &directory)
Acquired< std::filesystem::path > resolve_socket_path(const UnixSocketSource &source)
AcquireFailure make_failure(AcquireFailure::Reason reason, std::string request, std::vector< std::filesystem::path > tried={})
bool entry_is_socket(const std::filesystem::file_status &status, const std::filesystem::path &path)
constexpr size_t unix_socket_path_limit
A socket path that is safe to bind.

References MayaFlux::Platform::detail::entry_is_socket(), MayaFlux::Platform::AcquireFailure::InUse, MayaFlux::Platform::detail::make_failure(), MayaFlux::Platform::AcquireFailure::Missing, MayaFlux::Platform::AcquireFailure::NotDirectory, MayaFlux::Platform::AcquireFailure::NotSocket, MayaFlux::Platform::detail::path_exists(), MayaFlux::Platform::detail::path_is_directory(), MayaFlux::Platform::detail::resolve_socket_path(), MayaFlux::Platform::detail::socket_directory_is_safe(), MayaFlux::Platform::AcquireFailure::StaleNotRemoved, MayaFlux::Platform::AcquireFailure::TooLong, unix_socket_path_limit, and MayaFlux::Platform::AcquireFailure::Unsafe.

Referenced by MayaFlux::Core::UdsTransport::bind().

Here is the call graph for this function:
Here is the caller graph for this function: