cp: avoid speculative preallocation with --sparse=always
[coreutils.git] / init.cfg
blob03999eb2722180121349a7b489f967c4b7c81d37
1 # This file is sourced by init.sh, *before* its initialization.
3 # Copyright (C) 2010-2014 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 # This goes hand in hand with the "exec 9>&2;" in tests/Makefile.am's
19 # TESTS_ENVIRONMENT definition.
20 stderr_fileno_=9
22 # Having an unsearchable directory in PATH causes execve to fail with EACCES
23 # when applied to an unresolvable program name, contrary to the desired ENOENT.
24 # Avoid the problem by rewriting PATH to exclude unsearchable directories.
25 # Also, if PATH lacks /sbin and/or /usr/sbin, append it/them.
26 sanitize_path_()
28   # FIXME: remove double quotes around $IFS when all tests use init.sh.
29   # They constitute a work-around for a bug in FreeBSD 8.1's /bin/sh.
30   local saved_IFS="$IFS"
31     IFS=:
32     set -- $PATH
33   IFS=$saved_IFS
35   local d d1
36   local colon=
37   local new_path=
38   for d in "$@"; do
39     test -z "$d" && d1=. || d1=$d
40     if ls -d "$d1/." > /dev/null 2>&1; then
41       new_path="$new_path$colon$d"
42       colon=':'
43     fi
44   done
46   for d in /sbin /usr/sbin ; do
47     case ":$new_path:" in
48       *:$d:*) ;;
49       *) new_path="$new_path:$d" ;;
50     esac
51   done
53   PATH=$new_path
54   export PATH
57 getlimits_()
59   eval $(getlimits)
60   test "$INT_MAX" || fatal_ "running getlimits"
63 require_acl_()
65   getfacl --version < /dev/null > /dev/null 2>&1 \
66     && setfacl --version < /dev/null > /dev/null 2>&1 \
67       || skip_ "This test requires getfacl and setfacl."
69   id -u bin > /dev/null 2>&1 \
70     || skip_ "This test requires a local user named bin."
73 is_local_dir_()
75   test $# = 1 || framework_failure_
76   df --local "$1" >/dev/null 2>&1
79 require_mount_list_()
81   local mount_list_fail='cannot read table of mounted file systems'
82   df 2>&1 | grep -F "$mount_list_fail" >/dev/null &&
83     skip_ "$mount_list_fail"
86 require_local_dir_()
88   require_mount_list_
89   is_local_dir_ . ||
90     skip_ "This test must be run on a local file system."
93 require_selinux_()
95   # When in a chroot of an SELinux-enabled system, but with a mock-simulated
96   # SELinux-*disabled* system, recognize that SELinux is disabled system wide:
97   grep 'selinuxfs$' /proc/filesystems > /dev/null \
98     || skip_ "this system lacks SELinux support"
100   # Independent of whether SELinux is enabled system-wide,
101   # the current file system may lack SELinux support.
102   # Also the current build may have SELinux support disabled.
103   case $(ls -Zd .) in
104     '? .'|'unlabeled .')
105       test -z "$CONFIG_HEADER" \
106         && framework_failure_ 'CONFIG_HEADER not defined'
107       grep '^#define HAVE_SELINUX_SELINUX_H 1' "$CONFIG_HEADER" > /dev/null \
108         && selinux_missing_="(file) system" || selinux_missing_="build"
109       skip_ "this $selinux_missing_ lacks SELinux support"
110     ;;
111   esac
114 # Skip this test if we're not in SELinux "enforcing" mode.
115 require_selinux_enforcing_()
117   require_selinux_
118   test "$(getenforce)" = Enforcing \
119     || skip_ "This test is useful only with SELinux in Enforcing mode."
122 require_smack_()
124   grep 'smackfs$' /proc/filesystems > /dev/null \
125     || skip_ "this system lacks SMACK support"
127   test "$(ls -Zd .)" != '? .' \
128     || skip_ "this file system lacks SMACK support"
131 require_openat_support_()
133   # Skip this test if your system has neither the openat-style functions
134   # nor /proc/self/fd support with which to emulate them.
136   test -z "$CONFIG_HEADER" \
137     && framework_failure_ 'CONFIG_HEADER not defined'
139   _skip=yes
140   grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
141   test -d /proc/self/fd && _skip=no
142   if test $_skip = yes; then
143     skip_ 'this system lacks openat support'
144   fi
147 require_ulimit_v_()
149   local ulimit_works=yes
150   # Expect to be able to exec a program in 10MiB of virtual memory,
151   # (10MiB is usually plenty, but valgrind-wrapped date requires 19000KiB,
152   # so allow more in that case)
153   # but not in 20KiB.  I chose "date".  It must not be a shell built-in
154   # function, so you can't use echo, printf, true, etc.
155   # Of course, in coreutils, I could use $top_builddir/src/true,
156   # but this should be able to work for other projects, too.
157   local vm
158   case $(printenv LD_PRELOAD) in */valgrind/*) vm=22000;; *) vm=10000;; esac
160   ( ulimit -v $vm; date ) > /dev/null 2>&1 || ulimit_works=no
161   ( ulimit -v 20;  date ) > /dev/null 2>&1 && ulimit_works=no
163   test $ulimit_works = no \
164     && skip_ "this shell lacks ulimit support"
167 require_readable_root_()
169   test -r / || skip_ "/ is not readable"
172 # Skip the current test if strace is not available or doesn't work
173 # with the named syscall.  Usage: require_strace_ unlink
174 require_strace_()
176   test $# = 1 || framework_failure_
178   strace -V < /dev/null > /dev/null 2>&1 ||
179     skip_ 'no strace program'
181   strace -qe "$1" echo > /dev/null 2>&1 ||
182     skip_ 'strace -qe "'"$1"'" does not work'
184   # On some linux/sparc64 systems, strace works fine on 32-bit executables,
185   # but prints only one line of output for every 64-bit executable.
186   strace -o log-help ls --help >/dev/null || framework_failure_
187   n_lines_help=$(wc -l < log-help)
188   rm -f log-help
189   if test $n_lines_help = 0 || test $n_lines_help = 1; then
190     skip_ 'strace produces no more than one line of output'
191   fi
194 # Skip the current test if valgrind doesn't work,
195 # which could happen if not installed,
196 # or hasn't support for the built architecture,
197 # or hasn't appropriate error suppressions installed etc.
198 require_valgrind_()
200   valgrind --error-exitcode=1 true 2>/dev/null ||
201     skip_ "requires a working valgrind"
204 # Skip the current test if setfacl doesn't work on the current file system,
205 # which could happen if not installed, or if ACLs are not supported by the
206 # kernel or the file system, or are turned off via mount options.
208 # Work around the following two issues:
210 # 1) setfacl maps ACLs into file permission bits if on "noacl" file systems.
212 # On file systems which do not support ACLs (e.g. ext4 mounted with -o noacl),
213 # setfacl operates on the regular file permission bits, and only fails if the
214 # given ACL spec does not fit into there.  Thus, to test if ACLs really work
215 # on the current file system, pass an ACL spec which can't be mapped that way.
216 # "Default" ACLs (-d) seem to fulfill this requirement.
218 # 2) setfacl only invokes the underlying system call if the ACL would change.
220 # If the given ACL spec would not change the ACLs on the file, then setfacl
221 # does not invoke the underlying system call - setxattr().  Therefore, to test
222 # if setting ACLs really works on the current file system, call setfacl twice
223 # with conflictive ACL specs.
224 require_setfacl_()
226   local d='acltestdir_'
227   mkdir $d || framework_failure_
228   local f=0
230   setfacl -d -m user::r-x $d \
231     && setfacl -d -m user::rwx $d \
232     || f=1
233   rm -rf $d || framework_failure_
234   test $f = 0 \
235     || skip_ "setfacl does not work on the current file system"
238 # Require a controlling input 'terminal'.
239 require_controlling_input_terminal_()
241   tty -s || have_input_tty=no
242   test -t 0 || have_input_tty=no
243   if test "$have_input_tty" = no; then
244     skip_ 'requires controlling input terminal
245 This test must have a controlling input "terminal", so it may not be
246 run via "batch", "at", or "ssh".  On some systems, it may not even be
247 run in the background.'
248   fi
251 require_built_()
253   skip_=no
254   for i in "$@"; do
255     case " $built_programs " in
256       *" $i "*) ;;
257       *) echo "$i: not built" 1>&2; skip_=yes ;;
258     esac
259   done
261   test $skip_ = yes && skip_ "required program(s) not built"
264 require_file_system_bytes_free_()
266   local req=$1
267   local expr=$(stat -f --printf "$req / %S <= %a" .)
268   $AWK "BEGIN{ exit !($expr) }" \
269     || skip_ "this test needs at least $req bytes of free space"
272 uid_is_privileged_()
274   # Make sure id -u succeeds.
275   my_uid=$(id -u) \
276     || { echo "$0: cannot run 'id -u'" 1>&2; return 1; }
278   # Make sure it gives valid output.
279   case $my_uid in
280     0) ;;
281     *[!0-9]*)
282       echo "$0: invalid output ('$my_uid') from 'id -u'" 1>&2
283       return 1 ;;
284     *) return 1 ;;
285   esac
288 get_process_status_()
290   sed -n '/^State:[      ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
293 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
294 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
295 # =,u=rw,g=rx,o=wx).  Ignore ACLs.
296 rwx_to_mode_()
298   case $# in
299     1) rwx=$1;;
300     *) echo "$0: wrong number of arguments" 1>&2
301       echo "Usage: $0 ls-style-mode-string" 1>&2
302       return;;
303   esac
305   case $rwx in
306     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
307     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
308     *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
309   esac
311   # Perform these conversions:
312   # S  s
313   # s  xs
314   # T  t
315   # t  xt
316   # The 'T' and 't' ones are only valid for 'other'.
317   s='s/S/@/;s/s/x@/;s/@/s/'
318   t='s/T/@/;s/t/x@/;s/@/t/'
320   u=$(echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s)
321   g=$(echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s)
322   o=$(echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t)
323   echo "=$u$g$o"
326 # Set the global variable stty_reversible_ to a space-separated list of the
327 # reversible settings from stty.c.  stty_reversible_ also starts and ends
328 # with a space.
329 stty_reversible_init_()
331   # Pad start with one space for the first option to match in query function.
332   stty_reversible_=' '$(perl -lne '/^ *{"(.*?)",.*\bREV\b/ and print $1' \
333     "$abs_top_srcdir"/src/stty.c | tr '\n' ' ')
334   # Ensure that there are at least 62, i.e., so we're alerted if
335   # reformatting the source empties the list.
336   test 62 -le $(echo "$stty_reversible_"|wc -w)  \
337     || framework_failure_ "too few reversible settings"
340 # Test whether $1 is one of stty's reversible options.
341 stty_reversible_query_()
343   case $stty_reversible_ in
344     '')
345       framework_failure_ "stty_reversible_init_() not called?";;
346     *" $1 "*)
347       return 0;;
348     *)
349       return 1;;
350   esac
353 skip_if_()
355   case $1 in
356     root) skip_ must be run as root ;;
357     non-root) skip_ must be run as non-root ;;
358     *) ;;  # FIXME?
359   esac
362 very_expensive_()
364   if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
365     skip_ 'very expensive: disabled by default
366 This test is very expensive, so it is disabled by default.
367 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
368 environment variable set to yes.  E.g.,
370   env RUN_VERY_EXPENSIVE_TESTS=yes make check
372 or use the shortcut target of the toplevel Makefile,
374   make check-very-expensive
376   fi
379 expensive_()
381   if test "$RUN_EXPENSIVE_TESTS" != yes; then
382     skip_ 'expensive: disabled by default
383 This test is relatively expensive, so it is disabled by default.
384 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
385 environment variable set to yes.  E.g.,
387   env RUN_EXPENSIVE_TESTS=yes make check
389 or use the shortcut target of the toplevel Makefile,
391   make check-expensive
393   fi
396 # Test whether we can run our just-built root owned rm,
397 # i.e., that $NON_ROOT_USERNAME has access to the build directory.
398 nonroot_has_perm_()
400   require_built_ chroot
402   local rm_version=$(
403     chroot --skip-chdir --user=$NON_ROOT_USERNAME / env PATH="$PATH" \
404       rm --version |
405     sed -n '1s/.* //p'
406   )
407   case ":$rm_version:" in
408       :$PACKAGE_VERSION:) ;;
409       *) return 1;;
410   esac
413 require_root_()
415   uid_is_privileged_ || skip_ "must be run as root"
416   NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
417   NON_ROOT_GID=${NON_ROOT_GID=$(id -g $NON_ROOT_USERNAME)}
419   # When the current test invokes chroot, call nonroot_has_perm_
420   # to check for a common problem.
421   grep '^[ ]*chroot' "../$0" \
422     && { nonroot_has_perm_ \
423            || skip_ "user $NON_ROOT_USERNAME lacks execute permissions"; }
426 skip_if_root_() { uid_is_privileged_ && skip_ "must be run as non-root"; }
428 # Set 'groups' to a space-separated list of at least two groups
429 # of which the user is a member.
430 require_membership_in_two_groups_()
432   test $# = 0 || framework_failure_
434   groups=${COREUTILS_GROUPS-$( (id -G || /usr/xpg4/bin/id -G) 2>/dev/null)}
435   case "$groups" in
436     *' '*) ;;
437     *) skip_ 'requires membership in two groups
438 this test requires that you be a member of more than one group,
439 but running 'id -G'\'' either failed or found just one.  If you really
440 are a member of at least two groups, then rerun this test with
441 COREUTILS_GROUPS set in your environment to the space-separated list
442 of group names or numbers.  E.g.,
444   env COREUTILS_GROUPS='\''users cdrom'\'' make check
447      ;;
448   esac
451 # Is /proc/$PID/status supported?
452 require_proc_pid_status_()
454     sleep 2 &
455     local pid=$!
456     sleep .5
457     grep '^State:[       ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
458     skip_ "/proc/$pid/status: missing or 'different'"
459     kill $pid
462 # Return nonzero if the specified path is on a file system for
463 # which FIEMAP support exists.  Note some file systems (like ext3 and btrfs)
464 # only support FIEMAP for files, not directories.
465 fiemap_capable_()
467   if ! python < /dev/null; then
468     warn_ 'fiemap_capable_: python missing: assuming not fiemap capable'
469     return 1
470   fi
471   python "$abs_srcdir"/tests/fiemap-capable "$@"
474 # Skip the current test if "." lacks d_type support.
475 require_dirent_d_type_()
477   python < /dev/null \
478     || skip_ python missing: assuming no d_type support
480   # Manually exclude xfs, since the test would mistakenly report
481   # that it has d_type support: d_type == DT_DIR for "." and "..",
482   # but DT_UNKNOWN for all other types.
483   df -x xfs . > /dev/null 2>&1 \
484     || skip_ requires d_type support
486   python "$abs_srcdir"/tests/d_type-check \
487     || skip_ requires d_type support
490 # Skip the current test if we lack Perl.
491 require_perl_()
493   : ${PERL=perl}
494   $PERL -e 'use warnings' > /dev/null 2>&1 \
495     || skip_ 'configure did not find a usable version of Perl'
498 # Does the current (working-dir) file system support sparse files?
499 require_sparse_support_()
501   test $# = 0 || framework_failure_
502   # Test whether we can create a sparse file.
503   # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
504   # NTFS requires 128K before a hole appears in a sparse file.
505   t=sparse.$$
506   dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
507   set x $(du -sk $t)
508   kb_size=$2
509   rm -f $t
510   if test $kb_size -ge 128; then
511     skip_ 'this file system does not support sparse files'
512   fi
515 # Compile a shared lib using the GCC options for doing so.
516 # Pass input and output file as parameters respectively.
517 # Any other optional parmeters are passed to $CC.
518 gcc_shared_()
520   local in=$1
521   local out=$2
522   shift 2 || return 1
524   $CC -Wall -shared --std=gnu99 -fPIC -O2 $* "$in" -o "$out" -ldl
527 # There are a myriad of ways to build shared libs,
528 # so we only consider running tests requiring shared libs,
529 # on platforms that support building them as follows.
530 require_gcc_shared_()
532   gcc_shared_ '-' 'd.so' -xc < /dev/null 2>&1 \
533     || skip_ '$CC -shared ... failed to build a shared lib'
534   rm -f d.so
537 mkfifo_or_skip_()
539   test $# = 1 || framework_failure_
540   if ! mkfifo "$1"; then
541     # Make an exception of this case -- usually we interpret framework-creation
542     # failure as a test failure.  However, in this case, when running on a SunOS
543     # system using a disk NFS mounted from OpenBSD, the above fails like this:
544     # mkfifo: cannot make fifo 'fifo-10558': Not owner
545     skip_ 'unable to create a fifo'
546   fi
549 # Disable the current test if the working directory seems to have
550 # the setgid bit set.
551 skip_if_setgid_()
553   setgid_tmpdir=setgid-$$
554   (umask 77; mkdir $setgid_tmpdir)
555   perms=$(stat --printf %A $setgid_tmpdir)
556   rmdir $setgid_tmpdir
557   case $perms in
558     drwx------);;
559     drwxr-xr-x);;  # Windows98 + DJGPP 2.03
560     *) skip_ 'this directory has the setgid bit set';;
561   esac
564 # Skip if files are created with a different group to the current user
565 # This can happen due to a setgid dir, or by some other mechanism on OS X:
566 # http://unix.stackexchange.com/q/63865
567 # http://bugs.gnu.org/14024#41
568 skip_if_nondefault_group_()
570   touch grp.$$
571   gen_ug=$(stat -c '%u:%g' grp.$$)
572   rm grp.$$
573   test "$gen_ug" = "$(id -ru):$(id -rg)" ||
574     skip_ 'Files are created with a different gid'
577 skip_if_mcstransd_is_running_()
579   test $# = 0 || framework_failure_
581   # When mcstransd is running, you'll see only the 3-component
582   # version of file-system context strings.  Detect that,
583   # and if it's running, skip this test.
584   __ctx=$(stat --printf='%C\n' .) || framework_failure_
585   case $__ctx in
586     *:*:*:*) ;; # four components is ok
587     *) # anything else probably means mcstransd is running
588         skip_ "unexpected context '$__ctx'; turn off mcstransd" ;;
589   esac
592 # Skip the current test if umask doesn't work as usual.
593 # This test should be run in the temporary directory that ends
594 # up being removed via the trap commands.
595 working_umask_or_skip_()
597   umask 022
598   touch file1 file2
599   chmod 644 file2
600   perms=$(ls -l file1 file2 | sed 's/ .*//' | uniq)
601   rm -f file1 file2
603   case $perms in
604   *'
605   '*) skip_ 'your build directory has unusual umask semantics'
606   esac
609 # Retry a function requiring a sufficient delay to _pass_
610 # using a truncated exponential backoff method.
611 #     Example: retry_delay_ dd_reblock_1 .1 6
612 # This example will call the dd_reblock_1 function with
613 # an initial delay of .1 second and call it at most 6 times
614 # with a max delay of 3.2s (doubled each time), or a total of 6.3s
615 # Note ensure you do _not_ quote the parameter to GNU sleep in
616 # your function, as it may contain separate values that sleep
617 # needs to accumulate.
618 # Further function arguments will be forwarded to the test function.
619 retry_delay_()
621   local test_func=$1
622   local init_delay=$2
623   local max_n_tries=$3
624   shift 3 || return 1
626   local attempt=1
627   local num_sleeps=$attempt
628   local time_fail
629   while test $attempt -le $max_n_tries; do
630     local delay=$($AWK -v n=$num_sleeps -v s="$init_delay" \
631                   'BEGIN { print s * n }')
632     "$test_func" "$delay" "$@" && { time_fail=0; break; } || time_fail=1
633     attempt=$(expr $attempt + 1)
634     num_sleeps=$(expr $num_sleeps '*' 2)
635   done
636   test "$time_fail" = 0
639 # Call this with a list of programs under test immediately after
640 # sourcing init.sh.
641 print_ver_()
643   if test "$VERBOSE" = yes; then
644     local i
645     for i in $*; do
646       env $i --version
647     done
648   fi
651 # Are we running on GNU/Hurd?
652 require_gnu_()
654   test "$(uname)" = GNU \
655     || skip_ 'not running on GNU/Hurd'
658 sanitize_path_