Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
session_attach.cpp
Go to the documentation of this file.
1
5// standard includes
6#include <array>
7#include <chrono>
8#include <condition_variable>
9#include <cstdlib>
10#include <mutex>
11#include <string_view>
12#include <thread>
13
14// lib includes
15#include <unistd.h>
16
17// local includes
18#include "session_attach.h"
19#include "src/logging.h"
20#include "src/nvhttp.h"
21#include "src/rtsp.h"
22#include "virtual_display.h"
23
24using namespace std::literals;
25
26extern char **environ;
27
28namespace platf::session_attach {
29
30 namespace {
31
33 constexpr std::array display_variables {
34 "WAYLAND_DISPLAY"sv,
35 "DISPLAY"sv,
36 "XDG_CURRENT_DESKTOP"sv,
37 "XDG_SESSION_TYPE"sv,
38 };
39
40 std::vector<std::string> copy_environment() {
41 std::vector<std::string> assignments;
42 for (char **entry = environ; entry && *entry; ++entry) {
43 assignments.emplace_back(*entry);
44 }
45 return assignments;
46 }
47
55 const std::vector<std::string> startup_environment = copy_environment();
56
57 constexpr auto poll_interval = 5s;
58
64 constexpr int idle_polls_required = 3;
65
66 std::vector<std::string> pending_restart_environment;
67
68 class watcher_t: public platf::deinit_t {
69 public:
70 watcher_t():
71 thread {[this]() {
72 run();
73 }} {
74 }
75
76 ~watcher_t() override {
77 {
78 std::lock_guard lock {mutex};
79 stopping = true;
80 }
81 wake.notify_all();
82 if (thread.joinable()) {
83 thread.join();
84 }
85 }
86
87 private:
89 bool sleep() {
90 std::unique_lock lock {mutex};
91 return !wake.wait_for(lock, poll_interval, [this]() {
92 return stopping;
93 });
94 }
95
96 void run() {
97 int idle_polls = 0;
98 bool announced = false;
99 auto launches_seen = nvhttp::launch_count();
100
101 while (sleep()) {
102 // logind first: it is a file read, and it is "no" for as long as
103 // nobody has logged in, which keeps the bus out of the idle case.
104 if (!VDISPLAY::graphicalSessionPresent().value_or(false)) {
105 idle_polls = 0;
106 continue;
107 }
108 const auto published = VDISPLAY::userManagerEnvironment();
109 if (!published) {
110 idle_polls = 0;
111 continue;
112 }
113 const auto own = initial_environment();
114 const auto session = parse_environment(*published);
115 // Published is not the same as running: a desktop that does not
116 // clear its variables at logout leaves them behind for good.
117 if (!attach_needed(own, session) || !display_server_reachable(session)) {
118 idle_polls = 0;
119 continue;
120 }
121
122 const auto launches = nvhttp::launch_count();
123 if (rtsp_stream::session_count() > 0 || launches != launches_seen) {
124 launches_seen = launches;
125 idle_polls = 0;
126 if (!announced) {
127 announced = true;
128 BOOST_LOG(info) << "[Session] A graphical session has started that this Hermes is not part of. It will "
129 "restart itself into it once no client is connected; until then it cannot launch "
130 "applications into the desktop or change its layout."sv;
131 }
132 continue;
133 }
134 if (++idle_polls < idle_polls_required) {
135 continue;
136 }
137
138 BOOST_LOG(info) << "[Session] Restarting into the graphical session that started after Hermes did, with the "
139 "environment the user manager now holds."sv;
140 pending_restart_environment = merged_environment(own, session);
141 platf::restart();
142 return;
143 }
144 }
145
146 std::mutex mutex;
147 std::condition_variable wake;
148 bool stopping {false};
149 std::thread thread;
150 };
151
152 } // namespace
153
154 environment_t parse_environment(const std::vector<std::string> &assignments) {
155 environment_t environment;
156 for (const auto &assignment : assignments) {
157 const auto equals = assignment.find('=');
158 if (equals == std::string::npos || equals == 0) {
159 continue;
160 }
161 environment[assignment.substr(0, equals)] = assignment.substr(equals + 1);
162 }
163 return environment;
164 }
165
166 environment_t parse_environment_block(std::string_view block) {
167 std::vector<std::string> assignments;
168 while (!block.empty()) {
169 const auto end = block.find('\0');
170 assignments.emplace_back(block.substr(0, end));
171 if (end == std::string_view::npos) {
172 break;
173 }
174 block.remove_prefix(end + 1);
175 }
176 return parse_environment(assignments);
177 }
178
179 bool display_server_reachable(const environment_t &environment) {
180 const auto value = [&environment](const char *name) {
181 const auto it = environment.find(name);
182 return it == environment.end() ? std::string {} : it->second;
183 };
184 return VDISPLAY::displayServerReachable(value("WAYLAND_DISPLAY"), value("DISPLAY"), value("XDG_RUNTIME_DIR"));
185 }
186
188 environment_t live;
189 for (const auto name : display_variables) {
190 if (const char *value = std::getenv(std::string {name}.c_str()); value && *value) {
191 live[std::string {name}] = value;
192 }
193 }
194 if (const char *runtime_dir = std::getenv("XDG_RUNTIME_DIR")) {
195 live["XDG_RUNTIME_DIR"] = runtime_dir;
196 }
197 if (!live.contains("WAYLAND_DISPLAY") && !live.contains("DISPLAY")) {
198 return;
199 }
200 if (display_server_reachable(live)) {
201 return;
202 }
203
204 BOOST_LOG(warning) << "[Session] The environment names a display server that is not running (WAYLAND_DISPLAY="sv
205 << (live.contains("WAYLAND_DISPLAY") ? live["WAYLAND_DISPLAY"] : "") << ", DISPLAY="sv
206 << (live.contains("DISPLAY") ? live["DISPLAY"] : "")
207 << "). These are left over from a session that has ended; starting as if nobody were logged in."sv;
208 for (const auto name : display_variables) {
209 ::unsetenv(std::string {name}.c_str());
210 }
211 }
212
213 environment_t initial_environment() {
214 return parse_environment(startup_environment);
215 }
216
217 bool attach_needed(const environment_t &own, const environment_t &session) {
218 const auto value = [](const environment_t &environment, std::string_view name) {
219 const auto it = environment.find(std::string {name});
220 return it == environment.end() ? std::string {} : it->second;
221 };
222
223 if (value(session, "WAYLAND_DISPLAY"sv).empty() && value(session, "DISPLAY"sv).empty()) {
224 return false;
225 }
226 for (const auto name : display_variables) {
227 const auto published = value(session, name);
228 if (!published.empty() && published != value(own, name)) {
229 return true;
230 }
231 }
232 return false;
233 }
234
235 std::vector<std::string> merged_environment(const environment_t &own, const environment_t &session) {
236 auto merged = own;
237 for (const auto &[name, value] : session) {
238 merged[name] = value;
239 }
240
241 std::vector<std::string> assignments;
242 assignments.reserve(merged.size());
243 for (const auto &[name, value] : merged) {
244 assignments.push_back(name + '=' + value);
245 }
246 return assignments;
247 }
248
249 std::vector<std::string> restart_environment() {
250 if (!pending_restart_environment.empty()) {
251 return pending_restart_environment;
252 }
254 }
255
256 std::unique_ptr<platf::deinit_t> start() {
257 // A Hermes someone started from a terminal or over SSH has the environment
258 // that person chose; replacing it with the desktop's would be a surprise.
260 return nullptr;
261 }
262 // Without logind there is no telling when a session appears.
264 return nullptr;
265 }
266 return std::make_unique<watcher_t>();
267 }
268
269} // namespace platf::session_attach
Declarations for logging related functions.
uint32_t launch_count()
How many launch and resume requests have been accepted so far.
Definition nvhttp.cpp:231
Declarations for the nvhttp (GameStream) server.
Declarations for RTSP streaming.
environment_t initial_environment()
The environment Hermes was started with, not the one it has now.
std::vector< std::string > merged_environment(const environment_t &own, const environment_t &session)
own with every variable of session laid over it, as "NAME=value" strings ready for execve().
environment_t parse_environment_block(std::string_view block)
Parse an environment as the kernel keeps it: assignments separated by NUL bytes, as in /proc/self/env...
environment_t parse_environment(const std::vector< std::string > &assignments)
Split "NAME=value" assignments; entries without a name are dropped.
std::vector< std::string > restart_environment()
The environment the next restart must be given, as "NAME=value" strings for execve()....
void drop_stale_display_environment()
Forget display variables that point at a display server that is gone.
bool attach_needed(const environment_t &own, const environment_t &session)
Whether session names a display server that own does not talk to.
bool display_server_reachable(const environment_t &environment)
Whether a display server is listening where environment points.
std::unique_ptr< platf::deinit_t > start()
Start watching for a session to attach to.
Let a Hermes that started before the graphical session catch up with it.
bool inLoginSession()
Whether Hermes itself runs inside a login session - started from a terminal or over SSH - rather than...
bool displayServerReachable(const std::string &wayland_display, const std::string &x_display, const std::string &runtime_dir)
Whether a display server is listening where these variables point.
std::optional< std::vector< std::string > > userManagerEnvironment()
The environment the systemd user manager hands to the units it starts, as "NAME=value" strings.
std::optional< bool > graphicalSessionPresent()
Whether this user has a session of their own, as logind sees it - graphical or not,...
Virtual display declarations for Linux.