Skip to content

Commit

Permalink
Added FD info debugging function.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillt committed Dec 5, 2015
1 parent ffa8392 commit d6982ec
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions 3rdparty/libprocess/3rdparty/stout/include/stout/os/open.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,95 @@ inline Try<int> open(const std::string& path, int oflag, mode_t mode = 0)
return fd;
}


namespace debug {

// Borrowed from https://oroboro.com/file-handle-leaks-server.
inline void log_fd(int fd)
{
int fd_flags = fcntl(fd, F_GETFD);

if (fd_flags < 0) {
return;
}

int fl_flags = fcntl(fd, F_GETFL);

if (fl_flags < 0) {
return;
}

std::cerr << fd << " ";

#ifdef __linux__
char buf[256];
char path[256];
sprintf(path, "/proc/self/fd/%d", fd);

memset(&buf[0], 0, 256);
ssize_t s = readlink(path, &buf[0], 256);

if (s < 0) {
std::cerr << " (" << path << "): " << "not available" << "\n";
return;
}
std::cerr << "(" << buf << "): ";
#endif

if (fd_flags & FD_CLOEXEC) std::cerr << "cloexec ";

// file status
if (fl_flags & O_APPEND) std::cerr << "append ";
if (fl_flags & O_NONBLOCK) std::cerr << "nonblock ";

// acc mode
if (fl_flags & O_RDONLY) std::cerr << "read-only ";
if (fl_flags & O_RDWR) std::cerr << "read-write ";
if (fl_flags & O_WRONLY) std::cerr << "write-only ";

#ifdef __linux__
if (fl_flags & O_DSYNC) std::cerr << "dsync ";
if (fl_flags & O_RSYNC) std::cerr << "rsync ";
if (fl_flags & O_SYNC) std::cerr << "sync ";
#endif

struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = 0;
fl.l_start = 0;
fl.l_len = 0;

fcntl(fd, F_GETLK, &fl);

if (fl.l_type != F_UNLCK) {
if (fl.l_type == F_WRLCK) {
std::cerr << "write-locked";
} else {
std::cerr << "read-locked";
}
std::cerr << "(pid:" << fl.l_pid << ") ";
}

std::cerr << "\n";
}

inline void log_fds()
{
int handles = getdtablesize();

for (int i = 0; i < handles; i++) {
int fd_flags = fcntl( i, F_GETFD );

if (fd_flags < 0) {
continue;
}

log_fd(i);
}
}

} // namespace debug {

} // namespace os {

#endif // __STOUT_OS_OPEN_HPP__

0 comments on commit d6982ec

Please sign in to comment.