Skip to content

Commit 64e5a72

Browse files
committed
Update recursive_directory_iterator to skip permission denied errors
Signed-off-by: Dom Del Nano <ddelnano@gmail.com> (cherry picked from commit 6dd0f9a)
1 parent ce714e6 commit 64e5a72

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/shared/metadata/cgroup_path_resolver.cc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,24 @@ StatusOr<std::vector<std::string>> CGroupBasePaths(std::string_view sysfs_path)
7070

7171
StatusOr<std::string> FindSelfCGroupProcs(std::string_view base_path) {
7272
int pid = getpid();
73+
std::error_code ec;
7374

74-
for (auto& p : std::filesystem::recursive_directory_iterator(base_path)) {
75-
if (p.path().filename() == "cgroup.procs") {
76-
std::string contents = ReadFileToString(p.path().string()).ValueOr("");
75+
auto it = std::filesystem::recursive_directory_iterator(
76+
base_path, std::filesystem::directory_options::skip_permission_denied, ec);
77+
if (ec) {
78+
return error::Internal("Failed to iterate cgroup path: $0", ec.message());
79+
}
80+
81+
for (auto end = std::filesystem::recursive_directory_iterator(); it != end; it.increment(ec)) {
82+
if (ec) {
83+
ec.clear();
84+
continue;
85+
}
86+
if (it->path().filename() == "cgroup.procs") {
87+
std::string contents = ReadFileToString(it->path().string()).ValueOr("");
7788
int contents_pid;
7889
if (absl::SimpleAtoi(contents, &contents_pid) && pid == contents_pid) {
79-
return p.path().string();
90+
return it->path().string();
8091
}
8192
}
8293
}

0 commit comments

Comments
 (0)