]> granicus.if.org Git - procps-ng/commitdiff
watch: Don't leak extra fds to the child
authorJosh Stone <jistone@redhat.com>
Tue, 4 Feb 2014 17:46:58 +0000 (09:46 -0800)
committerJaromir Capik <jcapik@redhat.com>
Tue, 4 Feb 2014 18:23:59 +0000 (19:23 +0100)
Once the write side of the pipe has been duped to stdout for the child,
the original pipefd is no longer needed, so it can be closed to avoid
leaking to the child.

The leak can easily be seen with "watch ls -l /proc/self/fd", but I
found this due to "watch lvs" diagnosing itself:

    File descriptor 4 (pipe:[3163616]) leaked on lvs invocation.

Signed-off-by: Josh Stone <jistone@redhat.com>
watch.c

diff --git a/watch.c b/watch.c
index 032dfb78b21320dd6733808f1b34ad238a3f7d73..f0a3ec39ecfb33d5b75d4adde21dcb3a738bc867 100644 (file)
--- a/watch.c
+++ b/watch.c
@@ -387,6 +387,7 @@ static int run_command(char *restrict command, char **restrict command_argv)
                if (dup2(pipefd[1], 1) < 0) {   /* replace stdout with write side of pipe */
                        xerr(3, _("dup2 failed"));
                }
+               close(pipefd[1]);               /* once duped, the write fd isn't needed */
                dup2(1, 2);                     /* stderr should default to stdout */
 
                if (flags & WATCH_EXEC) {       /* pass command to exec instead of system */