Imported Upstream version 4.5.14
[platform/upstream/findutils.git] / find / testsuite / sv-34976-execdir-fd-leak.sh
1 #! /bin/sh
2 # Copyright (C) 2013 Free Software Foundation, Inc.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 # This test verifies that find does not leak a file descriptor for the working
19 # directory specified by the -execdir option [Savannah bug #34976].
20
21 testname="$(basename $0)"
22
23 . "${srcdir}"/binary_locations.sh
24
25 # seq is not required by POSIX, so we have manual lists of number here instead.
26 three_to_thirty_five="3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35"
27 three_to_hundred="3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100"
28
29 # Test if restricting the number of file descriptors via ulimit -n works.
30 # We always try to open 33 files (but the ulimit -n value changes).
31 test_ulimit() {
32   l="$1"  # limit to use
33   (
34     ulimit -n "$l" || exit 1
35     for i in ${three_to_thirty_five} ; do
36       printf "exec %d> /dev/null || exit 1\n" $i
37     done | sh -x ;
38   ) 2>/dev/null
39 }
40 # Opening 33 files with a limit of 50 should work.
41 test_ulimit 40 || { echo "SKIP: ulimit does not work (case 1)" >&2; exit 0 ; }
42 # Opening 33 files with a limit of 20 should fail.
43 test_ulimit 20 && { echo "SKIP: ulimit does not work (case 2)" >&2; exit 0 ; }
44
45 die() {
46   echo "$@" >&2
47   exit 1
48 }
49
50 # Create test files, each 98 in the directories ".", "one" and "two".
51 make_test_data() {
52   d="$1"
53   (
54     cd "$1" || exit 1
55     mkdir one two || exit 1
56     for i in ${three_to_hundred} ; do
57       printf "./%03d one/%03d two/%03d " $i $i $i
58     done \
59       | xargs touch || exit 1
60   ) \
61   || die "failed to set up the test in ${outdir}"
62 }
63
64 outdir="$(mktemp -d)" || die "FAIL: could not create a test files."
65
66 # Create some test files.
67 make_test_data "${outdir}" || die "FAIL: failed to set up the test in ${outdir}"
68
69 fail=0
70 for exe in "${ftsfind}" "${oldfind}"; do
71   ( ulimit -n 30 && \
72     ${exe} "${outdir}"  -type f -execdir cat '{}' \; >/dev/null; ) \
73   || { \
74     echo "Option -execdir of ${exe} leaks file descriptors" >&2 ; \
75     fail=1 ; \
76   }
77 done
78
79 rm -rf "${outdir}" || exit 1
80 exit $fail