From 537f9a4ab61b9be8f4bb6a8bc0d3c9d4c47993d4 Mon Sep 17 00:00:00 2001 From: Dominik Riebeling Date: Wed, 30 Dec 2015 18:27:13 +0100 Subject: [PATCH] Return process IDs in Utils::findRunningProcess(). Rework to return mapping between (filtered) process names and their pid values (if running). Note that this functionality is not implemented for Linux (but as it's mostly intended to detect Itunes currently not needed on that platform). Change-Id: Ie348bfed46bda390f469c37635a96c75e5782616 --- rbutil/rbutilqt/base/utils.cpp | 58 ++++++++++++++++++++-------------- rbutil/rbutilqt/base/utils.h | 2 +- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/rbutil/rbutilqt/base/utils.cpp b/rbutil/rbutilqt/base/utils.cpp index 6bd9e23230..f58d8c34e3 100644 --- a/rbutil/rbutilqt/base/utils.cpp +++ b/rbutil/rbutilqt/base/utils.cpp @@ -670,13 +670,13 @@ QStringList Utils::mountpoints(enum MountpointsFilter type) /** Check if a process with a given name is running - * @param names list of names to check - * @return list of detected processes. + * @param names list of names to filter on. All processes if empty list. + * @return list of processname, process ID pairs. */ -QStringList Utils::findRunningProcess(QStringList names) +QMap > Utils::findRunningProcess(QStringList names) { - QStringList processlist; - QStringList found; + QMap > processlist; + QMap > found; #if defined(Q_OS_WIN32) HANDLE hdl; PROCESSENTRY32 entry; @@ -694,14 +694,16 @@ QStringList Utils::findRunningProcess(QStringList names) return found; } - processlist.append(QString::fromWCharArray(entry.szExeFile)); do { + int pid = entry.th32ProcessID; // FIXME: DWORD vs int! + QString name = QString::fromWCharArray(entry.szExeFile); + if(processlist.find(name) == processlist.end()) { + processlist.insert(name, QList()); + } + processlist[name].append(pid); entry.dwSize = sizeof(PROCESSENTRY32); entry.szExeFile[0] = '\0'; result = Process32Next(hdl, &entry); - if(result) { - processlist.append(QString::fromWCharArray(entry.szExeFile)); - } } while(result); CloseHandle(hdl); #endif @@ -726,28 +728,38 @@ QStringList Utils::findRunningProcess(QStringList names) if(isprint(buf[i])) break; } // avoid adding duplicates. - QString process = QString::fromUtf8(&buf[i]); - if(!processlist.contains(process)) { - processlist.append(process); + QString name = QString::fromUtf8(&buf[i]); + if(processlist.find(name) == processlist.end()) { + processlist.insert(name, QList()); } - + processlist[name].append(pid); } } } while(err == noErr); #endif - // check for given names in list of processes - for(int i = 0; i < names.size(); ++i) { -#if defined(Q_OS_WIN32) - // the process name might be truncated. Allow the extension to be partial. - int index = processlist.indexOf(QRegExp(names.at(i) + "(\\.(e(x(e?)?)?)?)?")); -#else - int index = processlist.indexOf(names.at(i)); +#if defined(Q_OS_LINUX) + // not implemented for Linux! #endif - if(index != -1) { - found.append(processlist.at(index)); + // Filter for names (unless empty) + if(names.size() > 0) { + for(int i = 0; i < names.size(); ++i) { + QStringList k(processlist.keys()); +#if defined(Q_OS_WIN32) + // the process name might be truncated. Allow the extension to be partial. + int index = k.indexOf(QRegExp(names.at(i) + "(\\.(e(x(e?)?)?)?)?", + Qt::CaseInsensitive)); +#else + int index = k.indexOf(names.at(i)); +#endif + if(index != -1) { + found.insert(k[index], processlist[k[index]]); + } } } - LOG_INFO() << "Found listed processes running:" << found; + else { + found = processlist; + } + LOG_INFO() << "Looking for processes" << names << "found" << found; return found; } diff --git a/rbutil/rbutilqt/base/utils.h b/rbutil/rbutilqt/base/utils.h index bc45ebd857..7dfd797cbd 100644 --- a/rbutil/rbutilqt/base/utils.h +++ b/rbutil/rbutilqt/base/utils.h @@ -54,7 +54,7 @@ public: static QStringList mountpoints(enum MountpointsFilter type = MountpointsAll); static QString resolveDevicename(QString path); static QString resolveMountPoint(QString device); - static QStringList findRunningProcess(QStringList names); + static QMap > findRunningProcess(QStringList names); static bool ejectDevice(QString device); };