-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexhaust_fd
36 lines (25 loc) · 823 Bytes
/
exhaust_fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
import os
import itertools
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", "--limit", dest="fds", type="int", default=0,
help="How many file descriptors to leave.", metavar="FDS")
parser.add_option("-c", "--command", dest="cmd", type="string", default="/bin/bash",
help="Command to run.")
(options, args) = parser.parse_args()
handles = []
try:
while True:
handles.append(open('/dev/null', 'r'))
except IOError:
print "File descriptors exhausted."
pass
print "Freeing {0} file descriptors.".format(options.fds)
for f in itertools.islice(handles, 0, options.fds):
f.close()
print "Starting {0}.".format(options.cmd)
os.system('/bin/bash')
print "Closing remaining file descriptors."
for f in handles:
f.close()