File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -351,6 +351,7 @@ Optional<std::string> find_container_id_from_cgroup() {
351351} // namespace
352352
353353Optional<std::string> find_container_id (std::istream& source) {
354+ constexpr std::string_view docker_str = " docker-" ;
354355 static const std::string uuid_regex_str =
355356 " [0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}"
356357 " |(?:[0-9a-f]{8}(?:-[0-9a-f]{4}){4}$)" ;
@@ -361,6 +362,28 @@ Optional<std::string> find_container_id(std::istream& source) {
361362 " )(?:\\ .scope)?$" );
362363
363364 std::string line;
365+
366+ // First, iterate using the simple substring match.
367+ while (std::getline (source, line)) {
368+ // Example:
369+ // `0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope`
370+ if (auto beg = line.find (docker_str); beg != std::string::npos) {
371+ beg += docker_str.size ();
372+ auto end = line.find (" .scope" , beg);
373+ if (end == std::string::npos || end - beg <= 0 ) {
374+ continue ;
375+ }
376+
377+ auto container_id = line.substr (beg, end - beg);
378+ return container_id;
379+ }
380+ }
381+
382+ // Reset the stream to the beginning.
383+ source.clear ();
384+ source.seekg (0 );
385+
386+ // If no match is found, iterate using the regex match.
364387 while (std::getline (source, line)) {
365388 // Example:
366389 // `0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope`
You can’t perform that action at this time.
0 commit comments