refactor: Replace popen with QProcess for process count and kill oper…#421
Conversation
…ations - Updated `getProcessCountByName` to use `QProcess` for executing the `ps` command, improving reliability and handling of process output. - Refactored `killProcessByName` to utilize `QProcess` for killing processes, enhancing code consistency and error handling. - Improved null and empty string checks for process names in both functions. This change enhances the maintainability and performance of process management in the application.
Reviewer's GuideRefactors process management utilities to use Qt's QProcess instead of popen/system, adds basic logging, and tightens null/empty checks for process names. Sequence diagram for getProcessCountByName refactor using QProcesssequenceDiagram
participant Caller
participant Utils
participant QProcess
Caller->>Utils: getProcessCountByName(pstrName)
Utils->>Utils: check pstrName null or empty
alt invalid_name
Utils-->>Caller: return -1
else valid_name
Utils->>QProcess: start(ps, [-ef])
QProcess-->>Utils: started
Utils->>QProcess: waitForFinished(5000)
alt timeout
Utils-->>Caller: return -1
else finished
Utils->>QProcess: readAllStandardOutput()
QProcess-->>Utils: output
Utils->>Utils: split output by newline
Utils->>Utils: count lines containing processName
Utils-->>Caller: return count
end
end
Sequence diagram for killProcessByName refactor using QProcesssequenceDiagram
participant Caller
participant Utils
participant QProcess
Caller->>Utils: killProcessByName(pstrName)
Utils->>Utils: check pstrName null or empty
alt invalid_name
Utils-->>Caller: return
else valid_name
Utils->>QProcess: start(killall, [processName])
QProcess-->>Utils: started
Utils->>QProcess: waitForFinished(5000)
QProcess-->>Utils: finished
Utils-->>Caller: return
end
Class diagram for Utils process management refactorclassDiagram
class Utils {
+int getProcessCountByName(const char * pstrName)
+void killProcessByName(const char * pstrName)
}
class QProcess {
+void start(QString program, QStringList arguments)
+bool waitForFinished(int msecs)
+QByteArray readAllStandardOutput()
}
Utils ..> QProcess : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: LiHua000, max-lvs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
getProcessCountByNameimplementation may overcount because it blindly counts anyps -efline containing the name (e.g., header line or unrelated arguments); consider filtering out the header and matching on the executable column or with a stricter pattern. - Both
getProcessCountByNameandkillProcessByNamenow useQProcessbut ignore its exit code and error state; checkingprocess.exitStatus()/process.error()and logging failures would make the behavior more predictable and debuggable. - The added
qDebug()calls in these utility functions might be noisy in production or performance‑sensitive paths; consider guarding them with a debug flag or reducing verbosity.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `getProcessCountByName` implementation may overcount because it blindly counts any `ps -ef` line containing the name (e.g., header line or unrelated arguments); consider filtering out the header and matching on the executable column or with a stricter pattern.
- Both `getProcessCountByName` and `killProcessByName` now use `QProcess` but ignore its exit code and error state; checking `process.exitStatus()` / `process.error()` and logging failures would make the behavior more predictable and debuggable.
- The added `qDebug()` calls in these utility functions might be noisy in production or performance‑sensitive paths; consider guarding them with a debug flag or reducing verbosity.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
deepin pr auto review这段代码主要重构了两个函数: 总体来说,重构方向是正确的,使用 以下是详细的审查意见: 1. 语法逻辑与功能正确性主要问题:
2. 代码质量
3. 代码性能
4. 代码安全
改进后的代码示例针对 #include <QCoreApplication>
#include <QProcess>
#include <QStringList>
int Utils::getProcessCountByName(const char *pstrName)
{
// 1. 参数校验
if (NULL == pstrName || strlen(pstrName) == 0) {
return -1;
}
QString processName = QString::fromUtf8(pstrName);
// 2. 启动进程
QProcess process;
// 使用 -o 参数只输出我们需要的信息,减少数据量:PID 和 CMD
// 注意:不同系统 ps 参数可能略有不同,Linux 下通常如下
process.start("ps", QStringList() << "-e" << "-o" << "pid,comm");
if (!process.waitForFinished(5000)) {
qWarning() << "ps command timeout or failed";
process.kill();
return -1;
}
QByteArray output = process.readAllStandardOutput();
int count = 0;
qint64 currentPid = QCoreApplication::applicationPid();
bool ok = false;
// 3. 解析输出
// ps 输出第一行通常是标题 "PID CMD",需要跳过
QList<QByteArray> lines = output.split('\n');
for (int i = 1; i < lines.size(); ++i) {
const QByteArray &line = lines[i].trimmed();
if (line.isEmpty()) continue;
// 解析 PID 和 CMD
// 假设 ps 输出格式为 "PID CMD",以空格分隔
QList<QByteArray> parts = line.split(' ');
if (parts.size() < 2) continue;
qint64 pid = parts[0].toLongLong(&ok);
QString cmd = QString::fromLocal8Bit(parts[1]); // comm 通常只显示进程名不含路径
if (ok && pid != currentPid) {
// 严格匹配进程名
if (cmd == processName) {
count++;
}
}
}
return count;
}
void Utils::killProcessByName(const char *pstrName)
{
if (pstrName == NULL || strlen(pstrName) == 0) {
return;
}
QProcess process;
// killall 也是通过参数传递,安全
process.start("killall", QStringList() << QString::fromUtf8(pstrName));
// 等待结束,但不阻塞太久
if (!process.waitForFinished(3000)) {
qWarning() << "killall command timeout";
process.kill();
}
}总结修改点:
|
|
/merge |
|
This pr cannot be merged! (status: unstable) |
|
/merge |
…ations
getProcessCountByNameto useQProcessfor executing thepscommand, improving reliability and handling of process output.killProcessByNameto utilizeQProcessfor killing processes, enhancing code consistency and error handling.This change enhances the maintainability and performance of process management in the application.
Summary by Sourcery
Refactor process management utilities to use Qt's QProcess instead of C library process APIs.
Enhancements: