Iterates through all possible FDs and returns the currently open ones. Returns void so the implementation can use ASSERT_EQ.
| 86 | // |
| 87 | // Returns void so the implementation can use ASSERT_EQ. |
| 88 | void GetOpenFileDescriptors(std::unordered_set<int>* open_fds) { |
| 89 | int max_fd = 0; |
| 90 | GetMaxFileDescriptor(&max_fd); |
| 91 | |
| 92 | for (int fd = 0; fd < max_fd; ++fd) { |
| 93 | if (::dup2(fd, fd) != fd) { |
| 94 | // When given the same file descriptor twice, dup2() returns -1 if the |
| 95 | // file descriptor is closed, or the given file descriptor if it is open. |
| 96 | // |
| 97 | // Double-check that dup2() is saying the fd is closed. |
| 98 | ASSERT_EQ(EBADF, errno) |
| 99 | << "dup2() should set errno to EBADF on closed file descriptors"; |
| 100 | continue; |
| 101 | } |
| 102 | open_fds->insert(fd); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Finds an FD open since a previous call to GetOpenFileDescriptors(). |
| 107 | // |
no test coverage detected