feat(session): read session state and greeter entries from logind
Switching needs to know whether a session owns the seat, is closing, or is the greeter, and which session file the greeter will list for a desktop. The catalog now keeps the logind State, exposes targeted lookups, and prefers the active session so a closing one is never activated. Session ids are compared numerically without std::stoi.
This commit is contained in:
4 files changed
+205
-45
No files matched your search
+25
-3
@@ -46,16 +46,38 @@ bool isDesktopInstalled(const DesktopProfile& profile) {
|
||||
}
|
||||
|
||||
bool findActiveSession(const DesktopProfile& profile, std::string& sessionId) {
|
||||
// Prefer the session that owns the seat: a stale or closing session of the
|
||||
// same desktop would activate a VT that has nothing left to draw.
|
||||
std::string fallback;
|
||||
for (const SessionInfo& session : listGraphicalSessions()) {
|
||||
if (session.state == "closing" || session.state == "closing-failed") {
|
||||
continue;
|
||||
}
|
||||
const std::string desktop = lower(session.desktop);
|
||||
bool matches = false;
|
||||
for (const std::string& name : profile.sessionNames) {
|
||||
if (desktop == lower(name)) {
|
||||
sessionId = session.id;
|
||||
return true;
|
||||
matches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!matches) {
|
||||
continue;
|
||||
}
|
||||
if (session.active) {
|
||||
sessionId = session.id;
|
||||
return true;
|
||||
}
|
||||
if (fallback.empty()) {
|
||||
fallback = session.id;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
if (fallback.empty()) {
|
||||
return false;
|
||||
}
|
||||
sessionId = fallback;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::string> installArguments(const DesktopProfile& profile) {
|
||||
|
||||
@@ -17,7 +17,7 @@ const std::vector<DesktopProfile>& supportedDesktops();
|
||||
// True when every pacman target of the profile is installed.
|
||||
bool isDesktopInstalled(const DesktopProfile& profile);
|
||||
|
||||
// Finds an active logind session for the profile.
|
||||
// Finds a logind session for the profile, preferring the one on the seat.
|
||||
bool findActiveSession(const DesktopProfile& profile, std::string& sessionId);
|
||||
|
||||
// pkexec + pacman command used to install the profile.
|
||||
|
||||
+162
-41
@@ -74,6 +74,89 @@ std::string valueForKey(const std::vector<std::string>& lines, const std::string
|
||||
return {};
|
||||
}
|
||||
|
||||
bool isSessionId(const std::string& value) {
|
||||
return !value.empty() &&
|
||||
std::all_of(value.begin(), value.end(),
|
||||
[](unsigned char character) { return std::isdigit(character) != 0; });
|
||||
}
|
||||
|
||||
struct SessionRow {
|
||||
std::string id;
|
||||
std::string uid;
|
||||
std::string user;
|
||||
std::string seat;
|
||||
std::string tty;
|
||||
};
|
||||
|
||||
// Every logind session, whatever its owner: greeter sessions belong to the
|
||||
// display manager account, not to the person in front of the screen.
|
||||
std::vector<SessionRow> listSessionRows() {
|
||||
std::vector<SessionRow> rows;
|
||||
const CommandResult listing = runCommand({"loginctl", "list-sessions", "--no-legend"});
|
||||
if (!listing.started || listing.exitCode != 0) {
|
||||
return rows;
|
||||
}
|
||||
|
||||
for (const std::string& line : splitLines(listing.output)) {
|
||||
std::istringstream stream(line);
|
||||
SessionRow row;
|
||||
if (!(stream >> row.id >> row.uid >> row.user >> row.seat >> row.tty)) {
|
||||
continue;
|
||||
}
|
||||
if (!isSessionId(row.id)) {
|
||||
continue;
|
||||
}
|
||||
rows.push_back(std::move(row));
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
std::vector<std::string> sessionProperties(const std::string& id,
|
||||
const std::vector<std::string>& names) {
|
||||
std::vector<std::string> arguments = {"loginctl", "show-session", id};
|
||||
for (const std::string& name : names) {
|
||||
arguments.push_back("-p");
|
||||
arguments.push_back(name);
|
||||
}
|
||||
|
||||
const CommandResult details = runCommand(arguments);
|
||||
if (!details.started || details.exitCode != 0) {
|
||||
return {};
|
||||
}
|
||||
return splitLines(details.output);
|
||||
}
|
||||
|
||||
SessionInfo describeSession(const SessionRow& row) {
|
||||
SessionInfo info;
|
||||
info.id = row.id;
|
||||
info.user = row.user;
|
||||
info.seat = row.seat;
|
||||
info.tty = row.tty;
|
||||
|
||||
const std::vector<std::string> properties =
|
||||
sessionProperties(row.id, {"Type", "Class", "Active", "Desktop", "TTY", "Seat", "State"});
|
||||
if (properties.empty()) {
|
||||
return info;
|
||||
}
|
||||
|
||||
info.type = valueForKey(properties, "Type");
|
||||
// logind is authoritative: the list columns can lag behind a VT switch.
|
||||
info.seat = valueForKey(properties, "Seat");
|
||||
info.tty = valueForKey(properties, "TTY");
|
||||
info.desktop = valueForKey(properties, "Desktop");
|
||||
info.state = valueForKey(properties, "State");
|
||||
info.active = valueForKey(properties, "Active") == "yes";
|
||||
info.graphical = valueForKey(properties, "Class") == "user" &&
|
||||
(info.type == "x11" || info.type == "wayland");
|
||||
return info;
|
||||
}
|
||||
|
||||
// Session ids are numeric, so comparing length first keeps the order numeric
|
||||
// without parsing (and without std::stoi throwing on an unexpected value).
|
||||
bool idBefore(const std::string& left, const std::string& right) {
|
||||
return left.size() != right.size() ? left.size() < right.size() : left < right;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<DesktopEntry> listInstalledDesktops() {
|
||||
@@ -117,60 +200,98 @@ std::vector<DesktopEntry> listInstalledDesktops() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
std::string greeterSessionName(const std::vector<std::string>& desktopNames) {
|
||||
std::string fromDesktopNames;
|
||||
for (const DesktopEntry& entry : listInstalledDesktops()) {
|
||||
for (const std::string& name : desktopNames) {
|
||||
const std::string wanted = lower(name);
|
||||
if (lower(entry.id) == wanted || lower(entry.name) == wanted) {
|
||||
return entry.name;
|
||||
}
|
||||
// DesktopNames is a semicolon separated list, e.g. "XFCE".
|
||||
std::istringstream values(lower(entry.desktops));
|
||||
std::string value;
|
||||
while (std::getline(values, value, ';')) {
|
||||
if (trim(value) == wanted && fromDesktopNames.empty()) {
|
||||
fromDesktopNames = entry.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// DesktopNames also matches the other session files of the same desktop, so
|
||||
// the id/name match above wins and this is only the fallback.
|
||||
return fromDesktopNames;
|
||||
}
|
||||
|
||||
std::vector<SessionInfo> listGraphicalSessions() {
|
||||
std::vector<SessionInfo> sessions;
|
||||
const CommandResult listing = runCommand({"loginctl", "list-sessions", "--no-legend"});
|
||||
if (!listing.started || listing.exitCode != 0) {
|
||||
return sessions;
|
||||
}
|
||||
|
||||
const std::string uid = std::to_string(getuid());
|
||||
for (const std::string& line : splitLines(listing.output)) {
|
||||
std::istringstream stream(line);
|
||||
std::string id;
|
||||
std::string sessionUid;
|
||||
std::string user;
|
||||
std::string seat;
|
||||
std::string tty;
|
||||
if (!(stream >> id >> sessionUid >> user >> seat >> tty)) {
|
||||
for (const SessionRow& row : listSessionRows()) {
|
||||
if (row.uid != uid) {
|
||||
continue;
|
||||
}
|
||||
if (sessionUid != uid) {
|
||||
SessionInfo info = describeSession(row);
|
||||
if (!info.graphical) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const CommandResult details = runCommand(
|
||||
{"loginctl", "show-session", id, "-p", "Type", "-p", "Class", "-p", "Active",
|
||||
"-p", "Desktop", "-p", "TTY", "-p", "Seat"});
|
||||
if (!details.started || details.exitCode != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::vector<std::string> properties = splitLines(details.output);
|
||||
const std::string type = valueForKey(properties, "Type");
|
||||
const std::string sessionClass = valueForKey(properties, "Class");
|
||||
if (sessionClass != "user" || (type != "x11" && type != "wayland")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SessionInfo info;
|
||||
info.id = id;
|
||||
info.user = user;
|
||||
info.seat = valueForKey(properties, "Seat");
|
||||
info.tty = valueForKey(properties, "TTY");
|
||||
info.type = type;
|
||||
info.desktop = valueForKey(properties, "Desktop");
|
||||
info.active = valueForKey(properties, "Active") == "yes";
|
||||
info.graphical = true;
|
||||
sessions.push_back(std::move(info));
|
||||
}
|
||||
|
||||
std::sort(sessions.begin(), sessions.end(), [](const SessionInfo& left, const SessionInfo& right) {
|
||||
return std::stoi(left.id) < std::stoi(right.id);
|
||||
});
|
||||
std::sort(sessions.begin(), sessions.end(),
|
||||
[](const SessionInfo& left, const SessionInfo& right) { return idBefore(left.id, right.id); });
|
||||
return sessions;
|
||||
}
|
||||
|
||||
bool findGraphicalSession(const std::string& id, SessionInfo& session) {
|
||||
if (!isSessionId(id)) {
|
||||
return false;
|
||||
}
|
||||
for (const SessionInfo& candidate : listGraphicalSessions()) {
|
||||
if (candidate.id == id) {
|
||||
session = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isSessionActive(const std::string& id) {
|
||||
if (!isSessionId(id)) {
|
||||
return false;
|
||||
}
|
||||
const std::vector<std::string> properties = sessionProperties(id, {"Active"});
|
||||
return valueForKey(properties, "Active") == "yes";
|
||||
}
|
||||
|
||||
std::string activeGraphicalSessionId() {
|
||||
for (const SessionInfo& session : listGraphicalSessions()) {
|
||||
if (session.active) {
|
||||
return session.id;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string findGreeterSessionId(const std::string& seat) {
|
||||
std::string inactive;
|
||||
for (const SessionRow& row : listSessionRows()) {
|
||||
const std::vector<std::string> properties = sessionProperties(row.id, {"Class", "Seat", "Active"});
|
||||
if (properties.empty() || valueForKey(properties, "Class") != "greeter") {
|
||||
continue;
|
||||
}
|
||||
if (!seat.empty() && valueForKey(properties, "Seat") != seat) {
|
||||
continue;
|
||||
}
|
||||
if (valueForKey(properties, "Active") == "yes") {
|
||||
return row.id;
|
||||
}
|
||||
if (inactive.empty()) {
|
||||
inactive = row.id;
|
||||
}
|
||||
}
|
||||
return inactive;
|
||||
}
|
||||
|
||||
std::string currentSessionId() {
|
||||
const char* value = std::getenv("XDG_SESSION_ID");
|
||||
return value != nullptr ? value : "";
|
||||
|
||||
@@ -20,6 +20,7 @@ struct SessionInfo {
|
||||
std::string tty;
|
||||
std::string type; // x11, wayland, tty, ...
|
||||
std::string desktop; // KDE, GNOME, Hyprland, ...
|
||||
std::string state; // logind State: active, online, closing, ...
|
||||
bool active = false;
|
||||
bool graphical = false;
|
||||
};
|
||||
@@ -30,6 +31,22 @@ std::vector<DesktopEntry> listInstalledDesktops();
|
||||
// Reads the current user's graphical sessions from systemd-logind.
|
||||
std::vector<SessionInfo> listGraphicalSessions();
|
||||
|
||||
// Looks up one of the current user's graphical sessions by logind id.
|
||||
bool findGraphicalSession(const std::string& id, SessionInfo& session);
|
||||
|
||||
// True when logind reports the session as the active one on its seat.
|
||||
bool isSessionActive(const std::string& id);
|
||||
|
||||
// Id of the active graphical session of the current user, empty when none.
|
||||
std::string activeGraphicalSessionId();
|
||||
|
||||
// Greeter session (Class=greeter) known to logind, optionally on one seat.
|
||||
std::string findGreeterSessionId(const std::string& seat);
|
||||
|
||||
// Name of the installed session file the greeter lists for one of these desktop
|
||||
// names, empty when nothing matches (the greeter then keeps its own default).
|
||||
std::string greeterSessionName(const std::vector<std::string>& desktopNames);
|
||||
|
||||
std::string currentSessionId();
|
||||
std::string currentDesktopName();
|
||||
|
||||
|
||||
Reference in new issue
Block a user