2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
4 # This file is part of util-linux.
6 # This file is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This file is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
24 function ts_canonicalize
{
37 ts_failed
"ul_cd: not enough arguments"
39 DEST
=$
(readlink
-f "$1" 2>/dev
/null
)
40 if [ "x$DEST" = "x" ] ||
[ ! -d "$DEST" ]; then
41 ts_failed
"ul_cd: $1: no such directory"
43 cd "$DEST" 2>/dev
/null || ts_failed
"ul_cd: $1: cannot change directory"
44 if [ "$PWD" != "$DEST" ]; then
45 ts_failed
"ul_cd: $PWD is not $DEST"
49 function ts_separator
{
52 if [ -z "$header" ]; then
53 echo "============================================" >> $TS_OUTPUT
55 echo "=====$header================================" >> $TS_OUTPUT
62 if [ "$TS_PARSABLE" != "yes" ]; then
63 if [ $TS_NSUBTESTS -ne 0 ] && [ -z "$TS_SUBNAME" ]; then
64 desc
=$
(printf "%11s...")
70 if [ -n "$TS_SUBNAME" ]; then
71 desc
=$
(printf "%s: [%02d] %s" "$TS_DESC" "$TS_NSUBTESTS" "$TS_SUBNAME")
75 printf "%13s: %-45s ...%s\n" "$TS_COMPONENT" "$desc" "$1"
78 function ts_check_test_command
{
81 ts_failed
"invalid test_command requested"
85 if [ ! -x "$1" ]; then
86 ts_skip
"${1##*/} not found"
90 # just command names (e.g. --use-system-commands)
92 type "$cmd" >/dev
/null
2>&1
94 if [ "$TS_NOSKIP_COMMANDS" = "yes" ]; then
95 ts_failed
"missing in PATH: $cmd"
97 ts_skip
"missing in PATH: $cmd"
103 function ts_check_prog
{
105 [ -z "$cmd" ] && ts_failed
"invalid prog requested"
106 type "$cmd" >/dev
/null
2>&1 || ts_skip
"missing in PATH: $cmd"
109 function ts_check_losetup
{
111 ts_check_test_command
"$TS_CMD_LOSETUP"
113 if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then
114 ts_skip
"loop-device tests disabled"
117 # assuming that losetup -f works ... to be checked somewhere else
118 tmp
=$
($TS_CMD_LOSETUP -f 2>/dev
/null
)
119 if test -b "$tmp"; then
122 ts_skip
"no loop-device support"
125 function ts_check_wcsspn
{
126 # https://gitlab.com/qemu-project/qemu/-/issues/1248
127 if [ -e "$TS_HELPER_SYSINFO" ] &&
128 [ "$("$TS_HELPER_SYSINFO" wcsspn-ok)" = "0" ]; then
130 ts_skip
"non-functional widestring functions"
134 function ts_check_native_byteorder
{
135 if [ "$QEMU_USER" == "1" ] && [ ! -e /sys
/kernel
/cpu_byteorder
]; then
136 ts_skip
"non-native byteorder"
140 function ts_check_enotty
{
141 # https://lore.kernel.org/qemu-devel/20230426070659.80649-1-thomas@t-8ch.de/
142 if [ -e "$TS_HELPER_SYSINFO" ] &&
143 [ "$("$TS_HELPER_SYSINFO" enotty-ok)" = "0" ]; then
145 ts_skip
"broken ENOTTY return"
149 function ts_report_skip
{
150 ts_report
" SKIPPED ($1)"
160 function ts_skip_nonroot
{
161 if [ $UID -ne 0 ]; then
162 ts_skip
"no root permissions"
166 # Specify the capability needed in your test case like:
168 # ts_skip_capability cap_wake_alarm
170 function ts_skip_capability
{
171 ts_check_prog
"$TS_HELPER_CAP"
173 if ! "$TS_HELPER_CAP" "$1"; then
174 ts_skip
"no capability: $1"
178 function ts_skip_qemu_user
{
179 if [ "$QEMU_USER" == "1" ]; then
180 ts_skip
"running under qemu-user emulation"
184 function ts_failed_subtest
{
187 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
192 if [ x
"$1" == x
"" ]; then
193 ts_report
" $msg ($TS_NS)"
195 ts_report
" $msg ($1)"
202 ts_failed_subtest
"$1"
206 function ts_report_ok
{
207 if [ x
"$1" == x
"" ]; then
220 echo "$1" >> $TS_OUTPUT
221 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
225 echo "$1" >> $TS_ERRLOG
226 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
229 function ts_log_both
{
230 echo "$1" >> $TS_OUTPUT
231 echo "$1" >> $TS_ERRLOG
232 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
235 function ts_has_option
{
239 # user may set options by env for a single test or whole component
240 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
241 local v_test
=${TS_TESTNAME//[-.]/_}
242 local v_comp
=${TS_COMPONENT//[-.]/_}
243 local v_name
=${NAME//[-.]/_}
244 eval local env_opt_test
=\
$TS_OPT_${v_comp}_${v_test}_${v_name}
245 eval local env_opt_comp
=\
$TS_OPT_${v_comp}_
${v_name}
246 if [ "$env_opt_test" = "yes" \
247 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
250 elif [ "$env_opt_test" = "no" \
251 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
255 # or just check the global command line options
256 if [[ $ALL =~
([$
' \t\n']|^
)--$NAME([$
'= \t\n']|$
) ]]; then
261 # or the _global_ env, e.g TS_OPT_parsable="yes"
262 eval local env_opt
=\
$TS_OPT_${v_name}
263 if [ "$env_opt" = "yes" ]; then echo "yes"; fi
266 function ts_option_argument
{
271 echo "$ALL" |
sed -n "s/.*[ \t\n]--$NAME=\([^ \t\n]*\).*/\1/p" |
tail -n 1
274 function ts_init_core_env
{
276 TS_NS
="$TS_COMPONENT/$TS_TESTNAME"
277 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME"
278 TS_ERRLOG
="$TS_OUTDIR/$TS_TESTNAME.err"
279 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME.vgdump"
280 TS_EXIT_CODE
="$TS_OUTDIR/$TS_TESTNAME.exit_code"
281 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME"
282 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
283 TS_EXPECTED_ERR
="$TS_TOPDIR/expected/$TS_NS.err"
284 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-mnt"
287 function ts_init_core_subtest_env
{
288 TS_NS
="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
289 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
290 TS_ERRLOG
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.err"
291 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
292 TS_EXIT_CODE
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.exit_code"
293 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
294 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
295 TS_EXPECTED_ERR
="$TS_TOPDIR/expected/$TS_NS.err"
296 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
298 rm -f "$TS_OUTPUT" "$TS_ERRLOG" "$TS_VGDUMP" "$TS_EXIT_CODE"
299 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
301 touch "$TS_OUTPUT" "$TS_ERRLOG" "$TS_EXIT_CODE"
302 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
305 function ts_init_env
{
306 local mydir
=$
(ts_abspath
${0%/*})
313 ASAN_OPTIONS
="detect_leaks=0"
314 UBSAN_OPTIONS
="print_stacktrace=1:print_summary=1:halt_on_error=1"
316 export LANG LANGUAGE LC_ALL CHARSET ASAN_OPTIONS UBSAN_OPTIONS
318 mydir
=$
(ts_canonicalize
"$mydir")
320 # automake directories
321 top_srcdir
=$
(ts_option_argument
"srcdir" "$*")
322 top_builddir
=$
(ts_option_argument
"builddir" "$*")
324 # where is this script
325 TS_TOPDIR
=$
(ts_abspath
$mydir/..
/..
/)
328 if [ -z "$top_srcdir" ]; then
329 top_srcdir
="$TS_TOPDIR/.."
331 if [ -z "$top_builddir" ]; then
332 top_builddir
="$TS_TOPDIR/.."
335 top_srcdir
=$
(ts_abspath
$top_srcdir)
336 top_builddir
=$
(ts_abspath
$top_builddir)
338 if [ -e "$top_builddir/meson.conf" ]; then
339 .
"$top_builddir/meson.conf"
342 # We use helpser always from build tree
343 ts_helpersdir
="${top_builddir}/"
345 TS_USE_SYSTEM_COMMANDS
=$
(ts_has_option
"use-system-commands" "$*")
346 if [ "$TS_USE_SYSTEM_COMMANDS" == "yes" ]; then
347 # Don't define anything, just follow current PATH
350 # The default is to use commands from build tree
351 ts_commandsdir
="${top_builddir}/"
353 # some ul commands search other ul commands in $PATH
354 export PATH
="$ts_commandsdir:$PATH"
357 TS_SCRIPT
="$mydir/$(basename $0)"
358 TS_SUBDIR
=$
(dirname $TS_SCRIPT)
359 TS_TESTNAME
=$
(basename $TS_SCRIPT)
360 TS_COMPONENT
=$
(basename $TS_SUBDIR)
361 TS_DESC
=${TS_DESC:-$TS_TESTNAME}
368 TS_OUTDIR
="$top_builddir/tests/output/$TS_COMPONENT"
369 TS_DIFFDIR
="$top_builddir/tests/diff/$TS_COMPONENT"
371 TS_NOLOCKS
=$
(ts_has_option
"nolocks" "$*")
372 TS_LOCKDIR
="$top_builddir/tests/output"
374 # Don't lock if flock(1) is missing
375 type "flock" >/dev
/null
2>&1 || TS_NOLOCKS
="yes"
379 TS_NOSKIP_COMMANDS
=$
(ts_has_option
"noskip-commands" "$*")
380 TS_VERBOSE
=$
(ts_has_option
"verbose" "$*")
381 TS_SHOWDIFF
=$
(ts_has_option
"show-diff" "$*")
382 TS_PARALLEL
=$
(ts_has_option
"parallel" "$*")
383 TS_KNOWN_FAIL
=$
(ts_has_option
"known-fail" "$*")
384 TS_SKIP_LOOPDEVS
=$
(ts_has_option
"skip-loopdevs" "$*")
385 TS_PARSABLE
=$
(ts_has_option
"parsable" "$*")
386 [ "$TS_PARSABLE" = "yes" ] || TS_PARSABLE
="$TS_PARALLEL"
388 tmp
=$
( ts_has_option
"memcheck-valgrind" "$*")
389 if [ "$tmp" == "yes" -a -f /usr
/bin
/valgrind
]; then
390 TS_VALGRIND_CMD
="/usr/bin/valgrind"
392 tmp
=$
( ts_has_option
"memcheck-asan" "$*")
393 if [ "$tmp" == "yes" ]; then
396 tmp
=$
( ts_has_option
"memcheck-ubsan" "$*")
397 if [ "$tmp" == "yes" ]; then
398 TS_ENABLE_UBSAN
="yes"
401 TS_FSTAB
="$TS_OUTDIR/${TS_TESTNAME}.fstab"
402 BLKID_FILE
="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
404 declare -a TS_SUID_PROGS
405 declare -a TS_SUID_USER
406 declare -a TS_SUID_GROUP
407 declare -a TS_LOOP_DEVS
408 declare -a TS_LOCKFILE_FD
410 if [ -f $TS_TOPDIR/commands.sh
]; then
411 .
$TS_TOPDIR/commands.sh
416 rm -f $TS_OUTPUT $TS_ERRLOG $TS_VGDUMP $TS_EXIT_CODE
417 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
419 touch $TS_OUTPUT $TS_ERRLOG $TS_EXIT_CODE
420 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
422 if [ "$TS_VERBOSE" == "yes" ]; then
424 echo " script: $TS_SCRIPT"
425 echo " commands: $ts_commandsdir"
426 echo " helpers: $ts_helpersdir"
427 echo " sub dir: $TS_SUBDIR"
428 echo " top dir: $TS_TOPDIR"
429 echo " self: $TS_SELF"
430 echo " test name: $TS_TESTNAME"
431 echo " test desc: $TS_DESC"
432 echo " component: $TS_COMPONENT"
433 echo " namespace: $TS_NS"
434 echo " verbose: $TS_VERBOSE"
435 echo " output: $TS_OUTPUT"
436 echo " error log: $TS_ERRLOG"
437 echo " exit code: $TS_EXIT_CODE"
438 echo " valgrind: $TS_VGDUMP"
439 echo " expected: $TS_EXPECTED{.err}"
440 echo " mountpoint: $TS_MOUNTPOINT"
445 function ts_init_subtest
{
448 ts_init_core_subtest_env
449 TS_NSUBTESTS
=$
(( $TS_NSUBTESTS + 1 ))
451 if [ "$TS_PARSABLE" != "yes" ]; then
452 [ $TS_NSUBTESTS -eq 1 ] && echo
453 printf "%16s: %-27s ..." "" "$TS_SUBNAME"
460 local is_fake
=$
( ts_has_option
"fake" "$*")
461 local is_force
=$
( ts_has_option
"force" "$*")
463 if [ "$TS_PARSABLE" != "yes" ]; then
464 printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
467 [ "$is_fake" == "yes" ] && ts_skip
"fake mode"
468 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip
"optional"
471 function ts_init_suid
{
473 ct
=${#TS_SUID_PROGS[*]}
475 # Save info about original setting
476 TS_SUID_PROGS
[$ct]=$PROG
477 TS_SUID_USER
[$ct]=$
(stat
--printf="%U" $PROG)
478 TS_SUID_GROUP
[$ct]=$
(stat
--printf="%G" $PROG)
480 chown root
:root
$PROG &> /dev
/null
481 chmod u
+s
$PROG &> /dev
/null
484 function ts_init_py
{
487 if [ -f "$top_builddir/py${LIBNAME}.la" ]; then
489 export LD_LIBRARY_PATH
="$top_builddir/.libs:$LD_LIBRARY_PATH"
490 export PYTHONPATH
="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
492 PYTHON_VERSION
=$
(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile
)
493 PYTHON_MAJOR_VERSION
=$
(echo $PYTHON_VERSION |
sed 's/\..*//')
495 export PYTHON
="python${PYTHON_MAJOR_VERSION}"
497 elif compgen
-G "$top_builddir/$LIBNAME/python/py$LIBNAME*.so" >/dev
/null
; then
499 export PYTHONPATH
="$top_builddir/$LIBNAME/python:$PYTHONPATH"
502 ts_skip
"py${LIBNAME} not compiled"
508 local asan_options
="strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1"
513 if [ "$TS_ENABLE_ASAN" == "yes" -o "$TS_ENABLE_UBSAN" == "yes" ]; then
515 if [ "$TS_ENABLE_ASAN" == "yes" ]; then
516 # detect_leaks isn't supported on s390x: https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/lsan/lsan_common.h
517 if [ "$(uname -m)" != "s390x" ]; then
518 asan_options
="$asan_options:detect_leaks=1"
520 args
+=(ASAN_OPTIONS
=$asan_options)
522 if [ "$TS_ENABLE_UBSAN" == "yes" ]; then
523 args
+=(UBSAN_OPTIONS
=print_stacktrace
=1:print_summary
=1:halt_on_error
=1)
530 if [ -n "$TS_VALGRIND_CMD" ]; then
531 args
+=(libtool
--mode=execute
"$TS_VALGRIND_CMD" --tool=memcheck
--leak-check=full
)
532 args
+=(--leak-resolution=high
--num-callers=20 --log-file="$TS_VGDUMP")
536 echo $?
>$TS_EXIT_CODE
539 function ts_gen_diff_from
{
545 diff -u "$expected" "$output" > "$difffile"
547 if [ $?
-ne 0 ] ||
[ -s "$difffile" ]; then
549 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
563 function ts_gen_diff
{
568 [ -f "$TS_OUTPUT" ] ||
return 1
569 [ -f "$TS_EXPECTED" ] || TS_EXPECTED
=/dev
/null
571 # remove libtool lt- prefixes
572 sed --in-place 's/^lt\-\(.*\: \)/\1/g' "$TS_OUTPUT"
573 sed --in-place 's/^lt\-\(.*\: \)/\1/g' "$TS_ERRLOG"
575 [ -d "$TS_DIFFDIR" ] || mkdir
-p "$TS_DIFFDIR"
577 # error log is fully optional
578 [ -f "$TS_EXPECTED_ERR" ] || TS_EXPECTED_ERR
=/dev
/null
579 [ -f "$TS_ERRLOG" ] || TS_ERRLOG
=/dev
/null
581 if [ "$TS_COMPONENT" != "fuzzers" ]; then
582 ts_gen_diff_from
"$TS_EXPECTED" "$TS_OUTPUT" "$TS_DIFF"
585 ts_gen_diff_from
"$TS_EXPECTED_ERR" "$TS_ERRLOG" "$TS_DIFF.err"
588 # TS_EXIT_CODE is empty when tests aren't run with ts_run: https://github.com/util-linux/util-linux/issues/1072
589 # or when ts_finalize is called right after ts_finalize_subtest.
590 exit_code
="$(cat $TS_EXIT_CODE)"
591 if [ -z "$exit_code" ]; then
595 if [ $exit_code -ne 0 ]; then
596 ts_gen_diff_from
"$TS_EXPECTED" "$TS_OUTPUT" "$TS_DIFF"
597 ts_gen_diff_from
"$TS_EXPECTED_ERR" "$TS_ERRLOG" "$TS_DIFF.err"
601 if [ $status_out -ne 0 -o $status_err -ne 0 -o $exit_code -ne 0 ]; then
607 function tt_gen_mem_report
{
608 if [ -n "$TS_VALGRIND_CMD" ]; then
609 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev
/null
610 if [ $?
-eq 0 ]; then
611 echo "mem-error detected!"
618 function ts_finalize_subtest
{
622 if [ $?
-eq 1 ]; then
623 ts_failed_subtest
"$1"
626 ts_report_ok
"$(tt_gen_mem_report "$1")"
629 [ $res -ne 0 ] && TS_NSUBFAILED
=$
(( $TS_NSUBFAILED + 1 ))
631 # reset environment back to parental test
637 function ts_skip_subtest
{
639 # reset environment back to parental test
644 function ts_finalize
{
647 if [ $TS_NSUBTESTS -ne 0 ]; then
648 if ! ts_gen_diff ||
[ $TS_NSUBFAILED -ne 0 ]; then
649 ts_failed
"$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
651 ts_ok
"all $TS_NSUBTESTS sub-tests PASSED"
655 ts_gen_diff || ts_failed
"$1"
664 function ts_cleanup_on_exit
{
666 for idx
in $
(seq 0 $
((${#TS_SUID_PROGS[*]} - 1))); do
667 PROG
=${TS_SUID_PROGS[$idx]}
668 chmod a-s
$PROG &> /dev
/null
669 chown
${TS_SUID_USER[$idx]}:${TS_SUID_GROUP[$idx]} $PROG &> /dev
/null
672 for dev
in "${TS_LOOP_DEVS[@]}"; do
673 ts_device_deinit
"$dev"
680 function ts_image_md5sum
{
681 local img
=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
682 echo $
("$TS_HELPER_MD5" < "$img") $
(basename "$img")
685 function ts_image_init
{
686 local mib
=${1:-"5"} # size in MiBs
687 local img
=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
690 truncate
-s "${mib}M" "$img"
695 function ts_register_loop_device
{
696 local ct
=${#TS_LOOP_DEVS[*]}
700 function ts_device_init
{
704 img
=$
(ts_image_init
$1 $2)
705 dev
=$
($TS_CMD_LOSETUP --show --partscan -f "$img")
706 if [ "$?" != "0" -o "$dev" = "" ]; then
707 ts_die
"Cannot init device"
710 ts_register_loop_device
"$dev"
714 # call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance
715 function ts_device_deinit
{
718 if [ -b "$DEV" ]; then
719 $TS_CMD_UMOUNT "$DEV" &> /dev
/null
720 $TS_CMD_LOSETUP -d "$DEV" &> /dev
/null
724 function ts_blkidtag_by_devname
()
731 out
=$
($TS_CMD_BLKID -p -s "$tag" -o value
"$dev")
735 test -n "$out" -a "$rval" = "0"
739 function ts_uuid_by_devname
{
740 ts_blkidtag_by_devname
"UUID" "$1"
744 function ts_label_by_devname
{
745 ts_blkidtag_by_devname
"LABEL" "$1"
749 function ts_fstype_by_devname
{
750 ts_blkidtag_by_devname
"TYPE" "$1"
754 function ts_vfs_dump
{
755 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
757 echo "{{{{ VFS dump:"
763 function ts_blk_dump
{
764 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
766 echo "{{{{ blkdevs dump:"
772 function ts_device_has
{
779 vl
=$
(ts_blkidtag_by_devname
"$TAG" "$DEV")
780 test $?
= 0 -a "$vl" = "$VAL"
783 if [ "$res" != 0 ]; then
791 # Based on __SYSMACROS_DEFINE_MAKEDEV macro
792 # defined in /usr/include/bits/sysmacros.h of GNU libc.
799 dev
=$
(( ( major
& 0x00000fff ) << 8))
800 dev=$((dev | ( major & 0xfffff000 ) << 32))
801 dev=$((dev | ( minor & 0x000000ff ) << 0))
802 dev=$((dev | ( minor & 0xffffff00 ) << 12))
806 function ts_is_uuid()
808 printf "%s\n" "$1" | grep -E -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$
'
812 function ts_udevadm_settle()
814 local dev=$1 # optional, might be empty
815 shift # all other args are tags, LABEL, UUID, ...
827 out=$($TS_CMD_MOUNT "$@" 2>&1)
829 echo -n "$out" >> $TS_OUTPUT
831 if [ $result != 0 ] \
832 && msg=$(echo "$out" | grep -m1 "unknown filesystem type")
834 # skip only if reported fs correctly and if it's not available
835 fs
=$
(echo "$msg" |
sed -n "s/.*type '\(.*\)'$/\1/p")
836 [ "$fs" = "fs_exp" ] \
837 && grep -qe "[[:space:]]${fs}$" /proc
/filesystems
&>/dev
/null \
843 function ts_is_mounted
{
844 local DEV
=$
(ts_canonicalize
"$1")
846 grep -q "\(^\| \)$DEV " /proc
/mounts
&& return 0
848 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
849 grep -q "^/dev/loop${DEV#/dev/loop/} " /proc
/mounts
&& return 0
854 function ts_fstab_open
{
855 echo "# <!-- util-linux test entry" >> "$TS_FSTAB"
858 function ts_fstab_close
{
859 echo "# -->" >> "$TS_FSTAB"
860 sync
"$TS_FSTAB" 2>/dev
/null
863 function ts_fstab_addline
{
865 local MNT
=${2:-"$TS_MOUNTPOINT"}
866 local FS
=${3:-"auto"}
867 local OPT
=${4:-"defaults"}
869 echo "$SPEC $MNT $FS $OPT 0 0" >> "$TS_FSTAB"
872 function ts_fstab_lock
{
876 function ts_fstab_add
{
883 function ts_fstab_clean
{
884 ts_have_lock
"fstab" ||
return 0
886 /# <!-- util-linux/!b
892 s/# <!-- util-linux.*-->//;
895 sync
"$TS_FSTAB" 2>/dev
/null
899 function ts_fdisk_clean
{
902 # remove non comparable parts of fdisk output
903 if [ -n "${DEVNAME}" ]; then
904 # escape "@" with "\@" in $DEVNAME. This way sed correctly
905 # replaces paths containing "@" characters
906 sed -i -e "s@${DEVNAME//\@/\\\@}@<removed>@;" $TS_OUTPUT $TS_ERRLOG
910 -e 's/Disk identifier:.*/Disk identifier: <removed>/' \
911 -e 's/Created a new partition.*/Created a new partition <removed>./' \
912 -e 's/Created a new .* disklabel .*/Created a new disklabel./' \
913 -e 's/^Device[[:blank:]]*Start/Device Start/' \
914 -e 's/^Device[[:blank:]]*Boot/Device Boot/' \
915 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \
916 -e 's/typescript file.*/typescript file <removed>./' \
917 -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \
918 $TS_OUTPUT $TS_ERRLOG
922 # https://stackoverflow.com/questions/41603787/how-to-find-next-available-file-descriptor-in-bash
923 function ts_find_free_fd
()
927 for fd
in {3.
.200}; do
928 rco
="$(true 2>/dev/null >&${fd}; echo $?)"
929 rci
="$(true 2>/dev/null <&${fd}; echo $?)"
930 if [[ "${rco}${rci}" = "11" ]]; then
938 function ts_get_lock_fd
{
942 for fd
in "${!TS_LOCKFILE_FD[@]}"; do
943 if [ "${TS_LOCKFILE_FD["$fd"]}" = "$resource" ]; then
951 function ts_have_lock
{
954 test "$TS_NOLOCKS" = "yes" && return 0
955 ts_get_lock_fd
"$resource" >/dev
/null
&& return 0
961 local lockfile
="${TS_LOCKDIR}/${resource}.lock"
964 if [ "$TS_NOLOCKS" == "yes" ]; then
969 fd
=$
(ts_get_lock_fd
"$resource")
970 if [ -n "$fd" ]; then
971 echo "[$$ $TS_TESTNAME] ${resource} already locked!"
975 fd
=$
(ts_find_free_fd
) || ts_skip
"failed to find lock fd"
977 eval "exec $fd>$lockfile"
978 flock
--exclusive "$fd" || ts_skip
"failed to lock $resource"
980 TS_LOCKFILE_FD
["$fd"]="$resource"
981 ###echo "[$$ $TS_TESTNAME] Locked $resource"
986 local lockfile
="${TS_LOCKDIR}/${resource}.lock"
989 if [ "$TS_NOLOCKS" == "yes" ]; then
993 fd
=$
(ts_get_lock_fd
"$resource")
994 if [ -n "$fd" ]; then
996 TS_LOCKFILE_FD
["$fd"]=""
997 ###echo "[$$ $TS_TESTNAME] Unlocked $resource"
999 echo "[$$ $TS_TESTNAME] unlocking unlocked $resource!?"
1003 function ts_scsi_debug_init
{
1008 ts_lock
"scsi_debug"
1010 # dry run is not really reliable, real modprobe may still fail
1011 modprobe
--dry-run --quiet scsi_debug
&>/dev
/null \
1012 || ts_skip
"missing scsi_debug module (dry-run)"
1014 # skip if still in use or removal of modules not supported at all
1015 # We don't want a slow timeout here so we don't use ts_scsi_debug_rmmod!
1016 modprobe
-r scsi_debug
&>/dev
/null
1017 if [ "$?" -eq 1 ]; then
1018 ts_unlock
"scsi_debug"
1019 ts_skip
"cannot remove scsi_debug module (rmmod)"
1022 modprobe
-b scsi_debug
"$@" &>/dev
/null \
1023 || ts_skip
"cannot load scsi_debug module (modprobe)"
1025 # it might be still not loaded, modprobe.conf or whatever
1026 lsmod
2>/dev
/null |
grep -q "^scsi_debug " \
1027 || ts_skip
"scsi_debug module not loaded (lsmod)"
1031 # wait for device if udevadm settle does not work
1032 for t
in 0 0.02 0.05 0.1 1; do
1034 devname
=$
(grep --no-messages --with-filename scsi_debug
/sys
/block
/*/device
/model
) && break
1036 [ -n "${devname}" ] || ts_skip
"timeout waiting for scsi_debug device"
1038 devname
=$
(echo $devname |
awk -F '/' '{print $4}')
1039 TS_DEVICE
="/dev/${devname}"
1041 # TODO validate that device is really up, for now just a warning on stderr
1042 test -b $TS_DEVICE ||
echo "warning: scsi_debug device is still down" >&2
1045 # automatically called once in ts_cleanup_on_exit()
1046 function ts_scsi_debug_rmmod
{
1051 # We must not run if we don't have the lock
1052 ts_have_lock
"scsi_debug" ||
return 0
1054 # Return early most importantly in case we are not root or the module does
1056 [ $UID -eq 0 ] ||
return 0
1057 [ -n "$TS_DEVICE" ] ||
return 0
1058 lsmod
2>/dev
/null |
grep -q "^scsi_debug " ||
return 0
1062 # wait for successful rmmod if udevadm settle does not work
1063 for t
in 0 0.02 0.05 0.1 1; do
1065 lastmsg
="$(modprobe -r scsi_debug 2>&1)" && err
=0 && break
1068 if [ "$err" = "1" ]; then
1069 ts_log
"rmmod failed: '$lastmsg'"
1070 ts_log
"timeout removing scsi_debug module (rmmod)"
1073 if lsmod |
grep -q "^scsi_debug "; then
1074 ts_log
"BUG! scsi_debug still loaded"
1078 # TODO Do we need to validate that all devices are gone?
1080 test -b "$TS_DEVICE" && echo "warning: scsi_debug device is still up" >&2
1082 # TODO unset TS_DEVICE, check that nobody uses it later, e.g. ts_fdisk_clean
1084 ts_unlock
"scsi_debug"
1088 function ts_resolve_host
{
1092 # currently we just resolve default records (might be "A", ipv4 only)
1093 if type "dig" >/dev
/null
2>&1; then
1094 tmp
=$
(dig "$host" +short
2>/dev
/null
) ||
return 1
1095 elif type "nslookup" >/dev
/null
2>&1; then
1096 tmp
=$
(nslookup "$host" 2>/dev
/null
) ||
return 1
1097 tmp
=$
(echo "$tmp"|
grep -A1 "^Name:"|
grep "^Address:"| cut
-d" " -f2)
1098 elif type "host" >/dev
/null
2>&1; then
1099 tmp
=$
(host "$host" 2>/dev
/null
) ||
return 1
1100 tmp
=$
(echo "$tmp" |
grep " has address " | cut
-d " " -f4)
1101 elif type "getent" >/dev
/null
2>&1; then
1102 tmp
=$
(getent ahosts
"$host" 2>/dev
/null
) ||
return 1
1103 tmp
=$
(echo "$tmp" | cut
-d " " -f 1 |
sort -u)
1106 # we return 1 if tmp is empty
1107 test -n "$tmp" ||
return 1
1108 echo "$tmp" |
sort -R |
head -n 1
1111 # listen to unix socket (background socat)
1112 function ts_init_socket_to_file
{
1117 ts_check_prog
"socat"
1118 rm -f "$socket" "$outfile"
1120 # if socat is too old for these options we'll skip it below
1121 socat
-u UNIX-LISTEN
:$socket,fork
,max-children
=1,backlog
=128 \
1122 STDOUT
> "$outfile" 2>/dev
/null
&
1125 # check for running background process
1126 if [ "$pid" -le "0" ] ||
! kill -s 0 "$pid" &>/dev
/null
; then
1127 ts_skip
"unable to run socat"
1129 # wait for the socket listener
1130 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket,retry
=30,interval
=0.1 &>/dev
/null
; then
1131 kill -9 "$pid" &>/dev
/null
1132 ts_skip
"timeout waiting for socat socket"
1134 # check socket again
1135 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket &>/dev
/null
; then
1136 kill -9 "$pid" &>/dev
/null
1137 ts_skip
"socat socket stopped listening"
1141 function ts_has_ncurses_support
{
1142 grep -q '#define HAVE_LIBNCURSES' ${top_builddir}/config.h
1143 if [ $?
== 0 ]; then
1150 # Get path to the ASan runtime DSO the given binary was compiled with
1151 function ts_get_asan_rt_path
{
1152 local binary
="${1?}"
1158 rt_path
="$(ldd "$binary" | awk '/lib.+asan.*.so/ {print $3; exit}')"
1159 if [ -n "$rt_path" -a -f "$rt_path" ]; then
1164 function ts_skip_exitcode_not_supported
{
1165 if [ $?
-eq $TS_EXIT_NOTSUPP ]; then
1166 ts_skip
"functionality not implemented by system"
1170 function ts_inhibit_custom_colorscheme
{
1171 export XDG_CONFIG_HOME
=/dev
/null
1174 function ts_is_virt
{
1175 type "systemd-detect-virt" >/dev
/null
2>&1
1176 if [ $?
-ne 0 ]; then
1180 virt
="$(systemd-detect-virt)"
1182 if [ "$virt" = "$arg" ]; then
1189 function ts_check_enosys_syscalls
{
1190 ts_check_test_command
"$TS_CMD_ENOSYS"
1191 "$TS_CMD_ENOSYS" ${@/#/-s } true
2> /dev
/null
1192 [ $?
-ne 0 ] && ts_skip
"test_enosys does not work: $*"
1195 function ts_skip_docker
{
1196 test -e /.dockerenv
&& ts_skip
"unsupported in docker environment"