Skip to content

Commit

Permalink
Improve container ID extraction logic for complex Docker service format
Browse files Browse the repository at this point in the history
  • Loading branch information
iritb committed Jan 24, 2025
1 parent b47c170 commit 9f5a747
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions ProcessInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,21 @@ int ProcessInfo::ExtractCGroupContainerId(const std::string& content) {
// Check for complex docker format
const char *complex_format_pos = strstr(ptr, complex_docker_service_prefix);
if (complex_format_pos != nullptr && complex_format_pos < line_end) {
const char *id_start = strrchr(ptr, '/') + 1;
if (id_start != nullptr) {
_container_id = std::string(id_start, 12); // Extract the container ID from the end of the line
return 0;
// Search for '/' before line_end
const char *id_start = nullptr;
for (const char *p = line_end; p >= complex_format_pos; --p) {
if (*p == '/') {
id_start = p + 1;
break;
}
}

if (id_start != nullptr) {
// make sure we have 12 characters left in the line before i read them
if (line_end > id_start && id_start + 12 <= line_end) {
_container_id = std::string(id_start, 12); // Extract the container ID from the end of the line
return 0;
}
}
}

Expand Down

0 comments on commit 9f5a747

Please sign in to comment.