1"""When called as a script, print a comma-separated list of the open 2file descriptors on stdout. 3 4Usage: 5fd_stats.py: check all file descriptors 6fd_status.py fd1 fd2 ...: check only specified file descriptors 7""" 8 9import errno 10import os 11import stat 12import sys 13 14if __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))) 35