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