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