build: ensure make-prime-list doesn't access out of bounds memory
[platform/upstream/coreutils.git] / init.cfg
1 # This file is sourced by init.sh, *before* its initialization.
2
3 # Copyright (C) 2010-2013 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.
17
18 # This goes hand in hand with the "exec 9>&2;" in tests/Makefile.am's
19 # TESTS_ENVIRONMENT definition.
20 stderr_fileno_=9
21
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_()
27 {
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
34
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
45
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
52
53   PATH=$new_path
54   export PATH
55 }
56
57 getlimits_()
58 {
59   eval $(getlimits)
60   test "$INT_MAX" || fatal_ "running getlimits"
61 }
62
63 require_acl_()
64 {
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."
68
69   id -u bin > /dev/null 2>&1 \
70     || skip_ "This test requires a local user named bin."
71 }
72
73 is_local_dir_()
74 {
75   test $# = 1 || framework_failure_
76   df --local "$1" >/dev/null 2>&1
77 }
78
79 require_mount_list_()
80 {
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"
84 }
85
86 require_local_dir_()
87 {
88   require_mount_list_
89   is_local_dir_ . ||
90     skip_ "This test must be run on a local file system."
91 }
92
93 # Skip this test if we're not in SELinux "enforcing" mode.
94 require_selinux_enforcing_()
95 {
96   test "$(getenforce)" = Enforcing \
97     || skip_ "This test is useful only with SELinux in Enforcing mode."
98 }
99
100 require_openat_support_()
101 {
102   # Skip this test if your system has neither the openat-style functions
103   # nor /proc/self/fd support with which to emulate them.
104   test -z "$CONFIG_HEADER" \
105     && skip_ 'internal error: CONFIG_HEADER not defined'
106
107   _skip=yes
108   grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
109   test -d /proc/self/fd && _skip=no
110   if test $_skip = yes; then
111     skip_ 'this system lacks openat support'
112   fi
113 }
114
115 require_ulimit_()
116 {
117   local ulimit_works=yes
118   # Expect to be able to exec a program in 10MiB of virtual memory,
119   # (10MiB is usually plenty, but valgrind-wrapped date requires 19000KiB,
120   # so allow more in that case)
121   # but not in 20KiB.  I chose "date".  It must not be a shell built-in
122   # function, so you can't use echo, printf, true, etc.
123   # Of course, in coreutils, I could use $top_builddir/src/true,
124   # but this should be able to work for other projects, too.
125   local vm
126   case $(printenv LD_PRELOAD) in */valgrind/*) vm=22000;; *) vm=10000;; esac
127
128   ( ulimit -v $vm; date ) > /dev/null 2>&1 || ulimit_works=no
129   ( ulimit -v 20;  date ) > /dev/null 2>&1 && ulimit_works=no
130
131   test $ulimit_works = no \
132     && skip_ "this shell lacks ulimit support"
133 }
134
135 require_readable_root_()
136 {
137   test -r / || skip_ "/ is not readable"
138 }
139
140 # Skip the current test if strace is not available or doesn't work
141 # with the named syscall.  Usage: require_strace_ unlink
142 require_strace_()
143 {
144   test $# = 1 || framework_failure_
145
146   strace -V < /dev/null > /dev/null 2>&1 ||
147     skip_ 'no strace program'
148
149   strace -qe "$1" echo > /dev/null 2>&1 ||
150     skip_ 'strace -qe "'"$1"'" does not work'
151
152   # On some linux/sparc64 systems, strace works fine on 32-bit executables,
153   # but prints only one line of output for every 64-bit executable.
154   strace -o log-help ls --help >/dev/null || framework_failure_
155   n_lines_help=$(wc -l < log-help)
156   rm -f log-help
157   if test $n_lines_help = 0 || test $n_lines_help = 1; then
158     skip_ 'strace produces no more than one line of output'
159   fi
160 }
161
162 # Skip the current test if valgrind doesn't work,
163 # which could happen if not installed,
164 # or hasn't support for the built architecture,
165 # or hasn't appropriate error suppressions installed etc.
166 require_valgrind_()
167 {
168   valgrind --error-exitcode=1 true 2>/dev/null ||
169     skip_ "requires a working valgrind"
170 }
171
172 require_setfacl_()
173 {
174   setfacl -m user::rwx . \
175     || skip_ "setfacl does not work on the current file system"
176 }
177
178 # Require a controlling input 'terminal'.
179 require_controlling_input_terminal_()
180 {
181   tty -s || have_input_tty=no
182   test -t 0 || have_input_tty=no
183   if test "$have_input_tty" = no; then
184     skip_ 'requires controlling input terminal
185 This test must have a controlling input "terminal", so it may not be
186 run via "batch", "at", or "ssh".  On some systems, it may not even be
187 run in the background.'
188   fi
189 }
190
191 require_built_()
192 {
193   skip_=no
194   for i in "$@"; do
195     case " $built_programs " in
196       *" $i "*) ;;
197       *) echo "$i: not built" 1>&2; skip_=yes ;;
198     esac
199   done
200
201   test $skip_ = yes && skip_ "required program(s) not built"
202 }
203
204 require_file_system_bytes_free_()
205 {
206   local req=$1
207   local expr=$(stat -f --printf "$req / %S <= %a" .)
208   $AWK "BEGIN{ exit !($expr) }" \
209     || skip_ "this test needs at least $req bytes of free space"
210 }
211
212 uid_is_privileged_()
213 {
214   # Make sure id -u succeeds.
215   my_uid=$(id -u) \
216     || { echo "$0: cannot run 'id -u'" 1>&2; return 1; }
217
218   # Make sure it gives valid output.
219   case $my_uid in
220     0) ;;
221     *[!0-9]*)
222       echo "$0: invalid output ('$my_uid') from 'id -u'" 1>&2
223       return 1 ;;
224     *) return 1 ;;
225   esac
226 }
227
228 get_process_status_()
229 {
230   sed -n '/^State:[      ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
231 }
232
233 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
234 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
235 # =,u=rw,g=rx,o=wx).  Ignore ACLs.
236 rwx_to_mode_()
237 {
238   case $# in
239     1) rwx=$1;;
240     *) echo "$0: wrong number of arguments" 1>&2
241       echo "Usage: $0 ls-style-mode-string" 1>&2
242       return;;
243   esac
244
245   case $rwx in
246     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
247     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
248     *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
249   esac
250
251   # Perform these conversions:
252   # S  s
253   # s  xs
254   # T  t
255   # t  xt
256   # The 'T' and 't' ones are only valid for 'other'.
257   s='s/S/@/;s/s/x@/;s/@/s/'
258   t='s/T/@/;s/t/x@/;s/@/t/'
259
260   u=$(echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s)
261   g=$(echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s)
262   o=$(echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t)
263   echo "=$u$g$o"
264 }
265
266 # Set the global variable stty_reversible_ to a space-separated list of the
267 # reversible settings from stty.c.  stty_reversible_ also starts and ends
268 # with a space.
269 stty_reversible_init_()
270 {
271   # Pad start with one space for the first option to match in query function.
272   stty_reversible_=' '$(perl -lne '/^ *{"(.*?)",.*\bREV\b/ and print $1' \
273     "$abs_top_srcdir"/src/stty.c | tr '\n' ' ')
274   # Ensure that there are at least 62, i.e., so we're alerted if
275   # reformatting the source empties the list.
276   test 62 -le $(echo "$stty_reversible_"|wc -w)  \
277     || framework_failure_ "too few reversible settings"
278 }
279
280 # Test whether $1 is one of stty's reversible options.
281 stty_reversible_query_()
282 {
283   case $stty_reversible_ in
284     '')
285       framework_failure_ "stty_reversible_init_() not called?";;
286     *" $1 "*)
287       return 0;;
288     *)
289       return 1;;
290   esac
291 }
292
293 skip_if_()
294 {
295   case $1 in
296     root) skip_ must be run as root ;;
297     non-root) skip_ must be run as non-root ;;
298     *) ;;  # FIXME?
299   esac
300 }
301
302 require_selinux_()
303 {
304   # When in a chroot of an SELinux-enabled system, but with a mock-simulated
305   # SELinux-*disabled* system, recognize that SELinux is disabled system wide:
306   grep 'selinuxfs$' /proc/filesystems > /dev/null \
307     || skip_ "this system lacks SELinux support"
308
309   # Independent of whether SELinux is enabled system-wide,
310   # the current file system may lack SELinux support.
311   case $(ls -Zd .) in
312     '? .'|'unlabeled .')
313       skip_ "this system (or maybe just" \
314         "the current file system) lacks SELinux support"
315     ;;
316   esac
317 }
318
319 very_expensive_()
320 {
321   if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
322     skip_ 'very expensive: disabled by default
323 This test is very expensive, so it is disabled by default.
324 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
325 environment variable set to yes.  E.g.,
326
327   env RUN_VERY_EXPENSIVE_TESTS=yes make check
328
329 or use the shortcut target of the toplevel Makefile,
330
331   make check-very-expensive
332 '
333   fi
334 }
335
336 expensive_()
337 {
338   if test "$RUN_EXPENSIVE_TESTS" != yes; then
339     skip_ 'expensive: disabled by default
340 This test is relatively expensive, so it is disabled by default.
341 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
342 environment variable set to yes.  E.g.,
343
344   env RUN_EXPENSIVE_TESTS=yes make check
345
346 or use the shortcut target of the toplevel Makefile,
347
348   make check-expensive
349 '
350   fi
351 }
352
353 # Test whether we can run our just-built rm setuidgid-to-root,
354 # i.e., that $NON_ROOT_USERNAME has access to the build directory.
355 setuidgid_has_perm_()
356 {
357   local rm_version=$(
358     setuidgid $NON_ROOT_USERNAME env PATH="$PATH" rm --version |
359     sed -n '1s/.* //p'
360   )
361   case ":$rm_version:" in
362       :$PACKAGE_VERSION:) ;;
363       *) return 1;;
364   esac
365 }
366
367 require_root_()
368 {
369   uid_is_privileged_ || skip_ "must be run as root"
370   NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
371   NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
372
373   # When the current test invokes setuidgid, call setuidgid_has_perm_
374   # to check for a common problem.
375   grep '^[ ]*setuidgid' "../$0" \
376     && { setuidgid_has_perm_ \
377            || skip_ "user $NON_ROOT_USERNAME lacks execute permissions"; }
378 }
379
380 skip_if_root_() { uid_is_privileged_ && skip_ "must be run as non-root"; }
381
382 # Set 'groups' to a space-separated list of at least two groups
383 # of which the user is a member.
384 require_membership_in_two_groups_()
385 {
386   test $# = 0 || framework_failure_
387
388   groups=${COREUTILS_GROUPS-$( (id -G || /usr/xpg4/bin/id -G) 2>/dev/null)}
389   case "$groups" in
390     *' '*) ;;
391     *) skip_ 'requires membership in two groups
392 this test requires that you be a member of more than one group,
393 but running 'id -G'\'' either failed or found just one.  If you really
394 are a member of at least two groups, then rerun this test with
395 COREUTILS_GROUPS set in your environment to the space-separated list
396 of group names or numbers.  E.g.,
397
398   env COREUTILS_GROUPS='\''users cdrom'\'' make check
399
400 '
401      ;;
402   esac
403 }
404
405 # Is /proc/$PID/status supported?
406 require_proc_pid_status_()
407 {
408     sleep 2 &
409     local pid=$!
410     sleep .5
411     grep '^State:[       ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
412     skip_ "/proc/$pid/status: missing or 'different'"
413     kill $pid
414 }
415
416 # Return nonzero if the specified path is on a file system for
417 # which FIEMAP support exists.  Note some file systems (like ext3 and btrfs)
418 # only support FIEMAP for files, not directories.
419 fiemap_capable_()
420 {
421   if ! python < /dev/null; then
422     warn_ 'fiemap_capable_: python missing: assuming not fiemap capable'
423     return 1
424   fi
425   python "$abs_srcdir"/tests/fiemap-capable "$@"
426 }
427
428 # Skip the current test if "." lacks d_type support.
429 require_dirent_d_type_()
430 {
431   python < /dev/null \
432     || skip_ python missing: assuming no d_type support
433
434   # Manually exclude xfs, since the test would mistakenly report
435   # that it has d_type support: d_type == DT_DIR for "." and "..",
436   # but DT_UNKNOWN for all other types.
437   df -x xfs . > /dev/null 2>&1 \
438     || skip_ requires d_type support
439
440   python "$abs_srcdir"/tests/d_type-check \
441     || skip_ requires d_type support
442 }
443
444 # Skip the current test if we lack Perl.
445 require_perl_()
446 {
447   : ${PERL=perl}
448   $PERL -e 'use warnings' > /dev/null 2>&1 \
449     || skip_ 'configure did not find a usable version of Perl'
450 }
451
452 # Does the current (working-dir) file system support sparse files?
453 require_sparse_support_()
454 {
455   test $# = 0 || framework_failure_
456   # Test whether we can create a sparse file.
457   # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
458   # NTFS requires 128K before a hole appears in a sparse file.
459   t=sparse.$$
460   dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
461   set x $(du -sk $t)
462   kb_size=$2
463   rm -f $t
464   if test $kb_size -ge 128; then
465     skip_ 'this file system does not support sparse files'
466   fi
467 }
468
469 mkfifo_or_skip_()
470 {
471   test $# = 1 || framework_failure_
472   if ! mkfifo "$1"; then
473     # Make an exception of this case -- usually we interpret framework-creation
474     # failure as a test failure.  However, in this case, when running on a SunOS
475     # system using a disk NFS mounted from OpenBSD, the above fails like this:
476     # mkfifo: cannot make fifo 'fifo-10558': Not owner
477     skip_ 'unable to create a fifo'
478   fi
479 }
480
481 # Disable the current test if the working directory seems to have
482 # the setgid bit set.
483 skip_if_setgid_()
484 {
485   setgid_tmpdir=setgid-$$
486   (umask 77; mkdir $setgid_tmpdir)
487   perms=$(stat --printf %A $setgid_tmpdir)
488   rmdir $setgid_tmpdir
489   case $perms in
490     drwx------);;
491     drwxr-xr-x);;  # Windows98 + DJGPP 2.03
492     *) skip_ 'this directory has the setgid bit set';;
493   esac
494 }
495
496 skip_if_mcstransd_is_running_()
497 {
498   test $# = 0 || framework_failure_
499
500   # When mcstransd is running, you'll see only the 3-component
501   # version of file-system context strings.  Detect that,
502   # and if it's running, skip this test.
503   __ctx=$(stat --printf='%C\n' .) || framework_failure_
504   case $__ctx in
505     *:*:*:*) ;; # four components is ok
506     *) # anything else probably means mcstransd is running
507         skip_ "unexpected context '$__ctx'; turn off mcstransd" ;;
508   esac
509 }
510
511 # Skip the current test if umask doesn't work as usual.
512 # This test should be run in the temporary directory that ends
513 # up being removed via the trap commands.
514 working_umask_or_skip_()
515 {
516   umask 022
517   touch file1 file2
518   chmod 644 file2
519   perms=$(ls -l file1 file2 | sed 's/ .*//' | uniq)
520   rm -f file1 file2
521
522   case $perms in
523   *'
524   '*) skip_ 'your build directory has unusual umask semantics'
525   esac
526 }
527
528 # Retry a function requiring a sufficient delay to _pass_
529 # using a truncated exponential backoff method.
530 #     Example: retry_delay_ dd_reblock_1 .1 6
531 # This example will call the dd_reblock_1 function with
532 # an initial delay of .1 second and call it at most 6 times
533 # with a max delay of 3.2s (doubled each time), or a total of 6.3s
534 # Note ensure you do _not_ quote the parameter to GNU sleep in
535 # your function, as it may contain separate values that sleep
536 # needs to accumulate.
537 retry_delay_()
538 {
539   local test_func=$1
540   local init_delay=$2
541   local max_n_tries=$3
542
543   local attempt=1
544   local num_sleeps=$attempt
545   local time_fail
546   while test $attempt -le $max_n_tries; do
547     local delay=$($AWK -v n=$num_sleeps -v s="$init_delay" \
548                   'BEGIN { print s * n }')
549     "$test_func" "$delay" && { time_fail=0; break; } || time_fail=1
550     attempt=$(expr $attempt + 1)
551     num_sleeps=$(expr $num_sleeps '*' 2)
552   done
553   test "$time_fail" = 0
554 }
555
556 # Call this with a list of programs under test immediately after
557 # sourcing init.sh.
558 print_ver_()
559 {
560   if test "$VERBOSE" = yes; then
561     local i
562     for i in $*; do
563       env $i --version
564     done
565   fi
566 }
567
568 # Are we running on GNU/Hurd?
569 require_gnu_()
570 {
571   test "$(uname)" = GNU \
572     || skip_ 'not running on GNU/Hurd'
573 }
574
575 sanitize_path_