From 9b9d71ea2d701272a8c8607576c0e3955cd3541c Mon Sep 17 00:00:00 2001 From: zhanghongyuan Date: Fri, 18 Sep 2026 06:26:31 +0800 Subject: [PATCH] fix: use flock to check dpkg lock instead of ps scan MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: isDpkgLocked() scanned ps output for "dpkg" processes and only excluded dpkg-query, causing false positives from other dpkg-related processes like dpkg-stat 2. Fix: replace ps scanning with non-blocking exclusive flock on dpkg lock files (/var/lib/dpkg/lock-frontend and /var/lib/dpkg/lock) to accurately detect active dpkg operations 3. Impact: isDpkgLocked() now returns accurate results, eliminating false positives and improving performance by avoiding QProcess Influence: 1. Test isDpkgLocked() returns false when no dpkg operation is running 2. Test isDpkgLocked() returns true when dpkg is actively running 3. Verify no regression in driver install/uninstall workflows fix: 使用flock检查dpkg锁替代ps进程扫描 1. 根因:isDpkgLocked()通过扫描ps输出中包含"dpkg"的进程来判断锁状态, 仅排除dpkg-query,其他dpkg相关进程如dpkg-stat会导致误报 2. 方案:使用非阻塞排他flock检查dpkg锁文件(/var/lib/dpkg/lock-frontend 和/var/lib/dpkg/lock)替代ps扫描,准确检测dpkg操作状态 3. 影响:isDpkgLocked()返回结果更准确,消除误报,避免QProcess开销提升性能 Influence: 1. 测试无dpkg操作运行时isDpkgLocked()返回false 2. 测试dpkg正在运行时isDpkgLocked()返回true 3. 验证驱动安装/卸载流程无回归 PMS: DEFECT-002 --- .../src/drivercontrol/utils.cpp | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp b/deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp index 9fc154e17..f7242cd86 100644 --- a/deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp +++ b/deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp @@ -16,6 +16,7 @@ #include #include #include +#include using namespace DDLog; @@ -233,23 +234,23 @@ bool Utils::isFileLocked(const QString &filepath, bool bread) bool Utils::isDpkgLocked() { - QProcess proc; - proc.setProgram("ps"); - proc.setArguments(QStringList() << "-e" << "-o" << "comm"); - proc.start(); - proc.waitForFinished(); - QString info = proc.readAllStandardOutput(); - if (!info.contains("dpkg")) - return false; - - // Split the output search for the 'grep dpkg ' pattern - foreach (QString out, info.split("\n")) { - if (out.contains("dpkg")) { - if(out.trimmed() == "dpkg-query") - return false; - } - } - return true; + // Check dpkg lock files using non-blocking exclusive flock. + const QStringList lockFiles = { + "/var/lib/dpkg/lock-frontend", + "/var/lib/dpkg/lock" + }; + for (const QString &lockFile : lockFiles) { + int fd = open(lockFile.toLocal8Bit().constData(), O_RDONLY | O_CLOEXEC); + if (fd < 0) { + return true; + } + if (flock(fd, LOCK_EX | LOCK_NB) == -1) { + close(fd); + return true; + } + close(fd); + } + return false; } QString Utils::getUrl()