bool ProcParser::ParseProcessStatus()

in core/common/ProcParser.cpp [464:581]


bool ProcParser::ParseProcessStatus(pid_t pid, const std::string& content, ProcessStatus& ps) const {
    ps.pid = pid;

    StringViewSplitter lineSplitter(StringView(content), "\n");
    for (const auto& line : lineSplitter) {
        auto colonPos = line.find(':');
        if (colonPos == StringView::npos || colonPos == line.size() - 1) {
            continue;
        }

        StringView key = line.substr(0, colonPos);
        StringView value = line.substr(colonPos + 1);

        // 去除前导空格和制表符
        while (!value.empty() && (value[0] == ' ' || value[0] == '\t')) {
            value.remove_prefix(1);
        }

        if (key == "Uid") {
            // Uid: real_uid effective_uid saved_uid fs_uid
            StringViewSplitter uidSplitter(value, "\t");
            size_t index = 0;
            for (const auto& part : uidSplitter) {
                if (part.empty()) {
                    continue;
                }

                switch (index) {
                    case 0:
                        if (!StringTo(part, ps.realUid)) {
                            LOG_WARNING(sLogger, ("Invalid real_uid:", part));
                        }
                        break;
                    case 1:
                        if (!StringTo(part, ps.effectiveUid)) {
                            LOG_WARNING(sLogger, ("Invalid effective_uid:", part));
                        }
                        break;
                    case 2:
                        if (!StringTo(part, ps.savedUid)) {
                            LOG_WARNING(sLogger, ("Invalid saved_uid:", part));
                        }
                        break;
                    case 3:
                        if (!StringTo(part, ps.fsUid)) {
                            LOG_WARNING(sLogger, ("Invalid fs_uid:", part));
                        }
                        break;
                    default:
                        break;
                }
                ++index;
            }
        } else if (key == "Gid") {
            // Gid: real_gid effective_gid saved_gid fs_gid
            StringViewSplitter gidSplitter(value, "\t");
            size_t index = 0;
            for (const auto& part : gidSplitter) {
                if (part.empty()) {
                    continue;
                }

                switch (index) {
                    case 0:
                        if (!StringTo(part, ps.realGid)) {
                            LOG_WARNING(sLogger, ("Invalid real_gid:", part));
                        }
                        break;
                    case 1:
                        if (!StringTo(part, ps.effectiveGid)) {
                            LOG_WARNING(sLogger, ("Invalid effective_gid:", part));
                        }
                        break;
                    case 2:
                        if (!StringTo(part, ps.savedGid)) {
                            LOG_WARNING(sLogger, ("Invalid saved_gid:", part));
                        }
                        break;
                    case 3:
                        if (!StringTo(part, ps.fsGid)) {
                            LOG_WARNING(sLogger, ("Invalid fs_gid:", part));
                        }
                        break;
                    default:
                        break;
                }
                ++index;
            }
        } else if (key == "NStgid") {
            // NStgid: namespace_tgid [namespace_tgid ...]
            ps.nstgid.clear();
            StringViewSplitter nstgidSplitter(value, "\t");
            for (const auto& part : nstgidSplitter) {
                if (!part.empty()) {
                    pid_t nstgid = 0;
                    if (!StringTo(part, nstgid)) {
                        LOG_WARNING(sLogger, ("Invalid nstgid:", value));
                    }
                    ps.nstgid.push_back(nstgid);
                }
            }
        } else if (key == "CapPrm") { // CapPrm: 16进制表示的权限掩码
            if (!StringTo(value, ps.capPrm, 16)) {
                LOG_WARNING(sLogger, ("Invalid CapPrm:", value));
            }
        } else if (key == "CapEff") { // CapEff: 16进制表示的权限掩码
            if (!StringTo(value, ps.capEff, 16)) {
                LOG_WARNING(sLogger, ("Invalid CapEff:", value));
            }
        } else if (key == "CapInh") { // CapInh: 16进制表示的权限掩码
            if (!StringTo(value, ps.capInh, 16)) {
                LOG_WARNING(sLogger, ("Invalid CapInh:", value));
            }
        }
        // 可以根据需要解析更多字段...
    }
    return true;
}