17namespace platf::session_broker {
21 constexpr const char *default_socket_path =
"/run/hermes/session-broker.sock";
23 bool valid_client_id(std::string_view value) {
24 return !value.empty() && value.size() <= 64 && std::ranges::all_of(value, [](
char ch) {
25 return (ch >=
'0' && ch <=
'9') || (ch >=
'a' && ch <=
'f') ||
26 (ch >=
'A' && ch <=
'F') || ch ==
'-';
30 std::string socket_path() {
33 if (
const char *override_path = std::getenv(
"HERMES_SESSION_BROKER_SOCKET")) {
36 return default_socket_path;
48 std::optional<std::string> ask(
const std::string &request) {
51 BOOST_LOG(warning) <<
"[session-broker] " << result.error;
53 return std::move(result.reply);
56 std::vector<std::string> fields(
const std::string &line) {
57 std::vector<std::string> out;
58 std::istringstream stream {line};
60 while (stream >> field) {
61 out.emplace_back(field);
71 std::optional<std::vector<std::string>> succeeded(
72 const std::optional<std::string> &reply,
78 auto parts = fields(*reply);
79 if (parts.empty() || parts.front() !=
"OK") {
80 BOOST_LOG(warning) <<
"[session-broker] " << request <<
" was refused: " << *reply;
83 parts.erase(parts.begin());
90 std::optional<account_t> account_from(
91 const std::optional<std::string> &reply,
97 std::istringstream stream {*reply};
98 std::string tag, uid_text;
100 if (!(stream >> tag >> account.user >> uid_text) || tag !=
"OK") {
101 BOOST_LOG(warning) <<
"[session-broker] " << request <<
" was refused or malformed: " << *reply;
104 std::getline(stream, account.home);
105 if (account.home.starts_with(
' ')) {
106 account.home.erase(0, 1);
108 const auto parsed = std::from_chars(uid_text.data(), uid_text.data() + uid_text.size(), account.uid);
109 const bool name = account.user.starts_with(
"hermes-s") && account.user.size() >= 9 && account.user.size() <= 16 &&
110 std::ranges::all_of(std::string_view {account.user}.substr(8), [](
char ch) {
111 return ch >=
'0' && ch <=
'9';
113 if (!name || account.home.empty() || account.home.front() !=
'/' ||
114 parsed.ec != std::errc {} || parsed.ptr != uid_text.data() + uid_text.size() ||
115 account.uid == 0 || account.uid == std::numeric_limits<uid_t>::max()) {
116 BOOST_LOG(error) <<
"[session-broker] " << request <<
" returned an unusable account: " << *reply;
127 return ::stat(socket_path().c_str(), &info) == 0 && S_ISSOCK(info.st_mode);
130 std::optional<account_t>
ensure(
const std::string &client_uuid) {
131 if (!valid_client_id(client_uuid)) {
134 const auto account = account_from(ask(
"ENSURE " + client_uuid +
"\n"),
"ENSURE");
136 BOOST_LOG(info) <<
"[session-broker] Client " << client_uuid <<
" owns account "
137 << account->user <<
" (uid " << account->uid <<
").";
142 std::optional<account_t>
lookup(
const std::string &client_uuid) {
143 if (!valid_client_id(client_uuid)) {
146 return account_from(ask(
"LOOKUP " + client_uuid +
"\n"),
"LOOKUP");
150 const std::string &client_uuid,
151 const std::vector<std::string> &arguments,
152 const std::vector<std::string> &environment,
153 const std::string &scope
155 if (!valid_client_id(client_uuid) || arguments.empty()) {
158 if (scope.size() > 16 || !std::ranges::all_of(scope, [](
char ch) {
159 return (ch >=
'a' && ch <=
'z') || (ch >=
'0' && ch <=
'9');
161 BOOST_LOG(error) <<
"[session-broker] Refusing a malformed application scope.";
165 std::string request =
"START " + client_uuid +
"\n";
166 for (
const auto &argument : arguments) {
170 if (argument.find(
'\n') != std::string::npos) {
171 BOOST_LOG(error) <<
"[session-broker] Refusing to send an argument containing a newline.";
174 request +=
"ARG " + argument +
"\n";
176 for (
const auto &assignment : environment) {
177 if (assignment.find(
'\n') != std::string::npos) {
178 BOOST_LOG(error) <<
"[session-broker] Refusing to send an environment value "
179 "containing a newline.";
182 request +=
"ENV " + assignment +
"\n";
184 if (!scope.empty()) {
185 request +=
"SCOPE " + scope +
"\n";
189 const auto parts = succeeded(ask(request),
"START");
190 if (!parts || parts->empty()) {
193 BOOST_LOG(info) <<
"[session-broker] Started " << parts->front() <<
" for client "
194 << client_uuid <<
'.';
195 return parts->front();
199 if (!valid_client_id(client_uuid)) {
204 const auto reply = ask(
"SOCKET " + client_uuid +
"\n");
208 const auto parts = fields(*reply);
209 if (parts.size() < 2 || parts.front() !=
"OK") {
216 if (!valid_client_id(client_uuid)) {
219 const auto reply = ask(
"STATUS " + client_uuid +
"\n");
220 if (reply && reply->starts_with(
"ERR notfound ")) {
223 const auto parts = succeeded(reply,
"STATUS");
224 if (!parts || parts->size() != 2) {
227 constexpr std::string_view prefix {
"hermes-session-"}, suffix {
".service"};
228 const std::string_view unit {parts->back()};
229 if (!unit.starts_with(prefix) || !unit.ends_with(suffix) || unit.size() <= prefix.size() + suffix.size() ||
230 !std::ranges::all_of(unit.substr(prefix.size(), unit.size() - prefix.size() - suffix.size()), [](
char ch) {
231 return ch >=
'0' && ch <=
'9';
235 const auto &state = parts->front();
236 if (state ==
"active" || state ==
"activating" || state ==
"reloading") {
239 if (state ==
"inactive" || state ==
"failed" || state ==
"deactivating") {
245 bool active(
const std::string &client_uuid) {
249 bool stop(
const std::string &client_uuid) {
250 if (!valid_client_id(client_uuid)) {
253 return succeeded(ask(
"STOP " + client_uuid +
"\n"),
"STOP").has_value();
Declarations for logging related functions.
std::optional< account_t > lookup(const std::string &client_uuid)
The client's account if it already has one, without creating it.
std::optional< std::string > start(const std::string &client_uuid, const std::vector< std::string > &arguments, const std::vector< std::string > &environment, const std::string &scope)
Start arguments as the client's account, and return the unit name.
std::optional< account_t > ensure(const std::string &client_uuid)
The client's account, created if this is the first time it asked.
bool available()
Whether a broker socket is there to talk to.
std::optional< bool > query_active(const std::string &client_uuid)
Query liveness without treating communication failure as session exit.
bool stop(const std::string &client_uuid)
Stop the client's session unit. Stopping one that is already gone succeeds, so this is safe to call o...
std::optional< std::string > wayland_socket(const std::string &client_uuid)
The name of the Wayland socket the session's compositor created.
bool active(const std::string &client_uuid)
Whether the client's session is running right now.
Ask the privileged broker for a client's own Unix account, and for a session running under it.
exchange_result exchange(const std::string &path, const std::string &request, std::chrono::milliseconds timeout)
Send one request and read one complete reply within a shared deadline.
std::chrono::milliseconds request_timeout(std::string_view request)
Longer budgets for serialized account/lifetime mutations, short polls otherwise.
Deadline-bounded Unix transport for the session broker client.
The account a paired client owns.