31using namespace std::literals;
32namespace fs = std::filesystem;
34namespace platf::prelogin {
38 constexpr auto generated_marker =
"# hermes-prelogin: generated"sv;
41 constexpr std::array session_targets {
42 "graphical-session.target"sv,
43 "graphical-session-pre.target"sv,
44 "xdg-desktop-autostart.target"sv,
48 constexpr std::array dependency_keys {
59 constexpr auto docs_pointer =
"The steps are in docs/getting_started.md, under \"Before anyone logs in\"."sv;
61 std::string_view trim(std::string_view text) {
62 while (!text.empty() && std::isspace(
static_cast<unsigned char>(text.front()))) {
63 text.remove_prefix(1);
65 while (!text.empty() && std::isspace(
static_cast<unsigned char>(text.back()))) {
66 text.remove_suffix(1);
71 std::string lower(std::string_view text) {
72 std::string out {text};
73 std::transform(out.begin(), out.end(), out.begin(), [](
unsigned char c) {
74 return static_cast<char>(std::tolower(c));
79 std::vector<std::string_view> lines_of(std::string_view text) {
80 std::vector<std::string_view> lines;
81 while (!text.empty()) {
82 const auto end = text.find(
'\n');
83 lines.push_back(text.substr(0, end));
84 if (end == std::string_view::npos) {
87 text.remove_prefix(end + 1);
92 std::optional<std::string>
read_file(
const fs::path &path) {
93 std::ifstream in {path, std::ios::binary};
97 return std::string {std::istreambuf_iterator<char> {in}, std::istreambuf_iterator<char> {}};
100 std::string env_or(
const char *name,
const std::string &fallback) {
101 const char *value = std::getenv(name);
102 return value && *value ? std::string {value} : fallback;
106 if (
const char *value = std::getenv(
"HOME"); value && *value) {
109 if (
const auto *account = ::getpwuid(::getuid()); account && account->pw_dir) {
110 return account->pw_dir;
115 fs::path user_unit_dir() {
116 return fs::path {env_or(
"XDG_CONFIG_HOME", home() +
"/.config")} /
"systemd" /
"user";
120 fs::path linger_marker() {
121 return fs::path {env_or(
"XDG_STATE_HOME", home() +
"/.local/state")} /
"hermes" /
"prelogin-linger";
124 struct packaged_unit_t {
135 std::optional<packaged_unit_t> find_packaged_unit() {
137 for (
const char *name : {
"hermes.service",
"sunshine.service"}) {
138 for (
const char *dir : {
"/etc/systemd/user",
"/usr/local/lib/systemd/user",
"/usr/lib/systemd/user"}) {
139 const auto path = fs::path {dir} / name;
140 if (fs::is_regular_file(path, ec)) {
141 return packaged_unit_t {name, path};
155 std::optional<bool> opens_without_seat_grant(
const fs::path &node,
bool write_required =
true) {
157 if (::stat(node.c_str(), &info) != 0) {
160 const uid_t uid = ::getuid();
161 if (info.st_uid == uid) {
162 return (info.st_mode & S_IRUSR) && (!write_required || (info.st_mode & S_IWUSR));
165 const auto *account = ::getpwuid(uid);
166 std::vector<gid_t> groups;
169 groups.resize(
static_cast<size_t>(count));
170 if (::getgrouplist(account->pw_name, account->pw_gid, groups.data(), &count) < 0) {
171 groups.resize(
static_cast<size_t>(count));
172 ::getgrouplist(account->pw_name, account->pw_gid, groups.data(), &count);
174 groups.resize(
static_cast<size_t>(std::max(count, 0)));
176 if (std::find(groups.begin(), groups.end(), info.st_gid) != groups.end()) {
177 return (info.st_mode & S_IRGRP) && (!write_required || (info.st_mode & S_IWGRP));
179 return (info.st_mode & S_IROTH) && (!write_required || (info.st_mode & S_IWOTH));
182 std::optional<bool> hermes_kms_opens_without_seat_grant() {
184 std::optional<bool> result;
185 for (
const auto &entry : fs::directory_iterator {
"/sys/class/drm", ec}) {
186 const auto name = entry.path().filename().string();
187 if (!name.starts_with(
"renderD")) {
190 const auto driver = fs::read_symlink(entry.path() /
"device" /
"driver", ec);
191 if (ec || driver.filename() !=
"hermes-kms") {
198 const auto opens = opens_without_seat_grant(fs::path {
"/dev/dri"} / name,
false);
199 if (opens.value_or(
false)) {
210 void append_conf_dir(
const fs::path &dir, std::vector<std::string> &files) {
212 std::vector<fs::path> paths;
213 for (
const auto &entry : fs::directory_iterator {dir, ec}) {
214 if (entry.path().extension() ==
".conf") {
215 paths.push_back(entry.path());
218 std::sort(paths.begin(), paths.end());
219 for (
const auto &path : paths) {
221 files.push_back(std::move(*text));
226 const char *marker_for(
state_e state) {
241 std::cout <<
"Reaching this machine before anyone logs in: "
244 for (
const auto &check :
report.checks) {
245 std::cout <<
" " << marker_for(check.state) <<
" " << check.title <<
": " << check.detail <<
'\n';
246 if (!check.fix.empty()) {
247 std::cout <<
" -> " << check.fix <<
'\n';
250 std::cout << std::endl;
254 bool run(
const std::string &
command) {
255 bool timed_out =
false;
256 if (VDISPLAY::boundedCommandOutput(
command +
" >/dev/null 2>&1", 30s, &timed_out)) {
259 std::cerr << (timed_out ?
"Timed out: " :
"Failed: ") <<
command << std::endl;
263 int usage(
const char *name) {
264 std::cerr <<
"Usage: " << name <<
" --prelogin status|enable|disable\n\n"
265 <<
" status say what stands between this machine and being reachable before login\n"
266 <<
" enable start Hermes with your user manager, and that manager at boot\n"
267 <<
" disable go back to starting Hermes with the graphical session\n"
273 const auto packaged = find_packaged_unit();
275 std::cerr <<
"No packaged hermes.service or sunshine.service was found to start at boot." << std::endl;
278 const auto source =
read_file(packaged->path);
280 std::cerr <<
"Cannot read " << packaged->path.string() <<
'.' << std::endl;
284 const auto target = user_unit_dir() / packaged->name;
286 std::cerr << target.string() <<
" is a unit of your own, and this would replace it. Move it away first."
292 fs::create_directories(target.parent_path(), ec);
294 std::ofstream out {target, std::ios::binary | std::ios::trunc};
297 std::cerr <<
"Cannot write " << target.string() <<
'.' << std::endl;
301 if (!run(
"systemctl --user daemon-reload") || !run(
"systemctl --user enable " + packaged->name)) {
306 if (!run(
"loginctl enable-linger")) {
309 const auto marker = linger_marker();
310 fs::create_directories(marker.parent_path(), ec);
311 std::ofstream {marker} <<
"enabled by hermes --prelogin\n";
314 const auto result =
report();
316 std::cout <<
"This takes effect the next time Hermes starts: at the next boot, or with\n"
317 <<
"`systemctl --user restart " << packaged->name <<
"`, which ends a stream in progress." << std::endl;
318 return result.ready ? 0 : 1;
323 const auto packaged = find_packaged_unit();
325 const auto target = user_unit_dir() / packaged->name;
326 if (
const auto existing =
read_file(target)) {
328 std::cerr << target.string() <<
" is a unit of your own; it was left as it is." << std::endl;
332 run(
"systemctl --user disable " + packaged->name);
333 fs::remove(target, ec);
334 run(
"systemctl --user daemon-reload");
335 run(
"systemctl --user enable " + packaged->name);
340 const auto marker = linger_marker();
341 if (fs::exists(marker, ec)) {
342 run(
"loginctl disable-linger");
343 fs::remove(marker, ec);
352 std::string state_name(
state_e state) {
367 for (
const auto line : lines_of(unit)) {
368 if (trim(line) == generated_marker) {
376 for (
const auto raw : lines_of(unit)) {
377 const auto line = trim(raw);
378 if (line.empty() || line.front() ==
'#' || line.front() ==
';') {
381 const auto equals = line.find(
'=');
382 if (equals == std::string_view::npos) {
385 std::istringstream words {std::string {line.substr(equals + 1)}};
386 for (std::string word; words >> word;) {
387 if (std::find(session_targets.begin(), session_targets.end(), word) != session_targets.end()) {
395 std::string
from_boot_unit(std::string_view packaged_unit, std::string_view source_path) {
396 std::ostringstream out;
397 out << generated_marker <<
'\n'
398 <<
"# Written by `hermes --prelogin enable` from " << source_path <<
": the same\n"
399 <<
"# service, started with the user manager instead of the graphical session, so\n"
400 <<
"# that it is running before anyone logs in. `hermes --prelogin disable`\n"
401 <<
"# removes this file; changes made here are lost the next time it is written.\n";
404 bool restart_seen =
false;
405 bool install_written =
false;
408 const auto close_section = [&]() {
409 if (section ==
"[Service]" && !restart_seen) {
410 out <<
"Restart=on-failure\nRestartSec=5s\n";
412 if (section ==
"[Install]") {
413 out <<
"WantedBy=default.target\n";
414 install_written =
true;
418 for (
const auto raw : lines_of(packaged_unit)) {
419 const auto line = trim(raw);
421 if (line.empty() || line.front() ==
'#' || line.front() ==
';') {
424 if (line.front() ==
'[' && line.back() ==
']') {
426 section = std::string {line};
427 out <<
'\n' << section <<
'\n';
431 const auto equals = line.find(
'=');
432 const auto key = trim(line.substr(0, equals));
433 const auto value = equals == std::string_view::npos ? std::string_view {} : trim(line.substr(equals + 1));
435 if (section ==
"[Unit]" && std::find(dependency_keys.begin(), dependency_keys.end(), key) != dependency_keys.end()) {
437 std::istringstream words {std::string {value}};
438 for (std::string word; words >> word;) {
439 if (std::find(session_targets.begin(), session_targets.end(), word) == session_targets.end()) {
440 kept += kept.empty() ? word :
' ' + word;
444 out << key <<
'=' << kept <<
'\n';
448 if (section ==
"[Install]" && (key ==
"WantedBy" || key ==
"RequiredBy" || key ==
"UpheldBy")) {
451 if (section ==
"[Service]" && key ==
"Restart") {
457 if (!install_written) {
458 out <<
"\n[Install]\nWantedBy=default.target\n";
465 for (
const auto &file : files) {
467 for (
const auto raw : lines_of(file)) {
468 const auto line = trim(raw);
469 if (line.empty() || line.front() ==
'#' || line.front() ==
';') {
472 if (line.front() ==
'[' && line.back() ==
']') {
473 section = lower(line);
476 const auto equals = line.find(
'=');
477 if (equals == std::string_view::npos) {
480 const auto key = lower(trim(line.substr(0, equals)));
481 const auto value = trim(line.substr(equals + 1));
482 if (section ==
"[general]" && key ==
"displayserver") {
484 }
else if (section ==
"[autologin]" && key ==
"user") {
496 const auto add = [&
report](std::string id,
state_e state, std::string title, std::string detail, std::string fix = {}) {
497 report.checks.push_back({std::move(
id), state, std::move(title), std::move(detail), std::move(fix)});
500 const auto user = facts.user.empty() ?
"this user"s : facts.user;
504 ready &= add(
"unit",
state_e::missing,
"Start at boot",
"No packaged hermes.service or sunshine.service was found, so there is no unit to start at boot.",
"Install Hermes from a package, or write a user unit that starts it from default.target.");
506 ready &= add(
"unit",
state_e::ready,
"Start at boot", facts.
unit_name + (facts.
unit_generated ?
" starts with the user manager." :
" is a unit of your own that starts with the user manager."));
508 ready &= add(
"unit",
state_e::missing,
"Start at boot", facts.
unit_name +
" starts with the graphical session, and there is none before login.",
"Run `hermes --prelogin enable`.");
512 ready &= add(
"linger",
state_e::unknown,
"User manager at boot",
"Whether the user manager for " + user +
" runs from boot could not be read.");
513 }
else if (*facts.linger) {
514 ready &= add(
"linger",
state_e::ready,
"User manager at boot",
"The user manager for " + user +
" runs from boot.");
516 ready &= add(
"linger",
state_e::missing,
"User manager at boot",
"The user manager for " + user +
" only runs while they are logged in.",
"Run `hermes --prelogin enable`, or `loginctl enable-linger " + user +
"`.");
520 ready &= add(
"greeter",
state_e::unknown,
"Login screen",
"No display manager is enabled, so there is no login screen to stream.");
523 ready &= add(
"greeter",
state_e::unknown,
"Login screen",
"SDDM's configuration could not be read.");
524 }
else if (facts.
sddm->display_server ==
"wayland") {
525 ready &= add(
"greeter",
state_e::ready,
"Login screen",
"SDDM uses its Wayland greeter, which lights a new output by itself.");
527 ready &= add(
"greeter",
state_e::missing,
"Login screen",
"SDDM uses its X11 greeter, which never lights a virtual display.",
"As root, set DisplayServer=wayland for SDDM, in /etc/sddm.conf too if it is set there. " + std::string {docs_pointer});
529 if (facts.
sddm && !facts.
sddm->autologin_user.empty()) {
530 add(
"autologin",
state_e::note,
"Autologin",
"SDDM logs " + facts.
sddm->autologin_user +
" in by itself, so the login screen is never shown and none of this is used while that stays on.");
536 ready &= add(
"greeter",
state_e::ready,
"Login screen",
"Plasma Login Manager's greeter runs on KWin Wayland, which lights a new output by itself.");
538 ready &= add(
"greeter",
state_e::unknown,
"Login screen",
"GDM's greeter is a Wayland session and should light a new output, but it has not been run with Hermes.");
540 ready &= add(
"greeter",
state_e::unknown,
"Login screen", facts.
display_manager +
" has not been run with Hermes; its greeter has to light a new output by itself.");
544 ready &= add(
"input",
state_e::unknown,
"Input before login",
"/dev/uinput was not found.");
546 ready &= add(
"input",
state_e::ready,
"Input before login",
"Hermes can create its keyboard and mouse with no session of its own.");
548 ready &= add(
"input",
state_e::missing,
"Input before login",
"/dev/uinput is granted to whoever is on the seat, which before login is the greeter.",
"Run `sudo usermod -aG input " + user +
"` and reboot.");
551 if (facts.virtual_display_backend !=
"hermes_kms") {
552 ready &= add(
"virtual_display",
state_e::missing,
"Virtual display",
"Only the Hermes-KMS virtual display has been run before login; this host is set to '" + facts.virtual_display_backend +
"'.",
"Set virtual_display_backend to hermes_kms.");
554 ready &= add(
"virtual_display",
state_e::unknown,
"Virtual display",
"No Hermes-KMS device was found.",
"Install and load the Hermes-KMS driver.");
556 ready &= add(
"virtual_display",
state_e::ready,
"Virtual display",
"The Hermes-KMS device opens with no session.");
558 ready &= add(
"virtual_display",
state_e::missing,
"Virtual display",
"The Hermes-KMS device belongs to root until someone is on the seat.",
"Run `sudo /usr/lib/hermes-kms/hermes-kms-setup configure --user auto`, from the hermes-kms-seatd package (hermes-kms-seatd-git on Arch).");
569 if (
const auto *account = ::getpwuid(::getuid()); account && account->pw_name) {
570 facts.user = account->pw_name;
573 if (
const auto packaged = find_packaged_unit()) {
575 const auto dir = user_unit_dir();
576 if (
const auto text = read_file(dir / packaged->name)) {
580 facts.
unit_enabled = fs::is_symlink(dir /
"default.target.wants" / packaged->name, ec);
583 if (!facts.user.empty() && fs::is_directory(
"/var/lib/systemd/linger", ec)) {
584 facts.linger = fs::exists(fs::path {
"/var/lib/systemd/linger"} / facts.user, ec);
587 const auto display_manager = fs::read_symlink(
"/etc/systemd/system/display-manager.service", ec);
592 std::vector<std::string> files;
593 append_conf_dir(
"/usr/lib/sddm/sddm.conf.d", files);
594 append_conf_dir(
"/etc/sddm.conf.d", files);
595 if (
auto text = read_file(
"/etc/sddm.conf")) {
596 files.push_back(std::move(*text));
598 if (!files.empty()) {
604 facts.virtual_display_backend = config::video.virtual_display_backend;
614 const auto result =
report();
615 if (!result.enabled) {
619 BOOST_LOG(info) <<
"[Prelogin] This host is set up to be reached before anyone logs in."sv;
622 BOOST_LOG(warning) <<
"[Prelogin] Hermes starts at boot, but this host cannot be reached before login yet:"sv;
623 for (
const auto &check : result.checks) {
627 BOOST_LOG(warning) <<
"[Prelogin] "sv << check.title <<
": "sv << check.detail
628 << (check.fix.empty() ?
""s :
" Fix: " + check.fix);
632 int command(
const char *name,
int argc,
char *argv[]) {
636 if (::getuid() == 0) {
637 std::cerr <<
"Run this as the user Hermes streams for, not as root." << std::endl;
641 const std::string_view action {argv[0]};
642 if (action ==
"status"sv) {
643 const auto result =
report();
645 return result.ready ? 0 : 1;
647 if (action ==
"enable"sv) {
650 if (action ==
"disable"sv) {
Declarations for the configuration of Sunshine.
Declarations for logging related functions.
std::string read_file(const char *path)
Read a file to string.
@ existing
Someone else's rule already forwards to the requested destination.
report_t report()
assess(collect_facts()).
bool is_generated_unit(std::string_view unit)
Whether unit was written by from_boot_unit().
int command(const char *name, int argc, char *argv[])
hermes --prelogin status|enable|disable.
report_t assess(const facts_t &facts)
Decide every check from facts.
void log_report()
Say at startup what stands between this host and being reachable before login. Silent unless Hermes i...
facts_t collect_facts()
Look at this machine.
bool unit_waits_for_session(std::string_view unit)
Whether unit depends on a target that only exists once somebody has logged in, whoever wrote it.
sddm_settings_t sddm_settings(const std::vector< std::string > &files)
Read SDDM's settings from its configuration files.
std::string from_boot_unit(std::string_view packaged_unit, std::string_view source_path)
Turn the packaged unit into one that starts with the user manager.
Set up and check what it takes for Hermes to be reachable before login.
@ note
Worth saying, and no obstacle.
@ unknown
Could not be determined, or not something that has been run.
@ missing
Known to be absent; fix says what to do.
std::string unit_name
"hermes.service"; empty when no packaged unit was found.
std::string display_manager
"sddm", "gdm", ...; empty when none is enabled.
std::optional< bool > hermes_kms_without_session
A Hermes-KMS node opens with no seat grant.
bool unit_from_boot
A unit in the user's own directory that does not wait for the session.
bool unit_generated
...and it is the copy --prelogin enable writes.
std::optional< bool > input_without_session
/dev/uinput opens with no seat grant.
std::optional< sddm_settings_t > sddm
Unset when SDDM's configuration could not be read.
bool unit_enabled
The unit is wanted by default.target.
bool enabled
Hermes' unit is set to start at boot.
bool ready
Every requirement is known to be in place.
std::string display_server
[General] DisplayServer, lower-case; empty when unset.
std::string autologin_user
[Autologin] User; SDDM logs in by itself whenever it is set.
Declarations for utility functions.
Virtual display declarations for Linux.