feat(session): verify switches and recover when the greeter stays away
logind accepts loginctl activate and SwitchToGreeter before the graphics stack moves the seat, and SDDM 0.21 answers SwitchToGreeter with a greeter whose logins never start a session. Each request is now confirmed within a timeout and the previous session is reactivated on failure, so no path can leave a blank VT. When nothing answers, both front-ends offer to restart display-manager.service through polkit, and only after that is the session terminated.
This commit is contained in:
5 files changed
+521
-54
No files matched your search
+156
-16
@@ -4,6 +4,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "desktop-catalog.h"
|
||||
#include "session-catalog.h"
|
||||
#include "session-switcher.h"
|
||||
|
||||
namespace {
|
||||
@@ -69,7 +70,7 @@ private:
|
||||
self->switchTo(*row.profile);
|
||||
break;
|
||||
case RowMode::Greeter:
|
||||
self->askGreeter();
|
||||
self->askGreeter(*row.profile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -92,18 +93,36 @@ private:
|
||||
g_clear_object(&self->pending_);
|
||||
}
|
||||
|
||||
static void onGreeterResponse(GtkAlertDialog*, int response, gpointer data) {
|
||||
// GtkAlertDialog exposes no "response" signal: the chosen button index is
|
||||
// only available from gtk_alert_dialog_choose_finish().
|
||||
static void onGreeterChosen(GObject* source, GAsyncResult* result, gpointer data) {
|
||||
auto* self = static_cast<ManagerWindow*>(data);
|
||||
GError* error = nullptr;
|
||||
const int response =
|
||||
gtk_alert_dialog_choose_finish(GTK_ALERT_DIALOG(source), result, &error);
|
||||
if (error != nullptr) {
|
||||
g_error_free(error);
|
||||
return;
|
||||
}
|
||||
if (response != 1) {
|
||||
return;
|
||||
}
|
||||
self->goToGreeter(false);
|
||||
}
|
||||
|
||||
static void onForceChosen(GObject* source, GAsyncResult* result, gpointer data) {
|
||||
auto* self = static_cast<ManagerWindow*>(data);
|
||||
std::string error;
|
||||
if (!fws::switchToGreeter(error)) {
|
||||
self->showError("Impossible de revenir au greeter : " + error);
|
||||
} else {
|
||||
self->setStatus("Retour à l'écran de connexion…");
|
||||
GError* error = nullptr;
|
||||
const int response =
|
||||
gtk_alert_dialog_choose_finish(GTK_ALERT_DIALOG(source), result, &error);
|
||||
if (error != nullptr) {
|
||||
g_error_free(error);
|
||||
return;
|
||||
}
|
||||
if (response != 1) {
|
||||
return;
|
||||
}
|
||||
self->goToGreeter(true);
|
||||
}
|
||||
|
||||
void buildWindow(GtkApplication* application) {
|
||||
@@ -223,11 +242,30 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
// The session this window runs in: XDG_SESSION_ID, or the seat's active one
|
||||
// when the variable is missing (pkexec, systemd-run...).
|
||||
fws::SessionInfo currentSession() const {
|
||||
fws::SessionInfo session;
|
||||
if (fws::findGraphicalSession(fws::currentSessionId(), session)) {
|
||||
return session;
|
||||
}
|
||||
fws::findGraphicalSession(fws::activeGraphicalSessionId(), session);
|
||||
return session;
|
||||
}
|
||||
|
||||
// Lets the status text paint before the blocking logind calls run.
|
||||
static void flushPendingEvents() {
|
||||
while (g_main_context_iteration(nullptr, FALSE) != FALSE) {
|
||||
}
|
||||
}
|
||||
|
||||
void refreshRows() {
|
||||
const std::string current = currentSession().id;
|
||||
for (RowWidgets& row : rows_) {
|
||||
std::string sessionId;
|
||||
const bool installed = fws::isDesktopInstalled(*row.profile);
|
||||
const bool active = installed && fws::findActiveSession(*row.profile, sessionId);
|
||||
const bool isCurrent = active && !current.empty() && sessionId == current;
|
||||
|
||||
gtk_widget_remove_css_class(row.action, "suggested-action");
|
||||
gtk_widget_remove_css_class(row.action, "destructive-action");
|
||||
@@ -238,6 +276,14 @@ private:
|
||||
gtk_button_set_label(GTK_BUTTON(row.action), "Installer");
|
||||
gtk_widget_add_css_class(row.action, "suggested-action");
|
||||
row.mode = RowMode::Install;
|
||||
} else if (isCurrent) {
|
||||
gtk_label_set_markup(GTK_LABEL(row.status),
|
||||
"<span foreground='#26a269'>Installé</span> · session actuelle");
|
||||
// Activating the session already on screen would do nothing, so
|
||||
// the useful action from here is leaving for the greeter.
|
||||
gtk_button_set_label(GTK_BUTTON(row.action), "Écran de connexion");
|
||||
gtk_widget_add_css_class(row.action, "suggested-action");
|
||||
row.mode = RowMode::Greeter;
|
||||
} else if (active) {
|
||||
gtk_label_set_markup(GTK_LABEL(row.status),
|
||||
"<span foreground='#26a269'>Installé</span> · session active");
|
||||
@@ -245,8 +291,20 @@ private:
|
||||
gtk_widget_add_css_class(row.action, "suggested-action");
|
||||
row.mode = RowMode::SwitchTo;
|
||||
} else {
|
||||
gtk_label_set_markup(GTK_LABEL(row.status),
|
||||
"<span foreground='#26a269'>Installé</span> · aucune session");
|
||||
// The greeter opens on the session it remembered, not on the one
|
||||
// this row points at, so the entry to pick is named right here.
|
||||
const std::string entry = fws::greeterSessionName(row.profile->sessionNames);
|
||||
if (entry.empty()) {
|
||||
gtk_label_set_markup(GTK_LABEL(row.status),
|
||||
"<span foreground='#26a269'>Installé</span> · aucune session");
|
||||
} else {
|
||||
gchar* markup = g_markup_printf_escaped(
|
||||
"<span foreground='#26a269'>Installé</span> · aucune session · choisir "
|
||||
"« %s » au greeter",
|
||||
entry.c_str());
|
||||
gtk_label_set_markup(GTK_LABEL(row.status), markup);
|
||||
g_free(markup);
|
||||
}
|
||||
gtk_button_set_label(GTK_BUTTON(row.action), "Écran de connexion");
|
||||
gtk_widget_add_css_class(row.action, "destructive-action");
|
||||
row.mode = RowMode::Greeter;
|
||||
@@ -312,28 +370,107 @@ private:
|
||||
return;
|
||||
}
|
||||
|
||||
if (sessionId == currentSession().id) {
|
||||
setStatus("Vous êtes déjà sur " + profile.name + ".");
|
||||
return;
|
||||
}
|
||||
|
||||
// activateSession waits for the seat to move and rolls back, so the
|
||||
// window blocks for a few seconds instead of leaving a black VT behind.
|
||||
setStatus("Bascule vers " + profile.name + "…");
|
||||
flushPendingEvents();
|
||||
|
||||
std::string error;
|
||||
if (!fws::activateSession(sessionId, error)) {
|
||||
showError("Échec de la bascule : " + error);
|
||||
setStatus("La session précédente a été conservée.");
|
||||
return;
|
||||
}
|
||||
setStatus("Bascule vers " + profile.name + "…");
|
||||
}
|
||||
|
||||
void askGreeter() {
|
||||
GtkAlertDialog* dialog = gtk_alert_dialog_new(
|
||||
"%s",
|
||||
"La session graphique courante sera fermée. Les applications non enregistrées "
|
||||
"seront perdues. Continuer ?");
|
||||
// The greeter starts whichever session its own selector is set on, and no
|
||||
// bus API lets us choose it: the dialog names the entry to pick before the
|
||||
// user is sent to another VT, where this window is no longer visible.
|
||||
void askGreeter(const fws::DesktopProfile& profile) {
|
||||
std::string sessionId;
|
||||
const bool running = fws::findActiveSession(profile, sessionId);
|
||||
greeterIsCurrent_ = running && sessionId == currentSession().id;
|
||||
greeterEntry_ = fws::greeterSessionName(profile.sessionNames);
|
||||
|
||||
std::string message;
|
||||
if (greeterIsCurrent_) {
|
||||
message = profile.name +
|
||||
" tourne deja dans votre session. Sur l'ecran de connexion, ouvrez le "
|
||||
"selecteur de session et choisissez un autre environnement : relancer "
|
||||
"le meme environnement ne demarre pas une seconde session. Rien n'est "
|
||||
"ferme ici.";
|
||||
} else if (!greeterEntry_.empty()) {
|
||||
message = "L'ecran de connexion va s'afficher. Choisissez « " + greeterEntry_ +
|
||||
" » dans le selecteur de session, puis connectez-vous : c'est ce choix "
|
||||
"qui decide de l'environnement demarre.";
|
||||
} else {
|
||||
message = "L'ecran de connexion va s'afficher. Choisissez " + profile.name +
|
||||
" dans le selecteur de session, puis connectez-vous.";
|
||||
}
|
||||
|
||||
GtkAlertDialog* dialog = gtk_alert_dialog_new("%s", message.c_str());
|
||||
const char* buttons[] = {"Annuler", "Continuer", nullptr};
|
||||
gtk_alert_dialog_set_buttons(dialog, buttons);
|
||||
gtk_alert_dialog_set_cancel_button(dialog, 0);
|
||||
gtk_alert_dialog_set_default_button(dialog, 1);
|
||||
g_signal_connect(dialog, "response", G_CALLBACK(&ManagerWindow::onGreeterResponse), this);
|
||||
gtk_alert_dialog_show(dialog, GTK_WINDOW(window_));
|
||||
gtk_alert_dialog_choose(dialog, GTK_WINDOW(window_), nullptr,
|
||||
&ManagerWindow::onGreeterChosen, this);
|
||||
g_object_unref(dialog);
|
||||
}
|
||||
|
||||
void askForce(const std::string& reason) {
|
||||
const std::string message =
|
||||
reason +
|
||||
"\n\nRedemarrer le gestionnaire d'affichage pour retrouver un ecran de connexion ? "
|
||||
"L'authentification est demandee par polkit ; la session courante sera fermee et "
|
||||
"les applications non enregistrees seront perdues.";
|
||||
GtkAlertDialog* dialog = gtk_alert_dialog_new("%s", message.c_str());
|
||||
const char* buttons[] = {"Annuler", "Redemarrer et fermer", nullptr};
|
||||
gtk_alert_dialog_set_buttons(dialog, buttons);
|
||||
gtk_alert_dialog_set_cancel_button(dialog, 0);
|
||||
gtk_alert_dialog_set_default_button(dialog, 0);
|
||||
gtk_alert_dialog_choose(dialog, GTK_WINDOW(window_), nullptr,
|
||||
&ManagerWindow::onForceChosen, this);
|
||||
g_object_unref(dialog);
|
||||
}
|
||||
|
||||
void goToGreeter(bool force) {
|
||||
setStatus(force ? "Redémarrage de l'écran de connexion…"
|
||||
: "Ouverture de l'écran de connexion…");
|
||||
flushPendingEvents();
|
||||
|
||||
const fws::GreeterOutcome outcome = fws::switchToGreeter(force);
|
||||
if (outcome.ok) {
|
||||
if (force) {
|
||||
setStatus("Écran de connexion redémarré.");
|
||||
} else if (!greeterIsCurrent_ && !greeterEntry_.empty()) {
|
||||
setStatus("Écran de connexion affiché : choisissez « " + greeterEntry_ +
|
||||
" » dans le sélecteur de session pour l'ouvrir.");
|
||||
} else {
|
||||
setStatus("Écran de connexion affiché ; la session reste ouverte.");
|
||||
}
|
||||
refreshRows();
|
||||
return;
|
||||
}
|
||||
|
||||
// Nothing has been closed yet: offer the destructive path explicitly
|
||||
// instead of dropping the user on a blank VT.
|
||||
if (outcome.forceRequired && !force) {
|
||||
askForce(outcome.error);
|
||||
setStatus("La session courante a été conservée.");
|
||||
return;
|
||||
}
|
||||
|
||||
showError(outcome.error);
|
||||
setStatus("Retour à l'écran de connexion impossible.");
|
||||
}
|
||||
|
||||
void showError(const std::string& message) {
|
||||
GtkAlertDialog* dialog = gtk_alert_dialog_new("%s", message.c_str());
|
||||
gtk_alert_dialog_show(dialog, GTK_WINDOW(window_));
|
||||
@@ -369,6 +506,9 @@ private:
|
||||
std::vector<RowWidgets> rows_;
|
||||
GSubprocess* pending_ = nullptr;
|
||||
bool busy_ = false;
|
||||
// Filled by askGreeter(): the greeter entry to name once it is on screen.
|
||||
std::string greeterEntry_;
|
||||
bool greeterIsCurrent_ = false;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
+38
-9
@@ -27,7 +27,11 @@ void printUsage() {
|
||||
"Usage :\n"
|
||||
" fws-deskmanager list [--json] inventaire des environnements et sessions\n"
|
||||
" fws-deskmanager switch <id> activer une session graphique existante\n"
|
||||
" fws-deskmanager greeter [--yes] revenir au gestionnaire d'affichage\n"
|
||||
" fws-deskmanager greeter [--yes] [--force]\n"
|
||||
" afficher l'ecran de connexion (session "
|
||||
"conservee ;\n"
|
||||
" --force redemarre le gestionnaire "
|
||||
"d'affichage et ferme la session)\n"
|
||||
" fws-deskmanager ui selecteur zenity / kdialog / terminal\n"
|
||||
" fws-deskmanager help afficher cette aide\n\n"
|
||||
"Codes de sortie : 0 succes, 1 usage, 2 introuvable, 3 echec.\n";
|
||||
@@ -164,12 +168,18 @@ bool resolveSession(const std::string& target, const std::vector<fws::SessionInf
|
||||
return false;
|
||||
}
|
||||
|
||||
bool confirmGreeter(bool assumeYes) {
|
||||
bool confirmGreeter(bool assumeYes, bool force) {
|
||||
if (assumeYes) {
|
||||
return true;
|
||||
}
|
||||
std::cout << "La session graphique va etre fermee et les applications non enregistrees "
|
||||
"seront perdues.\nContinuer ? [o/N] ";
|
||||
if (force) {
|
||||
std::cout << "Le gestionnaire d'affichage va etre redemarre (authentification "
|
||||
"demandee) : la session graphique courante sera fermee et les "
|
||||
"applications non enregistrees seront perdues.\nContinuer ? [o/N] ";
|
||||
} else {
|
||||
std::cout << "L'ecran de connexion du gestionnaire d'affichage va s'afficher ; la "
|
||||
"session courante reste ouverte.\nContinuer ? [o/N] ";
|
||||
}
|
||||
std::string answer;
|
||||
if (!std::getline(std::cin, answer)) {
|
||||
return false;
|
||||
@@ -229,15 +239,34 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
|
||||
if (command == "greeter") {
|
||||
const bool assumeYes = argc >= 3 && std::string(argv[2]) == "--yes";
|
||||
if (!confirmGreeter(assumeYes)) {
|
||||
bool assumeYes = false;
|
||||
bool force = false;
|
||||
for (int index = 2; index < argc; ++index) {
|
||||
const std::string flag = argv[index];
|
||||
if (flag == "--yes" || flag == "-y") {
|
||||
assumeYes = true;
|
||||
} else if (flag == "--force") {
|
||||
force = true;
|
||||
} else {
|
||||
std::cerr << "Option inconnue : " << flag << "\n";
|
||||
return kExitUsage;
|
||||
}
|
||||
}
|
||||
|
||||
if (!confirmGreeter(assumeYes, force)) {
|
||||
std::cout << "Annule.\n";
|
||||
return kExitOk;
|
||||
}
|
||||
|
||||
std::string error;
|
||||
if (!fws::switchToGreeter(error)) {
|
||||
std::cerr << "Echec du retour au greeter : " << error << "\n";
|
||||
// Without --force nothing is terminated, so a display manager that stays
|
||||
// silent can never leave the machine on a blank VT.
|
||||
const fws::GreeterOutcome outcome = fws::switchToGreeter(force);
|
||||
if (!outcome.ok) {
|
||||
std::cerr << "Echec du retour au greeter : " << outcome.error << "\n";
|
||||
if (outcome.forceRequired) {
|
||||
std::cerr << "Relancez avec --force pour redemarrer le gestionnaire "
|
||||
"d'affichage et retrouver un ecran de connexion.\n";
|
||||
}
|
||||
return kExitFailure;
|
||||
}
|
||||
return kExitOk;
|
||||
|
||||
+297
-20
@@ -1,19 +1,213 @@
|
||||
#include "session-switcher.h"
|
||||
|
||||
#include "process.h"
|
||||
#include "session-catalog.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace fws {
|
||||
namespace {
|
||||
|
||||
constexpr int kPollIntervalMs = 100;
|
||||
constexpr int kVerifyTimeoutMs = 5000;
|
||||
// A display manager restart is slower than a VT switch: the unit stops, a new
|
||||
// greeter starts and logind hands it the seat.
|
||||
constexpr int kRestartTimeoutMs = 15000;
|
||||
|
||||
bool isSessionId(const std::string& value) {
|
||||
return !value.empty() &&
|
||||
std::all_of(value.begin(), value.end(),
|
||||
[](unsigned char character) { return std::isdigit(character) != 0; });
|
||||
}
|
||||
|
||||
void sleepMilliseconds(int milliseconds) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
|
||||
}
|
||||
|
||||
// logind accepts a request before the graphics stack has moved the seat, so
|
||||
// every switch is confirmed instead of assumed; a blank VT is the cost of an
|
||||
// assumption that turns out to be wrong.
|
||||
template <typename Predicate>
|
||||
bool waitUntil(Predicate predicate, int timeoutMs) {
|
||||
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
if (predicate()) {
|
||||
return true;
|
||||
}
|
||||
sleepMilliseconds(kPollIntervalMs);
|
||||
}
|
||||
return predicate();
|
||||
}
|
||||
|
||||
std::string commandError(const CommandResult& result, const std::string& fallback) {
|
||||
const std::string message = trim(result.output);
|
||||
return message.empty() ? fallback : message;
|
||||
}
|
||||
|
||||
// The session this process belongs to, falling back to the seat's active one
|
||||
// when XDG_SESSION_ID is missing or stale (pkexec, systemd-run, ssh...).
|
||||
SessionInfo resolveCurrentSession() {
|
||||
SessionInfo session;
|
||||
const std::string fromEnvironment = currentSessionId();
|
||||
if (isSessionId(fromEnvironment) && findGraphicalSession(fromEnvironment, session)) {
|
||||
return session;
|
||||
}
|
||||
const std::string active = activeGraphicalSessionId();
|
||||
if (!active.empty() && findGraphicalSession(active, session)) {
|
||||
return session;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<std::string> displayManagerSeats() {
|
||||
std::vector<std::string> paths;
|
||||
const CommandResult listing = runCommand(
|
||||
{"busctl", "--system", "call", "org.freedesktop.DisplayManager",
|
||||
"/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "GetSeats"});
|
||||
if (listing.started && listing.exitCode == 0) {
|
||||
const std::string& output = listing.output;
|
||||
std::size_t position = output.find("\"/org/freedesktop/DisplayManager/Seat");
|
||||
while (position != std::string::npos) {
|
||||
const auto end = output.find('"', position + 1);
|
||||
if (end == std::string::npos) {
|
||||
break;
|
||||
}
|
||||
paths.push_back(output.substr(position + 1, end - position - 1));
|
||||
position = output.find("\"/org/freedesktop/DisplayManager/Seat", end + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (paths.empty()) {
|
||||
// Older SDDM builds export the seat objects without a working manager
|
||||
// listing, so probe the usual seat paths as a fallback.
|
||||
for (int index = 0; index < 4; ++index) {
|
||||
paths.push_back("/org/freedesktop/DisplayManager/Seat" + std::to_string(index));
|
||||
}
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
|
||||
// GDM, SDDM and LightDM implement org.freedesktop.DisplayManager: this asks the
|
||||
// greeter to take the seat back and never ends the session, so the worst case is
|
||||
// that nothing happens.
|
||||
bool requestGreeterThroughDisplayManager() {
|
||||
for (const std::string& seat : displayManagerSeats()) {
|
||||
const CommandResult result = runCommand(
|
||||
{"busctl", "--system", "call", "org.freedesktop.DisplayManager", seat,
|
||||
"org.freedesktop.DisplayManager.Seat", "SwitchToGreeter"});
|
||||
if (result.started && result.exitCode == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// LightDM's own helper, kept for setups without the bus interface.
|
||||
bool requestGreeterThroughDmTool() {
|
||||
const std::string dmTool = findExecutable("dm-tool");
|
||||
if (dmTool.empty()) {
|
||||
return false;
|
||||
}
|
||||
const CommandResult result = runCommand({dmTool, "switch-to-greeter"});
|
||||
return result.started && result.exitCode == 0;
|
||||
}
|
||||
|
||||
bool greeterIsShowing(const SessionInfo& previous) {
|
||||
const std::string greeter = findGreeterSessionId(previous.seat);
|
||||
if (!greeter.empty() && isSessionActive(greeter)) {
|
||||
return true;
|
||||
}
|
||||
// GDM reuses its own seat, so a seat that left our session is the other
|
||||
// signal that the request was honoured.
|
||||
return !previous.id.empty() && !isSessionActive(previous.id);
|
||||
}
|
||||
|
||||
// Puts the user back on the session they came from when the greeter never
|
||||
// appeared: whatever the display manager ignored, the screen cannot stay blank.
|
||||
void restoreSession(const SessionInfo& session) {
|
||||
if (!session.id.empty() && !isSessionActive(session.id)) {
|
||||
runCommand({"loginctl", "activate", session.id});
|
||||
}
|
||||
}
|
||||
|
||||
// Unit behind display-manager.service: greeter behaviour is per display manager,
|
||||
// so the name decides which path is safe.
|
||||
std::string displayManagerUnit() {
|
||||
const CommandResult result =
|
||||
runCommand({"systemctl", "show", "-p", "Id", "--value", "display-manager.service"});
|
||||
return result.started && result.exitCode == 0 ? trim(result.output) : std::string();
|
||||
}
|
||||
|
||||
// SDDM 0.21 answers SwitchToGreeter by creating an extra greeter display. A login
|
||||
// made from that display stops the greeter and never starts the session, so the
|
||||
// seat falls back to the VT the user came from: the requested environment never
|
||||
// runs. This is the only per-display-manager exception in here: every other one
|
||||
// goes through the standard interfaces below.
|
||||
bool displayManagerIsSddm() { return displayManagerUnit() == "sddm.service"; }
|
||||
|
||||
bool displayManagerIsActive() {
|
||||
const CommandResult result = runCommand({"systemctl", "is-active", "display-manager.service"});
|
||||
return result.started && trim(result.output) == "active";
|
||||
}
|
||||
|
||||
// Last resort: end the session and let the display manager spawn a fresh
|
||||
// greeter. Refused when no display manager is around to take the seat back,
|
||||
// because that is exactly the configuration that ends in a black screen.
|
||||
bool terminateCurrentSession(std::string& error) {
|
||||
const SessionInfo current = resolveCurrentSession();
|
||||
if (current.id.empty()) {
|
||||
error = "Impossible d'identifier la session graphique courante.";
|
||||
return false;
|
||||
}
|
||||
if (!displayManagerIsActive()) {
|
||||
error = "Aucun gestionnaire d'affichage actif : fermer la session laisserait "
|
||||
"un ecran noir sans moyen de revenir.";
|
||||
return false;
|
||||
}
|
||||
|
||||
const CommandResult result = runCommand({"loginctl", "terminate-session", current.id});
|
||||
if (!result.started) {
|
||||
error = "Unable to run loginctl. Is systemd-logind available?";
|
||||
return false;
|
||||
}
|
||||
if (result.exitCode != 0) {
|
||||
error = commandError(result, "loginctl terminate-session failed.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Ending the session is what makes the greeter come back, but the way back is
|
||||
// display-manager specific. Restarting display-manager.service is not: the name
|
||||
// is the systemd alias every display manager installs, and a restarted daemon
|
||||
// takes the seat with a fresh greeter whatever its implementation. Unavailable
|
||||
// means nothing to restart (greetd, startx, no pkexec), and the caller has to
|
||||
// fall back to ending the session.
|
||||
enum class RestartOutcome { Restarted, Unavailable, Refused };
|
||||
|
||||
RestartOutcome restartDisplayManager(std::string& error) {
|
||||
if (displayManagerUnit().empty() || findExecutable("pkexec").empty()) {
|
||||
return RestartOutcome::Unavailable;
|
||||
}
|
||||
|
||||
const CommandResult result =
|
||||
runCommand({"pkexec", "systemctl", "restart", "display-manager.service"});
|
||||
if (!result.started) {
|
||||
error = "Impossible de lancer pkexec.";
|
||||
return RestartOutcome::Refused;
|
||||
}
|
||||
if (result.exitCode != 0) {
|
||||
error = commandError(result, "Le redemarrage du gestionnaire d'affichage a ete "
|
||||
"refuse ou annule ; la session est restee ouverte.");
|
||||
return RestartOutcome::Refused;
|
||||
}
|
||||
return RestartOutcome::Restarted;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool activateSession(const std::string& sessionId, std::string& error) {
|
||||
@@ -23,39 +217,122 @@ bool activateSession(const std::string& sessionId, std::string& error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SessionInfo target;
|
||||
if (!findGraphicalSession(sessionId, target)) {
|
||||
error = "La session " + sessionId + " n'existe plus.";
|
||||
return false;
|
||||
}
|
||||
if (target.active) {
|
||||
// Already on this VT: logind has nothing to do.
|
||||
return true;
|
||||
}
|
||||
if (target.state == "closing" || target.state == "closing-failed") {
|
||||
error = "La session " + sessionId + " est en cours de fermeture.";
|
||||
return false;
|
||||
}
|
||||
// A session without a seat or a VT cannot be displayed; activating it is how
|
||||
// a black screen with no way back happens.
|
||||
if (target.seat.empty() || target.tty.empty()) {
|
||||
error = "La session " + sessionId + " n'est rattachee a aucun ecran.";
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string previous = activeGraphicalSessionId();
|
||||
const CommandResult result = runCommand({"loginctl", "activate", sessionId});
|
||||
if (!result.started) {
|
||||
error = "Unable to run loginctl. Is systemd-logind available?";
|
||||
return false;
|
||||
}
|
||||
if (result.exitCode != 0) {
|
||||
error = trim(result.output);
|
||||
if (error.empty()) {
|
||||
error = "loginctl activate failed.";
|
||||
}
|
||||
error = commandError(result, "loginctl activate failed.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool switchToGreeter(std::string& error) {
|
||||
// Freedesktop DisplayManager interface, implemented by SDDM and LightDM.
|
||||
const CommandResult busctl = runCommand(
|
||||
{"busctl", "--system", "call", "org.freedesktop.DisplayManager",
|
||||
"/org/freedesktop/DisplayManager/Seat0", "org.freedesktop.DisplayManager.Seat",
|
||||
"SwitchToGreeter"});
|
||||
if (busctl.started && busctl.exitCode == 0) {
|
||||
if (waitUntil([&sessionId] { return isSessionActive(sessionId); }, kVerifyTimeoutMs)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// LightDM's own helper, kept as a fallback for older setups.
|
||||
const CommandResult dmTool = runCommand({"dm-tool", "switch-to-greeter"});
|
||||
if (dmTool.started && dmTool.exitCode == 0) {
|
||||
return true;
|
||||
// logind answered but the seat never moved: return to the session that is
|
||||
// still alive instead of leaving the new VT blank.
|
||||
if (!previous.empty() && previous != sessionId) {
|
||||
runCommand({"loginctl", "activate", previous});
|
||||
}
|
||||
|
||||
error = "No compatible display manager answered (SDDM/LightDM expected).";
|
||||
error = "La bascule vers la session " + sessionId +
|
||||
" n'a pas abouti ; retour a la session precedente.";
|
||||
return false;
|
||||
}
|
||||
|
||||
GreeterOutcome switchToGreeter(bool allowTerminate) {
|
||||
GreeterOutcome outcome;
|
||||
const SessionInfo current = resolveCurrentSession();
|
||||
|
||||
const bool sddm = displayManagerIsSddm();
|
||||
|
||||
if (!sddm) {
|
||||
if (requestGreeterThroughDisplayManager() &&
|
||||
waitUntil([¤t] { return greeterIsShowing(current); }, kVerifyTimeoutMs)) {
|
||||
outcome.ok = true;
|
||||
return outcome;
|
||||
}
|
||||
|
||||
if (requestGreeterThroughDmTool() &&
|
||||
waitUntil([¤t] { return greeterIsShowing(current); }, kVerifyTimeoutMs)) {
|
||||
outcome.ok = true;
|
||||
return outcome;
|
||||
}
|
||||
|
||||
// A greeter session that logind already knows can simply be activated.
|
||||
const std::string greeter = findGreeterSessionId(current.seat);
|
||||
if (!greeter.empty() && greeter != current.id && isSessionId(greeter)) {
|
||||
const CommandResult result = runCommand({"loginctl", "activate", greeter});
|
||||
if (result.started && result.exitCode == 0 &&
|
||||
waitUntil([&greeter] { return isSessionActive(greeter); }, kVerifyTimeoutMs)) {
|
||||
outcome.ok = true;
|
||||
return outcome;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!allowTerminate) {
|
||||
// Nothing has been killed at this point, so make sure the screen still
|
||||
// shows the session the user started from.
|
||||
restoreSession(current);
|
||||
outcome.forceRequired = true;
|
||||
outcome.error =
|
||||
"Le gestionnaire d'affichage n'a pas repris l'ecran. Le redemarrer "
|
||||
"(authentification demandee) ferme la session courante et affiche un ecran "
|
||||
"de connexion neuf.";
|
||||
return outcome;
|
||||
}
|
||||
|
||||
std::string failure;
|
||||
const RestartOutcome restart = restartDisplayManager(failure);
|
||||
if (restart == RestartOutcome::Refused) {
|
||||
restoreSession(current);
|
||||
outcome.error = failure;
|
||||
return outcome;
|
||||
}
|
||||
if (restart == RestartOutcome::Restarted) {
|
||||
// Nothing can be rolled back past this point: the caller dies with its
|
||||
// session, so a restart that leaves the seat empty is exactly the black
|
||||
// VT this path exists to avoid. Say so instead of reporting success.
|
||||
if (!waitUntil([¤t] { return greeterIsShowing(current); }, kRestartTimeoutMs)) {
|
||||
outcome.error =
|
||||
"Le gestionnaire d'affichage a redemarre mais aucun ecran de connexion "
|
||||
"n'a repris l'ecran. Basculez sur un autre terminal (Ctrl+Alt+F2) puis "
|
||||
"redemarrez-le a la main.";
|
||||
return outcome;
|
||||
}
|
||||
outcome.ok = true;
|
||||
return outcome;
|
||||
}
|
||||
|
||||
if (!terminateCurrentSession(failure)) {
|
||||
restoreSession(current);
|
||||
outcome.error = failure;
|
||||
return outcome;
|
||||
}
|
||||
outcome.ok = true;
|
||||
return outcome;
|
||||
}
|
||||
|
||||
} // namespace fws
|
||||
+18
-3
@@ -4,10 +4,25 @@
|
||||
|
||||
namespace fws {
|
||||
|
||||
// Switches the seat to the given logind session (VT activation).
|
||||
// Switches the seat to the given logind session (VT activation). The switch is
|
||||
// verified and rolled back to the previous session when the seat never moves.
|
||||
bool activateSession(const std::string& sessionId, std::string& error);
|
||||
|
||||
// Returns to the display manager greeter without rebooting.
|
||||
bool switchToGreeter(std::string& error);
|
||||
struct GreeterOutcome {
|
||||
bool ok = false;
|
||||
// True when no display manager answered and showing the greeter would end
|
||||
// the current session: the caller has to confirm before retrying with
|
||||
// allowTerminate = true.
|
||||
bool forceRequired = false;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
// Shows the display manager greeter again. GDM and LightDM are asked to switch
|
||||
// without ending anything; SDDM 0.21 cannot do that safely (its SwitchToGreeter
|
||||
// leaves a greeter whose logins never start a session), so the call reports
|
||||
// forceRequired and the caller has to confirm first. With allowTerminate the
|
||||
// session does not come back: GDM and LightDM have it terminated, and SDDM is
|
||||
// restarted through polkit so a working greeter takes the seat again.
|
||||
GreeterOutcome switchToGreeter(bool allowTerminate);
|
||||
|
||||
} // namespace fws
|
||||
+12
-6
@@ -34,7 +34,7 @@ std::vector<Choice> buildChoices(const std::vector<SessionInfo>& sessions) {
|
||||
return choices;
|
||||
}
|
||||
|
||||
bool confirm(const std::string& question) {
|
||||
bool ask(const std::string& question) {
|
||||
const std::string zenity = findExecutable("zenity");
|
||||
if (!zenity.empty()) {
|
||||
const CommandResult result =
|
||||
@@ -51,15 +51,21 @@ bool confirm(const std::string& question) {
|
||||
}
|
||||
|
||||
bool runGreeter() {
|
||||
if (!confirm("La session graphique va etre fermee et les applications non enregistrees "
|
||||
"seront perdues. Continuer ?")) {
|
||||
if (!ask("Afficher l'ecran de connexion ? La session courante reste ouverte.")) {
|
||||
std::cout << "Annule.\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string error;
|
||||
if (!switchToGreeter(error)) {
|
||||
std::cerr << "Echec du retour au greeter : " << error << "\n";
|
||||
fws::GreeterOutcome outcome = switchToGreeter(false);
|
||||
if (!outcome.ok && outcome.forceRequired &&
|
||||
ask(outcome.error +
|
||||
"\nRedemarrer le gestionnaire d'affichage (la session courante sera fermee et "
|
||||
"les applications non enregistrees perdues) ?")) {
|
||||
outcome = switchToGreeter(true);
|
||||
}
|
||||
|
||||
if (!outcome.ok) {
|
||||
std::cerr << "Echec du retour au greeter : " << outcome.error << "\n";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user