fd_status.py 865 B

12345678910111213141516171819202122232425262728293031323334
  1. """When called as a script, print a comma-separated list of the open
  2. file descriptors on stdout.
  3. Usage:
  4. fd_stats.py: check all file descriptors
  5. fd_status.py fd1 fd2 ...: check only specified file descriptors
  6. """
  7. import errno
  8. import os
  9. import stat
  10. import sys
  11. if __name__ == "__main__":
  12. fds = []
  13. if len(sys.argv) == 1:
  14. try:
  15. _MAXFD = os.sysconf("SC_OPEN_MAX")
  16. except:
  17. _MAXFD = 256
  18. test_fds = range(0, _MAXFD)
  19. else:
  20. test_fds = map(int, sys.argv[1:])
  21. for fd in test_fds:
  22. try:
  23. st = os.fstat(fd)
  24. except OSError, e:
  25. if e.errno == errno.EBADF:
  26. continue
  27. raise
  28. # Ignore Solaris door files
  29. if not hasattr(stat, 'S_ISDOOR') or not stat.S_ISDOOR(st.st_mode):
  30. fds.append(fd)
  31. print ','.join(map(str, fds))