Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu/ar7.git] / tests / qemu-iotests / common.qemu
blob85f66b852cd898d7780c099aed525a6ed5914837
1 #!/bin/bash
3 # This allows for launching of multiple QEMU instances, with independent
4 # communication possible to each instance.
6 # Each instance can choose, at launch, to use either the QMP or the
7 # HMP (monitor) interface.
9 # All instances are cleaned up via _cleanup_qemu, including killing the
10 # running qemu instance.
12 # Copyright (C) 2014 Red Hat, Inc.
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 QEMU_COMM_TIMEOUT=10
30 QEMU_FIFO_IN="${QEMU_TEST_DIR}/qmp-in-$$"
31 QEMU_FIFO_OUT="${QEMU_TEST_DIR}/qmp-out-$$"
33 QEMU_HANDLE=0
34 export _QEMU_HANDLE=0
36 # If bash version is >= 4.1, these will be overwritten and dynamic
37 # file descriptor values assigned.
38 _out_fd=3
39 _in_fd=4
41 # Wait for expected QMP response from QEMU. Will time out
42 # after 10 seconds, which counts as failure.
44 # Override QEMU_COMM_TIMEOUT for a timeout different than the
45 # default 10 seconds
47 # $1: The handle to use
48 # $2+ All remaining arguments comprise the string to search for
49 # in the response.
51 # If $silent is set to anything but an empty string, then
52 # response is not echoed out.
53 # If $mismatch_only is set, only non-matching responses will
54 # be echoed.
55 function _timed_wait_for()
57 local h=${1}
58 shift
60 QEMU_STATUS[$h]=0
61 while IFS= read -t ${QEMU_COMM_TIMEOUT} resp <&${QEMU_OUT[$h]}
63 if [ -z "${silent}" ] && [ -z "${mismatch_only}" ]; then
64 echo "${resp}" | _filter_testdir | _filter_qemu \
65 | _filter_qemu_io | _filter_qmp | _filter_hmp
67 grep -q "${*}" < <(echo "${resp}")
68 if [ $? -eq 0 ]; then
69 return
70 elif [ -z "${silent}" ] && [ -n "${mismatch_only}" ]; then
71 echo "${resp}" | _filter_testdir | _filter_qemu \
72 | _filter_qemu_io | _filter_qmp | _filter_hmp
75 done
76 QEMU_STATUS[$h]=-1
77 if [ -z "${qemu_error_no_exit}" ]; then
78 echo "Timeout waiting for ${*} on handle ${h}"
79 exit 1 # Timeout means the test failed
84 # Sends QMP or HMP command to QEMU, and waits for the expected response
86 # $1: QEMU handle to use
87 # $2: String of the QMP command to send
88 # ${@: -1} (Last string passed)
89 # String that the QEMU response should contain. If it is a null
90 # string, do not wait for a response
92 # Set qemu_cmd_repeat to the number of times to repeat the cmd
93 # until either timeout, or a response. If it is not set, or <=0,
94 # then the command is only sent once.
96 # If $qemu_error_no_exit is set, then even if the expected response
97 # is not seen, we will not exit. $QEMU_STATUS[$1] will be set it -1 in
98 # that case.
99 function _send_qemu_cmd()
101 local h=${1}
102 local count=1
103 local cmd=
104 local use_error=${qemu_error_no_exit}
105 shift
107 if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev/null; then
108 count=${qemu_cmd_repeat}
109 use_error="no"
111 # This array element extraction is done to accommodate pathnames with spaces
112 cmd=${@: 1:${#@}-1}
113 shift $(($# - 1))
115 while [ ${count} -gt 0 ]
117 echo "${cmd}" >&${QEMU_IN[${h}]}
118 if [ -n "${1}" ]; then
119 qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}"
120 if [ ${QEMU_STATUS[$h]} -eq 0 ]; then
121 return
124 let count--;
125 done
126 if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then
127 echo "Timeout waiting for ${1} on handle ${h}"
128 exit 1 #Timeout means the test failed
133 # Launch a QEMU process.
135 # Input parameters:
136 # $qemu_comm_method: set this variable to 'monitor' (case insensitive)
137 # to use the QEMU HMP monitor for communication.
138 # Otherwise, the default of QMP is used.
139 # $qmp_pretty: Set this variable to 'y' to enable QMP pretty printing.
140 # $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on stderr.
141 # If this variable is empty, stderr will be redirected to stdout.
142 # Returns:
143 # $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance.
145 function _launch_qemu()
147 local comm=
148 local fifo_out=
149 local fifo_in=
151 if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor" ]])
152 then
153 comm="-monitor stdio"
154 else
155 local qemu_comm_method="qmp"
156 if [ "$qmp_pretty" = "y" ]; then
157 comm="-monitor none -qmp-pretty stdio"
158 else
159 comm="-monitor none -qmp stdio"
163 fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE}
164 fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE}
165 mkfifo "${fifo_out}"
166 mkfifo "${fifo_in}"
168 object_options=
169 if [ -n "$IMGKEYSECRET" ]; then
170 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
173 if [ -z "$keep_stderr" ]; then
174 QEMU_NEED_PID='y'\
175 ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
176 2>&1 \
177 <"${fifo_in}" &
178 elif [ "$keep_stderr" = "y" ]; then
179 QEMU_NEED_PID='y'\
180 ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
181 <"${fifo_in}" &
182 else
183 exit 1
186 if [[ "${BASH_VERSINFO[0]}" -ge "5" ||
187 ("${BASH_VERSINFO[0]}" -ge "4" && "${BASH_VERSINFO[1]}" -ge "1") ]]
188 then
189 # bash >= 4.1 required for automatic fd
190 exec {_out_fd}<"${fifo_out}"
191 exec {_in_fd}>"${fifo_in}"
192 else
193 let _out_fd++
194 let _in_fd++
195 eval "exec ${_out_fd}<'${fifo_out}'"
196 eval "exec ${_in_fd}>'${fifo_in}'"
199 QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd}
200 QEMU_IN[${_QEMU_HANDLE}]=${_in_fd}
201 QEMU_STATUS[${_QEMU_HANDLE}]=0
203 if [ "${qemu_comm_method}" == "qmp" ]
204 then
205 # Don't print response, since it has version information in it
206 silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities"
207 if [ "$qmp_pretty" = "y" ]; then
208 silent=yes _timed_wait_for ${_QEMU_HANDLE} "^}"
211 QEMU_HANDLE=${_QEMU_HANDLE}
212 let _QEMU_HANDLE++
216 # Silenty kills the QEMU process
218 # If $wait is set to anything other than the empty string, the process will not
219 # be killed but only waited for, and any output will be forwarded to stdout. If
220 # $wait is empty, the process will be killed and all output will be suppressed.
221 function _cleanup_qemu()
223 # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices
224 for i in "${!QEMU_OUT[@]}"
226 local QEMU_PID
227 if [ -f "${QEMU_TEST_DIR}/qemu-${i}.pid" ]; then
228 read QEMU_PID < "${QEMU_TEST_DIR}/qemu-${i}.pid"
229 rm -f "${QEMU_TEST_DIR}/qemu-${i}.pid"
230 if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then
231 kill -KILL ${QEMU_PID} 2>/dev/null
233 if [ -n "${QEMU_PID}" ]; then
234 wait ${QEMU_PID} 2>/dev/null # silent kill
238 if [ -n "${wait}" ]; then
239 cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \
240 | _filter_qemu_io | _filter_qmp | _filter_hmp
242 rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}"
243 eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors
244 eval "exec ${QEMU_OUT[$i]}<&-"
246 unset QEMU_IN[$i]
247 unset QEMU_OUT[$i]
248 done