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