From 638ac34b0ce08a458c16310b58b17be4d07d19bf Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 25 Jul 2011 12:02:24 +0200 Subject: [PATCH] Don't leak file descriptors when setting up daemon output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a daemon's output is configured using βutils.SetupDaemonFDsβ, the function must use dup2(2). Unfortunately the code didn't close the original file descriptors, leaking them in the process. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/utils/process.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/utils/process.py b/lib/utils/process.py index be7d77496..2e015de7f 100644 --- a/lib/utils/process.py +++ b/lib/utils/process.py @@ -251,8 +251,10 @@ def SetupDaemonFDs(output_file, output_fd): # Open /dev/null (read-only, only for stdin) devnull_fd = os.open(os.devnull, os.O_RDONLY) + output_close = True + if output_fd is not None: - pass + output_close = False elif output_file is not None: # Open output file try: @@ -268,6 +270,12 @@ def SetupDaemonFDs(output_file, output_fd): os.dup2(output_fd, 1) os.dup2(output_fd, 2) + if devnull_fd > 2: + utils_wrapper.CloseFdNoError(devnull_fd) + + if output_close and output_fd > 2: + utils_wrapper.CloseFdNoError(output_fd) + def StartDaemon(cmd, env=None, cwd="/", output=None, output_fd=None, pidfile=None): -- GitLab