-
Notifications
You must be signed in to change notification settings - Fork 81
refactor: Replace popen with QProcess for process count and kill oper… #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+758
to
+765
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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
-1on timeout without explicitly stoppingps, it may keep running unnecessarily. Even thoughQProcesswill eventually be destroyed, it’s clearer and safer to explicitlyterminate()(and, if needed after a short wait,kill()) the process whenwaitForFinishedtimes out.