std::string ProcFS::GetProcessExecutableName(pid_t pid) { std::string name(GetProcessExecutablePath(pid)); if (name.empty()) return std::string(); return basename(&name[0]); }
bool ProcFS::ReadProcessInfo(pid_t pid, ProcessInfo &info) { pid_t ppid; uid_t uid, euid; gid_t gid, egid; ELFInfo elf; std::string path; info.clear(); if (!ReadProcessIds(pid, ppid, uid, euid, gid, egid) || !GetProcessELFInfo(pid, elf) || (path = GetProcessExecutablePath(pid)).empty()) return false; info.pid = pid; info.parentPid = ppid; info.name.swap(path); info.realUid = uid; info.effectiveUid = euid; info.realGid = gid; info.effectiveGid = egid; if (!ELFSupport::MachineTypeToCPUType(elf.machine, elf.is64Bit, info.cpuType, info.cpuSubType)) { info.cpuType = kCPUTypeAny; info.cpuSubType = kCPUSubTypeInvalid; } info.nativeCPUType = elf.machine; info.nativeCPUSubType = kInvalidCPUType; info.endian = elf.endian; info.pointerSize = elf.is64Bit ? 8 : 4; info.osType = Platform::GetOSTypeName(); info.osVendor = Platform::GetOSVendorName(); return true; }
static std::string GetRealPath(const std::string& path) { std::string pathReal = path; // using NULL here is not supported in very old systems, // but should be no problem for spring // see for older systems: // http://stackoverflow.com/questions/4109638/what-is-the-safe-alternative-to-realpath char* pathRealC = realpath(path.c_str(), NULL); if (pathRealC != NULL) { pathReal = pathRealC; free(pathRealC); pathRealC = NULL; } if (FileSystem::GetDirectory(pathReal).empty()) { pathReal = GetProcessExecutablePath() + pathReal; } return pathReal; }
bool ProcFS::EnumerateProcesses(bool allUsers, uid_t uid, std::function<void(pid_t, uid_t)> const &cb) { DIR *dir = OpenDIR(""); if (dir == nullptr) return false; while (struct dirent *dp = readdir(dir)) { pid_t pid = strtol(dp->d_name, nullptr, 0); if (pid == 0) continue; // // Get the uid. // struct stat stbuf; char path[PATH_MAX + 1]; MakePath(path, PATH_MAX, pid, ""); if (stat(path, &stbuf) < 0) continue; // // Compare if necessary. // if (!allUsers && stbuf.st_uid != uid) continue; // // We don't want kernel threads, so exclude them from the list, // we know they are kernel threads because "exe" points to nothing. // if (GetProcessExecutablePath(pid).empty()) continue; cb(pid, stbuf.st_uid); } closedir(dir); return true; }