&& { echo '$(ME): do not use EXIT_FAILURE in the above' \
1>&2; exit 1; } || :
-# Ensure that tests call the require_ulimit_v_ function if using ulimit -v
+# Ensure that tests call the get_min_ulimit_v_ function if using ulimit -v
sc_prohibit_test_ulimit_without_require_:
- @(git grep -l require_ulimit_v_ $(srcdir)/tests; \
+ @(git grep -l get_min_ulimit_v_ $(srcdir)/tests; \
git grep -l 'ulimit -v' $(srcdir)/tests) \
| sort | uniq -u | grep . && { echo "$(ME): the above test(s)"\
- " should match require_ulimit_v_ with ulimit -v" 1>&2; exit 1; } || :
+ " should match get_min_ulimit_v_ with ulimit -v" 1>&2; exit 1; } || :
# Ensure that tests call the cleanup_ function if using background processes
sc_prohibit_test_background_without_cleanup_:
fi
}
-require_ulimit_v_()
-{
- local ulimit_works=yes
- # Expect to be able to exec a program in 10MiB of virtual memory,
- # (10MiB is usually plenty, but valgrind-wrapped date requires 19000KiB,
- # so allow more in that case)
- # but not in 20KiB. I chose "date". It must not be a shell built-in
- # function, so you can't use echo, printf, true, etc.
- # Of course, in coreutils, I could use $top_builddir/src/true,
- # but this should be able to work for other projects, too.
+# Determine the minimum required VM limit to run the given command.
+# Output that value to stdout ... to be used by the caller.
+# Return 0 in case of success, and a non-Zero value otherwise.
+get_min_ulimit_v_()
+{
local vm
- case $(printenv LD_PRELOAD) in */valgrind/*) vm=22000;; *) vm=10000;; esac
-
- ( ulimit -v $vm; date ) > /dev/null 2>&1 || ulimit_works=no
- ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
-
- test $ulimit_works = no \
- && skip_ "this shell lacks ulimit support"
+ for v in $( seq 5000 5000 50000 ); do
+ if ( ulimit -v $v && "$@" ) >/dev/null; then
+ local vm_prev
+ prev_v=$v
+ for v in $( seq $(($prev_v-1000)) -1000 1000 ); do
+ ( ulimit -v $v && "$@" ) >/dev/null \
+ || { echo $prev_v; return 0; }
+ prev_v=$v
+ done
+ fi
+ done
+ # The above did not find a working limit. Echo a very small number - just
+ # in case the caller does not handle the non-Zero return value.
+ echo 1; return 1
}
require_readable_root_()
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ cp
expensive_
-require_ulimit_v_
+
+# Determine basic amount of memory needed for 'cp -al'.
+touch f || framework_failure_
+vm=$(get_min_ulimit_v_ cp -al f f2) \
+ || skip_ "this shell lacks ulimit support"
+rm f f2 || framework_failure_
a=$(printf %031d 0)
b=$(printf %031d 1)
mkdir e || framework_failure_
mv $a $b e || framework_failure_
-# Increased from 20000 to 22000 in 2012, for pre-F18 rawhide.
-(ulimit -v 22000; cp -al e f) || fail=1
+# Allow cp(1) to use 4MiB more virtual memory than for the above trivial case.
+(ulimit -v $(($vm+4000)) && cp -al e f) || fail=1
Exit $fail
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ dd
-require_ulimit_v_
+
+# Determine basic amount of memory needed.
+echo . > f || framework_failure_
+vm=$(get_min_ulimit_v_ dd if=f of=f2 status=none) \
+ || skip_ "this shell lacks ulimit support"
+rm -f f || framework_failure_
# count and skip are zero, we don't need to allocate memory
-(ulimit -v 20000; dd bs=30M count=0) || fail=1
-(ulimit -v 20000; dd ibs=30M count=0) || fail=1
-(ulimit -v 20000; dd obs=30M count=0) || fail=1
+(ulimit -v $vm && dd bs=30M count=0) || fail=1
+(ulimit -v $vm && dd ibs=30M count=0) || fail=1
+(ulimit -v $vm && dd obs=30M count=0) || fail=1
check_dd_seek_alloc() {
local file="$1"
timeout 10 dd count=1 if=/dev/zero of=tape&
# Allocate buffer and read from the "tape"
- (ulimit -v 20000; timeout 10 dd $dd_buf=30M $dd_op=1 count=0 $dd_file=tape)
+ (ulimit -v $(($vm+4000)) \
+ && timeout 10 dd $dd_buf=30M $dd_op=1 count=0 $dd_file=tape)
local ret=$?
# Be defensive in case the tape reader is blocked for some reason
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ csplit
-require_ulimit_v_
+# Determine basic amount of memory needed.
+{ echo y; echo n; } > f || framework_failure_
+vm=$(get_min_ulimit_v_ csplit -z f %n%1) \
+ || skip_ "this shell lacks ulimit support"
(
- ulimit -v 20000
- { yes | head -n2500000; echo n; } | csplit -z - %n%1
+ ulimit -v $vm \
+ && { yes | head -n2500000; echo n; } | csplit -z - %n%1
) || fail=1
Exit $fail
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ cut
-require_ulimit_v_
getlimits_
+vm=$(get_min_ulimit_v_ cut -b1 /dev/null) \
+ || skip_ "this shell lacks ulimit support"
+
# sed script to subtract one from the input.
# Each input line should consist of a positive decimal number.
# Each output line's number is one less than the input's.
# From coreutils-8.10 through 8.20, this would make cut try to allocate
# a 256MiB bit vector. With a 20MB limit on VM, the following would fail.
-(ulimit -v 20000; : | cut -b$CUT_MAX- > err 2>&1) || fail=1
+(ulimit -v $vm && : | cut -b$CUT_MAX- > err 2>&1) || fail=1
# Up to and including coreutils-8.21, cut would allocate possibly needed
# memory upfront. Subsequently extra memory is no longer needed.
-(ulimit -v 20000; : | cut -b1-$CUT_MAX >> err 2>&1) || fail=1
+(ulimit -v $vm && : | cut -b1-$CUT_MAX >> err 2>&1) || fail=1
# Explicitly disallow values above CUT_MAX
-(ulimit -v 20000; : | returns_ 1 cut -b$SIZE_MAX 2>/dev/null) || fail=1
-(ulimit -v 20000; : | returns_ 1 cut -b$SIZE_OFLOW 2>/dev/null) || fail=1
+(ulimit -v $vm && : | returns_ 1 cut -b$SIZE_MAX 2>/dev/null) || fail=1
+(ulimit -v $vm && : | returns_ 1 cut -b$SIZE_OFLOW 2>/dev/null) || fail=1
compare /dev/null err || fail=1
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ head
-require_ulimit_v_
getlimits_
+vm=$(get_min_ulimit_v_ head -c1 /dev/null) \
+ || skip_ "this shell lacks ulimit support"
+
# exercise the fix of 2001-08-18, based on test case from Ian Bruce
echo abc > in || framework_failure_
(head -c1; head -c1) < in > out || fail=1
# Only allocate memory as needed.
# Coreutils <= 8.21 would allocate memory up front
# based on the value passed to -c
-(ulimit -v 20000; head --bytes=-$SSIZE_MAX < /dev/null) || fail=1
+(ulimit -v $vm && head --bytes=-$SSIZE_MAX < /dev/null) || fail=1
# Make sure it works on funny files in /proc and /sys.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ printf
-require_ulimit_v_
+vm=$(get_min_ulimit_v_ env $prog %20f 0) \
+ || skip_ "this shell lacks ulimit support"
# Up to coreutils-6.9, "printf %.Nf 0" would encounter an ENOMEM internal
# error from glibc's printf(3) function whenever N was large relative to
# Choosing the virtual memory limit, 11000 is enough, but 10000 is too
# little and provokes a "memory exhausted" diagnostic on FreeBSD 9.0-p3.
-( ulimit -v 15000; env $prog %20000000f 0 2>err-msg > fifo )
+( ulimit -v $vm && env $prog %20000000f 0 2>err-msg > fifo )
exit=$?
# Map this longer, and rarer, diagnostic to the common one.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ rm du chmod
-require_ulimit_v_
-
expensive_
+mkdir d2 \
+ && touch d2/f || framework_failure_
+
+# Restrict memory. Each of these coreutils-8.12 programs would fail
+# with a diagnostic like "rm: fts_read failed: Cannot allocate memory".
+vm=$(get_min_ulimit_v_ du -sh d2) \
+ || skip_ "this shell lacks ulimit support"
+
# With many files in a single directory...
-mkdir d && cd d || framework_failure_
-seq 200000|xargs touch || framework_failure_
+mkdir d || framework_failure_
+seq --format="d/%06g" 200000 | xargs touch || framework_failure_
-cd ..
+# Allow 35MiB more memory as for the trivial case above.
+(ulimit -v $(($vm + 35000)) && du -sh d) || fail=1
-# Restricted to 40MB, each of these coreutils-8.12 programs would fail
-# with a diagnostic like "rm: fts_read failed: Cannot allocate memory".
-ulimit -v 40000
-du -sh d || fail=1
-chmod -R 700 d || fail=1
-rm -rf d || fail=1
+vm=$(get_min_ulimit_v_ chmod -R 700 d2) \
+ || skip_ "this shell lacks ulimit support"
+(ulimit -v $(($vm + 35000)) && chmod -R 700 d) || fail=1
+
+vm=$(get_min_ulimit_v_ rm -rf d2) \
+ || skip_ "this shell lacks ulimit support"
+(ulimit -v $(($vm + 35000)) && rm -rf d) || fail=1
Exit $fail
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ split
-require_ulimit_v_
+vm=$(get_min_ulimit_v_ split -C 'K' /dev/null) \
+ || skip_ "this shell lacks ulimit support"
# Ensure memory is not allocated up front
-(ulimit -v 20000; split -C 'G' /dev/null) || fail=1
+(ulimit -v $vm && split -C 'G' /dev/null) || fail=1
# Ensure correct operation with various split and buffer size combinations