From bb569d0d85f65d02bb60119c0bc4f10602493cfa Mon Sep 17 00:00:00 2001 From: groveer Date: Thu, 12 Nov 2026 10:53:50 +0800 Subject: [PATCH] fix: honour logind block inhibitors for power actions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. ddm runs as root and called the old Suspend(bool)-style logind 2. methods, which only set SD_LOGIND_INTERACTIVE, so logind let root 3. ignore block inhibitors and a file copy task in a Treeland session 4. could still be suspended from the lock screen shutdown menu 5. Call the *WithFlags variants with SD_LOGIND_ROOT_CHECK_INHIBITORS so 6. logind enforces the inhibitors for root as well, and log a warning 7. when the request is refused Log: Power actions from the lock screen now honour app block inhibitors Influence: 1. Start a large copy in dde-file-manager, then choose Suspend from the shutdown menu; the system must stay awake and ddm must log a warning 2. Without any inhibitor, verify Suspend, Hibernate, Power Off and Reboot still work from the lock screen 3. Verify power actions from the greeter before login still work, and a ConsoleKit2 build keeps the old logind call fix: 电源操作不再绕过 logind 的待机/关机抑制 1. ddm 以 root 运行且调用旧式 Suspend(bool) 系列 logind 接口,该接口只 设置 SD_LOGIND_INTERACTIVE,logind 因此允许 root 忽略 block inhibitor,Treeland 会话中有拷贝任务时仍能从关机菜单待机 2. 改用 *WithFlags 系列接口并传入 SD_LOGIND_ROOT_CHECK_INHIBITORS,由 logind 对 root 一并校验抑制,被拒时记录告警日志 Log: 关机菜单发起的待机、关机等电源操作现在会遵守应用的待机抑制 Influence: 1. 用文件管理器拷贝大文件,再从关机菜单点待机,系统应保持唤醒且 ddm 日志中出现告警 2. 无任何抑制时,锁屏下待机、休眠、关机、重启均正常工作 3. 登录界面(greeter)下的电源操作正常,ConsoleKit2 环境仍走旧接口 PMS: BUG-380947 --- src/daemon/PowerManager.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/daemon/PowerManager.cpp b/src/daemon/PowerManager.cpp index 67f4ac6..c937fe9 100644 --- a/src/daemon/PowerManager.cpp +++ b/src/daemon/PowerManager.cpp @@ -171,26 +171,46 @@ const QString CK2_OBJECT = QStringLiteral("org.freedesktop.ConsoleKit.Manager"); } void powerOff() const { - m_interface->call(QStringLiteral("PowerOff"), true); + callWithInhibitCheck(QStringLiteral("PowerOff"), QStringLiteral("PowerOffWithFlags")); } void reboot() const { - m_interface->call(QStringLiteral("Reboot"), true); + callWithInhibitCheck(QStringLiteral("Reboot"), QStringLiteral("RebootWithFlags")); } void suspend() const { - m_interface->call(QStringLiteral("Suspend"), true); + callWithInhibitCheck(QStringLiteral("Suspend"), QStringLiteral("SuspendWithFlags")); } void hibernate() const { - m_interface->call(QStringLiteral("Hibernate"), true); + callWithInhibitCheck(QStringLiteral("Hibernate"), QStringLiteral("HibernateWithFlags")); } void hybridSleep() const { - m_interface->call(QStringLiteral("HybridSleep"), true); + callWithInhibitCheck(QStringLiteral("HybridSleep"), QStringLiteral("HybridSleepWithFlags")); } private: + // The old Suspend(bool) style API only sets SD_LOGIND_INTERACTIVE, so logind lets a + // root caller (us) ignore block inhibitors and the desktop's "don't sleep/shut down" + // requests are lost. The *WithFlags variants can ask logind to enforce them for root + // too, which keeps a user-initiated power action behind the lock screen behaving like + // it does on X11. ConsoleKit2 has no such API and no inhibitors, so it keeps the old + // call. + void callWithInhibitCheck(const QString &plainMethod, const QString &flagsMethod) const { + if (m_interface->interface() != LOGIN1_OBJECT) { + m_interface->call(plainMethod, true); + return; + } + + // logind declares the parameter as uint64 ("t"); an int64 ("x") is refused with + // InvalidArgs before the flag is even looked at. + constexpr qulonglong rootCheckInhibitors = 1; // SD_LOGIND_ROOT_CHECK_INHIBITORS + auto reply = m_interface->call(flagsMethod, rootCheckInhibitors); + if (reply.type() == QDBusMessage::ErrorMessage) + qWarning() << flagsMethod << "refused:" << reply.errorMessage(); + } + QDBusInterface *m_interface { nullptr }; };