]> git.ipfire.org Git - thirdparty/util-linux.git/blob - tests/functions.sh
docs: update github URL
[thirdparty/util-linux.git] / tests / functions.sh
1 #
2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3 #
4 # This file is part of util-linux.
5 #
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.
10 #
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.
15 #
16
17
18 function ts_abspath {
19 cd $1
20 pwd
21 }
22
23 function ts_canonicalize {
24 P="$1"
25 C=$(readlink -f $P)
26
27 if [ -n "$C" ]; then
28 echo "$C"
29 else
30 echo "$P"
31 fi
32 }
33
34 function ts_cd {
35 if [ $# -eq 0 ]; then
36 ts_failed "ul_cd: not enough arguments"
37 fi
38 DEST=$(readlink -f "$1" 2>/dev/null)
39 if [ "x$DEST" = "x" ] || [ ! -d "$DEST" ]; then
40 ts_failed "ul_cd: $1: no such directory"
41 fi
42 cd "$DEST" 2>/dev/null || ts_failed "ul_cd: $1: cannot change directory"
43 if [ "$PWD" != "$DEST" ]; then
44 ts_failed "ul_cd: $PWD is not $DEST"
45 fi
46 }
47
48 function ts_separator {
49 local header="$1"
50 echo >> $TS_OUTPUT
51 if [ -z "$header" ]; then
52 echo "============================================" >> $TS_OUTPUT
53 else
54 echo "=====$header================================" >> $TS_OUTPUT
55 fi
56 }
57
58 function ts_report {
59 local desc=
60
61 if [ "$TS_PARSABLE" != "yes" ]; then
62 if [ $TS_NSUBTESTS -ne 0 ] && [ -z "$TS_SUBNAME" ]; then
63 desc=$(printf "%11s...")
64 fi
65 echo "$desc$1"
66 return
67 fi
68
69 if [ -n "$TS_SUBNAME" ]; then
70 desc=$(printf "%s: [%02d] %s" "$TS_DESC" "$TS_NSUBTESTS" "$TS_SUBNAME")
71 else
72 desc=$TS_DESC
73 fi
74 printf "%13s: %-45s ...%s\n" "$TS_COMPONENT" "$desc" "$1"
75 }
76
77 function ts_check_test_command {
78 case "$1" in
79 */*)
80 # paths
81 if [ ! -x "$1" ]; then
82 ts_skip "${1##*/} not found"
83 fi
84 ;;
85 *)
86 # just command names (e.g. --use-system-commands)
87 local cmd=$1
88 type "$cmd" >/dev/null 2>&1
89 if [ $? -ne 0 ]; then
90 if [ "$TS_NOSKIP_COMMANDS" = "yes" ]; then
91 ts_failed "missing in PATH: $cmd"
92 fi
93 ts_skip "missing in PATH: $cmd"
94 fi
95 ;;
96 esac
97 }
98
99 function ts_check_prog {
100 local cmd=$1
101 type "$cmd" >/dev/null 2>&1 || ts_skip "missing in PATH: $cmd"
102 }
103
104 function ts_check_losetup {
105 local tmp
106 ts_check_test_command "$TS_CMD_LOSETUP"
107
108 if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then
109 ts_skip "loop-device tests disabled"
110 fi
111
112 # assuming that losetup -f works ... to be checked somewhere else
113 tmp=$($TS_CMD_LOSETUP -f 2>/dev/null)
114 if test -b "$tmp"; then
115 return 0
116 fi
117 ts_skip "no loop-device support"
118 }
119
120 function ts_report_skip {
121 ts_report " SKIPPED ($1)"
122 }
123
124 function ts_skip {
125 ts_report_skip "$1"
126
127 ts_cleanup_on_exit
128 exit 0
129 }
130
131 function ts_skip_nonroot {
132 if [ $UID -ne 0 ]; then
133 ts_skip "no root permissions"
134 fi
135 }
136
137 function ts_failed_subtest {
138 local msg="FAILED"
139 local ret=1
140 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
141 msg="KNOWN FAILED"
142 ret=0
143 fi
144
145 if [ x"$1" == x"" ]; then
146 ts_report " $msg ($TS_NS)"
147 else
148 ts_report " $msg ($1)"
149 fi
150
151 return $ret
152 }
153
154 function ts_failed {
155 ts_failed_subtest "$1"
156 exit $?
157 }
158
159 function ts_report_ok {
160 if [ x"$1" == x"" ]; then
161 ts_report " OK"
162 else
163 ts_report " OK ($1)"
164 fi
165 }
166
167 function ts_ok {
168 ts_report_ok "$1"
169 exit 0
170 }
171
172 function ts_log {
173 echo "$1" >> $TS_OUTPUT
174 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
175 }
176
177 function ts_logerr {
178 echo "$1" >> $TS_ERRLOG
179 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
180 }
181
182 function ts_log_both {
183 echo "$1" >> $TS_OUTPUT
184 echo "$1" >> $TS_ERRLOG
185 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
186 }
187
188 function ts_has_option {
189 NAME="$1"
190 ALL="$2"
191
192 # user may set options by env for a single test or whole component
193 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
194 local v_test=${TS_TESTNAME//[-.]/_}
195 local v_comp=${TS_COMPONENT//[-.]/_}
196 local v_name=${NAME//[-.]/_}
197 eval local env_opt_test=\$TS_OPT_${v_comp}_${v_test}_${v_name}
198 eval local env_opt_comp=\$TS_OPT_${v_comp}_${v_name}
199 if [ "$env_opt_test" = "yes" \
200 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
201 echo "yes"
202 return
203 elif [ "$env_opt_test" = "no" \
204 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
205 return
206 fi
207
208 # or just check the global command line options
209 if [[ $ALL =~ ([$' \t\n']|^)--$NAME([$'= \t\n']|$) ]]; then
210 echo yes
211 return
212 fi
213
214 # or the _global_ env, e.g TS_OPT_parsable="yes"
215 eval local env_opt=\$TS_OPT_${v_name}
216 if [ "$env_opt" = "yes" ]; then echo "yes"; fi
217 }
218
219 function ts_option_argument {
220 NAME="$1"
221 ALL="$2"
222
223 # last option wins!
224 echo "$ALL" | sed -n "s/.*[ \t\n]--$NAME=\([^ \t\n]*\).*/\1/p" | tail -n 1
225 }
226
227 function ts_init_core_env {
228 TS_SUBNAME=""
229 TS_NS="$TS_COMPONENT/$TS_TESTNAME"
230 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME"
231 TS_ERRLOG="$TS_OUTDIR/$TS_TESTNAME.err"
232 TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME.vgdump"
233 TS_EXIT_CODE="$TS_OUTDIR/$TS_TESTNAME.exit_code"
234 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME"
235 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
236 TS_EXPECTED_ERR="$TS_TOPDIR/expected/$TS_NS.err"
237 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-mnt"
238 }
239
240 function ts_init_core_subtest_env {
241 TS_NS="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
242 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
243 TS_ERRLOG="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.err"
244 TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
245 TS_EXIT_CODE="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.exit_code"
246 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
247 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
248 TS_EXPECTED_ERR="$TS_TOPDIR/expected/$TS_NS.err"
249 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
250
251 rm -f $TS_OUTPUT $TS_ERRLOG $TS_VGDUMP $TS_EXIT_CODE
252 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
253
254 touch $TS_OUTPUT $TS_ERRLOG $TS_EXIT_CODE
255 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
256 }
257
258 function ts_init_env {
259 local mydir=$(ts_abspath ${0%/*})
260 local tmp
261
262 LANG="POSIX"
263 LANGUAGE="POSIX"
264 LC_ALL="POSIX"
265 CHARSET="UTF-8"
266 ASAN_OPTIONS="detect_leaks=0"
267 UBSAN_OPTIONS="print_stacktrace=1:print_summary=1:halt_on_error=1"
268
269 export LANG LANGUAGE LC_ALL CHARSET ASAN_OPTIONS UBSAN_OPTIONS
270
271 mydir=$(ts_canonicalize "$mydir")
272
273 # automake directories
274 top_srcdir=$(ts_option_argument "srcdir" "$*")
275 top_builddir=$(ts_option_argument "builddir" "$*")
276
277 # where is this script
278 TS_TOPDIR=$(ts_abspath $mydir/../../)
279
280 # default
281 if [ -z "$top_srcdir" ]; then
282 top_srcdir="$TS_TOPDIR/.."
283 fi
284 if [ -z "$top_builddir" ]; then
285 top_builddir="$TS_TOPDIR/.."
286 fi
287
288 top_srcdir=$(ts_abspath $top_srcdir)
289 top_builddir=$(ts_abspath $top_builddir)
290
291 if [ -e "$top_builddir/meson.conf" ]; then
292 . "$top_builddir/meson.conf"
293 fi
294
295 # We use helpser always from build tree
296 ts_helpersdir="${top_builddir}/"
297
298 TS_USE_SYSTEM_COMMANDS=$(ts_has_option "use-system-commands" "$*")
299 if [ "$TS_USE_SYSTEM_COMMANDS" == "yes" ]; then
300 # Don't define anything, just follow current PATH
301 ts_commandsdir=""
302 else
303 # The default is to use commands from build tree
304 ts_commandsdir="${top_builddir}/"
305
306 # some ul commands search other ul commands in $PATH
307 export PATH="$ts_commandsdir:$PATH"
308 fi
309
310 TS_SCRIPT="$mydir/$(basename $0)"
311 TS_SUBDIR=$(dirname $TS_SCRIPT)
312 TS_TESTNAME=$(basename $TS_SCRIPT)
313 TS_COMPONENT=$(basename $TS_SUBDIR)
314 TS_DESC=${TS_DESC:-$TS_TESTNAME}
315
316 TS_NSUBTESTS=0
317 TS_NSUBFAILED=0
318
319 TS_SELF="$TS_SUBDIR"
320
321 TS_OUTDIR="$top_builddir/tests/output/$TS_COMPONENT"
322 TS_DIFFDIR="$top_builddir/tests/diff/$TS_COMPONENT"
323
324 TS_NOLOCKS=$(ts_has_option "nolocks" "$*")
325 TS_LOCKDIR="$top_builddir/tests/output"
326
327 # Don't lock if flock(1) is missing
328 type "flock" >/dev/null 2>&1 || TS_NOLOCKS="yes"
329
330 ts_init_core_env
331
332 TS_NOSKIP_COMMANDS=$(ts_has_option "noskip-commands" "$*")
333 TS_VERBOSE=$(ts_has_option "verbose" "$*")
334 TS_SHOWDIFF=$(ts_has_option "show-diff" "$*")
335 TS_PARALLEL=$(ts_has_option "parallel" "$*")
336 TS_KNOWN_FAIL=$(ts_has_option "known-fail" "$*")
337 TS_SKIP_LOOPDEVS=$(ts_has_option "skip-loopdevs" "$*")
338 TS_PARSABLE=$(ts_has_option "parsable" "$*")
339 [ "$TS_PARSABLE" = "yes" ] || TS_PARSABLE="$TS_PARALLEL"
340
341 tmp=$( ts_has_option "memcheck-valgrind" "$*")
342 if [ "$tmp" == "yes" -a -f /usr/bin/valgrind ]; then
343 TS_VALGRIND_CMD="/usr/bin/valgrind"
344 fi
345 tmp=$( ts_has_option "memcheck-asan" "$*")
346 if [ "$tmp" == "yes" ]; then
347 TS_ENABLE_ASAN="yes"
348 fi
349 tmp=$( ts_has_option "memcheck-ubsan" "$*")
350 if [ "$tmp" == "yes" ]; then
351 TS_ENABLE_UBSAN="yes"
352 fi
353
354 BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
355
356 declare -a TS_SUID_PROGS
357 declare -a TS_SUID_USER
358 declare -a TS_SUID_GROUP
359 declare -a TS_LOOP_DEVS
360 declare -a TS_LOCKFILE_FD
361
362 if [ -f $TS_TOPDIR/commands.sh ]; then
363 . $TS_TOPDIR/commands.sh
364 fi
365
366 export BLKID_FILE
367
368 rm -f $TS_OUTPUT $TS_ERRLOG $TS_VGDUMP $TS_EXIT_CODE
369 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
370
371 touch $TS_OUTPUT $TS_ERRLOG $TS_EXIT_CODE
372 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
373
374 if [ "$TS_VERBOSE" == "yes" ]; then
375 echo
376 echo " script: $TS_SCRIPT"
377 echo " commands: $ts_commandsdir"
378 echo " helpers: $ts_helpersdir"
379 echo " sub dir: $TS_SUBDIR"
380 echo " top dir: $TS_TOPDIR"
381 echo " self: $TS_SELF"
382 echo " test name: $TS_TESTNAME"
383 echo " test desc: $TS_DESC"
384 echo " component: $TS_COMPONENT"
385 echo " namespace: $TS_NS"
386 echo " verbose: $TS_VERBOSE"
387 echo " output: $TS_OUTPUT"
388 echo " error log: $TS_ERRLOG"
389 echo " exit code: $TS_EXIT_CODE"
390 echo " valgrind: $TS_VGDUMP"
391 echo " expected: $TS_EXPECTED{.err}"
392 echo " mountpoint: $TS_MOUNTPOINT"
393 echo
394 fi
395 }
396
397 function ts_init_subtest {
398
399 TS_SUBNAME="$1"
400 ts_init_core_subtest_env
401 TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 ))
402
403 if [ "$TS_PARSABLE" != "yes" ]; then
404 [ $TS_NSUBTESTS -eq 1 ] && echo
405 printf "%16s: %-27s ..." "" "$TS_SUBNAME"
406 fi
407 }
408
409 function ts_init {
410 ts_init_env "$*"
411
412 local is_fake=$( ts_has_option "fake" "$*")
413 local is_force=$( ts_has_option "force" "$*")
414
415 if [ "$TS_PARSABLE" != "yes" ]; then
416 printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
417 fi
418
419 [ "$is_fake" == "yes" ] && ts_skip "fake mode"
420 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip "optional"
421 }
422
423 function ts_init_suid {
424 PROG="$1"
425 ct=${#TS_SUID_PROGS[*]}
426
427 # Save info about original setting
428 TS_SUID_PROGS[$ct]=$PROG
429 TS_SUID_USER[$ct]=$(stat --printf="%U" $PROG)
430 TS_SUID_GROUP[$ct]=$(stat --printf="%G" $PROG)
431
432 chown root.root $PROG &> /dev/null
433 chmod u+s $PROG &> /dev/null
434 }
435
436 function ts_init_py {
437 LIBNAME="$1"
438
439 if [ -f "$top_builddir/py${LIBNAME}.la" ]; then
440 # autotoolz build
441 export LD_LIBRARY_PATH="$top_builddir/.libs:$LD_LIBRARY_PATH"
442 export PYTHONPATH="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
443
444 PYTHON_VERSION=$(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile)
445 PYTHON_MAJOR_VERSION=$(echo $PYTHON_VERSION | sed 's/\..*//')
446
447 export PYTHON="python${PYTHON_MAJOR_VERSION}"
448
449 elif compgen -G "$top_builddir/$LIBNAME/python/py$LIBNAME*.so" >/dev/null; then
450 # mezon!
451 export PYTHONPATH="$top_builddir/$LIBNAME/python:$PYTHONPATH"
452
453 else
454 ts_skip "py${LIBNAME} not compiled"
455 fi
456 }
457
458 function ts_run {
459 declare -a args
460 local asan_options="strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1"
461
462 #
463 # ASAN mode
464 #
465 if [ "$TS_ENABLE_ASAN" == "yes" -o "$TS_ENABLE_UBSAN" == "yes" ]; then
466 args+=(env)
467 if [ "$TS_ENABLE_ASAN" == "yes" ]; then
468 # detect_leaks isn't supported on s390x: https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/lsan/lsan_common.h
469 if [ "$(uname -m)" != "s390x" ]; then
470 asan_options="$asan_options:detect_leaks=1"
471 fi
472 args+=(ASAN_OPTIONS=$asan_options)
473 fi
474 if [ "$TS_ENABLE_UBSAN" == "yes" ]; then
475 args+=(UBSAN_OPTIONS=print_stacktrace=1:print_summary=1:halt_on_error=1)
476 fi
477 fi
478
479 #
480 # valgrind mode
481 #
482 if [ -n "$TS_VALGRIND_CMD" ]; then
483 args+=(libtool --mode=execute "$TS_VALGRIND_CMD" --tool=memcheck --leak-check=full)
484 args+=(--leak-resolution=high --num-callers=20 --log-file="$TS_VGDUMP")
485 fi
486
487 "${args[@]}" "$@"
488 echo $? >$TS_EXIT_CODE
489 }
490
491 function ts_gen_diff_from {
492 local res=0
493 local expected="$1"
494 local output="$2"
495 local difffile="$3"
496
497 diff -u $expected $output > $difffile
498
499 if [ $? -ne 0 ] || [ -s $difffile ]; then
500 res=1
501 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
502 echo
503 echo "diff-{{{"
504 cat $difffile
505 echo "}}}-diff"
506 echo
507 fi
508 else
509 rm -f $difffile;
510 fi
511
512 return $res
513 }
514
515 function ts_gen_diff {
516 local status_out=0
517 local status_err=0
518 local exit_code=0
519
520 [ -f "$TS_OUTPUT" ] || return 1
521 [ -f "$TS_EXPECTED" ] || TS_EXPECTED=/dev/null
522
523 # remove libtool lt- prefixes
524 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
525 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_ERRLOG
526
527 [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR"
528
529 # error log is fully optional
530 [ -f "$TS_EXPECTED_ERR" ] || TS_EXPECTED_ERR=/dev/null
531 [ -f "$TS_ERRLOG" ] || TS_ERRLOG=/dev/null
532
533 if [ "$TS_COMPONENT" != "fuzzers" ]; then
534 ts_gen_diff_from $TS_EXPECTED $TS_OUTPUT $TS_DIFF
535 status_out=$?
536
537 ts_gen_diff_from $TS_EXPECTED_ERR $TS_ERRLOG $TS_DIFF.err
538 status_err=$?
539 else
540 # TS_EXIT_CODE is empty when tests aren't run with ts_run: https://github.com/util-linux/util-linux/issues/1072
541 # or when ts_finalize is called right after ts_finalize_subtest.
542 exit_code="$(cat $TS_EXIT_CODE)"
543 if [ -z "$exit_code" ]; then
544 exit_code=0
545 fi
546
547 if [ $exit_code -ne 0 ]; then
548 ts_gen_diff_from $TS_EXPECTED $TS_OUTPUT $TS_DIFF
549 ts_gen_diff_from $TS_EXPECTED_ERR $TS_ERRLOG $TS_DIFF.err
550 fi
551 fi
552
553 if [ $status_out -ne 0 -o $status_err -ne 0 -o $exit_code -ne 0 ]; then
554 return 1
555 fi
556 return 0
557 }
558
559 function tt_gen_mem_report {
560 if [ -n "$TS_VALGRIND_CMD" ]; then
561 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev/null
562 if [ $? -eq 0 ]; then
563 echo "mem-error detected!"
564 fi
565 else
566 echo "$1"
567 fi
568 }
569
570 function ts_finalize_subtest {
571 local res=0
572
573 ts_gen_diff
574 if [ $? -eq 1 ]; then
575 ts_failed_subtest "$1"
576 res=1
577 else
578 ts_report_ok "$(tt_gen_mem_report "$1")"
579 fi
580
581 [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 ))
582
583 # reset environment back to parental test
584 ts_init_core_env
585
586 return $res
587 }
588
589 function ts_skip_subtest {
590 ts_report_skip "$1"
591 # reset environment back to parental test
592 ts_init_core_env
593
594 }
595
596 function ts_finalize {
597 ts_cleanup_on_exit
598
599 if [ $TS_NSUBTESTS -ne 0 ]; then
600 if ! ts_gen_diff || [ $TS_NSUBFAILED -ne 0 ]; then
601 ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
602 else
603 ts_ok "all $TS_NSUBTESTS sub-tests PASSED"
604 fi
605 fi
606
607 ts_gen_diff || ts_failed "$1"
608 ts_ok "$1"
609 }
610
611 function ts_die {
612 ts_log "$1"
613 ts_finalize
614 }
615
616 function ts_cleanup_on_exit {
617
618 for idx in $(seq 0 $((${#TS_SUID_PROGS[*]} - 1))); do
619 PROG=${TS_SUID_PROGS[$idx]}
620 chmod a-s $PROG &> /dev/null
621 chown ${TS_SUID_USER[$idx]}.${TS_SUID_GROUP[$idx]} $PROG &> /dev/null
622 done
623
624 for dev in "${TS_LOOP_DEVS[@]}"; do
625 ts_device_deinit "$dev"
626 done
627 unset TS_LOOP_DEVS
628
629 ts_scsi_debug_rmmod
630 }
631
632 function ts_image_md5sum {
633 local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
634 echo $("$TS_HELPER_MD5" < "$img") $(basename "$img")
635 }
636
637 function ts_image_init {
638 local mib=${1:-"5"} # size in MiBs
639 local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
640
641 rm -f $img
642 truncate -s "${mib}M" "$img"
643 echo "$img"
644 return 0
645 }
646
647 function ts_register_loop_device {
648 local ct=${#TS_LOOP_DEVS[*]}
649 TS_LOOP_DEVS[$ct]=$1
650 }
651
652 function ts_device_init {
653 local img
654 local dev
655
656 img=$(ts_image_init $1 $2)
657 dev=$($TS_CMD_LOSETUP --show -f "$img")
658 if [ "$?" != "0" -o "$dev" = "" ]; then
659 ts_die "Cannot init device"
660 fi
661
662 ts_register_loop_device "$dev"
663 TS_LODEV=$dev
664 }
665
666 # call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance
667 function ts_device_deinit {
668 local DEV="$1"
669
670 if [ -b "$DEV" ]; then
671 $TS_CMD_UMOUNT "$DEV" &> /dev/null
672 $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
673 fi
674 }
675
676 function ts_blkidtag_by_devname()
677 {
678 local tag=$1
679 local dev=$2
680 local out
681 local rval
682
683 out=$($TS_CMD_BLKID -p -s "$tag" -o value "$dev")
684 rval=$?
685 printf "%s\n" "$out"
686
687 test -n "$out" -a "$rval" = "0"
688 return $?
689 }
690
691 function ts_uuid_by_devname {
692 ts_blkidtag_by_devname "UUID" "$1"
693 return $?
694 }
695
696 function ts_label_by_devname {
697 ts_blkidtag_by_devname "LABEL" "$1"
698 return $?
699 }
700
701 function ts_fstype_by_devname {
702 ts_blkidtag_by_devname "TYPE" "$1"
703 return $?
704 }
705
706 function ts_vfs_dump {
707 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
708 echo
709 echo "{{{{ VFS dump:"
710 findmnt
711 echo "}}}}"
712 fi
713 }
714
715 function ts_blk_dump {
716 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
717 echo
718 echo "{{{{ blkdevs dump:"
719 lsblk -o+FSTYPE
720 echo "}}}}"
721 fi
722 }
723
724 function ts_device_has {
725 local TAG="$1"
726 local VAL="$2"
727 local DEV="$3"
728 local vl=""
729 local res=""
730
731 vl=$(ts_blkidtag_by_devname "$TAG" "$DEV")
732 test $? = 0 -a "$vl" = "$VAL"
733 res=$?
734
735 if [ "$res" != 0 ]; then
736 ts_vfs_dump
737 ts_blk_dump
738 fi
739
740 return $res
741 }
742
743 function ts_is_uuid()
744 {
745 printf "%s\n" "$1" | egrep -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$'
746 return $?
747 }
748
749 function ts_udevadm_settle()
750 {
751 local dev=$1 # optional, might be empty
752 shift # all other args are tags, LABEL, UUID, ...
753 udevadm settle
754 }
755
756 function ts_mount {
757 local out
758 local result
759 local msg
760 local fs
761 local fs_exp=$1
762 shift
763
764 out=$($TS_CMD_MOUNT "$@" 2>&1)
765 result=$?
766 echo -n "$out" >> $TS_OUTPUT
767
768 if [ $result != 0 ] \
769 && msg=$(echo "$out" | grep -m1 "unknown filesystem type")
770 then
771 # skip only if reported fs correctly and if it's not available
772 fs=$(echo "$msg" | sed -n "s/.*type '\(.*\)'$/\1/p")
773 [ "$fs" = "fs_exp" ] \
774 && grep -qe "[[:space:]]${fs}$" /proc/filesystems &>/dev/null \
775 || ts_skip "$msg"
776 fi
777 return $result
778 }
779
780 function ts_is_mounted {
781 local DEV=$(ts_canonicalize "$1")
782
783 grep -q "\(^\| \)$DEV " /proc/mounts && return 0
784
785 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
786 grep -q "^/dev/loop${DEV#/dev/loop/} " /proc/mounts && return 0
787 fi
788 return 1
789 }
790
791 function ts_fstab_open {
792 echo "# <!-- util-linux test entry" >> /etc/fstab
793 }
794
795 function ts_fstab_close {
796 echo "# -->" >> /etc/fstab
797 sync /etc/fstab 2>/dev/null
798 }
799
800 function ts_fstab_addline {
801 local SPEC="$1"
802 local MNT=${2:-"$TS_MOUNTPOINT"}
803 local FS=${3:-"auto"}
804 local OPT=${4:-"defaults"}
805
806 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc/fstab
807 }
808
809 function ts_fstab_lock {
810 ts_lock "fstab"
811 }
812
813 function ts_fstab_add {
814 ts_fstab_lock
815 ts_fstab_open
816 ts_fstab_addline $*
817 ts_fstab_close
818 }
819
820 function ts_fstab_clean {
821 ts_have_lock "fstab" || return 0
822 sed --in-place "
823 /# <!-- util-linux/!b
824 :a
825 /# -->/!{
826 N
827 ba
828 }
829 s/# <!-- util-linux.*-->//;
830 /^$/d" /etc/fstab
831
832 sync /etc/fstab 2>/dev/null
833 ts_unlock "fstab"
834 }
835
836 function ts_fdisk_clean {
837 local DEVNAME=$1
838
839 # remove non comparable parts of fdisk output
840 if [ -n "${DEVNAME}" ]; then
841 sed -i -e "s@${DEVNAME}@<removed>@;" $TS_OUTPUT $TS_ERRLOG
842 fi
843
844 sed -i \
845 -e 's/Disk identifier:.*/Disk identifier: <removed>/' \
846 -e 's/Created a new partition.*/Created a new partition <removed>./' \
847 -e 's/Created a new .* disklabel .*/Created a new disklabel./' \
848 -e 's/^Device[[:blank:]]*Start/Device Start/' \
849 -e 's/^Device[[:blank:]]*Boot/Device Boot/' \
850 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \
851 -e 's/typescript file.*/typescript file <removed>./' \
852 -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \
853 $TS_OUTPUT $TS_ERRLOG
854 }
855
856
857 # https://stackoverflow.com/questions/41603787/how-to-find-next-available-file-descriptor-in-bash
858 function ts_find_free_fd()
859 {
860 local rco
861 local rci
862 for fd in {3..200}; do
863 rco="$(true 2>/dev/null >&${fd}; echo $?)"
864 rci="$(true 2>/dev/null <&${fd}; echo $?)"
865 if [[ "${rco}${rci}" = "11" ]]; then
866 echo "$fd"
867 return 0
868 fi
869 done
870 return 1
871 }
872
873 function ts_get_lock_fd {
874 local resource=$1
875 local fd
876
877 for fd in "${!TS_LOCKFILE_FD[@]}"; do
878 if [ "${TS_LOCKFILE_FD["$fd"]}" = "$resource" ]; then
879 echo "$fd"
880 return 0
881 fi
882 done
883 return 1
884 }
885
886 function ts_have_lock {
887 local resource=$1
888
889 test "$TS_NOLOCKS" = "yes" && return 0
890 ts_get_lock_fd "$resource" >/dev/null && return 0
891 return 1
892 }
893
894 function ts_lock {
895 local resource="$1"
896 local lockfile="${TS_LOCKDIR}/${resource}.lock"
897 local fd
898
899 if [ "$TS_NOLOCKS" == "yes" ]; then
900 return 0
901 fi
902
903 # Don't lock again
904 fd=$(ts_get_lock_fd "$resource")
905 if [ -n "$fd" ]; then
906 echo "[$$ $TS_TESTNAME] ${resource} already locked!"
907 return 0
908 fi
909
910 fd=$(ts_find_free_fd) || ts_skip "failed to find lock fd"
911
912 eval "exec $fd>$lockfile"
913 flock --exclusive "$fd" || ts_skip "failed to lock $resource"
914
915 TS_LOCKFILE_FD["$fd"]="$resource"
916 ###echo "[$$ $TS_TESTNAME] Locked $resource"
917 }
918
919 function ts_unlock {
920 local resource="$1"
921 local lockfile="${TS_LOCKDIR}/${resource}.lock"
922 local fd
923
924 if [ "$TS_NOLOCKS" == "yes" ]; then
925 return 0
926 fi
927
928 fd=$(ts_get_lock_fd "$resource")
929 if [ -n "$fd" ]; then
930 eval "exec $fd<&-"
931 TS_LOCKFILE_FD["$fd"]=""
932 ###echo "[$$ $TS_TESTNAME] Unlocked $resource"
933 else
934 echo "[$$ $TS_TESTNAME] unlocking unlocked $resource!?"
935 fi
936 }
937
938 function ts_scsi_debug_init {
939 local devname
940 local t
941 TS_DEVICE="none"
942
943 ts_lock "scsi_debug"
944
945 # dry run is not really reliable, real modprobe may still fail
946 modprobe --dry-run --quiet scsi_debug &>/dev/null \
947 || ts_skip "missing scsi_debug module (dry-run)"
948
949 # skip if still in use or removal of modules not supported at all
950 # We don't want a slow timeout here so we don't use ts_scsi_debug_rmmod!
951 modprobe -r scsi_debug &>/dev/null
952 if [ "$?" -eq 1 ]; then
953 ts_unlock "scsi_debug"
954 ts_skip "cannot remove scsi_debug module (rmmod)"
955 fi
956
957 modprobe -b scsi_debug "$@" &>/dev/null \
958 || ts_skip "cannot load scsi_debug module (modprobe)"
959
960 # it might be still not loaded, modprobe.conf or whatever
961 lsmod 2>/dev/null | grep -q "^scsi_debug " \
962 || ts_skip "scsi_debug module not loaded (lsmod)"
963
964 udevadm settle
965
966 # wait for device if udevadm settle does not work
967 for t in 0 0.02 0.05 0.1 1; do
968 sleep $t
969 devname=$(grep --no-messages --with-filename scsi_debug /sys/block/*/device/model) && break
970 done
971 [ -n "${devname}" ] || ts_skip "timeout waiting for scsi_debug device"
972
973 devname=$(echo $devname | awk -F '/' '{print $4}')
974 TS_DEVICE="/dev/${devname}"
975
976 # TODO validate that device is really up, for now just a warning on stderr
977 test -b $TS_DEVICE || echo "warning: scsi_debug device is still down" >&2
978 }
979
980 # automatically called once in ts_cleanup_on_exit()
981 function ts_scsi_debug_rmmod {
982 local err=1
983 local t
984 local lastmsg
985
986 # We must not run if we don't have the lock
987 ts_have_lock "scsi_debug" || return 0
988
989 # Return early most importantly in case we are not root or the module does
990 # not exist at all.
991 [ $UID -eq 0 ] || return 0
992 [ -n "$TS_DEVICE" ] || return 0
993 lsmod 2>/dev/null | grep -q "^scsi_debug " || return 0
994
995 udevadm settle
996
997 # wait for successful rmmod if udevadm settle does not work
998 for t in 0 0.02 0.05 0.1 1; do
999 sleep $t
1000 lastmsg="$(modprobe -r scsi_debug 2>&1)" && err=0 && break
1001 done
1002
1003 if [ "$err" = "1" ]; then
1004 ts_log "rmmod failed: '$lastmsg'"
1005 ts_log "timeout removing scsi_debug module (rmmod)"
1006 return 1
1007 fi
1008 if lsmod | grep -q "^scsi_debug "; then
1009 ts_log "BUG! scsi_debug still loaded"
1010 return 1
1011 fi
1012
1013 # TODO Do we need to validate that all devices are gone?
1014 udevadm settle
1015 test -b "$TS_DEVICE" && echo "warning: scsi_debug device is still up" >&2
1016
1017 # TODO unset TS_DEVICE, check that nobody uses it later, e.g. ts_fdisk_clean
1018
1019 ts_unlock "scsi_debug"
1020 return 0
1021 }
1022
1023 function ts_resolve_host {
1024 local host="$1"
1025 local tmp
1026
1027 # currently we just resolve default records (might be "A", ipv4 only)
1028 if type "dig" >/dev/null 2>&1; then
1029 tmp=$(dig "$host" +short 2>/dev/null) || return 1
1030 elif type "nslookup" >/dev/null 2>&1; then
1031 tmp=$(nslookup "$host" 2>/dev/null) || return 1
1032 tmp=$(echo "$tmp"| grep -A1 "^Name:"| grep "^Address:"| cut -d" " -f2)
1033 elif type "host" >/dev/null 2>&1; then
1034 tmp=$(host "$host" 2>/dev/null) || return 1
1035 tmp=$(echo "$tmp" | grep " has address " | cut -d " " -f4)
1036 elif type "getent" >/dev/null 2>&1; then
1037 tmp=$(getent ahosts "$host" 2>/dev/null) || return 1
1038 tmp=$(echo "$tmp" | cut -d " " -f 1 | sort -u)
1039 fi
1040
1041 # we return 1 if tmp is empty
1042 test -n "$tmp" || return 1
1043 echo "$tmp" | sort -R | head -n 1
1044 }
1045
1046 # listen to unix socket (background socat)
1047 function ts_init_socket_to_file {
1048 local socket=$1
1049 local outfile=$2
1050 local pid="0"
1051
1052 ts_check_prog "socat"
1053 rm -f "$socket" "$outfile"
1054
1055 # if socat is too old for these options we'll skip it below
1056 socat -u UNIX-LISTEN:$socket,fork,max-children=1,backlog=128 \
1057 STDOUT > "$outfile" 2>/dev/null &
1058 pid=$!
1059
1060 # check for running background process
1061 if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then
1062 ts_skip "unable to run socat"
1063 fi
1064 # wait for the socket listener
1065 if ! socat -u /dev/null UNIX-CONNECT:$socket,retry=30,interval=0.1 &>/dev/null; then
1066 kill -9 "$pid" &>/dev/null
1067 ts_skip "timeout waiting for socat socket"
1068 fi
1069 # check socket again
1070 if ! socat -u /dev/null UNIX-CONNECT:$socket &>/dev/null; then
1071 kill -9 "$pid" &>/dev/null
1072 ts_skip "socat socket stopped listening"
1073 fi
1074 }
1075
1076 function ts_has_mtab_support {
1077 grep -q '#define USE_LIBMOUNT_SUPPORT_MTAB' ${top_builddir}/config.h
1078 if [ $? == 0 ]; then
1079 echo "yes"
1080 else
1081 echo "no"
1082 fi
1083 }
1084
1085 function ts_has_ncurses_support {
1086 grep -q '#define HAVE_LIBNCURSES' ${top_builddir}/config.h
1087 if [ $? == 0 ]; then
1088 echo "yes"
1089 else
1090 echo "no"
1091 fi
1092 }
1093
1094 # Get path to the ASan runtime DSO the given binary was compiled with
1095 function ts_get_asan_rt_path {
1096 local binary="${1?}"
1097 local rt_path
1098
1099 ts_check_prog "ldd"
1100 ts_check_prog "awk"
1101
1102 rt_path="$(ldd "$binary" | awk '/lib.+asan.*.so/ {print $3; exit}')"
1103 if [ -n "$rt_path" -a -f "$rt_path" ]; then
1104 echo "$rt_path"
1105 fi
1106 }