From 1746e5da40bfd9d4b834b234fe2c89ed455c71e2 Mon Sep 17 00:00:00 2001 From: zhaominyong Date: Fri, 10 Jun 2022 16:28:29 +0800 Subject: [PATCH] =?UTF-8?q?8688=20=E3=80=90=E7=B3=BB=E7=BB=9F=E5=85=B3?= =?UTF-8?q?=E6=9C=BA/=E6=B3=A8=E9=94=80=E3=80=91=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=B3=E6=9C=BA/=E6=B3=A8=E9=94=80=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backup-daemon/mybackupmanager.cpp | 69 +++++++++++++++++++++++++++++++ backup-daemon/mybackupmanager.h | 4 ++ 2 files changed, 73 insertions(+) diff --git a/backup-daemon/mybackupmanager.cpp b/backup-daemon/mybackupmanager.cpp index d3c1389..ce9b6b9 100755 --- a/backup-daemon/mybackupmanager.cpp +++ b/backup-daemon/mybackupmanager.cpp @@ -4,6 +4,12 @@ #include #include +#include +#include +#include +#include +#include +#include #include #include #include @@ -449,6 +455,11 @@ bool MyBackupManager::lock(int frontUid) return false; } + // 用于替换上面的文件锁的,对应需求8688 +// if (!inhibit()) +// return false; + inhibit(); + m_fdLockFile = lock_file_fd; m_backupState = BackupState::PREPARING; m_isActive = true; @@ -458,6 +469,63 @@ bool MyBackupManager::lock(int frontUid) return true; } +/** + * @brief 阻止session关机用dbus新接口Inhibit来替换锁文件的方式 + * Inhibti函数有四个参数,按顺序分别为: + what:需要阻止的是哪些过程,如关机、重启;可以输入的值有:”shutdown”(会同时阻止关机重启),”sleep”(会同时阻止睡眠休眠),需要同时阻止关机和睡眠可以输入 ”shutdown:sleep” + who:是谁要阻止,调用该接口的程序名,如”ukui-sessions” + why:阻止的原因,简单描述即可,如”package installing” + mode:阻止的方式;可输入的值有”block”(无限期阻止,等到主动取消阻止才会取消),”delay”(超时阻止,超时一段时间后不管阻止锁,直接执行) + 建议使用”block”的方式阻止,加锁后记得解锁 + 可以使用同服务下ListInhibitors方法获取当前的inhibit锁,测试验证是否成功注册 + * @return + */ +bool MyBackupManager::inhibit() +{ + QDBusMessage message; + message = QDBusMessage::createMethodCall("org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + QStringLiteral("Inhibit")); + QString why("The backup and restore tool is busy, please wait."); + // 区分中英文 + QString locale = QLocale::system().name(); + if (locale == "zh_CN") { + why = QString("备份还原工具正在忙碌,请稍等"); + } else if (locale == "bo_CN") { + why = QString("གྲབས་ཉར་སོར་ཆུད་ལག་ཆ་བྲེལ་བཞིན་ཡོད་།ཏོག་ཙམ་སྒུག་རོགས་།"); + } + + // QVariantList({what, who, why, mode}) + QVariantList args; + args << QString("shutdown:sleep"); + args << QString("backup-daemon"); + args << why; + args << QString("block"); + message.setArguments(args); + + QDBusPendingReply reply = QDBusConnection::systemBus().call(message); + + if (!reply.isValid()) { + qDebug() << "inhibit faile!"; + return false; + } + QDBusUnixFileDescriptor inhibitFileDescriptor = reply.value(); + inhibitFileDescriptor.swap(m_inhibitFileDescriptor); + qDebug() << "inhibit success!"; + return true; +} + +void MyBackupManager::uninhibit() +{ + if (!m_inhibitFileDescriptor.isValid()) { + return; + } + + qDebug() << "uninhibit success!"; + m_inhibitFileDescriptor = QDBusUnixFileDescriptor(); +} + /** * @brief 解锁应用 * @return bool @@ -469,6 +537,7 @@ bool MyBackupManager::unlock() setKysecStatus(true); Utils::unLockProgram(m_fdLockFile); + uninhibit(); m_fdLockFile = -1; m_backupState = BackupState::BACKUP_STATE_INIT; diff --git a/backup-daemon/mybackupmanager.h b/backup-daemon/mybackupmanager.h index 6491877..d2fb3ae 100755 --- a/backup-daemon/mybackupmanager.h +++ b/backup-daemon/mybackupmanager.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "mythread.h" #include "../common/mydefine.h" #include "../common/spinlock_mutex.h" @@ -74,8 +75,10 @@ private slots: private: // 锁定 bool lock(int frontUid); + bool inhibit(); // 解锁 bool unlock(); + void uninhibit(); // 自旋锁 spinlock_mutex m_mutex; @@ -85,6 +88,7 @@ private: private: // 锁文件描述符 int m_fdLockFile = -1; + QDBusUnixFileDescriptor m_inhibitFileDescriptor; // 备份还原状态 BackupState m_backupState = BackupState::BACKUP_STATE_INIT; // 是否活动的,兼容以前用的,和m_backupState的实际使用功能有重复