| 298 | static int ParseFd(dirent* dir_entry, int dir_fd); |
| 299 | |
| 300 | std::unique_ptr<std::set<int>> GetOpenFds(fail_fn_t fail_fn) { |
| 301 | DIR* proc_fd_dir = opendir(kFdPath); |
| 302 | if (proc_fd_dir == nullptr) { |
| 303 | fail_fn(android::base::StringPrintf("Unable to open directory %s: %s", |
| 304 | kFdPath, |
| 305 | strerror(errno))); |
| 306 | return nullptr; // Return nullptr on error |
| 307 | } |
| 308 | auto result = std::make_unique<std::set<int>>(); |
| 309 | int dir_fd = dirfd(proc_fd_dir); |
| 310 | dirent* dir_entry; |
| 311 | while ((dir_entry = readdir(proc_fd_dir)) != nullptr) { |
| 312 | const int fd = ParseFd(dir_entry, dir_fd); |
| 313 | if (fd == -1) { |
| 314 | continue; |
| 315 | } |
| 316 | |
| 317 | result->insert(fd); |
| 318 | } |
| 319 | if (closedir(proc_fd_dir) == -1) { |
| 320 | fail_fn(android::base::StringPrintf("Unable to close directory: %s", strerror(errno))); |
| 321 | } |
| 322 | return result; |
| 323 | } |
| 324 | |
| 325 | static int ParseFd(dirent* dir_entry, int dir_fd) { |
| 326 | char* end; |
no test coverage detected