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