[Feature] (Client): read a system config file and decline EGL/GLX/GBM when no server answers

This commit is contained in:
2026-10-02 19:36:38 -04:00
parent d24abe9287
commit 69e96f5b8d
18 changed files with 932 additions and 13 deletions
+105
View File
@@ -0,0 +1,105 @@
// MobileGL - MobileGL/MG_Remote/Client/ServerProbe.cpp
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "ServerProbe.h"
#include <Init.h>
#include <MG_Remote/Transport/SocketTransport.h>
#include <MG_Util/Debug/Log.h>
#include <algorithm>
#include <atomic>
#include <cctype>
#include <cerrno>
#include <chrono>
#include <cstdlib>
namespace MobileGL::MG_Remote::Client {
namespace {
constexpr Uint32 kDefaultProbeTimeoutMs = 250;
constexpr Uint32 kMaxProbeTimeoutMs = 60000;
std::atomic<Bool> g_available{false};
std::atomic<Bool> g_declineLogged{false};
std::string Lowered(std::string text) {
std::transform(text.begin(), text.end(), text.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return text;
}
Uint32 ProbeTimeoutMs() {
String text;
if (!MG_ConfigLoader::LookupSetting("MOBILEGL_IPC_PROBE_TIMEOUT_MS", text)) return kDefaultProbeTimeoutMs;
char* end = nullptr;
errno = 0;
const unsigned long value = std::strtoul(text.c_str(), &end, 10);
if (end == text.c_str() || *end != '\0' || errno == ERANGE || value > kMaxProbeTimeoutMs) {
return kDefaultProbeTimeoutMs;
}
return static_cast<Uint32>(value);
}
} // namespace
Bool ConfiguredProbeTarget(ServerProbeTarget* out) {
String transport;
String control;
if (!MG_ConfigLoader::LookupSetting("MOBILEGL_TRANSPORT", transport) || Lowered(transport) != "spawn") {
return false;
}
if (!MG_ConfigLoader::LookupSetting("MOBILEGL_IPC_CONTROL", control)) return false; // "fork": launches its own
std::string endpoint;
if (control.compare(0, 5, "unix:") == 0) {
endpoint = control.substr(5);
} else if (control.compare(0, 6, "tcp://") == 0) {
endpoint = control;
} else {
return false; // fork, fd:<control>,<aux>: nothing to dial
}
if (endpoint.empty()) return false;
if (out != nullptr) {
out->endpoint = endpoint;
out->timeoutMs = ProbeTimeoutMs();
}
return true;
}
ServerProbeResult ProbeConfiguredServer() {
ServerProbeTarget target;
if (!ConfiguredProbeTarget(&target) || target.timeoutMs == 0) return ServerProbeResult::NotApplicable;
return Transport::SocketTransport::Probe(target.endpoint, target.timeoutMs) ? ServerProbeResult::Reachable
: ServerProbeResult::Unreachable;
}
Bool ConfiguredServerAvailable() {
if (g_available.load(std::memory_order_acquire)) return true;
const auto started = std::chrono::steady_clock::now();
const ServerProbeResult result = ProbeConfiguredServer();
if (result != ServerProbeResult::Unreachable) {
g_available.store(true, std::memory_order_release);
return true;
}
if (!g_declineLogged.exchange(true)) {
ServerProbeTarget target;
(void)ConfiguredProbeTarget(&target);
const auto tookUs = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - started).count();
MGLOG_I("MG_Remote client: no MobileGL server answers at \"%s\" (probed in %lld us); declining, so "
"the system's next GL implementation serves this process",
target.endpoint.c_str(), static_cast<long long>(tookUs));
}
return false;
}
void ResetServerProbeCache() {
g_available.store(false, std::memory_order_release);
g_declineLogged.store(false, std::memory_order_release);
}
} // namespace MobileGL::MG_Remote::Client
+59
View File
@@ -0,0 +1,59 @@
// MobileGL - MobileGL/MG_Remote/Client/ServerProbe.h
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
// IS THE SERVER THIS PROCESS WOULD DIAL THERE AT ALL.
//
// Installed as the system's EGL/GLX vendor, this library is asked first by every GL process on the
// machine, and the session bring-up behind its first display waits up to 20 s for a server that
// may simply not be running. The loaders that ask (EGL and GLX dispatchers, the GBM loader) all
// move on to the next implementation when one declines - so the honest, cheap answer when nobody
// is listening is to decline before that wait starts, and this is the question that decides it.
//
// Only a CONNECT dial can be probed: MOBILEGL_TRANSPORT=spawn with MOBILEGL_IPC_CONTROL naming
// `unix:<path>`, `unix:@<abstract>` or `tcp://host:port`. Everything else - monolith, inproc, a
// fork control that launches its own server, an fd: pair somebody already connected - has no
// remote to be absent and answers NotApplicable. Both settings are read the way the configuration
// loader reads them (environment over the system configuration files), because the probe runs
// before MobileGL initializes.
//
// MOBILEGL_IPC_PROBE_TIMEOUT_MS bounds one probe (default 250, 1..60000); 0 turns probing off.
#pragma once
#include <Includes.h>
#include <string>
namespace MobileGL::MG_Remote::Client {
enum class ServerProbeResult {
NotApplicable, // nothing to dial: the configuration serves the process another way
Reachable, // something accepted a connection at the configured endpoint
Unreachable, // the configured endpoint refused, does not exist, or did not answer in time
};
struct ServerProbeTarget {
std::string endpoint; // SocketTransport form: a path, "@name", or "tcp://host:port"
Uint32 timeoutMs = 0; // 0: probing disabled
};
// What the configuration says to probe; false when it names nothing probeable (NotApplicable).
Bool ConfiguredProbeTarget(ServerProbeTarget* out);
// One probe of the configured server, uncached.
ServerProbeResult ProbeConfiguredServer();
// The cached form the entry points use: once a probe found the server (or there is nothing to
// probe) the answer stays true for the life of the process; a negative answer is re-probed on
// the next call, which is one refused connect.
Bool ConfiguredServerAvailable();
// Forgets a positive answer (tests; and the library's teardown, so a re-initialization asks again).
void ResetServerProbeCache();
} // namespace MobileGL::MG_Remote::Client
@@ -512,6 +512,54 @@ namespace MobileGL::MG_Remote::Transport {
return MOBILEGL_OK;
}
namespace {
// One non-blocking connect to `address`, waited on until `deadline`.
bool ProbeOne(int family, const sockaddr* address, socklen_t length,
std::chrono::steady_clock::time_point deadline) {
const int fd = ::socket(family, SOCK_STREAM, 0);
if (fd < 0) return false;
::fcntl(fd, F_SETFD, FD_CLOEXEC);
::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
bool reachable = ::connect(fd, address, length) == 0;
if (!reachable && family == AF_UNIX && errno == EAGAIN) {
// A unix listener whose backlog is full: it exists and is busy, which is a server.
reachable = true;
} else if (!reachable && errno == EINPROGRESS) {
pollfd pfd{fd, POLLOUT, 0};
int rc;
do {
const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(
deadline - std::chrono::steady_clock::now()).count();
rc = ::poll(&pfd, 1, static_cast<int>(std::max<std::int64_t>(0, remaining)));
} while (rc < 0 && errno == EINTR);
int error = 0;
socklen_t size = sizeof(error);
reachable = rc > 0 && ::getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &size) == 0 && error == 0;
}
::close(fd);
return reachable;
}
} // namespace
bool SocketTransport::Probe(const std::string& path, std::uint32_t timeoutMs) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(std::max(1u, timeoutMs));
if (TcpName(path)) {
addrinfo* addresses = nullptr;
if (!TcpAddress(path, &addresses)) return false;
bool reachable = false;
for (auto* address = addresses; address != nullptr && !reachable; address = address->ai_next) {
reachable = ProbeOne(address->ai_family, address->ai_addr, address->ai_addrlen, deadline);
if (std::chrono::steady_clock::now() >= deadline) break;
}
::freeaddrinfo(addresses);
return reachable;
}
sockaddr_un address{};
socklen_t length = 0;
if (!FillAddress(path, &address, &length)) return false;
return ProbeOne(AF_UNIX, reinterpret_cast<const sockaddr*>(&address), length, deadline);
}
MobileGLResult SocketTransport::ConnectControl(const std::string& path, std::uint32_t timeoutMs,
std::unique_ptr<SocketTransport>& outClient) {
if (!TcpName(path)) return ConnectTo(path, timeoutMs, outClient);
@@ -906,6 +954,8 @@ namespace MobileGL::MG_Remote::Transport {
return MOBILEGL_ERR_UNSUPPORTED;
}
bool SocketTransport::Probe(const std::string&, std::uint32_t) { return false; }
SocketTransport::SocketTransport(int, int, TransportRole role) : m_role(role) {}
SocketTransport::~SocketTransport() = default;
int SocketTransport::TakeDataFd() { return -1; }
@@ -132,6 +132,14 @@ namespace MobileGL::MG_Remote::Transport {
// socket the Welcome's descriptors cross), and since P11 PAIR they too are matched by a
// value - the client's PairBind nonce - not by order.
// Is anything listening at `path` (a ConnectTo endpoint: an AF_UNIX path, '@' + an abstract
// name, or tcp://host:port)? One non-blocking connect per address, bounded by `timeoutMs`
// in total and never retried, then closed without a byte written - the server's pairing
// drops a connection that closes before presenting a PairBind. For deciding whether to
// offer a display at all, so it must not stall: a refused or absent endpoint answers at
// once, and only an unresponsive TCP peer can take the whole bound.
static bool Probe(const std::string& path, std::uint32_t timeoutMs);
// Connects the control connection only on `tcp://`; identical to ConnectTo otherwise.
static MobileGLResult ConnectControl(const std::string& path, std::uint32_t timeoutMs,
std::unique_ptr<SocketTransport>& outClient);