e0bc27
From 08c5fbec81ff04a6fd307ed07eddccefa3b43d44 Mon Sep 17 00:00:00 2001
e0bc27
From: Bernhard Voelker <mail@bernhard-voelker.de>
e0bc27
Date: Fri, 2 Aug 2013 12:16:44 +0200
e0bc27
Subject: [PATCH] find: fix fd leak with --execdir option (bug#34976)
e0bc27
e0bc27
Prevent "Failed to save working dir[...]: Too many open files"
e0bc27
error by closing the file descriptor of the working directory.
e0bc27
e0bc27
* find/exec.c (impl_pred_exec): Free the working directory if find
e0bc27
executes the command in the local dir, i.e. if it has been saved
e0bc27
by record_exec_dir().
e0bc27
* find/testsuite/sv-34976-execdir-fd-leak.sh: Add test.
e0bc27
* find/testsuite/Makefile.am (test_shell_progs): Mention the test.
e0bc27
e0bc27
Upstream-commit: 183115d0484816336f9c4d2a3198b5eae2ad4b92
e0bc27
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
e0bc27
---
e0bc27
 find/exec.c                                |  2 +
e0bc27
 find/testsuite/Makefile.am                 |  2 +-
e0bc27
 find/testsuite/sv-34976-execdir-fd-leak.sh | 76 ++++++++++++++++++++++++++++++
e0bc27
 3 files changed, 79 insertions(+), 1 deletion(-)
e0bc27
 create mode 100644 find/testsuite/sv-34976-execdir-fd-leak.sh
e0bc27
e0bc27
diff --git a/find/exec.c b/find/exec.c
e0bc27
index f731d82..60d9109 100644
e0bc27
--- a/find/exec.c
e0bc27
+++ b/find/exec.c
e0bc27
@@ -213,6 +213,8 @@ impl_pred_exec (const char *pathname,
e0bc27
 	{
e0bc27
 	  result = false;
e0bc27
 	}
e0bc27
+      if (local)
e0bc27
+	free_cwd (execp->wd_for_exec);
e0bc27
     }
e0bc27
   if (buf)
e0bc27
     {
e0bc27
diff --git a/find/testsuite/Makefile.am b/find/testsuite/Makefile.am
e0bc27
index a1e49b8..e8b1173 100644
e0bc27
--- a/find/testsuite/Makefile.am
e0bc27
+++ b/find/testsuite/Makefile.am
e0bc27
@@ -247,7 +247,7 @@ EXTRA_DIST_GOLDEN = \
e0bc27
 	test_escapechars.golden
e0bc27
 
e0bc27
 test_shell_progs = sv-bug-32043.sh test_escapechars.sh test_escape_c.sh \
e0bc27
-	test_inode.sh sv-34079.sh
e0bc27
+	test_inode.sh sv-34079.sh sv-34976-execdir-fd-leak.sh
e0bc27
 
e0bc27
 EXTRA_DIST = $(EXTRA_DIST_EXP) $(EXTRA_DIST_XO) $(EXTRA_DIST_GOLDEN) \
e0bc27
 	$(test_shell_progs) binary_locations.sh
e0bc27
diff --git a/find/testsuite/sv-34976-execdir-fd-leak.sh b/find/testsuite/sv-34976-execdir-fd-leak.sh
e0bc27
new file mode 100644
e0bc27
index 0000000..2d5dace
e0bc27
--- /dev/null
e0bc27
+++ b/find/testsuite/sv-34976-execdir-fd-leak.sh
e0bc27
@@ -0,0 +1,76 @@
e0bc27
+#! /bin/sh
e0bc27
+# Copyright (C) 2013 Free Software Foundation, Inc.
e0bc27
+#
e0bc27
+# This program is free software: you can redistribute it and/or modify
e0bc27
+# it under the terms of the GNU General Public License as published by
e0bc27
+# the Free Software Foundation, either version 3 of the License, or
e0bc27
+# (at your option) any later version.
e0bc27
+#
e0bc27
+# This program is distributed in the hope that it will be useful,
e0bc27
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
e0bc27
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e0bc27
+# GNU General Public License for more details.
e0bc27
+#
e0bc27
+# You should have received a copy of the GNU General Public License
e0bc27
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
e0bc27
+#
e0bc27
+
e0bc27
+# This test verifies that find does not leak a file descriptor for the working
e0bc27
+# directory specified by the -execdir option [Savannah bug #34976].
e0bc27
+
e0bc27
+testname="$(basename $0)"
e0bc27
+
e0bc27
+. "${srcdir}"/binary_locations.sh
e0bc27
+
e0bc27
+# Test if restricting the number of file descriptors via ulimit -n works.
e0bc27
+test_ulimit() {
e0bc27
+  n="$1"  # number of files
e0bc27
+  l="$2"  # limit to use
e0bc27
+  (
e0bc27
+    ulimit -n "$l" || exit 1
e0bc27
+    for i in $(seq $n) ; do
e0bc27
+      printf "exec %d> /dev/null || exit 1\n" $i
e0bc27
+    done | sh ;
e0bc27
+  ) 2>/dev/null
e0bc27
+}
e0bc27
+# Opening 30 files with a limit of 40 should work.
e0bc27
+test_ulimit 30 40 || { echo "SKIP: ulimit does not work" >&2; exit 0 ; }
e0bc27
+# Opening 30 files with a limit of 20 should fail.
e0bc27
+test_ulimit 30 20 && { echo "SKIP: ulimit does not work" >&2; exit 0 ; }
e0bc27
+
e0bc27
+die() {
e0bc27
+  echo "$@" >&2
e0bc27
+  exit 1
e0bc27
+}
e0bc27
+
e0bc27
+# Create test files, each 100 in the directories ".", "one" and "two".
e0bc27
+make_test_data() {
e0bc27
+  d="$1"
e0bc27
+  (
e0bc27
+    cd "$1" || exit 1
e0bc27
+    mkdir one two || exit 1
e0bc27
+    for i in $(seq 0 100) ; do
e0bc27
+      printf "./%03d one/%03d two/%03d " $i $i $i
e0bc27
+    done \
e0bc27
+      | xargs touch || exit 1
e0bc27
+  ) \
e0bc27
+  || die "failed to set up the test in ${outdir}"
e0bc27
+}
e0bc27
+
e0bc27
+outdir="$(mktemp -d)" || die "FAIL: could not create a test files."
e0bc27
+
e0bc27
+# Create some test files.
e0bc27
+make_test_data "${outdir}" || die "FAIL: failed to set up the test in ${outdir}"
e0bc27
+
e0bc27
+fail=0
e0bc27
+for exe in "${ftsfind}" "${oldfind}"; do
e0bc27
+  ( ulimit -n 30 && \
e0bc27
+    ${exe} "${outdir}"  -type f -execdir cat '{}' \; >/dev/null; ) \
e0bc27
+  || { \
e0bc27
+    echo "Option -execdir of ${exe} leaks file descriptors" >&2 ; \
e0bc27
+    fail=1 ; \
e0bc27
+  }
e0bc27
+done
e0bc27
+
e0bc27
+rm -rf "${outdir}" || exit 1
e0bc27
+exit $fail
e0bc27
-- 
e0bc27
2.5.0
e0bc27