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/>.
30 QEMU_FIFO_IN
="${QEMU_TEST_DIR}/qmp-in-$$"
31 QEMU_FIFO_OUT
="${QEMU_TEST_DIR}/qmp-out-$$"
36 # If bash version is >= 4.1, these will be overwritten and dynamic
37 # file descriptor values assigned.
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
47 # $1: The handle to use
48 # $2+ All remaining arguments comprise the string to search for
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
56 # If $capture_events is non-empty, then any QMP event names it lists
57 # will not be echoed out, but instead collected in the $QEMU_EVENTS
58 # variable. The _wait_event function can later be used to receive
61 # If $only_capture_events is set to anything but an empty string,
62 # then an error will be raised if a QMP message is seen which is
63 # not an event listed in $capture_events.
65 # If $success_or_failure is set, the meaning of the arguments is
67 # $2: A string to search for in the response; if found, this indicates
68 # success and ${QEMU_STATUS[$1]} is set to 0.
69 # $3: A string to search for in the response; if found, this indicates
70 # failure and the test is either aborted (if $qemu_error_no_exit
71 # is not set) or ${QEMU_STATUS[$1]} is set to -1 (otherwise).
77 if [ -z "${success_or_failure}" ]; then
88 while IFS
= read -t ${QEMU_COMM_TIMEOUT} resp
<&${QEMU_OUT[$h]}
90 if [ -n "$capture_events" ]; then
93 for evname
in $capture_events
96 *\"event
\":\
\"${evname}\"* ) capture
=1 ;;
101 ev
=$
(echo "${resp}" |
tr -d '\r' |
tr % .
)
102 QEMU_EVENTS
="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
103 if [ -n "$only_capture_events" ]; then
110 if [ -n "$only_capture_events" ]; then
111 echo "Only expected $capture_events but got ${resp}"
115 if [ -z "${silent}" ] && [ -z "${mismatch_only}" ]; then
116 echo "${resp}" | _filter_testdir | _filter_qemu \
117 | _filter_qemu_io | _filter_qmp | _filter_hmp
119 if [ -n "${failure_match}" ]; then
120 grep -q "${failure_match}" < <(echo "${resp}")
121 if [ $?
-eq 0 ]; then
126 grep -q "${success_match}" < <(echo "${resp}")
127 if [ $?
-eq 0 ]; then
130 if [ -z "${silent}" ] && [ -n "${mismatch_only}" ]; then
131 echo "${resp}" | _filter_testdir | _filter_qemu \
132 | _filter_qemu_io | _filter_qmp | _filter_hmp
137 if [ -z "${qemu_error_no_exit}" ]; then
138 if [ -n "${timeout}" ]; then
139 echo "Timeout waiting for ${success_match} on handle ${h}"
141 echo "Wrong response matching ${failure_match} on handle ${h}"
143 exit 1 # Timeout or wrong match mean the test failed
148 # Sends QMP or HMP command to QEMU, and waits for the expected response
150 # $1: QEMU handle to use
151 # $2: String of the QMP command to send
152 # ${@: -1} (Last string passed)
153 # String that the QEMU response should contain. If it is a null
154 # string, do not wait for a response
156 # Set qemu_cmd_repeat to the number of times to repeat the cmd
157 # until either timeout, or a response. If it is not set, or <=0,
158 # then the command is only sent once.
160 # If neither $silent nor $mismatch_only is set, and $cmd begins with '{',
161 # echo the command before sending it the first time.
163 # If $qemu_error_no_exit is set, then even if the expected response
164 # is not seen, we will not exit. $QEMU_STATUS[$1] will be set it -1 in
167 # If $success_or_failure is set, then the last two strings are the
168 # strings the response will be scanned for. The first of the two
169 # indicates success, the latter indicates failure. Failure is handled
176 local use_error
=${qemu_error_no_exit}
179 if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev
/null
; then
180 count
=${qemu_cmd_repeat}
187 # Display QMP being sent, but not HMP (since HMP already echoes its
188 # input back to output); decide based on leading '{'
189 if [ -z "$silent" ] && [ -z "$mismatch_only" ] &&
190 [ "$cmd" != "${cmd#\{}" ]; then
191 echo "${cmd}" | _filter_testdir | _filter_imgfmt
193 while [ ${count} -gt 0 ]
195 echo "${cmd}" >&${QEMU_IN[${h}]}
196 if [ -n "${1}" ]; then
197 if [ -z "${success_or_failure}" ]; then
198 qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}"
200 qemu_error_no_exit
=${use_error} _timed_wait_for ${h} "${1}" "${2}"
202 if [ ${QEMU_STATUS[$h]} -eq 0 ]; then
208 if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then
209 echo "Timeout waiting
for command ${1} response on handle
${h}"
210 exit 1 #Timeout means the test failed
215 # Check event cache for a named QMP event
218 # $1: Name of the QMP event to check for
220 # Checks if the named QMP event that was previously captured
221 # into $QEMU_EVENTS. When matched, the QMP event will be echoed
222 # and the $matched variable set to 1.
224 # _wait_event is more suitable for test usage in most cases
225 _check_cached_events()
229 local match="\"event
\": \"$evname\""
232 if [ -n "$QEMU_EVENTS" ]; then
233 CURRENT_QEMU_EVENTS=$QEMU_EVENTS
237 for ev in $CURRENT_QEMU_EVENTS
239 grep -q "$match" < <(echo "${ev}")
240 if [ $? -eq 0 ] && [ $matched = 0 ]; then
241 echo "${ev}" | _filter_testdir | _filter_qemu \
242 | _filter_qemu_io | _filter_qmp | _filter_hmp
245 QEMU_EVENTS="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
252 # Wait for a named QMP event
255 # $1: QEMU handle to use
256 # $2: Name of the QMP event to wait for
258 # Checks if the named QMP even was previously captured
259 # into $QEMU_EVENTS. If none are present, then waits for the
260 # event to arrive on the QMP channel. When matched, the QMP
261 # event will be echoed
269 _check_cached_events $evname
276 only_capture_events=1 qemu_error_no_exit=1 _timed_wait_for ${h}
278 if [ ${QEMU_STATUS[$h]} -ne 0 ] ; then
279 echo "Timeout waiting
for event
${evname} on handle
${h}"
280 exit 1 #Timeout means the test failed
285 # Launch a QEMU process.
288 # $qemu_comm_method: set this variable to 'monitor' (case insensitive)
289 # to use the QEMU HMP monitor for communication.
290 # Otherwise, the default of QMP is used.
291 # $qmp_pretty: Set this variable to 'y' to enable QMP pretty printing.
292 # $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on stderr.
293 # If this variable is empty, stderr will be redirected to stdout.
295 # $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance.
303 if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor
" ]])
305 comm="-monitor stdio
"
307 local qemu_comm_method="qmp
"
308 if [ "$qmp_pretty" = "y
" ]; then
309 comm="-monitor none
-qmp-pretty stdio
"
311 comm="-monitor none
-qmp stdio
"
315 fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE}
316 fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE}
321 if [ -n "$IMGKEYSECRET" ]; then
322 object_options="--object secret
,id
=keysec0
,data
=$IMGKEYSECRET"
325 if [ -z "$keep_stderr" ]; then
327 ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
330 elif [ "$keep_stderr" = "y" ]; then
332 ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
338 if [[ "${BASH_VERSINFO[0]}" -ge "5" ||
339 ("${BASH_VERSINFO[0]}" -ge "4" && "${BASH_VERSINFO[1]}" -ge "1") ]]
341 # bash >= 4.1 required for automatic fd
342 exec {_out_fd}<"${fifo_out}"
343 exec {_in_fd}>"${fifo_in}"
347 eval "exec ${_out_fd}<'${fifo_out}'"
348 eval "exec ${_in_fd}>'${fifo_in}'"
351 QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd}
352 QEMU_IN[${_QEMU_HANDLE}]=${_in_fd}
353 QEMU_STATUS[${_QEMU_HANDLE}]=0
355 if [ "${qemu_comm_method}" == "qmp
" ]
357 # Don't print response, since it has version information in it
358 silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities
"
359 if [ "$qmp_pretty" = "y
" ]; then
360 silent=yes _timed_wait_for ${_QEMU_HANDLE} "^
}"
363 QEMU_HANDLE=${_QEMU_HANDLE}
368 # Silently kills the QEMU process
370 # If $wait is set to anything other than the empty string, the process will not
371 # be killed but only waited for, and any output will be forwarded to stdout. If
372 # $wait is empty, the process will be killed and all output will be suppressed.
375 # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices
376 for i in "${!QEMU_OUT[@]}"
379 if [ -f "${QEMU_TEST_DIR}/qemu-
${i}.pid
" ]; then
380 read QEMU_PID < "${QEMU_TEST_DIR}/qemu-
${i}.pid
"
381 rm -f "${QEMU_TEST_DIR}/qemu-
${i}.pid
"
382 if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then
383 kill -KILL ${QEMU_PID} 2>/dev/null
385 if [ -n "${QEMU_PID}" ]; then
386 wait ${QEMU_PID} 2>/dev/null # silent kill
390 if [ -n "${wait}" ]; then
391 cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \
392 | _filter_qemu_io | _filter_qmp | _filter_hmp
394 rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}"
395 eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors
396 eval "exec ${QEMU_OUT[$i]}<&-"