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