xref: /aosp_15_r20/external/ltp/testcases/lib/test.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker#!/bin/sh
2*49cdfc7eSAndroid Build Coastguard Worker#
3*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) Linux Test Project, 2014-2017
4*49cdfc7eSAndroid Build Coastguard Worker#
5*49cdfc7eSAndroid Build Coastguard Worker# This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker# it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker# the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker# (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker#
10*49cdfc7eSAndroid Build Coastguard Worker# This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker# but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*49cdfc7eSAndroid Build Coastguard Worker# GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker#
15*49cdfc7eSAndroid Build Coastguard Worker# You should have received a copy of the GNU General Public License along
16*49cdfc7eSAndroid Build Coastguard Worker# with this program; if not, write to the Free Software Foundation, Inc.,
17*49cdfc7eSAndroid Build Coastguard Worker# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*49cdfc7eSAndroid Build Coastguard Worker#
19*49cdfc7eSAndroid Build Coastguard Worker# Written by Cyril Hrubis <[email protected]>
20*49cdfc7eSAndroid Build Coastguard Worker#
21*49cdfc7eSAndroid Build Coastguard Worker# This is a LTP test library for shell.
22*49cdfc7eSAndroid Build Coastguard Worker#
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Workerexport LTP_RET_VAL=0
25*49cdfc7eSAndroid Build Coastguard Workerexport TST_COUNT=1
26*49cdfc7eSAndroid Build Coastguard Workerexport TST_PASS_COUNT=0
27*49cdfc7eSAndroid Build Coastguard Workerexport TST_LIB_LOADED=1
28*49cdfc7eSAndroid Build Coastguard Workerexport TST_TMPDIR_RHOST=0
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker. tst_ansi_color.sh
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker# Exit values map
33*49cdfc7eSAndroid Build Coastguard Workertst_flag2mask()
34*49cdfc7eSAndroid Build Coastguard Worker{
35*49cdfc7eSAndroid Build Coastguard Worker	case "$1" in
36*49cdfc7eSAndroid Build Coastguard Worker	TPASS) return 0;;
37*49cdfc7eSAndroid Build Coastguard Worker	TFAIL) return 1;;
38*49cdfc7eSAndroid Build Coastguard Worker	TBROK) return 2;;
39*49cdfc7eSAndroid Build Coastguard Worker	TWARN) return 4;;
40*49cdfc7eSAndroid Build Coastguard Worker	TINFO) return 16;;
41*49cdfc7eSAndroid Build Coastguard Worker	TCONF) return 32;;
42*49cdfc7eSAndroid Build Coastguard Worker	*) tst_brkm TBROK "Invalid resm type '$1'";;
43*49cdfc7eSAndroid Build Coastguard Worker	esac
44*49cdfc7eSAndroid Build Coastguard Worker}
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Workertst_resm()
47*49cdfc7eSAndroid Build Coastguard Worker{
48*49cdfc7eSAndroid Build Coastguard Worker	local ttype="$1"
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker	tst_flag2mask "$ttype"
51*49cdfc7eSAndroid Build Coastguard Worker	local mask=$?
52*49cdfc7eSAndroid Build Coastguard Worker	LTP_RET_VAL=$((LTP_RET_VAL|mask))
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker	local ret=$1
55*49cdfc7eSAndroid Build Coastguard Worker	shift
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker	printf "$TCID $TST_COUNT "
58*49cdfc7eSAndroid Build Coastguard Worker	tst_print_colored $ret "$ret:"
59*49cdfc7eSAndroid Build Coastguard Worker	echo " $@"
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker	case "$ret" in
62*49cdfc7eSAndroid Build Coastguard Worker	TPASS|TFAIL|TCONF) TST_COUNT=$((TST_COUNT+1));;
63*49cdfc7eSAndroid Build Coastguard Worker	esac
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker	if [ "$ret" = TPASS ]; then
66*49cdfc7eSAndroid Build Coastguard Worker		TST_PASS_COUNT=$((TST_PASS_COUNT+1))
67*49cdfc7eSAndroid Build Coastguard Worker	fi
68*49cdfc7eSAndroid Build Coastguard Worker}
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Workertst_brkm()
71*49cdfc7eSAndroid Build Coastguard Worker{
72*49cdfc7eSAndroid Build Coastguard Worker	case "$1" in
73*49cdfc7eSAndroid Build Coastguard Worker	TFAIL) ;;
74*49cdfc7eSAndroid Build Coastguard Worker	TBROK) ;;
75*49cdfc7eSAndroid Build Coastguard Worker	TCONF) ;;
76*49cdfc7eSAndroid Build Coastguard Worker	*) tst_brkm TBROK "Invalid tst_brkm type '$1'";;
77*49cdfc7eSAndroid Build Coastguard Worker	esac
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker	local ret=$1
80*49cdfc7eSAndroid Build Coastguard Worker	shift
81*49cdfc7eSAndroid Build Coastguard Worker	tst_resm "$ret" "$@"
82*49cdfc7eSAndroid Build Coastguard Worker	tst_exit
83*49cdfc7eSAndroid Build Coastguard Worker}
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Workertst_record_childstatus()
86*49cdfc7eSAndroid Build Coastguard Worker{
87*49cdfc7eSAndroid Build Coastguard Worker	if [ $# -ne 1 ]; then
88*49cdfc7eSAndroid Build Coastguard Worker		tst_brkm TBROK "Requires child pid as parameter"
89*49cdfc7eSAndroid Build Coastguard Worker	fi
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker	local child_pid=$1
92*49cdfc7eSAndroid Build Coastguard Worker	local ret=0
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker	wait $child_pid
95*49cdfc7eSAndroid Build Coastguard Worker	ret=$?
96*49cdfc7eSAndroid Build Coastguard Worker	if [ $ret -eq 127 ]; then
97*49cdfc7eSAndroid Build Coastguard Worker		tst_brkm TBROK "Child process pid='$child_pid' does not exist"
98*49cdfc7eSAndroid Build Coastguard Worker	fi
99*49cdfc7eSAndroid Build Coastguard Worker	LTP_RET_VAL=$((LTP_RET_VAL|ret))
100*49cdfc7eSAndroid Build Coastguard Worker}
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Workertst_require_root()
103*49cdfc7eSAndroid Build Coastguard Worker{
104*49cdfc7eSAndroid Build Coastguard Worker	if [ "$(id -ru)" != 0 ]; then
105*49cdfc7eSAndroid Build Coastguard Worker		tst_brkm TCONF "Must be super/root for this test!"
106*49cdfc7eSAndroid Build Coastguard Worker	fi
107*49cdfc7eSAndroid Build Coastguard Worker}
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Workertst_exit()
110*49cdfc7eSAndroid Build Coastguard Worker{
111*49cdfc7eSAndroid Build Coastguard Worker	if [ -n "${TST_CLEANUP:-}" -a -z "${LTP_NO_CLEANUP:-}" ]; then
112*49cdfc7eSAndroid Build Coastguard Worker		$TST_CLEANUP
113*49cdfc7eSAndroid Build Coastguard Worker	fi
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker	if [ -n "${LTP_IPC_PATH:-}" -a -f "${LTP_IPC_PATH:-}" ]; then
116*49cdfc7eSAndroid Build Coastguard Worker		rm -f "$LTP_IPC_PATH"
117*49cdfc7eSAndroid Build Coastguard Worker	fi
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker	# Mask out TCONF if no TFAIL/TBROK/TWARN but has TPASS
120*49cdfc7eSAndroid Build Coastguard Worker	if [ $((LTP_RET_VAL & 7)) -eq 0 -a $TST_PASS_COUNT -gt 0 ]; then
121*49cdfc7eSAndroid Build Coastguard Worker		LTP_RET_VAL=$((LTP_RET_VAL & ~32))
122*49cdfc7eSAndroid Build Coastguard Worker	fi
123*49cdfc7eSAndroid Build Coastguard Worker	# Mask out TINFO
124*49cdfc7eSAndroid Build Coastguard Worker	exit $((LTP_RET_VAL & ~16))
125*49cdfc7eSAndroid Build Coastguard Worker}
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Workertst_tmpdir()
128*49cdfc7eSAndroid Build Coastguard Worker{
129*49cdfc7eSAndroid Build Coastguard Worker	if [ -z "$TMPDIR" ]; then
130*49cdfc7eSAndroid Build Coastguard Worker		export TMPDIR="/tmp"
131*49cdfc7eSAndroid Build Coastguard Worker	fi
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker	TST_TMPDIR=$(mktemp -d "$TMPDIR/$TCID.XXXXXXXXXX")
134*49cdfc7eSAndroid Build Coastguard Worker
135*49cdfc7eSAndroid Build Coastguard Worker	chmod 777 "$TST_TMPDIR"
136*49cdfc7eSAndroid Build Coastguard Worker
137*49cdfc7eSAndroid Build Coastguard Worker	TST_STARTWD=$(pwd)
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker	cd "$TST_TMPDIR"
140*49cdfc7eSAndroid Build Coastguard Worker}
141*49cdfc7eSAndroid Build Coastguard Worker
142*49cdfc7eSAndroid Build Coastguard Workertst_rmdir()
143*49cdfc7eSAndroid Build Coastguard Worker{
144*49cdfc7eSAndroid Build Coastguard Worker	if [ -n "$TST_TMPDIR" ]; then
145*49cdfc7eSAndroid Build Coastguard Worker		cd "$LTPROOT"
146*49cdfc7eSAndroid Build Coastguard Worker		rm -r "$TST_TMPDIR"
147*49cdfc7eSAndroid Build Coastguard Worker		[ "$TST_TMPDIR_RHOST" = 1 ] && tst_cleanup_rhost
148*49cdfc7eSAndroid Build Coastguard Worker	fi
149*49cdfc7eSAndroid Build Coastguard Worker}
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker#
152*49cdfc7eSAndroid Build Coastguard Worker# Checks if commands passed as arguments exists
153*49cdfc7eSAndroid Build Coastguard Worker#
154*49cdfc7eSAndroid Build Coastguard Workertst_require_cmds()
155*49cdfc7eSAndroid Build Coastguard Worker{
156*49cdfc7eSAndroid Build Coastguard Worker	local cmd
157*49cdfc7eSAndroid Build Coastguard Worker	for cmd in $*; do
158*49cdfc7eSAndroid Build Coastguard Worker		if ! command -v $cmd > /dev/null 2>&1; then
159*49cdfc7eSAndroid Build Coastguard Worker			tst_brkm TCONF "'$cmd' not found"
160*49cdfc7eSAndroid Build Coastguard Worker		fi
161*49cdfc7eSAndroid Build Coastguard Worker	done
162*49cdfc7eSAndroid Build Coastguard Worker}
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard Worker# tst_retry "command" [times]
165*49cdfc7eSAndroid Build Coastguard Worker# try run command for specified times, default is 3.
166*49cdfc7eSAndroid Build Coastguard Worker# Function returns 0 if succeed in RETRIES times or the last retcode the cmd
167*49cdfc7eSAndroid Build Coastguard Worker# returned
168*49cdfc7eSAndroid Build Coastguard Workertst_retry()
169*49cdfc7eSAndroid Build Coastguard Worker{
170*49cdfc7eSAndroid Build Coastguard Worker	local cmd="$1"
171*49cdfc7eSAndroid Build Coastguard Worker	local RETRIES=${2:-"3"}
172*49cdfc7eSAndroid Build Coastguard Worker	local i=$RETRIES
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker	while [ $i -gt 0 ]; do
175*49cdfc7eSAndroid Build Coastguard Worker		eval "$cmd"
176*49cdfc7eSAndroid Build Coastguard Worker		ret=$?
177*49cdfc7eSAndroid Build Coastguard Worker		if [ $ret -eq 0 ]; then
178*49cdfc7eSAndroid Build Coastguard Worker			break
179*49cdfc7eSAndroid Build Coastguard Worker		fi
180*49cdfc7eSAndroid Build Coastguard Worker		i=$((i-1))
181*49cdfc7eSAndroid Build Coastguard Worker		sleep 1
182*49cdfc7eSAndroid Build Coastguard Worker	done
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker	if [ $ret -ne 0 ]; then
185*49cdfc7eSAndroid Build Coastguard Worker		tst_resm TINFO "Failed to execute '$cmd' after $RETRIES retries"
186*49cdfc7eSAndroid Build Coastguard Worker	fi
187*49cdfc7eSAndroid Build Coastguard Worker
188*49cdfc7eSAndroid Build Coastguard Worker	return $ret
189*49cdfc7eSAndroid Build Coastguard Worker}
190*49cdfc7eSAndroid Build Coastguard Worker
191*49cdfc7eSAndroid Build Coastguard Worker# tst_timeout "command arg1 arg2 ..." timeout
192*49cdfc7eSAndroid Build Coastguard Worker# Runs command for specified timeout (in seconds).
193*49cdfc7eSAndroid Build Coastguard Worker# Function returns retcode of command or 1 if arguments are invalid.
194*49cdfc7eSAndroid Build Coastguard Workertst_timeout()
195*49cdfc7eSAndroid Build Coastguard Worker{
196*49cdfc7eSAndroid Build Coastguard Worker	local command=$1
197*49cdfc7eSAndroid Build Coastguard Worker	local timeout=$(echo $2 | grep -o "^[0-9]\+$")
198*49cdfc7eSAndroid Build Coastguard Worker
199*49cdfc7eSAndroid Build Coastguard Worker	# command must be non-empty string with command to run
200*49cdfc7eSAndroid Build Coastguard Worker	if [ -z "$command" ]; then
201*49cdfc7eSAndroid Build Coastguard Worker		echo "first argument must be non-empty string"
202*49cdfc7eSAndroid Build Coastguard Worker		return 1
203*49cdfc7eSAndroid Build Coastguard Worker	fi
204*49cdfc7eSAndroid Build Coastguard Worker
205*49cdfc7eSAndroid Build Coastguard Worker	# accept only numbers as timeout
206*49cdfc7eSAndroid Build Coastguard Worker	if [ -z "$timeout" ]; then
207*49cdfc7eSAndroid Build Coastguard Worker		echo "only numbers as second argument"
208*49cdfc7eSAndroid Build Coastguard Worker		return 1
209*49cdfc7eSAndroid Build Coastguard Worker	fi
210*49cdfc7eSAndroid Build Coastguard Worker
211*49cdfc7eSAndroid Build Coastguard Worker	setsid sh -c "eval $command" 2>&1 &
212*49cdfc7eSAndroid Build Coastguard Worker	local pid=$!
213*49cdfc7eSAndroid Build Coastguard Worker	while [ $timeout -gt 0 ]; do
214*49cdfc7eSAndroid Build Coastguard Worker		kill -s 0 $pid 2>/dev/null
215*49cdfc7eSAndroid Build Coastguard Worker		if [ $? -ne 0 ]; then
216*49cdfc7eSAndroid Build Coastguard Worker			break
217*49cdfc7eSAndroid Build Coastguard Worker		fi
218*49cdfc7eSAndroid Build Coastguard Worker		timeout=$((timeout - 1))
219*49cdfc7eSAndroid Build Coastguard Worker		sleep 1
220*49cdfc7eSAndroid Build Coastguard Worker	done
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker	local ret=0
223*49cdfc7eSAndroid Build Coastguard Worker	if [ $timeout -le 0 ]; then
224*49cdfc7eSAndroid Build Coastguard Worker		ret=128
225*49cdfc7eSAndroid Build Coastguard Worker		kill -TERM -- -$pid
226*49cdfc7eSAndroid Build Coastguard Worker	fi
227*49cdfc7eSAndroid Build Coastguard Worker
228*49cdfc7eSAndroid Build Coastguard Worker	wait $pid
229*49cdfc7eSAndroid Build Coastguard Worker	ret=$((ret | $?))
230*49cdfc7eSAndroid Build Coastguard Worker
231*49cdfc7eSAndroid Build Coastguard Worker	return $ret
232*49cdfc7eSAndroid Build Coastguard Worker}
233*49cdfc7eSAndroid Build Coastguard Worker
234*49cdfc7eSAndroid Build Coastguard WorkerROD_SILENT()
235*49cdfc7eSAndroid Build Coastguard Worker{
236*49cdfc7eSAndroid Build Coastguard Worker	local tst_out
237*49cdfc7eSAndroid Build Coastguard Worker
238*49cdfc7eSAndroid Build Coastguard Worker	tst_out="$($@ 2>&1)"
239*49cdfc7eSAndroid Build Coastguard Worker	if [ $? -ne 0 ]; then
240*49cdfc7eSAndroid Build Coastguard Worker		echo "$tst_out"
241*49cdfc7eSAndroid Build Coastguard Worker		tst_brkm TBROK "$@ failed"
242*49cdfc7eSAndroid Build Coastguard Worker	fi
243*49cdfc7eSAndroid Build Coastguard Worker}
244*49cdfc7eSAndroid Build Coastguard Worker
245*49cdfc7eSAndroid Build Coastguard WorkerROD_BASE()
246*49cdfc7eSAndroid Build Coastguard Worker{
247*49cdfc7eSAndroid Build Coastguard Worker	local cmd
248*49cdfc7eSAndroid Build Coastguard Worker	local arg
249*49cdfc7eSAndroid Build Coastguard Worker	local file
250*49cdfc7eSAndroid Build Coastguard Worker	local flag
251*49cdfc7eSAndroid Build Coastguard Worker
252*49cdfc7eSAndroid Build Coastguard Worker	for arg; do
253*49cdfc7eSAndroid Build Coastguard Worker		file="${arg#\>}"
254*49cdfc7eSAndroid Build Coastguard Worker		if [ "$file" != "$arg" ]; then
255*49cdfc7eSAndroid Build Coastguard Worker			flag=1
256*49cdfc7eSAndroid Build Coastguard Worker			if [ -n "$file" ]; then
257*49cdfc7eSAndroid Build Coastguard Worker				break
258*49cdfc7eSAndroid Build Coastguard Worker			fi
259*49cdfc7eSAndroid Build Coastguard Worker			continue
260*49cdfc7eSAndroid Build Coastguard Worker		fi
261*49cdfc7eSAndroid Build Coastguard Worker
262*49cdfc7eSAndroid Build Coastguard Worker		if [ -n "$flag" ]; then
263*49cdfc7eSAndroid Build Coastguard Worker			file="$arg"
264*49cdfc7eSAndroid Build Coastguard Worker			break
265*49cdfc7eSAndroid Build Coastguard Worker		fi
266*49cdfc7eSAndroid Build Coastguard Worker
267*49cdfc7eSAndroid Build Coastguard Worker		cmd="$cmd $arg"
268*49cdfc7eSAndroid Build Coastguard Worker	done
269*49cdfc7eSAndroid Build Coastguard Worker
270*49cdfc7eSAndroid Build Coastguard Worker	if [ -n "$flag" ]; then
271*49cdfc7eSAndroid Build Coastguard Worker		$cmd > $file
272*49cdfc7eSAndroid Build Coastguard Worker	else
273*49cdfc7eSAndroid Build Coastguard Worker		$@
274*49cdfc7eSAndroid Build Coastguard Worker	fi
275*49cdfc7eSAndroid Build Coastguard Worker}
276*49cdfc7eSAndroid Build Coastguard Worker
277*49cdfc7eSAndroid Build Coastguard WorkerROD()
278*49cdfc7eSAndroid Build Coastguard Worker{
279*49cdfc7eSAndroid Build Coastguard Worker	ROD_BASE "$@"
280*49cdfc7eSAndroid Build Coastguard Worker	if [ $? -ne 0 ]; then
281*49cdfc7eSAndroid Build Coastguard Worker		tst_brkm TBROK "$@ failed"
282*49cdfc7eSAndroid Build Coastguard Worker	fi
283*49cdfc7eSAndroid Build Coastguard Worker}
284*49cdfc7eSAndroid Build Coastguard Worker
285*49cdfc7eSAndroid Build Coastguard WorkerEXPECT_PASS()
286*49cdfc7eSAndroid Build Coastguard Worker{
287*49cdfc7eSAndroid Build Coastguard Worker	ROD_BASE "$@"
288*49cdfc7eSAndroid Build Coastguard Worker	if [ $? -eq 0 ]; then
289*49cdfc7eSAndroid Build Coastguard Worker		tst_resm TPASS "$@ passed as expected"
290*49cdfc7eSAndroid Build Coastguard Worker	else
291*49cdfc7eSAndroid Build Coastguard Worker		tst_resm TFAIL "$@ failed unexpectedly"
292*49cdfc7eSAndroid Build Coastguard Worker	fi
293*49cdfc7eSAndroid Build Coastguard Worker}
294*49cdfc7eSAndroid Build Coastguard Worker
295*49cdfc7eSAndroid Build Coastguard WorkerEXPECT_FAIL()
296*49cdfc7eSAndroid Build Coastguard Worker{
297*49cdfc7eSAndroid Build Coastguard Worker	# redirect stderr since we expect the command to fail
298*49cdfc7eSAndroid Build Coastguard Worker	ROD_BASE "$@" 2> /dev/null
299*49cdfc7eSAndroid Build Coastguard Worker	if [ $? -ne 0 ]; then
300*49cdfc7eSAndroid Build Coastguard Worker		tst_resm TPASS "$@ failed as expected"
301*49cdfc7eSAndroid Build Coastguard Worker	else
302*49cdfc7eSAndroid Build Coastguard Worker		tst_resm TFAIL "$@ passed unexpectedly"
303*49cdfc7eSAndroid Build Coastguard Worker	fi
304*49cdfc7eSAndroid Build Coastguard Worker}
305*49cdfc7eSAndroid Build Coastguard Worker
306*49cdfc7eSAndroid Build Coastguard Workertst_mkfs()
307*49cdfc7eSAndroid Build Coastguard Worker{
308*49cdfc7eSAndroid Build Coastguard Worker	local fs_type=$1
309*49cdfc7eSAndroid Build Coastguard Worker	local device=$2
310*49cdfc7eSAndroid Build Coastguard Worker	shift 2
311*49cdfc7eSAndroid Build Coastguard Worker	local fs_opts="$@"
312*49cdfc7eSAndroid Build Coastguard Worker
313*49cdfc7eSAndroid Build Coastguard Worker	if [ -z "$fs_type" ]; then
314*49cdfc7eSAndroid Build Coastguard Worker		tst_brkm TBROK "No fs_type specified"
315*49cdfc7eSAndroid Build Coastguard Worker	fi
316*49cdfc7eSAndroid Build Coastguard Worker
317*49cdfc7eSAndroid Build Coastguard Worker	if [ -z "$device" ]; then
318*49cdfc7eSAndroid Build Coastguard Worker		tst_brkm TBROK "No device specified"
319*49cdfc7eSAndroid Build Coastguard Worker	fi
320*49cdfc7eSAndroid Build Coastguard Worker
321*49cdfc7eSAndroid Build Coastguard Worker	tst_resm TINFO "Formatting $device with $fs_type extra opts='$fs_opts'"
322*49cdfc7eSAndroid Build Coastguard Worker
323*49cdfc7eSAndroid Build Coastguard Worker	ROD_SILENT mkfs.$fs_type $fs_opts $device
324*49cdfc7eSAndroid Build Coastguard Worker}
325*49cdfc7eSAndroid Build Coastguard Worker
326*49cdfc7eSAndroid Build Coastguard Worker# Detect whether running under hypervisor: Microsoft Hyper-V
327*49cdfc7eSAndroid Build Coastguard Worker# Return 0: running under Hyper-V
328*49cdfc7eSAndroid Build Coastguard Worker# Return 1: not running under Hyper-V (bare metal, other hypervisor or
329*49cdfc7eSAndroid Build Coastguard Worker#           failure of detection)
330*49cdfc7eSAndroid Build Coastguard Workertst_virt_hyperv()
331*49cdfc7eSAndroid Build Coastguard Worker{
332*49cdfc7eSAndroid Build Coastguard Worker	local v
333*49cdfc7eSAndroid Build Coastguard Worker
334*49cdfc7eSAndroid Build Coastguard Worker	v="$(systemd-detect-virt)"
335*49cdfc7eSAndroid Build Coastguard Worker
336*49cdfc7eSAndroid Build Coastguard Worker	[ $? -eq 0 ] || return 1
337*49cdfc7eSAndroid Build Coastguard Worker	[ "$v" = "microsoft" ] || return 1
338*49cdfc7eSAndroid Build Coastguard Worker
339*49cdfc7eSAndroid Build Coastguard Worker	return 0
340*49cdfc7eSAndroid Build Coastguard Worker}
341*49cdfc7eSAndroid Build Coastguard Worker
342*49cdfc7eSAndroid Build Coastguard Workertst_umount()
343*49cdfc7eSAndroid Build Coastguard Worker{
344*49cdfc7eSAndroid Build Coastguard Worker	local device="$1"
345*49cdfc7eSAndroid Build Coastguard Worker	local i=0
346*49cdfc7eSAndroid Build Coastguard Worker
347*49cdfc7eSAndroid Build Coastguard Worker	if ! grep -q "$device" /proc/mounts; then
348*49cdfc7eSAndroid Build Coastguard Worker		tst_resm TINFO "The $device is not mounted, skipping umount"
349*49cdfc7eSAndroid Build Coastguard Worker		return
350*49cdfc7eSAndroid Build Coastguard Worker	fi
351*49cdfc7eSAndroid Build Coastguard Worker
352*49cdfc7eSAndroid Build Coastguard Worker	while [ "$i" -lt 50 ]; do
353*49cdfc7eSAndroid Build Coastguard Worker		if umount "$device" > /dev/null; then
354*49cdfc7eSAndroid Build Coastguard Worker			return
355*49cdfc7eSAndroid Build Coastguard Worker		fi
356*49cdfc7eSAndroid Build Coastguard Worker
357*49cdfc7eSAndroid Build Coastguard Worker		i=$((i+1))
358*49cdfc7eSAndroid Build Coastguard Worker
359*49cdfc7eSAndroid Build Coastguard Worker		tst_resm TINFO "umount($device) failed, try $i ..."
360*49cdfc7eSAndroid Build Coastguard Worker		tst_resm TINFO "Likely gvfsd-trash is probing newly mounted "\
361*49cdfc7eSAndroid Build Coastguard Worker			       "fs, kill it to speed up tests."
362*49cdfc7eSAndroid Build Coastguard Worker
363*49cdfc7eSAndroid Build Coastguard Worker		tst_sleep 100ms
364*49cdfc7eSAndroid Build Coastguard Worker	done
365*49cdfc7eSAndroid Build Coastguard Worker
366*49cdfc7eSAndroid Build Coastguard Worker	tst_resm TWARN "Failed to umount($device) after 50 retries"
367*49cdfc7eSAndroid Build Coastguard Worker}
368*49cdfc7eSAndroid Build Coastguard Worker
369*49cdfc7eSAndroid Build Coastguard Worker# Check a module file existence
370*49cdfc7eSAndroid Build Coastguard Worker# Should be called after tst_tmpdir()
371*49cdfc7eSAndroid Build Coastguard Workertst_module_exists()
372*49cdfc7eSAndroid Build Coastguard Worker{
373*49cdfc7eSAndroid Build Coastguard Worker	local mod_name="$1"
374*49cdfc7eSAndroid Build Coastguard Worker
375*49cdfc7eSAndroid Build Coastguard Worker	if [ -f "$mod_name" ]; then
376*49cdfc7eSAndroid Build Coastguard Worker		TST_MODPATH="$mod_name"
377*49cdfc7eSAndroid Build Coastguard Worker		return
378*49cdfc7eSAndroid Build Coastguard Worker	fi
379*49cdfc7eSAndroid Build Coastguard Worker
380*49cdfc7eSAndroid Build Coastguard Worker	local mod_path="$LTPROOT/testcases/bin/$mod_name"
381*49cdfc7eSAndroid Build Coastguard Worker	if [ -f "$mod_path" ]; then
382*49cdfc7eSAndroid Build Coastguard Worker		TST_MODPATH="$mod_path"
383*49cdfc7eSAndroid Build Coastguard Worker		return
384*49cdfc7eSAndroid Build Coastguard Worker	fi
385*49cdfc7eSAndroid Build Coastguard Worker
386*49cdfc7eSAndroid Build Coastguard Worker	if [ -n "$TST_TMPDIR" ]; then
387*49cdfc7eSAndroid Build Coastguard Worker		mod_path="$TST_STARTWD/$mod_name"
388*49cdfc7eSAndroid Build Coastguard Worker		if [ -f "$mod_path" ]; then
389*49cdfc7eSAndroid Build Coastguard Worker			TST_MODPATH="$mod_path"
390*49cdfc7eSAndroid Build Coastguard Worker			return
391*49cdfc7eSAndroid Build Coastguard Worker		fi
392*49cdfc7eSAndroid Build Coastguard Worker	fi
393*49cdfc7eSAndroid Build Coastguard Worker
394*49cdfc7eSAndroid Build Coastguard Worker	tst_brkm TCONF "Failed to find module '$mod_name'"
395*49cdfc7eSAndroid Build Coastguard Worker}
396*49cdfc7eSAndroid Build Coastguard Worker
397*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAIT()
398*49cdfc7eSAndroid Build Coastguard Worker{
399*49cdfc7eSAndroid Build Coastguard Worker	ROD tst_checkpoint wait 10000 "$1"
400*49cdfc7eSAndroid Build Coastguard Worker}
401*49cdfc7eSAndroid Build Coastguard Worker
402*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAKE()
403*49cdfc7eSAndroid Build Coastguard Worker{
404*49cdfc7eSAndroid Build Coastguard Worker	ROD tst_checkpoint wake 10000 "$1" 1
405*49cdfc7eSAndroid Build Coastguard Worker}
406*49cdfc7eSAndroid Build Coastguard Worker
407*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAKE2()
408*49cdfc7eSAndroid Build Coastguard Worker{
409*49cdfc7eSAndroid Build Coastguard Worker	ROD tst_checkpoint wake 10000 "$1" "$2"
410*49cdfc7eSAndroid Build Coastguard Worker}
411*49cdfc7eSAndroid Build Coastguard Worker
412*49cdfc7eSAndroid Build Coastguard WorkerTST_CHECKPOINT_WAKE_AND_WAIT()
413*49cdfc7eSAndroid Build Coastguard Worker{
414*49cdfc7eSAndroid Build Coastguard Worker	TST_CHECKPOINT_WAKE "$1"
415*49cdfc7eSAndroid Build Coastguard Worker	TST_CHECKPOINT_WAIT "$1"
416*49cdfc7eSAndroid Build Coastguard Worker}
417*49cdfc7eSAndroid Build Coastguard Worker
418*49cdfc7eSAndroid Build Coastguard Worker# Check that test name is set
419*49cdfc7eSAndroid Build Coastguard Workerif [ -z "$TCID" ]; then
420*49cdfc7eSAndroid Build Coastguard Worker	tst_brkm TBROK "TCID is not defined"
421*49cdfc7eSAndroid Build Coastguard Workerfi
422*49cdfc7eSAndroid Build Coastguard Worker
423*49cdfc7eSAndroid Build Coastguard Workerif [ -z "$TST_TOTAL" ]; then
424*49cdfc7eSAndroid Build Coastguard Worker	tst_brkm TBROK "TST_TOTAL is not defined"
425*49cdfc7eSAndroid Build Coastguard Workerfi
426*49cdfc7eSAndroid Build Coastguard Worker
427*49cdfc7eSAndroid Build Coastguard Workerexport TCID="$TCID"
428*49cdfc7eSAndroid Build Coastguard Workerexport TST_TOTAL="$TST_TOTAL"
429*49cdfc7eSAndroid Build Coastguard Worker
430*49cdfc7eSAndroid Build Coastguard Worker# Setup LTPROOT, default to current directory if not set
431*49cdfc7eSAndroid Build Coastguard Workerif [ -z "$LTPROOT" ]; then
432*49cdfc7eSAndroid Build Coastguard Worker	export LTPROOT="$PWD"
433*49cdfc7eSAndroid Build Coastguard Worker	export LTP_DATAROOT="$LTPROOT/datafiles"
434*49cdfc7eSAndroid Build Coastguard Workerelse
435*49cdfc7eSAndroid Build Coastguard Worker	export LTP_DATAROOT="$LTPROOT/testcases/data/$TCID"
436*49cdfc7eSAndroid Build Coastguard Workerfi
437*49cdfc7eSAndroid Build Coastguard Worker
438*49cdfc7eSAndroid Build Coastguard Workerif [ "$TST_NEEDS_CHECKPOINTS" = "1" ]; then
439*49cdfc7eSAndroid Build Coastguard Worker	LTP_IPC_PATH="/dev/shm/ltp_${TCID}_$$"
440*49cdfc7eSAndroid Build Coastguard Worker
441*49cdfc7eSAndroid Build Coastguard Worker	LTP_IPC_SIZE=$(tst_getconf PAGESIZE)
442*49cdfc7eSAndroid Build Coastguard Worker	if [ $? -ne 0 ]; then
443*49cdfc7eSAndroid Build Coastguard Worker		tst_brkm TBROK "tst_getconf PAGESIZE failed"
444*49cdfc7eSAndroid Build Coastguard Worker	fi
445*49cdfc7eSAndroid Build Coastguard Worker
446*49cdfc7eSAndroid Build Coastguard Worker	ROD_SILENT dd if=/dev/zero of="$LTP_IPC_PATH" bs="$LTP_IPC_SIZE" count=1
447*49cdfc7eSAndroid Build Coastguard Worker	ROD_SILENT chmod 600 "$LTP_IPC_PATH"
448*49cdfc7eSAndroid Build Coastguard Worker	export LTP_IPC_PATH
449*49cdfc7eSAndroid Build Coastguard Workerfi
450