]> git.ipfire.org Git - thirdparty/util-linux.git/blob - tests/functions.sh
Merge branch 'travis-osx' of https://github.com/rudimeier/util-linux
[thirdparty/util-linux.git] / tests / functions.sh
1 #
2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3 #
4 # This file is part of util-linux.
5 #
6 # This file is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This file is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16
17
18 function ts_abspath {
19 cd $1
20 pwd
21 }
22
23 function ts_canonicalize {
24 P="$1"
25 C=$(readlink -f $P)
26
27 if [ -n "$C" ]; then
28 echo "$C"
29 else
30 echo "$P"
31 fi
32 }
33
34 function ts_cd {
35 if [ $# -eq 0 ]; then
36 ts_failed "ul_cd: not enough arguments"
37 fi
38 DEST=$(readlink -f "$1" 2>/dev/null)
39 if [ "x$DEST" = "x" ] || [ ! -d "$DEST" ]; then
40 ts_failed "ul_cd: $1: no such directory"
41 fi
42 cd "$DEST" 2>/dev/null || ts_failed "ul_cd: $1: cannot change directory"
43 if [ "$PWD" != "$DEST" ]; then
44 ts_failed "ul_cd: $PWD is not $DEST"
45 fi
46 }
47
48 function ts_report {
49 local desc=
50
51 if [ "$TS_PARSABLE" != "yes" ]; then
52 if [ $TS_NSUBTESTS -ne 0 ] && [ -z "$TS_SUBNAME" ]; then
53 desc=$(printf "%11s...")
54 fi
55 echo "$desc$1"
56 return
57 fi
58
59 if [ -n "$TS_SUBNAME" ]; then
60 desc=$(printf "%s: [%02d] %s" "$TS_DESC" "$TS_NSUBTESTS" "$TS_SUBNAME")
61 else
62 desc=$TS_DESC
63 fi
64 printf "%13s: %-45s ...%s\n" "$TS_COMPONENT" "$desc" "$1"
65 }
66
67 function ts_check_test_command {
68 if [ ! -x "$1" ]; then
69 ts_skip "${1##*/} not found"
70 fi
71 }
72
73 function ts_check_prog {
74 local cmd=$1
75 type "$cmd" >/dev/null 2>&1 || ts_skip "missing in PATH: $cmd"
76 }
77
78 function ts_check_losetup {
79 local tmp
80 ts_check_test_command "$TS_CMD_LOSETUP"
81
82 if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then
83 ts_skip "loop-device tests disabled"
84 fi
85
86 # assuming that losetup -f works ... to be checked somewhere else
87 tmp=$($TS_CMD_LOSETUP -f 2>/dev/null)
88 if test -b "$tmp"; then
89 return 0
90 fi
91 ts_skip "no loop-device support"
92 }
93
94 function ts_skip_subtest {
95 ts_report " SKIPPED ($1)"
96 }
97
98 function ts_skip {
99 ts_skip_subtest "$1"
100
101 ts_cleanup_on_exit
102 exit 0
103 }
104
105 function ts_skip_nonroot {
106 if [ $UID -ne 0 ]; then
107 ts_skip "no root permissions"
108 fi
109 }
110
111 function ts_failed_subtest {
112 local msg="FAILED"
113 local ret=1
114 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
115 msg="KNOWN FAILED"
116 ret=0
117 fi
118
119 if [ x"$1" == x"" ]; then
120 ts_report " $msg ($TS_NS)"
121 else
122 ts_report " $msg ($1)"
123 fi
124
125 return $ret
126 }
127
128 function ts_failed {
129 ts_failed_subtest "$1"
130 exit $?
131 }
132
133 function ts_ok_subtest {
134 if [ x"$1" == x"" ]; then
135 ts_report " OK"
136 else
137 ts_report " OK ($1)"
138 fi
139 }
140
141 function ts_ok {
142 ts_ok_subtest "$1"
143 exit 0
144 }
145
146 function ts_log {
147 echo "$1" >> $TS_OUTPUT
148 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
149 }
150
151 function ts_has_option {
152 NAME="$1"
153 ALL="$2"
154
155 # user may set options by env for a single test or whole component
156 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
157 local v_test=${TS_TESTNAME//[-.]/_}
158 local v_comp=${TS_COMPONENT//[-.]/_}
159 local v_name=${NAME//[-.]/_}
160 eval local env_opt_test=\$TS_OPT_${v_comp}_${v_test}_${v_name}
161 eval local env_opt_comp=\$TS_OPT_${v_comp}_${v_name}
162 if [ "$env_opt_test" = "yes" \
163 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
164 echo "yes"
165 return
166 elif [ "$env_opt_test" = "no" \
167 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
168 return
169 fi
170
171 # or just check the global command line options
172 if [[ $ALL =~ ([$' \t\n']|^)--$NAME([$'= \t\n']|$) ]]; then
173 echo yes
174 return
175 fi
176
177 # or the _global_ env, e.g TS_OPT_parsable="yes"
178 eval local env_opt=\$TS_OPT_${v_name}
179 if [ "$env_opt" = "yes" ]; then echo "yes"; fi
180 }
181
182 function ts_option_argument {
183 NAME="$1"
184 ALL="$2"
185
186 # last option wins!
187 echo "$ALL" | sed -n "s/.*[ \t\n]--$NAME=\([^ \t\n]*\).*/\1/p" | tail -n 1
188 }
189
190 function ts_init_core_env {
191 TS_SUBNAME=""
192 TS_NS="$TS_COMPONENT/$TS_TESTNAME"
193 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME"
194 TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME.vgdump"
195 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME"
196 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
197 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-mnt"
198 }
199
200 function ts_init_core_subtest_env {
201 TS_NS="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
202 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
203 TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
204 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
205 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
206 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
207
208 rm -f $TS_OUTPUT $TS_VGDUMP
209 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
210
211 touch $TS_OUTPUT
212 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
213 }
214
215 function ts_init_env {
216 local mydir=$(ts_abspath ${0%/*})
217 local tmp
218
219 LANG="POSIX"
220 LANGUAGE="POSIX"
221 LC_ALL="POSIX"
222 CHARSET="UTF-8"
223
224 export LANG LANGUAGE LC_ALL CHARSET
225
226 mydir=$(ts_canonicalize "$mydir")
227
228 # automake directories
229 top_srcdir=$(ts_option_argument "srcdir" "$*")
230 top_builddir=$(ts_option_argument "builddir" "$*")
231
232 # where is this script
233 TS_TOPDIR=$(ts_abspath $mydir/../../)
234
235 # default
236 if [ -z "$top_srcdir" ]; then
237 top_srcdir="$TS_TOPDIR/.."
238 fi
239 if [ -z "$top_builddir" ]; then
240 top_builddir="$TS_TOPDIR/.."
241 fi
242
243 top_srcdir=$(ts_abspath $top_srcdir)
244 top_builddir=$(ts_abspath $top_builddir)
245
246 # some ul commands search other ul commands in $PATH
247 export PATH="$top_builddir:$PATH"
248
249 TS_SCRIPT="$mydir/$(basename $0)"
250 TS_SUBDIR=$(dirname $TS_SCRIPT)
251 TS_TESTNAME=$(basename $TS_SCRIPT)
252 TS_COMPONENT=$(basename $TS_SUBDIR)
253 TS_DESC=${TS_DESC:-$TS_TESTNAME}
254
255 TS_NSUBTESTS=0
256 TS_NSUBFAILED=0
257
258 TS_SELF="$TS_SUBDIR"
259
260 TS_OUTDIR="$top_builddir/tests/output/$TS_COMPONENT"
261 TS_DIFFDIR="$top_builddir/tests/diff/$TS_COMPONENT"
262
263 ts_init_core_env
264
265 TS_VERBOSE=$(ts_has_option "verbose" "$*")
266 TS_PARALLEL=$(ts_has_option "parallel" "$*")
267 TS_KNOWN_FAIL=$(ts_has_option "known-fail" "$*")
268 TS_SKIP_LOOPDEVS=$(ts_has_option "skip-loopdevs" "$*")
269 TS_PARSABLE=$(ts_has_option "parsable" "$*")
270 [ "$TS_PARSABLE" = "yes" ] || TS_PARSABLE="$TS_PARALLEL"
271
272 tmp=$( ts_has_option "memcheck" "$*")
273 if [ "$tmp" == "yes" -a -f /usr/bin/valgrind ]; then
274 TS_VALGRIND_CMD="/usr/bin/valgrind"
275 fi
276
277 BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
278
279 declare -a TS_SUID_PROGS
280 declare -a TS_SUID_USER
281 declare -a TS_SUID_GROUP
282 declare -a TS_LOOP_DEVS
283
284 if [ -f $TS_TOPDIR/commands.sh ]; then
285 . $TS_TOPDIR/commands.sh
286 fi
287
288 export BLKID_FILE
289
290 rm -f $TS_OUTPUT $TS_VGDUMP
291 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
292
293 touch $TS_OUTPUT
294 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
295
296 if [ "$TS_VERBOSE" == "yes" ]; then
297 echo
298 echo " script: $TS_SCRIPT"
299 echo " sub dir: $TS_SUBDIR"
300 echo " top dir: $TS_TOPDIR"
301 echo " self: $TS_SELF"
302 echo " test name: $TS_TESTNAME"
303 echo " test desc: $TS_DESC"
304 echo " component: $TS_COMPONENT"
305 echo " namespace: $TS_NS"
306 echo " verbose: $TS_VERBOSE"
307 echo " output: $TS_OUTPUT"
308 echo " valgrind: $TS_VGDUMP"
309 echo " expected: $TS_EXPECTED"
310 echo " mountpoint: $TS_MOUNTPOINT"
311 echo
312 fi
313 }
314
315 function ts_init_subtest {
316
317 TS_SUBNAME="$1"
318 ts_init_core_subtest_env
319 TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 ))
320
321 if [ "$TS_PARSABLE" != "yes" ]; then
322 [ $TS_NSUBTESTS -eq 1 ] && echo
323 printf "%16s: %-27s ..." "" "$TS_SUBNAME"
324 fi
325 }
326
327 function ts_init {
328 ts_init_env "$*"
329
330 local is_fake=$( ts_has_option "fake" "$*")
331 local is_force=$( ts_has_option "force" "$*")
332
333 if [ "$TS_PARSABLE" != "yes" ]; then
334 printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
335 fi
336
337 [ "$is_fake" == "yes" ] && ts_skip "fake mode"
338 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip "optional"
339 }
340
341 function ts_init_suid {
342 PROG="$1"
343 ct=${#TS_SUID_PROGS[*]}
344
345 # Save info about original setting
346 TS_SUID_PROGS[$ct]=$PROG
347 TS_SUID_USER[$ct]=$(stat --printf="%U" $PROG)
348 TS_SUID_GROUP[$ct]=$(stat --printf="%G" $PROG)
349
350 chown root.root $PROG &> /dev/null
351 chmod u+s $PROG &> /dev/null
352 }
353
354 function ts_init_py {
355 LIBNAME="$1"
356
357 [ -f "$top_builddir/py${LIBNAME}.la" ] || ts_skip "py${LIBNAME} not compiled"
358
359 export LD_LIBRARY_PATH="$top_builddir/.libs:$LD_LIBRARY_PATH"
360 export PYTHONPATH="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
361
362 export PYTHON_VERSION=$(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile)
363 export PYTHON_MAJOR_VERSION=$(echo $PYTHON_VERSION | sed 's/\..*//')
364
365 export PYTHON="python${PYTHON_MAJOR_VERSION}"
366 }
367
368 function ts_valgrind {
369 if [ -z "$TS_VALGRIND_CMD" ]; then
370 $*
371 else
372 $TS_VALGRIND_CMD --tool=memcheck --leak-check=full \
373 --leak-resolution=high --num-callers=20 \
374 --log-file="$TS_VGDUMP" $*
375 fi
376 }
377
378 function ts_gen_diff {
379 local res=0
380
381 [ -f "$TS_OUTPUT" ] || return 1
382 [ -f "$TS_EXPECTED" ] || TS_EXPECTED=/dev/null
383
384 # remove libtool lt- prefixes
385 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
386
387 [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR"
388 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
389
390 if [ $? -ne 0 ] || [ -s $TS_DIFF ]; then
391 res=1
392 else
393 rm -f $TS_DIFF;
394 fi
395
396 return $res
397 }
398
399 function tt_gen_mem_report {
400 [ -z "$TS_VALGRIND_CMD" ] && echo "$1"
401
402 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev/null
403 if [ $? -eq 0 ]; then
404 echo "mem-error detected!"
405 fi
406 }
407
408 function ts_finalize_subtest {
409 local res=0
410
411 ts_gen_diff
412 if [ $? -eq 1 ]; then
413 ts_failed_subtest "$1"
414 res=1
415 else
416 ts_ok_subtest "$(tt_gen_mem_report "$1")"
417 fi
418
419 [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 ))
420
421 # reset environment back to parental test
422 ts_init_core_env
423
424 return $res
425 }
426
427 function ts_finalize {
428 ts_cleanup_on_exit
429
430 if [ $TS_NSUBTESTS -ne 0 ]; then
431 if ! ts_gen_diff || [ $TS_NSUBFAILED -ne 0 ]; then
432 ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
433 else
434 ts_ok "all $TS_NSUBTESTS sub-tests PASSED"
435 fi
436 fi
437
438 ts_gen_diff || ts_failed "$1"
439 ts_ok "$1"
440 }
441
442 function ts_die {
443 ts_log "$1"
444 ts_finalize
445 }
446
447 function ts_cleanup_on_exit {
448
449 for idx in $(seq 0 $((${#TS_SUID_PROGS[*]} - 1))); do
450 PROG=${TS_SUID_PROGS[$idx]}
451 chmod a-s $PROG &> /dev/null
452 chown ${TS_SUID_USER[$idx]}.${TS_SUID_GROUP[$idx]} $PROG &> /dev/null
453 done
454
455 for dev in "${TS_LOOP_DEVS[@]}"; do
456 ts_device_deinit "$dev"
457 done
458 unset TS_LOOP_DEVS
459 }
460
461 function ts_image_md5sum {
462 local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
463 echo $(md5sum "$img" | awk '{printf $1}') $(basename "$img")
464 }
465
466 function ts_image_init {
467 local mib=${1:-"5"} # size in MiBs
468 local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
469
470 rm -f $img
471 truncate -s "${mib}M" "$img"
472 echo "$img"
473 return 0
474 }
475
476 function ts_register_loop_device {
477 local ct=${#TS_LOOP_DEVS[*]}
478 TS_LOOP_DEVS[$ct]=$1
479 }
480
481 function ts_device_init {
482 local img
483 local dev
484
485 img=$(ts_image_init $1 $2)
486 dev=$($TS_CMD_LOSETUP --show -f "$img")
487 if [ "$?" != "0" -o "$dev" = "" ]; then
488 ts_die "Cannot init device"
489 fi
490
491 ts_register_loop_device "$dev"
492 TS_LODEV=$dev
493 }
494
495 # call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance
496 function ts_device_deinit {
497 local DEV="$1"
498
499 if [ -b "$DEV" ]; then
500 $TS_CMD_UMOUNT "$DEV" &> /dev/null
501 $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
502 fi
503 }
504
505 function ts_uuid_by_devname {
506 echo $($TS_CMD_BLKID -p -s UUID -o value $1)
507 }
508
509 function ts_label_by_devname {
510 echo $($TS_CMD_BLKID -p -s LABEL -o value $1)
511 }
512
513 function ts_fstype_by_devname {
514 echo $($TS_CMD_BLKID -p -s TYPE -o value $1)
515 }
516
517 function ts_device_has {
518 local TAG="$1"
519 local VAL="$2"
520 local DEV="$3"
521 local vl=""
522
523 case $TAG in
524 "TYPE") vl=$(ts_fstype_by_devname $DEV);;
525 "LABEL") vl=$(ts_label_by_devname $DEV);;
526 "UUID") vl=$(ts_uuid_by_devname $DEV);;
527 *) return 1;;
528 esac
529
530 if [ "$vl" == "$VAL" ]; then
531 return 0
532 fi
533 return 1
534 }
535
536 function ts_device_has_uuid {
537 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}$'
538 return $?
539 }
540
541 function ts_mount {
542 local out
543 local result
544 local msg
545 local fs
546 local fs_exp=$1
547 shift
548
549 out=$($TS_CMD_MOUNT "$@" 2>&1)
550 result=$?
551 echo -n "$out" >> $TS_OUTPUT
552
553 if [ $result != 0 ] \
554 && msg=$(echo "$out" | grep -m1 "unknown filesystem type")
555 then
556 # skip only if reported fs correctly and if it's not available
557 fs=$(echo "$msg" | sed -n "s/.*type '\(.*\)'$/\1/p")
558 [ "$fs" = "fs_exp" ] \
559 && grep -qe "[[:space:]]${fs}$" /proc/filesystems &>/dev/null \
560 || ts_skip "$msg"
561 fi
562 return $result
563 }
564
565 function ts_is_mounted {
566 local DEV=$(ts_canonicalize "$1")
567
568 grep -q $DEV /proc/mounts && return 0
569
570 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
571 grep -q "/dev/loop${DEV#/dev/loop/}" /proc/mounts && return 0
572 fi
573 return 1
574 }
575
576 function ts_fstab_open {
577 echo "# <!-- util-linux test entry" >> /etc/fstab
578 }
579
580 function ts_fstab_close {
581 echo "# -->" >> /etc/fstab
582 }
583
584 function ts_fstab_addline {
585 local SPEC="$1"
586 local MNT=${2:-"$TS_MOUNTPOINT"}
587 local FS=${3:-"auto"}
588 local OPT=${4:-"defaults"}
589
590 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc/fstab
591 }
592
593 function ts_fstab_add {
594 ts_fstab_open
595 ts_fstab_addline $*
596 ts_fstab_close
597 }
598
599 function ts_fstab_clean {
600 sed --in-place "
601 /# <!-- util-linux/!b
602 :a
603 /# -->/!{
604 N
605 ba
606 }
607 s/# <!-- util-linux.*-->//;
608 /^$/d" /etc/fstab
609 }
610
611 function ts_fdisk_clean {
612 local DEVNAME=$1
613
614 # remove non comparable parts of fdisk output
615 if [ -n "${DEVNAME}" ]; then
616 sed -i -e "s@${DEVNAME}@<removed>@;" $TS_OUTPUT
617 fi
618
619 sed -i \
620 -e 's/Disk identifier:.*/Disk identifier: <removed>/' \
621 -e 's/Created a new.*/Created a new <removed>./' \
622 -e 's/^Device[[:blank:]]*Start/Device Start/' \
623 -e 's/^Device[[:blank:]]*Boot/Device Boot/' \
624 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \
625 -e 's/typescript file.*/typescript file <removed>./' \
626 -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \
627 $TS_OUTPUT
628 }
629
630 function ts_scsi_debug_init {
631 local devname
632 local t
633 TS_DEVICE="none"
634
635 # dry run is not really reliable, real modprobe may still fail
636 modprobe --dry-run --quiet scsi_debug &>/dev/null \
637 || ts_skip "missing scsi_debug module (dry-run)"
638
639 # skip if still in use or removal of modules not supported at all
640 modprobe -r scsi_debug &>/dev/null \
641 || ts_skip "cannot remove scsi_debug module (rmmod)"
642
643 # TODO validate that all devices are gone, add function ts_scsi_debug_rmmod
644 # to be used by the tests too. Tests which produce non-removable scsi
645 # devices should fail!
646
647 modprobe -b scsi_debug "$@" &>/dev/null \
648 || ts_skip "cannot load scsi_debug module (modprobe)"
649
650 # it might be still not loaded, modprobe.conf or whatever
651 lsmod | grep -q "^scsi_debug " \
652 || ts_skip "scsi_debug module not loaded (lsmod)"
653
654 udevadm settle
655
656 # wait for device if udevadm settle does not work
657 for t in 0 0.02 0.05 0.1 1; do
658 sleep $t
659 devname=$(grep --with-filename scsi_debug /sys/block/*/device/model) && break
660 done
661 [ -n "${devname}" ] || ts_die "timeout waiting for scsi_debug device"
662
663 devname=$(echo $devname | awk -F '/' '{print $4}')
664 TS_DEVICE="/dev/${devname}"
665
666 # TODO validate that device is really up, for now just a warning on stderr
667 test -b $TS_DEVICE || echo "warning: scsi_debug device is still down"
668 }
669
670 function ts_resolve_host {
671 local host="$1"
672 local tmp
673
674 # currently we just resolve default records (might be "A", ipv4 only)
675 if type "dig" >/dev/null 2>&1; then
676 tmp=$(dig "$host" +short 2>/dev/null) || return 1
677 elif type "nslookup" >/dev/null 2>&1; then
678 tmp=$(nslookup "$host" 2>/dev/null) || return 1
679 tmp=$(echo "$tmp"| grep -A1 "^Name:"| grep "^Address:"| cut -d" " -f2)
680 elif type "host" >/dev/null 2>&1; then
681 tmp=$(host "$host" 2>/dev/null) || return 1
682 tmp=$(echo "$tmp" | grep " has address " | cut -d " " -f4)
683 elif type "getent" >/dev/null 2>&1; then
684 tmp=$(getent ahosts "$host" 2>/dev/null) || return 1
685 tmp=$(echo "$tmp" | cut -d " " -f 1 | sort -u)
686 fi
687
688 # we return 1 if tmp is empty
689 test -n "$tmp" || return 1
690 echo "$tmp" | sort -R | head -n 1
691 }
692
693 # listen to unix socket (background socat)
694 function ts_init_socket_to_file {
695 local socket=$1
696 local outfile=$2
697 local pid="0"
698
699 ts_check_prog "socat"
700 rm -f "$socket" "$outfile"
701
702 # if socat is too old for these options we'll skip it below
703 socat -u UNIX-LISTEN:$socket,fork,max-children=1,backlog=128 \
704 STDOUT > "$outfile" 2>/dev/null &
705 pid=$!
706
707 # check for running background process
708 if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then
709 ts_skip "unable to run socat"
710 fi
711 # wait for the socket listener
712 if ! socat -u /dev/null UNIX-CONNECT:$socket,retry=30,interval=0.1 &>/dev/null; then
713 kill -9 "$pid" &>/dev/null
714 ts_skip "timeout waiting for socat socket"
715 fi
716 # check socket again
717 if ! socat -u /dev/null UNIX-CONNECT:$socket &>/dev/null; then
718 kill -9 "$pid" &>/dev/null
719 ts_skip "socat socket stopped listening"
720 fi
721 }