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