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