Skip to content
Merged
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)) {
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)
Expand Down
Loading