rbutilqt: Replace Utils::findRunningProcess() on macOS with a libproc implementation

GetNextProcess, GetProcessPID and GetProcessInformation are replaced by proc_listallpids() and proc_pidpath()

Co-authored-by: ChatGPT (GPT-5.3 Mini) <chatgpt@openai.com>
Change-Id: Ic4fe13c405e11b658b1a9540e49741d4e69069f3
This commit is contained in:
Vencislav Atanasov 2026-06-29 01:10:18 +03:00
parent 45bd7c4400
commit 6aa8d80ba2

View file

@ -56,6 +56,8 @@
#include <tlhelp32.h> #include <tlhelp32.h>
#endif #endif
#if defined(Q_OS_MACOS) #if defined(Q_OS_MACOS)
#include <libproc.h>
#include <unistd.h>
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h> #include <CoreServices/CoreServices.h>
@ -693,34 +695,45 @@ QMap<QString, QList<int> > Utils::findRunningProcess(QStringList names)
CloseHandle(hdl); CloseHandle(hdl);
#endif #endif
#if defined(Q_OS_MACOS) #if defined(Q_OS_MACOS)
ProcessSerialNumber psn = { 0, kNoProcess }; int bufferSize = proc_listallpids(nullptr, 0);
OSErr err; if (bufferSize <= 0) {
do { LOG_ERROR() << "proc_listallpids failed.";
pid_t pid; return found;
err = GetNextProcess(&psn); }
err = GetProcessPID(&psn, &pid);
if(err == noErr) { int count = bufferSize / sizeof(pid_t);
char buf[32] = {0}; QVector<pid_t> pids(count);
ProcessInfoRec info; bufferSize = proc_listallpids(pids.data(), (int)(pids.size() * sizeof(pid_t)));
memset(&info, 0, sizeof(ProcessInfoRec));
info.processName = (unsigned char*)buf; if (bufferSize <= 0) {
info.processInfoLength = sizeof(ProcessInfoRec); LOG_ERROR() << "proc_listallpids failed (2).";
err = GetProcessInformation(&psn, &info); return found;
if(err == noErr) { }
// some processes start with nonprintable characters. Skip those.
int i; char pathBuffer[PROC_PIDPATHINFO_MAXSIZE];
for(i = 0; i < 32; i++) {
if(isprint(buf[i])) break; for (int i = 0; i < count; ++i) {
} pid_t pid = pids[i];
// avoid adding duplicates. if (pid <= 0) continue;
QString name = QString::fromUtf8(&buf[i]);
if(processlist.find(name) == processlist.end()) { // Get executable path
processlist.insert(name, QList<int>()); int ret = proc_pidpath(pid, pathBuffer, sizeof(pathBuffer));
} if (ret <= 0) continue;
processlist[name].append(pid);
} QString fullPath = QString::fromUtf8(pathBuffer);
// Extract process name from path
QString name = QFileInfo(fullPath).fileName();
if (name.isEmpty())
continue;
if (!processlist.contains(name)) {
processlist.insert(name, QList<int>());
} }
} while(err == noErr);
processlist[name].append(pid);
}
#endif #endif
#if defined(Q_OS_LINUX) #if defined(Q_OS_LINUX)
// not implemented for Linux! // not implemented for Linux!