1
0
mirror of https://github.com/CumulusNetworks/ifupdown2.git synced 2024-05-06 15:54:50 +00:00

performance fix: better handling fd to allow subprocess.close_fds=False and code re-organisation

Ticket: None
Reviewed By: CCR-4692
Testing Done: smoke + scale tests

If called with close_fds=True the subprocess module will try to close every fd
from 3 to MAXFD before executing the specified command. This is done in Python
not even with a C-implementation which truly affecting performances.

This patch aims to better handle the file descriptor used by ifupdown2. Either
by closing them after use or by setting the close-on-exec flag for the file
descriptor, which causes the file descriptor to be automatically
(and atomically) closed when any of the exec-family functions succeed.

With the actual patch all tests are passing, I can't think of any future issue
but if any a possible future modification might be to use the parameter
'preexec_fn', which allows us to set function which will be executed in the
child process before executing the command line. We can always manually close
any remaining open file descriptors with something like:

>>> os.listdir('/proc/self/fd/')
['0', '1', '2', ‘3’, etc..]
>>> for fd in os.listdir('/proc/self/fd/')
>>>    if int(fd) > 2:
>>>    	  os.close(fd)

This patch is also totally re-organising the use of subprocesses. By removing
all subprocess code redundancy.
This commit is contained in:
Julien Fortin
2016-05-13 19:52:57 +02:00
parent afe5125163
commit a193d8d1c0
18 changed files with 343 additions and 464 deletions

View File

@@ -4,9 +4,7 @@
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
#
import subprocess
import ifupdownaddons
import signal
from ifupdown.utils import utils
import ifupdown.ifupdownflags as ifupdownflags
@@ -29,46 +27,16 @@ class usercmds(ifupdownaddons.modulebase.moduleBase):
'post-down' :
{'help' : 'run command after bringing interface down'}}}
def _exec_user_cmd(self, cmd):
""" exec's commands using subprocess Popen
special wrapper using use closefds=True and shell=True
for user commands
"""
cmd_returncode = 0
try:
self.logger.info('executing %s' %cmd)
if ifupdownflags.flags.DRYRUN:
return
ch = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
shell=True,
stderr=subprocess.STDOUT,
close_fds=True)
utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT)
cmd_returncode = ch.wait()
cmdout = ch.communicate()[0]
except Exception, e:
raise Exception('failed to execute cmd \'%s\' (%s)'
%(cmd, str(e)))
finally:
utils.disable_subprocess_signal_forwarding(signal.SIGINT)
if cmd_returncode != 0:
raise Exception(cmdout)
return cmdout
def _run_command(self, ifaceobj, op):
cmd_list = ifaceobj.get_attr_value(op)
if cmd_list:
for cmd in cmd_list:
self.logger.info('executing cmd \'%s\'' %cmd)
try:
self._exec_user_cmd(cmd)
utils.exec_user_command(cmd)
except Exception, e:
if not self.ignore_error(str(e)):
self.logger.warn('%s: %s cmd \'%s\' failed (%s)'
%(ifaceobj.name, op, cmd, str(e).strip('\n')))
self.logger.warn('%s: %s %s' % (ifaceobj.name, op,
str(e).strip('\n')))
pass
_run_ops = {'pre-up' : _run_command,