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