This closes fds other than 0, 1, 2.
This ensures only the correct *.stderr and *.stdout is attached, via
the stdout/stderr parameter to Popen(), but not every other FD
currently open in python at the time Popen is called.
For the tail invocation and other calls to Popen(), because fds 0, 1,
2 are still attached, these function as before.
Per https://docs.python.org/2.6/library/subprocess.html:
"If close_fds is true, all file descriptors except 0, 1 and
2 will be closed before the child process is executed. (Unix only)."
And regarding the passed in parameters:
"stdin, stdout and stderr specify the executed programs’ standard
input,
standard output and standard error file handles, respectively. "
...
"With None (the default), no redirection will occur;
the child’s file handles will be inherited from the parent. "
(The unwanted inherited files would be on a random high FD, where the
program wouldn't know what to do with them, but counting towards the
process FD limit).
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13591
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
if show:
do_print("Running: '%s' in '%s'" % (cmd, dir))
if output:
- return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
+ return Popen([cmd], shell=True, stdout=PIPE, cwd=dir, close_fds=True).communicate()[0]
elif checkfail:
return check_call(cmd, shell=True, cwd=dir)
else:
cwd = os.getcwd()
os.chdir("%s/%s" % (self.sdir, self.dir))
self.proc = Popen(self.cmd, shell=True,
+ close_fds=True,
stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
os.chdir(cwd)
self.next += 1
cwd = os.getcwd()
cmd = "tail -f *.stdout *.stderr"
os.chdir(gitroot)
- self.tail_proc = Popen(cmd, shell=True)
+ self.tail_proc = Popen(cmd, shell=True, close_fds=True)
os.chdir(cwd)