xref: /kvm-unit-tests/scripts/arch-run.bash (revision f0ca153cc2d1e6a670b3571a8ac24c2ef7042594)
1##############################################################################
2# run_qemu translates the ambiguous exit status in Table1 to that in Table2.
3# Table3 simply documents the complete status table.
4#
5# Table1: Before fixup
6# --------------------
7# 0      - Unexpected exit from QEMU (possible signal), or the unittest did
8#          not use debug-exit
9# 1      - most likely unittest succeeded, or QEMU failed
10#
11# Table2: After fixup
12# -------------------
13# 0      - Everything succeeded
14# 1      - most likely QEMU failed
15#
16# Table3: Complete table
17# ----------------------
18# 0      - SUCCESS
19# 1      - most likely QEMU failed
20# 2      - most likely a run script failed
21# 3      - most likely the unittest failed
22# 124    - most likely the unittest timed out
23# 127    - most likely the unittest called abort()
24# 1..127 - FAILURE (could be QEMU, a run script, or the unittest)
25# >= 128 - Signal (signum = status - 128)
26##############################################################################
27run_qemu ()
28{
29	local stdout errors ret sig
30
31	# stdout to {stdout}, stderr to $errors and stderr
32	exec {stdout}>&1
33	errors=$("${@}" 2> >(tee /dev/stderr) > /dev/fd/$stdout)
34	ret=$?
35	exec {stdout}>&-
36
37	if [ "$errors" ]; then
38		sig=$(grep 'terminating on signal' <<<"$errors")
39		if [ "$sig" ]; then
40			sig=$(sed 's/.*terminating on signal \([0-9][0-9]*\).*/\1/' <<<"$sig")
41		fi
42	fi
43
44	if [ $ret -eq 0 ]; then
45		# Some signals result in a zero return status, but the
46		# error log tells the truth.
47		if [ "$sig" ]; then
48			((ret=sig+128))
49		else
50			# Exiting with zero (non-debugexit) is an error
51			ret=1
52		fi
53	elif [ $ret -eq 1 ]; then
54		# Even when ret==1 (unittest success) if we also got stderr
55		# logs, then we assume a QEMU failure. Otherwise we translate
56		# status of 1 to 0 (SUCCESS)
57		if [ -z "$(echo "$errors" | grep -vi warning)" ]; then
58			ret=0
59		fi
60	fi
61
62	return $ret
63}
64
65timeout_cmd ()
66{
67	if [ "$TIMEOUT" ] && [ "$TIMEOUT" != "0" ]; then
68		echo "timeout -k 1s --foreground $TIMEOUT"
69	fi
70}
71