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