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