xref: /kvm-unit-tests/scripts/arch-run.bash (revision 533a738f363e83f810f5ab89a87d6abbbe944b26)
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=$("${@}" </dev/null 2> >(tee /dev/stderr) > /dev/fd/$stdout)
34	ret=$?
35	exec {stdout}>&-
36
37	[ $ret -eq 134 ] && echo "QEMU Aborted" >&2
38
39	if [ "$errors" ]; then
40		sig=$(grep 'terminating on signal' <<<"$errors")
41		if [ "$sig" ]; then
42			sig=$(sed 's/.*terminating on signal \([0-9][0-9]*\).*/\1/' <<<"$sig")
43		fi
44	fi
45
46	if [ $ret -eq 0 ]; then
47		# Some signals result in a zero return status, but the
48		# error log tells the truth.
49		if [ "$sig" ]; then
50			((ret=sig+128))
51		else
52			# Exiting with zero (non-debugexit) is an error
53			ret=1
54		fi
55	elif [ $ret -eq 1 ]; then
56		# Even when ret==1 (unittest success) if we also got stderr
57		# logs, then we assume a QEMU failure. Otherwise we translate
58		# status of 1 to 0 (SUCCESS)
59		if [ -z "$(echo "$errors" | grep -vi warning)" ]; then
60			ret=0
61		fi
62	fi
63
64	return $ret
65}
66
67timeout_cmd ()
68{
69	if [ "$TIMEOUT" ] && [ "$TIMEOUT" != "0" ]; then
70		echo "timeout -k 1s --foreground $TIMEOUT"
71	fi
72}
73