From 2461ca1c2f57a06542b9f99c8160b516765ffe18 Mon Sep 17 00:00:00 2001 From: dengzhongyuan Date: Sat, 31 Jan 2026 11:24:21 +0800 Subject: [PATCH] refactor: Replace popen with QProcess for process count and kill operations - 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. --- src/common/utils.cpp | 47 ++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 88fcb13db..424f20252 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -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)) { + qDebug() << "ps command timeout"; + 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)