Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
#include <QFile>
#include <QLoggingCategory>

#include <sys/utsname.h>

Check warning on line 16 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/utsname.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unistd.h>

Check warning on line 17 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fcntl.h>

Check warning on line 18 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <fcntl.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/file.h>

Check warning on line 19 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/file.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace DDLog;

Expand Down Expand Up @@ -231,25 +232,25 @@
return false;
}

bool Utils::isDpkgLocked()

Check warning on line 235 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'isDpkgLocked' is never used.
{
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) {

Check warning on line 242 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Consider using std::any_of algorithm instead of a raw loop.
int fd = open(lockFile.toLocal8Bit().constData(), O_RDONLY | O_CLOEXEC);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): flock(fd, LOCK_EX | LOCK_NB) fails with EBADF because each lock file is opened with O_RDONLY, while an exclusive flock requires a file descriptor opened for writing. Since every existing dpkg lock file follows this path, isDpkgLocked() returns true even when no dpkg operation is running, causing installation workflows to retry until timeout.

Triggers: When either dpkg lock file exists, including the normal idle system state.

Suggested fix: Open the lock files with a writable mode such as O_RDWR before attempting LOCK_EX, or use a lock-checking operation compatible with the descriptor mode.

Suggested change
int fd = open(lockFile.toLocal8Bit().constData(), O_RDONLY | O_CLOEXEC);
int fd = open(lockFile.toLocal8Bit().constData(), O_RDWR | 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()
Expand Down
Loading