Passing tap devices to KVM as file descriptors requires that the respective
file decriptors remain open during utils.RunCmd execution. To this direction,
we add a “noclose_fds” keyword argument to utils.RunCmd, accepting a list of
file descriptors to keep open. The actual fd handling is implemented in
_RunCmdPipe and _RunCmdFile using subprocess.Popen's “preexec_fn”[1],
since subprocess.Popen provides no other way to selectively handle fds.

A small modification is also made to test/ganeti.utils_unittest.py to comply
with _RunCmdPipe's new API.

[1] “If preexec_fn is set to a callable object, this object will be called in
     the child process just before the child is executed. (Unix only)”
    Subprocess documentation

Signed-off-by: Apollon Oikonomopoulos <[email protected]>
---
 lib/utils.py                  |   45 ++++++++++++++++++++++++++++++++--------
 test/ganeti.utils_unittest.py |    2 +-
 2 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/lib/utils.py b/lib/utils.py
index 359c443..fd2c7fb 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -190,7 +190,7 @@ def _BuildCmdEnvironment(env, reset):
 
 
 def RunCmd(cmd, env=None, output=None, cwd="/", reset_env=False,
-           interactive=False, timeout=None):
+           interactive=False, timeout=None, noclose_fds=None):
   """Execute a (shell) command.
 
   The command should not read from its standard input, as it will be
@@ -215,6 +215,9 @@ def RunCmd(cmd, env=None, output=None, cwd="/", 
reset_env=False,
   @type timeout: int
   @param timeout: If not None, timeout in seconds until child process gets
                   killed
+  @type noclose_fds: list
+  @param noclose_fds: list of additional (fd >=3) file descriptors to leave
+                      open for the child process
   @rtype: L{RunResult}
   @return: RunResult instance
   @raise errors.ProgrammerError: if we call this when forks are disabled
@@ -245,10 +248,11 @@ def RunCmd(cmd, env=None, output=None, cwd="/", 
reset_env=False,
   try:
     if output is None:
       out, err, status, timeout_action = _RunCmdPipe(cmd, cmd_env, shell, cwd,
-                                                     interactive, timeout)
+                                                     interactive, timeout,
+                                                     noclose_fds)
     else:
       timeout_action = _TIMEOUT_NONE
-      status = _RunCmdFile(cmd, cmd_env, shell, output, cwd)
+      status = _RunCmdFile(cmd, cmd_env, shell, output, cwd, noclose_fds)
       out = err = ""
   except OSError, err:
     if err.errno == errno.ENOENT:
@@ -510,7 +514,7 @@ def _WaitForProcess(child, timeout):
     pass
 
 
-def _RunCmdPipe(cmd, env, via_shell, cwd, interactive, timeout,
+def _RunCmdPipe(cmd, env, via_shell, cwd, interactive, timeout, noclose_fds,
                 _linger_timeout=constants.CHILD_LINGER_TIMEOUT):
   """Run a command and return its output.
 
@@ -526,6 +530,9 @@ def _RunCmdPipe(cmd, env, via_shell, cwd, interactive, 
timeout,
   @param interactive: Run command interactive (without piping)
   @type timeout: int
   @param timeout: Timeout after the programm gets terminated
+  @type noclose_fds: list
+  @param noclose_fds: list of additional (fd >=3) file descriptors to leave
+                      open for the child process
   @rtype: tuple
   @return: (out, err, status)
 
@@ -539,12 +546,20 @@ def _RunCmdPipe(cmd, env, via_shell, cwd, interactive, 
timeout,
   if interactive:
     stderr = stdout = stdin = None
 
+  if noclose_fds:
+    preexec_fn = lambda: CloseFDs(noclose_fds)
+    close_fds = False
+  else:
+    preexec_fn = None
+    close_fds = True
+
   child = subprocess.Popen(cmd, shell=via_shell,
                            stderr=stderr,
                            stdout=stdout,
                            stdin=stdin,
-                           close_fds=True, env=env,
-                           cwd=cwd)
+                           close_fds=close_fds, env=env,
+                           cwd=cwd,
+                           preexec_fn=preexec_fn)
 
   out = StringIO()
   err = StringIO()
@@ -637,7 +652,7 @@ def _RunCmdPipe(cmd, env, via_shell, cwd, interactive, 
timeout,
   return out, err, status, timeout_action
 
 
-def _RunCmdFile(cmd, env, via_shell, output, cwd):
+def _RunCmdFile(cmd, env, via_shell, output, cwd, noclose_fds):
   """Run a command and save its output to a file.
 
   @type  cmd: string or list
@@ -650,18 +665,30 @@ def _RunCmdFile(cmd, env, via_shell, output, cwd):
   @param output: the filename in which to save the output
   @type cwd: string
   @param cwd: the working directory for the program
+  @type noclose_fds: list
+  @param noclose_fds: list of additional (fd >=3) file descriptors to leave
+                      open for the child process
   @rtype: int
   @return: the exit status
 
   """
   fh = open(output, "a")
+
+  if noclose_fds:
+    preexec_fn = lambda: CloseFDs(noclose_fds + [fh.fileno()])
+    close_fds = False
+  else:
+    preexec_fn = None
+    close_fds = True
+
   try:
     child = subprocess.Popen(cmd, shell=via_shell,
                              stderr=subprocess.STDOUT,
                              stdout=fh,
                              stdin=subprocess.PIPE,
-                             close_fds=True, env=env,
-                             cwd=cwd)
+                             close_fds=close_fds, env=env,
+                             cwd=cwd,
+                             preexec_fn=preexec_fn)
 
     child.stdin.close()
     status = child.wait()
diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py
index d01a005..e8afad4 100755
--- a/test/ganeti.utils_unittest.py
+++ b/test/ganeti.utils_unittest.py
@@ -299,7 +299,7 @@ class TestRunCmd(testutils.GanetiTestCase):
     cmd = ["/bin/sh", "-c", "trap '' TERM; read < %s" % self.fifo_file]
     timeout = 0.2
     out, err, status, ta = utils._RunCmdPipe(cmd, {}, False, "/", False,
-                                             timeout, _linger_timeout=0.2)
+                                             timeout, None, 
_linger_timeout=0.2)
     self.assert_(status < 0)
     self.assertEqual(-status, signal.SIGKILL)
 
-- 
1.7.1

Reply via email to