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
+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 : "";
|
||||
|
||||
Reference in new issue
Block a user