Skip to content

Commit

Permalink
Add ExtractContainerIdFromCgroup method and update AddProcess to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
iritb committed Jan 11, 2025
1 parent 7c14deb commit 63689cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions ProcessTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,8 @@ std::shared_ptr<ProcessTreeItem> ProcessTree::AddProcess(enum ProcessTreeSource
std::string containerid = ExtractContainerId(exe, cmdline);
std::string cgroupContainerid;

if (containerid.empty()) {
auto p_temp = ReadProcEntry(pid);
cgroupContainerid = p_temp->_cgroupContainerId;
if (containerid.empty()) {
cgroupContainerid = ExtractContainerIdFromCgroup(pid);
}

auto it = _processes.find(pid);
Expand Down Expand Up @@ -651,6 +650,20 @@ void ProcessTree::SetContainerId(const std::shared_ptr<ProcessTreeItem>& p, cons
}
}

std::string ProcessTree::ExtractContainerIdFromCgroup(const int pid)
{
std::string containerid = "";
auto pinfo = ProcessInfo::OpenPid(pid, CMDLINE_SIZE_LIMIT);
if (!pinfo) {
Logger::Error("IB Failed to open proc entry for %d", pid);
return containerid;
}

containerid = pinfo->container_id();
Logger::Debug("IB (2) CGroup container id for %d is %s", pid, containerid.c_str());
return containerid;
}

std::string ProcessTree::ExtractContainerId(const std::string& exe, const std::string& cmdline)
{
// cmdline example:
Expand Down
4 changes: 2 additions & 2 deletions ProcessTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,13 @@ class ProcessTree: public RunBase {
std::shared_ptr<ProcessTreeItem> AddProcess(enum ProcessTreeSource source, int pid, int ppid, int uid, int gid, const std::string& exe, const std::string& cmdline);
void Clean();
std::shared_ptr<ProcessTreeItem> GetInfoForPid(int pid);
std::shared_ptr<ProcessTreeItem> ReadProcEntry(int pid);

void PopulateTree();
void UpdateFlags();
void ShowTree();
void ShowProcess(std::shared_ptr<ProcessTreeItem> p);
static std::string ExtractContainerId(const std::string& exe, const std::string& cmdline);

std::string ExtractContainerIdFromCgroup(const int pid)

protected:
void on_stopping() override;
Expand All @@ -243,6 +242,7 @@ class ProcessTree: public RunBase {
void RemovePid(int pid);
void ApplyFlags(const std::shared_ptr<ProcessTreeItem>& process);
void SetContainerId(const std::shared_ptr<ProcessTreeItem>& p, const std::string& containerid);
std::shared_ptr<ProcessTreeItem> ReadProcEntry(int pid);

std::shared_ptr<UserDB> _user_db;
std::shared_ptr<FiltersEngine> _filtersEngine;
Expand Down

0 comments on commit 63689cd

Please sign in to comment.