Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
session_broker.cpp
Go to the documentation of this file.
1
5#include "session_broker.h"
6
8#include "src/logging.h"
9
10#include <algorithm>
11#include <charconv>
12#include <cstdlib>
13#include <limits>
14#include <sstream>
15#include <sys/stat.h>
16
17namespace platf::session_broker {
18
19 namespace {
20
21 constexpr const char *default_socket_path = "/run/hermes/session-broker.sock";
22
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 == '-';
27 });
28 }
29
30 std::string socket_path() {
31 // Overridable so the suite can point at a broker of its own rather than
32 // at the machine's.
33 if (const char *override_path = std::getenv("HERMES_SESSION_BROKER_SOCKET")) {
34 return override_path;
35 }
36 return default_socket_path;
37 }
38
48 std::optional<std::string> ask(const std::string &request) {
49 auto result = detail::exchange(socket_path(), request, detail::request_timeout(request));
50 if (!result.reply) {
51 BOOST_LOG(warning) << "[session-broker] " << result.error;
52 }
53 return std::move(result.reply);
54 }
55
56 std::vector<std::string> fields(const std::string &line) {
57 std::vector<std::string> out;
58 std::istringstream stream {line};
59 std::string field;
60 while (stream >> field) {
61 out.emplace_back(field);
62 }
63 return out;
64 }
65
71 std::optional<std::vector<std::string>> succeeded(
72 const std::optional<std::string> &reply,
73 const char *request
74 ) {
75 if (!reply) {
76 return std::nullopt;
77 }
78 auto parts = fields(*reply);
79 if (parts.empty() || parts.front() != "OK") {
80 BOOST_LOG(warning) << "[session-broker] " << request << " was refused: " << *reply;
81 return std::nullopt;
82 }
83 parts.erase(parts.begin());
84 return parts;
85 }
86
90 std::optional<account_t> account_from(
91 const std::optional<std::string> &reply,
92 const char *request
93 ) {
94 if (!reply) {
95 return std::nullopt;
96 }
97 std::istringstream stream {*reply};
98 std::string tag, uid_text;
99 account_t account;
100 if (!(stream >> tag >> account.user >> uid_text) || tag != "OK") {
101 BOOST_LOG(warning) << "[session-broker] " << request << " was refused or malformed: " << *reply;
102 return std::nullopt;
103 }
104 std::getline(stream, account.home);
105 if (account.home.starts_with(' ')) {
106 account.home.erase(0, 1);
107 }
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';
112 });
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;
117 return std::nullopt;
118 }
119 return account;
120 }
121
122 } // namespace
123
124 bool available() {
125 struct stat info {};
126
127 return ::stat(socket_path().c_str(), &info) == 0 && S_ISSOCK(info.st_mode);
128 }
129
130 std::optional<account_t> ensure(const std::string &client_uuid) {
131 if (!valid_client_id(client_uuid)) {
132 return std::nullopt;
133 }
134 const auto account = account_from(ask("ENSURE " + client_uuid + "\n"), "ENSURE");
135 if (account) {
136 BOOST_LOG(info) << "[session-broker] Client " << client_uuid << " owns account "
137 << account->user << " (uid " << account->uid << ").";
138 }
139 return account;
140 }
141
142 std::optional<account_t> lookup(const std::string &client_uuid) {
143 if (!valid_client_id(client_uuid)) {
144 return std::nullopt;
145 }
146 return account_from(ask("LOOKUP " + client_uuid + "\n"), "LOOKUP");
147 }
148
149 std::optional<std::string> start(
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
154 ) {
155 if (!valid_client_id(client_uuid) || arguments.empty()) {
156 return std::nullopt;
157 }
158 if (scope.size() > 16 || !std::ranges::all_of(scope, [](char ch) {
159 return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
160 })) {
161 BOOST_LOG(error) << "[session-broker] Refusing a malformed application scope.";
162 return std::nullopt;
163 }
164
165 std::string request = "START " + client_uuid + "\n";
166 for (const auto &argument : arguments) {
167 // A newline in an argument would end the line the broker is reading and
168 // turn the rest into a request of its own. Nothing Hermes builds contains
169 // one, so this is a refusal rather than an escape.
170 if (argument.find('\n') != std::string::npos) {
171 BOOST_LOG(error) << "[session-broker] Refusing to send an argument containing a newline.";
172 return std::nullopt;
173 }
174 request += "ARG " + argument + "\n";
175 }
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.";
180 return std::nullopt;
181 }
182 request += "ENV " + assignment + "\n";
183 }
184 if (!scope.empty()) {
185 request += "SCOPE " + scope + "\n";
186 }
187 request += "END\n";
188
189 const auto parts = succeeded(ask(request), "START");
190 if (!parts || parts->empty()) {
191 return std::nullopt;
192 }
193 BOOST_LOG(info) << "[session-broker] Started " << parts->front() << " for client "
194 << client_uuid << '.';
195 return parts->front();
196 }
197
198 std::optional<std::string> wayland_socket(const std::string &client_uuid) {
199 if (!valid_client_id(client_uuid)) {
200 return std::nullopt;
201 }
202 // Quiet on failure: this is polled while a compositor starts, and for most
203 // of that time the honest answer is "not yet".
204 const auto reply = ask("SOCKET " + client_uuid + "\n");
205 if (!reply) {
206 return std::nullopt;
207 }
208 const auto parts = fields(*reply);
209 if (parts.size() < 2 || parts.front() != "OK") {
210 return std::nullopt;
211 }
212 return parts[1];
213 }
214
215 std::optional<bool> query_active(const std::string &client_uuid) {
216 if (!valid_client_id(client_uuid)) {
217 return false;
218 }
219 const auto reply = ask("STATUS " + client_uuid + "\n");
220 if (reply && reply->starts_with("ERR notfound ")) {
221 return false;
222 }
223 const auto parts = succeeded(reply, "STATUS");
224 if (!parts || parts->size() != 2) {
225 return std::nullopt;
226 }
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';
232 })) {
233 return std::nullopt;
234 }
235 const auto &state = parts->front();
236 if (state == "active" || state == "activating" || state == "reloading") {
237 return true;
238 }
239 if (state == "inactive" || state == "failed" || state == "deactivating") {
240 return false;
241 }
242 return std::nullopt;
243 }
244
245 bool active(const std::string &client_uuid) {
246 return query_active(client_uuid).value_or(false);
247 }
248
249 bool stop(const std::string &client_uuid) {
250 if (!valid_client_id(client_uuid)) {
251 return false;
252 }
253 return succeeded(ask("STOP " + client_uuid + "\n"), "STOP").has_value();
254 }
255
256} // namespace platf::session_broker
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.