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 read_timeout
="-t ${QEMU_COMM_TIMEOUT}"
89 if [ -n "${GDB_OPTIONS}" ]; then
93 while IFS
= read ${read_timeout} resp
<&${QEMU_OUT[$h]}
95 if [ -n "$capture_events" ]; then
98 for evname
in $capture_events
101 *\"event
\":\
\"${evname}\"* ) capture
=1 ;;
106 ev
=$
(echo "${resp}" |
tr -d '\r' |
tr % .
)
107 QEMU_EVENTS
="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
108 if [ -n "$only_capture_events" ]; then
115 if [ -n "$only_capture_events" ]; then
116 echo "Only expected $capture_events but got ${resp}"
120 if [ -z "${silent}" ] && [ -z "${mismatch_only}" ]; then
121 echo "${resp}" | _filter_testdir | _filter_qemu \
122 | _filter_qemu_io | _filter_qmp | _filter_hmp
124 if [ -n "${failure_match}" ]; then
125 grep -q "${failure_match}" < <(echo "${resp}")
126 if [ $?
-eq 0 ]; then
131 grep -q "${success_match}" < <(echo "${resp}")
132 if [ $?
-eq 0 ]; then
135 if [ -z "${silent}" ] && [ -n "${mismatch_only}" ]; then
136 echo "${resp}" | _filter_testdir | _filter_qemu \
137 | _filter_qemu_io | _filter_qmp | _filter_hmp
142 if [ -z "${qemu_error_no_exit}" ]; then
143 if [ -n "${timeout}" ]; then
144 echo "Timeout waiting for ${success_match} on handle ${h}"
146 echo "Wrong response matching ${failure_match} on handle ${h}"
148 exit 1 # Timeout or wrong match mean the test failed
153 # Sends QMP or HMP command to QEMU, and waits for the expected response
155 # $1: QEMU handle to use
156 # $2: String of the QMP command to send
157 # ${@: -1} (Last string passed)
158 # String that the QEMU response should contain. If it is a null
159 # string, do not wait for a response
161 # Set qemu_cmd_repeat to the number of times to repeat the cmd
162 # until either timeout, or a response. If it is not set, or <=0,
163 # then the command is only sent once.
165 # If neither $silent nor $mismatch_only is set, and $cmd begins with '{',
166 # echo the command before sending it the first time.
168 # If $qemu_error_no_exit is set, then even if the expected response
169 # is not seen, we will not exit. $QEMU_STATUS[$1] will be set it -1 in
172 # If $success_or_failure is set, then the last two strings are the
173 # strings the response will be scanned for. The first of the two
174 # indicates success, the latter indicates failure. Failure is handled
181 local use_error
=${qemu_error_no_exit}
184 if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev
/null
; then
185 count
=${qemu_cmd_repeat}
192 # Display QMP being sent, but not HMP (since HMP already echoes its
193 # input back to output); decide based on leading '{'
194 if [ -z "$silent" ] && [ -z "$mismatch_only" ] &&
195 [ "$cmd" != "${cmd#\{}" ]; then
196 echo "${cmd}" | _filter_testdir | _filter_imgfmt
198 while [ ${count} -gt 0 ]
200 echo "${cmd}" >&${QEMU_IN[${h}]}
201 if [ -n "${1}" ]; then
202 if [ -z "${success_or_failure}" ]; then
203 qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}"
205 qemu_error_no_exit
=${use_error} _timed_wait_for ${h} "${1}" "${2}"
207 if [ ${QEMU_STATUS[$h]} -eq 0 ]; then
213 if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then
214 echo "Timeout waiting
for command ${1} response on handle
${h}"
215 exit 1 #Timeout means the test failed
220 # Check event cache for a named QMP event
223 # $1: Name of the QMP event to check for
225 # Checks if the named QMP event that was previously captured
226 # into $QEMU_EVENTS. When matched, the QMP event will be echoed
227 # and the $matched variable set to 1.
229 # _wait_event is more suitable for test usage in most cases
230 _check_cached_events()
234 local match="\"event
\": \"$evname\""
237 if [ -n "$QEMU_EVENTS" ]; then
238 CURRENT_QEMU_EVENTS=$QEMU_EVENTS
242 for ev in $CURRENT_QEMU_EVENTS
244 grep -q "$match" < <(echo "${ev}")
245 if [ $? -eq 0 ] && [ $matched = 0 ]; then
246 echo "${ev}" | _filter_testdir | _filter_qemu \
247 | _filter_qemu_io | _filter_qmp | _filter_hmp
250 QEMU_EVENTS="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
257 # Wait for a named QMP event
260 # $1: QEMU handle to use
261 # $2: Name of the QMP event to wait for
263 # Checks if the named QMP even was previously captured
264 # into $QEMU_EVENTS. If none are present, then waits for the
265 # event to arrive on the QMP channel. When matched, the QMP
266 # event will be echoed
274 _check_cached_events $evname
281 only_capture_events=1 qemu_error_no_exit=1 _timed_wait_for ${h}
283 if [ ${QEMU_STATUS[$h]} -ne 0 ] ; then
284 echo "Timeout waiting
for event
${evname} on handle
${h}"
285 exit 1 #Timeout means the test failed
290 # Launch a QEMU process.
293 # $qemu_comm_method: set this variable to 'monitor' (case insensitive)
294 # to use the QEMU HMP monitor for communication.
295 # Otherwise, the default of QMP is used.
296 # $qmp_pretty: Set this variable to 'y' to enable QMP pretty printing.
297 # $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on stderr.
298 # If this variable is empty, stderr will be redirected to stdout.
300 # $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance.
308 if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor
" ]])
310 comm="-monitor stdio
"
312 local qemu_comm_method="qmp
"
313 if [ "$qmp_pretty" = "y
" ]; then
314 comm="-monitor none
-qmp-pretty stdio
"
316 comm="-monitor none
-qmp stdio
"
320 fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE}
321 fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE}
326 if [ -n "$IMGKEYSECRET" ]; then
327 object_options="--object secret
,id
=keysec0
,data
=$IMGKEYSECRET"
330 if [ -z "$keep_stderr" ]; then
332 ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
335 elif [ "$keep_stderr" = "y" ]; then
337 ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
343 if [[ "${BASH_VERSINFO[0]}" -ge "5" ||
344 ("${BASH_VERSINFO[0]}" -ge "4" && "${BASH_VERSINFO[1]}" -ge "1") ]]
346 # bash >= 4.1 required for automatic fd
347 exec {_out_fd}<"${fifo_out}"
348 exec {_in_fd}>"${fifo_in}"
352 eval "exec ${_out_fd}<'${fifo_out}'"
353 eval "exec ${_in_fd}>'${fifo_in}'"
356 QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd}
357 QEMU_IN[${_QEMU_HANDLE}]=${_in_fd}
358 QEMU_STATUS[${_QEMU_HANDLE}]=0
360 if [ "${qemu_comm_method}" == "qmp
" ]
362 # Don't print response, since it has version information in it
363 silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities
"
364 if [ "$qmp_pretty" = "y
" ]; then
365 silent=yes _timed_wait_for ${_QEMU_HANDLE} "^
}"
368 QEMU_HANDLE=${_QEMU_HANDLE}
373 # Silently kills the QEMU process
375 # If $wait is set to anything other than the empty string, the process will not
376 # be killed but only waited for, and any output will be forwarded to stdout. If
377 # $wait is empty, the process will be killed and all output will be suppressed.
380 # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices
381 for i in "${!QEMU_OUT[@]}"
384 if [ -f "${QEMU_TEST_DIR}/qemu-
${i}.pid
" ]; then
385 read QEMU_PID < "${QEMU_TEST_DIR}/qemu-
${i}.pid
"
386 rm -f "${QEMU_TEST_DIR}/qemu-
${i}.pid
"
387 if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then
388 kill -KILL ${QEMU_PID} 2>/dev/null
390 if [ -n "${QEMU_PID}" ]; then
391 wait ${QEMU_PID} 2>/dev/null # silent kill
395 if [ -n "${wait}" ]; then
396 cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \
397 | _filter_qemu_io | _filter_qmp | _filter_hmp
399 rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}"
400 eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors
401 eval "exec ${QEMU_OUT[$i]}<&-"