Skip to content
Closed
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
47 changes: 28 additions & 19 deletions src/common/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,39 +748,48 @@ void Utils::setChildrenFocus(QWidget *pWidget, Qt::FocusPolicy policy)

int Utils::getProcessCountByName(const char *pstrName)
{
FILE *fp = NULL;
int count = -1;
char command[1024];
qDebug() << "Enter getProcessCountByName, pstrName:" << pstrName;

if (NULL == pstrName || strlen(pstrName) == 0) {
return count;
qDebug() << "pstrName is null";
return -1;
}

memset(command, 0, sizeof(command));
sprintf(command, "ps -ef | grep %s | grep -v grep | wc -l", pstrName);
QProcess process;
process.start("ps", QStringList() << "-ef");
if (!process.waitForFinished(5000)) {
Comment on lines +759 to +760
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Consider cleaning up or killing the ps process on timeout instead of just returning.

Since we return -1 on timeout without explicitly stopping ps, it may keep running unnecessarily. Even though QProcess will eventually be destroyed, it’s clearer and safer to explicitly terminate() (and, if needed after a short wait, kill()) the process when waitForFinished times out.

qDebug() << "ps command timeout";
return -1;
}

QString output = process.readAllStandardOutput();
Comment on lines +758 to +765
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Handle ps execution errors and non-zero exit statuses explicitly.

Currently only the timeout is handled; all other outcomes are treated as success. Please also check process.error(), process.exitStatus(), and process.exitCode(), and return -1 (or another explicit error code) when ps fails instead of proceeding to parse a potentially empty or partial output buffer.

Suggested change
QProcess process;
process.start("ps", QStringList() << "-ef");
if (!process.waitForFinished(5000)) {
qDebug() << "ps command timeout";
return -1;
}
QString output = process.readAllStandardOutput();
QProcess process;
process.start("ps", QStringList() << "-ef");
if (!process.waitForFinished(5000)) {
qDebug() << "ps command timeout";
return -1;
}
if (process.error() != QProcess::UnknownError) {
qDebug() << "ps command failed, error:" << process.errorString();
return -1;
}
if (process.exitStatus() != QProcess::NormalExit) {
qDebug() << "ps command did not exit normally, exitStatus:" << process.exitStatus();
return -1;
}
if (process.exitCode() != 0) {
qDebug() << "ps command exited with non-zero code:" << process.exitCode();
return -1;
}
QString output = process.readAllStandardOutput();

QString processName = QString::fromUtf8(pstrName);
int count = 0;

if ((fp = popen(command, "r")) != NULL) {
char buf[1024];
memset(buf, 0, sizeof(buf));
if ((fgets(buf, sizeof(buf) - 1, fp)) != NULL) {
count = atoi(buf);
for (const QString &line : output.split('\n')) {
if (line.contains(processName)) {
count++;
}
pclose(fp);
} else {
qDebug() << ">>> popen error";
}

qDebug() << "Exit getProcessCountByName, count:" << count;
return count;
}

void Utils::killProcessByName(const char *pstrName)
{
if (pstrName != NULL && strlen(pstrName) > 0) {
char command[1024];
memset(command, 0, sizeof(command));
sprintf(command, "killall %s", pstrName);
system(command);
qDebug() << "Enter killProcessByName, pstrName:" << pstrName;

if (pstrName == NULL || strlen(pstrName) == 0) {
qDebug() << "pstrName is null or empty";
return;
}

QProcess process;
process.start("killall", QStringList() << QString::fromUtf8(pstrName));
process.waitForFinished(5000);

qDebug() << "Exit killProcessByName";
}

QString Utils::getStringMD5Hash(const QString &input)
Expand Down
Loading