From dc6816e713f29a04a6d23f27e6786b5e6e251e82 Mon Sep 17 00:00:00 2001 From: April & May & June Date: Thu, 5 Feb 2026 17:18:06 +0800 Subject: [PATCH] feat: Use Lock/Unlock signal from org.freedesktop.login1.Session for session lock This is more portable and robustic. Refined the logging system to make them more informative as well. --- src/common/Messages.h | 2 +- src/daemon/Display.cpp | 48 +++++++++++++++++----------- src/daemon/Display.h | 11 +++---- src/daemon/SocketServer.cpp | 13 +++++--- src/daemon/SocketServer.h | 3 +- src/daemon/TreelandDisplayServer.cpp | 6 ---- src/daemon/TreelandDisplayServer.h | 1 - src/daemon/UserSession.cpp | 2 +- 8 files changed, 48 insertions(+), 38 deletions(-) diff --git a/src/common/Messages.h b/src/common/Messages.h index 30ca8a1..c314b05 100644 --- a/src/common/Messages.h +++ b/src/common/Messages.h @@ -32,6 +32,7 @@ namespace DDM { Hibernate, HybridSleep, BackToNormal, + Lock, Unlock, Logout, }; @@ -45,7 +46,6 @@ namespace DDM { enum class DaemonMessages { HostName, Capabilities, - LoginSucceeded, LoginFailed, InformationMessage, UserActivateMessage, diff --git a/src/daemon/Display.cpp b/src/daemon/Display.cpp index 36da2a0..f68a7fb 100644 --- a/src/daemon/Display.cpp +++ b/src/daemon/Display.cpp @@ -115,12 +115,14 @@ namespace DDM { // connect logout signal connect(m_socketServer, &SocketServer::logout, this, &Display::logout); + // connect lock signal + connect(m_socketServer, &SocketServer::lock, this, &Display::lock); + // connect unlock signal connect(m_socketServer, &SocketServer::unlock,this, &Display::unlock); // connect login result signals connect(this, &Display::loginFailed, m_socketServer, &SocketServer::loginFailed); - connect(this, &Display::loginSucceeded, m_socketServer, &SocketServer::loginSucceeded); } Display::~Display() { @@ -206,10 +208,13 @@ namespace DDM { const QString &user, const QString &password, const Session &session) { if (user == QLatin1String("dde")) { + qWarning() << "Login attempt for user dde"; emit loginFailed(socket, user); return; } + qInfo() << "Start login user" << user; + // Get Auth object Auth *auth = nullptr; for (auto *item : std::as_const(auths)) { @@ -252,8 +257,8 @@ namespace DDM { } // some information - qDebug() << "Session" << session.fileName() << "selected, command:" << session.exec() - << "for VT" << auth->tty; + qInfo() << "Authentication succeed for user" << user << ", opening session" + << session.fileName() << ", command:" << session.exec() << ", VT:" << auth->tty; // save last user and last session DaemonApp::instance()->displayManager()->setLastActivatedUser(user); @@ -299,11 +304,13 @@ namespace DDM { } else if (session.xdgSessionType() == QLatin1String("x11")) { auth->type = X11; + qInfo() << "Stopping Treeland"; daemonApp->treelandConnector()->disconnect(); m_treeland->stop(); QThread::msleep(500); // give some time to treeland to stop properly // Start X server + qInfo() << "Starting X11 display server"; m_x11Server = new XorgDisplayServer(this); connect(m_x11Server, &XorgDisplayServer::stopped, this, &Display::stop); if (!m_x11Server->start(auth->tty)) { @@ -317,6 +324,7 @@ namespace DDM { } else { auth->type = Wayland; + qInfo() << "Stopping Treeland"; daemonApp->treelandConnector()->disconnect(); m_treeland->stop(); QThread::msleep(500); // give some time to treeland to stop properly @@ -331,12 +339,6 @@ namespace DDM { return; } - if (auth->type == Treeland) { - Q_EMIT loginSucceeded(socket, user); - // Tell Treeland to enter the session - activateSession(auth->user, xdgSessionId); - } - connect(auth, &Auth::sessionFinished, this, [this, auth]() { qWarning() << "Session for user" << auth->user << "finished"; auths.removeAll(auth); @@ -349,9 +351,11 @@ namespace DDM { // The user process is ongoing, append to active auths // The auth will be delete later in userProcessFinished() auths << auth; + qInfo() << "Successfully logged in user" << user; } void Display::logout([[maybe_unused]] QLocalSocket *socket, int id) { + qDebug() << "Logout requested for session id" << id; // Do not kill the session leader process before // TerminateSession! Logind will only kill the session's // cgroup (session_stop_scope) when the session is not in @@ -365,13 +369,22 @@ namespace DDM { manager.TerminateSession(QString::number(id)); } + void Display::lock([[maybe_unused]] QLocalSocket *socket, int id) { + qDebug() << "Lock requested for session id" << id; + + OrgFreedesktopLogin1ManagerInterface manager(Logind::serviceName(), + Logind::managerPath(), + QDBusConnection::systemBus()); + manager.LockSession(QString::number(id)); + } + void Display::unlock(QLocalSocket *socket, const QString &user, const QString &password) { if (user == QLatin1String("dde")) { emit loginFailed(socket, user); return; } - qDebug() << "Start identify user" << user; + qInfo() << "Start identify user" << user; // Only run password check // @@ -394,17 +407,16 @@ namespace DDM { // Find the auth that started the session, which contains full informations for (auto *auth : std::as_const(auths)) { if (auth->user == user && auth->xdgSessionId > 0) { - if (auth->type == Treeland) { - // TODO: Use exact ID when there're multiple sessions for a user - // TODO: Jump to auth->tty - Q_EMIT loginSucceeded(socket, user); - activateSession(user, auth->xdgSessionId); - } else { - VirtualTerminal::jumpToVt(auth->tty, false); - } + OrgFreedesktopLogin1ManagerInterface manager(Logind::serviceName(), + Logind::managerPath(), + QDBusConnection::systemBus()); + manager.UnlockSession(QString::number(auth->xdgSessionId)); + VirtualTerminal::jumpToVt(auth->tty, false); + qInfo() << "Successfully identified user" << user; return; } } + qWarning() << "No active session found for user" << user; Q_EMIT loginFailed(socket, user); } } diff --git a/src/daemon/Display.h b/src/daemon/Display.h index 4f517a5..9027415 100644 --- a/src/daemon/Display.h +++ b/src/daemon/Display.h @@ -100,12 +100,12 @@ namespace DDM { void connected(QLocalSocket *socket); void login(QLocalSocket *socket, - const QString &user, const QString &password, + const QString &user, + const QString &password, const Session &session); - void logout(QLocalSocket *socket, - int id); - void unlock(QLocalSocket *socket, - const QString &user, const QString &password); + void logout(QLocalSocket *socket, int id); + void lock(QLocalSocket *socket, int id); + void unlock(QLocalSocket *socket, const QString &user, const QString &password); signals: /** Emitted when stop() */ @@ -116,7 +116,6 @@ namespace DDM { ///////////////////////////////////////////////////// void loginFailed(QLocalSocket *socket, const QString &user); - void loginSucceeded(QLocalSocket *socket, const QString &user); private: /** Indicates whether the display is started */ diff --git a/src/daemon/SocketServer.cpp b/src/daemon/SocketServer.cpp index e7744a5..a040b00 100644 --- a/src/daemon/SocketServer.cpp +++ b/src/daemon/SocketServer.cpp @@ -163,6 +163,15 @@ namespace DDM { emit logout(socket, id); } break; + case GreeterMessages::Lock : { + // log message + qDebug() << "Message received from greeter: Lock"; + int id; + + input >> id; + emit lock(socket, id); + } + break; case GreeterMessages::Unlock : { // log message qDebug() << "Message received from greeter: Unlock"; @@ -232,10 +241,6 @@ namespace DDM { SocketWriter(socket) << quint32(DaemonMessages::LoginFailed) << user; } - void SocketServer::loginSucceeded(QLocalSocket *socket, const QString &user) { - SocketWriter(socket) << quint32(DaemonMessages::LoginSucceeded) << user; - } - void SocketServer::informationMessage(QLocalSocket *socket, const QString &message) { SocketWriter(socket) << quint32(DaemonMessages::InformationMessage) << message; } diff --git a/src/daemon/SocketServer.h b/src/daemon/SocketServer.h index e6a96bc..e66382d 100644 --- a/src/daemon/SocketServer.h +++ b/src/daemon/SocketServer.h @@ -48,7 +48,6 @@ namespace DDM { public slots: void informationMessage(QLocalSocket *socket, const QString &message); void loginFailed(QLocalSocket *socket, const QString &user); - void loginSucceeded(QLocalSocket *socket, const QString &user); signals: void login(QLocalSocket *socket, @@ -56,6 +55,8 @@ namespace DDM { const Session &session); void logout(QLocalSocket *socket, int id); + void lock(QLocalSocket *socket, + int id); void unlock(QLocalSocket *socket, const QString &user, const QString &password); void connected(QLocalSocket *socket); diff --git a/src/daemon/TreelandDisplayServer.cpp b/src/daemon/TreelandDisplayServer.cpp index fabe2d2..06bf3ca 100644 --- a/src/daemon/TreelandDisplayServer.cpp +++ b/src/daemon/TreelandDisplayServer.cpp @@ -82,9 +82,3 @@ void TreelandDisplayServer::onLoginFailed(const QString &user) { SocketWriter(greeter) << quint32(DaemonMessages::LoginFailed) << user; } } - -void TreelandDisplayServer::onLoginSucceeded(const QString &user) { - for (auto greeter : m_greeterSockets) { - SocketWriter(greeter) << quint32(DaemonMessages::LoginSucceeded) << user; - } -} diff --git a/src/daemon/TreelandDisplayServer.h b/src/daemon/TreelandDisplayServer.h index 4ae5979..c548ea4 100644 --- a/src/daemon/TreelandDisplayServer.h +++ b/src/daemon/TreelandDisplayServer.h @@ -23,7 +23,6 @@ namespace DDM { void stop(); void activateUser(const QString &user, int xdgSessionId); void onLoginFailed(const QString &user); - void onLoginSucceeded(const QString &user); private: SocketServer *m_socketServer; diff --git a/src/daemon/UserSession.cpp b/src/daemon/UserSession.cpp index c9732b0..92f0d29 100644 --- a/src/daemon/UserSession.cpp +++ b/src/daemon/UserSession.cpp @@ -58,7 +58,7 @@ namespace DDM { case Display::Treeland: { setProgram(mainConfig.Single.SessionCommand.get()); setArguments(QStringList{ command }); - qInfo() << "Starting Wayland user session:" << program() << command; + qInfo() << "Starting Treeland session:" << program() << command; QProcess::start(); closeWriteChannel(); closeReadChannel(QProcess::StandardOutput);