8#include <condition_variable>
24using namespace std::literals;
28namespace platf::session_attach {
33 constexpr std::array display_variables {
36 "XDG_CURRENT_DESKTOP"sv,
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);
55 const std::vector<std::string> startup_environment = copy_environment();
57 constexpr auto poll_interval = 5s;
64 constexpr int idle_polls_required = 3;
66 std::vector<std::string> pending_restart_environment;
68 class watcher_t:
public platf::deinit_t {
76 ~watcher_t()
override {
78 std::lock_guard lock {mutex};
82 if (thread.joinable()) {
90 std::unique_lock lock {mutex};
91 return !wake.wait_for(lock, poll_interval, [
this]() {
98 bool announced =
false;
104 if (!VDISPLAY::graphicalSessionPresent().value_or(
false)) {
123 if (rtsp_stream::session_count() > 0 || launches != launches_seen) {
124 launches_seen = launches;
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;
134 if (++idle_polls < idle_polls_required) {
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;
147 std::condition_variable wake;
148 bool stopping {
false};
155 environment_t environment;
156 for (
const auto &assignment : assignments) {
157 const auto equals = assignment.find(
'=');
158 if (equals == std::string::npos || equals == 0) {
161 environment[assignment.substr(0, equals)] = assignment.substr(equals + 1);
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) {
174 block.remove_prefix(end + 1);
180 const auto value = [&environment](
const char *name) {
181 const auto it = environment.find(name);
182 return it == environment.end() ? std::string {} : it->second;
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;
194 if (
const char *runtime_dir = std::getenv(
"XDG_RUNTIME_DIR")) {
195 live[
"XDG_RUNTIME_DIR"] = runtime_dir;
197 if (!live.contains(
"WAYLAND_DISPLAY") && !live.contains(
"DISPLAY")) {
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());
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;
223 if (value(session,
"WAYLAND_DISPLAY"sv).empty() && value(session,
"DISPLAY"sv).empty()) {
226 for (
const auto name : display_variables) {
227 const auto published = value(session, name);
228 if (!published.empty() && published != value(own, name)) {
235 std::vector<std::string>
merged_environment(
const environment_t &own,
const environment_t &session) {
237 for (
const auto &[name, value] : session) {
238 merged[name] = value;
241 std::vector<std::string> assignments;
242 assignments.reserve(merged.size());
243 for (
const auto &[name, value] : merged) {
244 assignments.push_back(name +
'=' + value);
250 if (!pending_restart_environment.empty()) {
251 return pending_restart_environment;
256 std::unique_ptr<platf::deinit_t>
start() {
266 return std::make_unique<watcher_t>();
Declarations for logging related functions.
uint32_t launch_count()
How many launch and resume requests have been accepted so far.
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.