xref: /openbmc/qemu/tests/qemu-iotests/common.qemu (revision 9093028dd48c50bc0392791f78aab44afef57ead)
111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash
2e940bc13SJeff Cody#
3e940bc13SJeff Cody# This allows for launching of multiple QEMU instances, with independent
4e940bc13SJeff Cody# communication possible to each instance.
5e940bc13SJeff Cody#
6e940bc13SJeff Cody# Each instance can choose, at launch, to use either the QMP or the
7e940bc13SJeff Cody# HMP (monitor) interface.
8e940bc13SJeff Cody#
9e940bc13SJeff Cody# All instances are cleaned up via _cleanup_qemu, including killing the
10e940bc13SJeff Cody# running qemu instance.
11e940bc13SJeff Cody#
12e940bc13SJeff Cody# Copyright (C) 2014 Red Hat, Inc.
13e940bc13SJeff Cody#
14e940bc13SJeff Cody# This program is free software; you can redistribute it and/or modify
15e940bc13SJeff Cody# it under the terms of the GNU General Public License as published by
16e940bc13SJeff Cody# the Free Software Foundation; either version 2 of the License, or
17e940bc13SJeff Cody# (at your option) any later version.
18e940bc13SJeff Cody#
19e940bc13SJeff Cody# This program is distributed in the hope that it will be useful,
20e940bc13SJeff Cody# but WITHOUT ANY WARRANTY; without even the implied warranty of
21e940bc13SJeff Cody# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22e940bc13SJeff Cody# GNU General Public License for more details.
23e940bc13SJeff Cody#
24e940bc13SJeff Cody# You should have received a copy of the GNU General Public License
25e940bc13SJeff Cody# along with this program.  If not, see <http://www.gnu.org/licenses/>.
26e940bc13SJeff Cody#
27e940bc13SJeff Cody
28e940bc13SJeff CodyQEMU_COMM_TIMEOUT=10
29e940bc13SJeff Cody
30846a1d11SJeff CodyQEMU_FIFO_IN="${QEMU_TEST_DIR}/qmp-in-$$"
31846a1d11SJeff CodyQEMU_FIFO_OUT="${QEMU_TEST_DIR}/qmp-out-$$"
32e940bc13SJeff Cody
33e940bc13SJeff CodyQEMU_HANDLE=0
34cce293a2SPaolo Bonziniexport _QEMU_HANDLE=0
35e940bc13SJeff Cody
36e940bc13SJeff Cody# If bash version is >= 4.1, these will be overwritten and dynamic
37e940bc13SJeff Cody# file descriptor values assigned.
38e940bc13SJeff Cody_out_fd=3
39e940bc13SJeff Cody_in_fd=4
40e940bc13SJeff Cody
41e940bc13SJeff Cody# Wait for expected QMP response from QEMU.  Will time out
42e940bc13SJeff Cody# after 10 seconds, which counts as failure.
43e940bc13SJeff Cody#
44e940bc13SJeff Cody# Override QEMU_COMM_TIMEOUT for a timeout different than the
45e940bc13SJeff Cody# default 10 seconds
46e940bc13SJeff Cody#
47e940bc13SJeff Cody# $1: The handle to use
48e940bc13SJeff Cody# $2+ All remaining arguments comprise the string to search for
49e940bc13SJeff Cody#    in the response.
50e940bc13SJeff Cody#
51e940bc13SJeff Cody# If $silent is set to anything but an empty string, then
52e940bc13SJeff Cody# response is not echoed out.
53a2339699SJeff Cody# If $mismatch_only is set, only non-matching responses will
54a2339699SJeff Cody# be echoed.
5581c6ddf4SMax Reitz#
56aae12d4bSDaniel P. Berrangé# If $capture_events is non-empty, then any QMP event names it lists
57aae12d4bSDaniel P. Berrangé# will not be echoed out, but instead collected in the $QEMU_EVENTS
58aae12d4bSDaniel P. Berrangé# variable. The _wait_event function can later be used to receive
59aae12d4bSDaniel P. Berrangé# the cached events.
60aae12d4bSDaniel P. Berrangé#
61aae12d4bSDaniel P. Berrangé# If $only_capture_events is set to anything but an empty string,
62aae12d4bSDaniel P. Berrangé# then an error will be raised if a QMP message is seen which is
63aae12d4bSDaniel P. Berrangé# not an event listed in $capture_events.
64aae12d4bSDaniel P. Berrangé#
6581c6ddf4SMax Reitz# If $success_or_failure is set, the meaning of the arguments is
6681c6ddf4SMax Reitz# changed as follows:
6781c6ddf4SMax Reitz# $2: A string to search for in the response; if found, this indicates
6881c6ddf4SMax Reitz#     success and ${QEMU_STATUS[$1]} is set to 0.
6981c6ddf4SMax Reitz# $3: A string to search for in the response; if found, this indicates
7081c6ddf4SMax Reitz#     failure and the test is either aborted (if $qemu_error_no_exit
7181c6ddf4SMax Reitz#     is not set) or ${QEMU_STATUS[$1]} is set to -1 (otherwise).
728cedcffdSEric Blake_timed_wait_for()
73e940bc13SJeff Cody{
74e940bc13SJeff Cody    local h=${1}
75e940bc13SJeff Cody    shift
76e940bc13SJeff Cody
7781c6ddf4SMax Reitz    if [ -z "${success_or_failure}" ]; then
7881c6ddf4SMax Reitz        success_match=${*}
7981c6ddf4SMax Reitz        failure_match=
8081c6ddf4SMax Reitz    else
8181c6ddf4SMax Reitz        success_match=${1}
8281c6ddf4SMax Reitz        failure_match=${2}
8381c6ddf4SMax Reitz    fi
8481c6ddf4SMax Reitz
8581c6ddf4SMax Reitz    timeout=yes
8681c6ddf4SMax Reitz
87e940bc13SJeff Cody    QEMU_STATUS[$h]=0
88*4d14db04SEmanuele Giuseppe Esposito    read_timeout="-t ${QEMU_COMM_TIMEOUT}"
89*4d14db04SEmanuele Giuseppe Esposito    if [ -n "${GDB_OPTIONS}" ]; then
90*4d14db04SEmanuele Giuseppe Esposito        read_timeout=
91*4d14db04SEmanuele Giuseppe Esposito    fi
92*4d14db04SEmanuele Giuseppe Esposito
93*4d14db04SEmanuele Giuseppe Esposito    while IFS= read ${read_timeout} resp <&${QEMU_OUT[$h]}
94e940bc13SJeff Cody    do
95aae12d4bSDaniel P. Berrangé        if [ -n "$capture_events" ]; then
96aae12d4bSDaniel P. Berrangé            capture=0
97aae12d4bSDaniel P. Berrangé            local evname
98aae12d4bSDaniel P. Berrangé            for evname in $capture_events
99aae12d4bSDaniel P. Berrangé            do
100aae12d4bSDaniel P. Berrangé                case ${resp} in
101aae12d4bSDaniel P. Berrangé                    *\"event\":\ \"${evname}\"* ) capture=1 ;;
102aae12d4bSDaniel P. Berrangé                esac
103aae12d4bSDaniel P. Berrangé            done
104aae12d4bSDaniel P. Berrangé            if [ $capture = 1 ];
105aae12d4bSDaniel P. Berrangé            then
106aae12d4bSDaniel P. Berrangé                ev=$(echo "${resp}" | tr -d '\r' | tr % .)
107aae12d4bSDaniel P. Berrangé                QEMU_EVENTS="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
108aae12d4bSDaniel P. Berrangé                if [ -n "$only_capture_events" ]; then
109aae12d4bSDaniel P. Berrangé                    return
110aae12d4bSDaniel P. Berrangé                else
111aae12d4bSDaniel P. Berrangé                    continue
112aae12d4bSDaniel P. Berrangé                fi
113aae12d4bSDaniel P. Berrangé            fi
114aae12d4bSDaniel P. Berrangé        fi
115aae12d4bSDaniel P. Berrangé        if [ -n "$only_capture_events" ]; then
116aae12d4bSDaniel P. Berrangé            echo "Only expected $capture_events but got ${resp}"
117aae12d4bSDaniel P. Berrangé            exit 1
118aae12d4bSDaniel P. Berrangé        fi
119aae12d4bSDaniel P. Berrangé
120a2339699SJeff Cody        if [ -z "${silent}" ] && [ -z "${mismatch_only}" ]; then
121e940bc13SJeff Cody            echo "${resp}" | _filter_testdir | _filter_qemu \
12269404d9eSKevin Wolf                           | _filter_qemu_io | _filter_qmp | _filter_hmp
123e940bc13SJeff Cody        fi
12481c6ddf4SMax Reitz        if [ -n "${failure_match}" ]; then
12581c6ddf4SMax Reitz            grep -q "${failure_match}" < <(echo "${resp}")
12681c6ddf4SMax Reitz            if [ $? -eq 0 ]; then
12781c6ddf4SMax Reitz                timeout=
12881c6ddf4SMax Reitz                break
12981c6ddf4SMax Reitz            fi
13081c6ddf4SMax Reitz        fi
13181c6ddf4SMax Reitz        grep -q "${success_match}" < <(echo "${resp}")
132e940bc13SJeff Cody        if [ $? -eq 0 ]; then
133e940bc13SJeff Cody            return
13481c6ddf4SMax Reitz        fi
13581c6ddf4SMax Reitz        if [ -z "${silent}" ] && [ -n "${mismatch_only}" ]; then
136a2339699SJeff Cody            echo "${resp}" | _filter_testdir | _filter_qemu \
137a2339699SJeff Cody                           | _filter_qemu_io | _filter_qmp | _filter_hmp
138e940bc13SJeff Cody        fi
139a2339699SJeff Cody
140e940bc13SJeff Cody    done
141e940bc13SJeff Cody    QEMU_STATUS[$h]=-1
142e940bc13SJeff Cody    if [ -z "${qemu_error_no_exit}" ]; then
14381c6ddf4SMax Reitz        if [ -n "${timeout}" ]; then
14481c6ddf4SMax Reitz            echo "Timeout waiting for ${success_match} on handle ${h}"
14581c6ddf4SMax Reitz        else
14681c6ddf4SMax Reitz            echo "Wrong response matching ${failure_match} on handle ${h}"
14781c6ddf4SMax Reitz        fi
14881c6ddf4SMax Reitz        exit 1  # Timeout or wrong match mean the test failed
149e940bc13SJeff Cody    fi
150e940bc13SJeff Cody}
151e940bc13SJeff Cody
152e940bc13SJeff Cody
153e940bc13SJeff Cody# Sends QMP or HMP command to QEMU, and waits for the expected response
154e940bc13SJeff Cody#
155e940bc13SJeff Cody# $1:       QEMU handle to use
156e940bc13SJeff Cody# $2:       String of the QMP command to send
157e940bc13SJeff Cody# ${@: -1}  (Last string passed)
158e940bc13SJeff Cody#             String that the QEMU response should contain. If it is a null
159e940bc13SJeff Cody#             string, do not wait for a response
160e940bc13SJeff Cody#
161e940bc13SJeff Cody# Set qemu_cmd_repeat to the number of times to repeat the cmd
162e940bc13SJeff Cody# until either timeout, or a response.  If it is not set, or <=0,
163e940bc13SJeff Cody# then the command is only sent once.
164e940bc13SJeff Cody#
165a98b1a1fSEric Blake# If neither $silent nor $mismatch_only is set, and $cmd begins with '{',
166a98b1a1fSEric Blake# echo the command before sending it the first time.
167a98b1a1fSEric Blake#
168e940bc13SJeff Cody# If $qemu_error_no_exit is set, then even if the expected response
169e940bc13SJeff Cody# is not seen, we will not exit.  $QEMU_STATUS[$1] will be set it -1 in
170e940bc13SJeff Cody# that case.
17181c6ddf4SMax Reitz#
17281c6ddf4SMax Reitz# If $success_or_failure is set, then the last two strings are the
17381c6ddf4SMax Reitz# strings the response will be scanned for.  The first of the two
17481c6ddf4SMax Reitz# indicates success, the latter indicates failure.  Failure is handled
17581c6ddf4SMax Reitz# like a timeout.
1768cedcffdSEric Blake_send_qemu_cmd()
177e940bc13SJeff Cody{
178e940bc13SJeff Cody    local h=${1}
179e940bc13SJeff Cody    local count=1
180e940bc13SJeff Cody    local cmd=
181e940bc13SJeff Cody    local use_error=${qemu_error_no_exit}
182e940bc13SJeff Cody    shift
183e940bc13SJeff Cody
184e940bc13SJeff Cody    if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev/null; then
185e940bc13SJeff Cody        count=${qemu_cmd_repeat}
186e940bc13SJeff Cody        use_error="no"
187e940bc13SJeff Cody    fi
1880e720781SMax Reitz
1890e720781SMax Reitz    cmd=$1
1900e720781SMax Reitz    shift
191e940bc13SJeff Cody
192a98b1a1fSEric Blake    # Display QMP being sent, but not HMP (since HMP already echoes its
193a98b1a1fSEric Blake    # input back to output); decide based on leading '{'
194a98b1a1fSEric Blake    if [ -z "$silent" ] && [ -z "$mismatch_only" ] &&
195a98b1a1fSEric Blake            [ "$cmd" != "${cmd#\{}" ]; then
196a98b1a1fSEric Blake        echo "${cmd}" | _filter_testdir | _filter_imgfmt
197a98b1a1fSEric Blake    fi
198e940bc13SJeff Cody    while [ ${count} -gt 0 ]
199e940bc13SJeff Cody    do
200e940bc13SJeff Cody        echo "${cmd}" >&${QEMU_IN[${h}]}
201e940bc13SJeff Cody        if [ -n "${1}" ]; then
20281c6ddf4SMax Reitz            if [ -z "${success_or_failure}" ]; then
203e940bc13SJeff Cody                qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}"
20481c6ddf4SMax Reitz            else
20581c6ddf4SMax Reitz                qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}" "${2}"
20681c6ddf4SMax Reitz            fi
207e940bc13SJeff Cody            if [ ${QEMU_STATUS[$h]} -eq 0 ]; then
208e940bc13SJeff Cody                return
209e940bc13SJeff Cody            fi
210e940bc13SJeff Cody        fi
211e940bc13SJeff Cody        let count--;
212e940bc13SJeff Cody    done
213e940bc13SJeff Cody    if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then
214aae12d4bSDaniel P. Berrangé        echo "Timeout waiting for command ${1} response on handle ${h}"
215e940bc13SJeff Cody        exit 1 #Timeout means the test failed
216e940bc13SJeff Cody    fi
217e940bc13SJeff Cody}
218e940bc13SJeff Cody
219e940bc13SJeff Cody
220aae12d4bSDaniel P. Berrangé# Check event cache for a named QMP event
221aae12d4bSDaniel P. Berrangé#
222aae12d4bSDaniel P. Berrangé# Input parameters:
223aae12d4bSDaniel P. Berrangé# $1:       Name of the QMP event to check for
224aae12d4bSDaniel P. Berrangé#
225aae12d4bSDaniel P. Berrangé# Checks if the named QMP event that was previously captured
226aae12d4bSDaniel P. Berrangé# into $QEMU_EVENTS. When matched, the QMP event will be echoed
227aae12d4bSDaniel P. Berrangé# and the $matched variable set to 1.
228aae12d4bSDaniel P. Berrangé#
229aae12d4bSDaniel P. Berrangé# _wait_event is more suitable for test usage in most cases
230aae12d4bSDaniel P. Berrangé_check_cached_events()
231aae12d4bSDaniel P. Berrangé{
232aae12d4bSDaniel P. Berrangé    local evname=${1}
233aae12d4bSDaniel P. Berrangé
234aae12d4bSDaniel P. Berrangé    local match="\"event\": \"$evname\""
235aae12d4bSDaniel P. Berrangé
236aae12d4bSDaniel P. Berrangé    matched=0
237aae12d4bSDaniel P. Berrangé    if [ -n "$QEMU_EVENTS" ]; then
238aae12d4bSDaniel P. Berrangé        CURRENT_QEMU_EVENTS=$QEMU_EVENTS
239aae12d4bSDaniel P. Berrangé        QEMU_EVENTS=
240aae12d4bSDaniel P. Berrangé        old_IFS=$IFS
241aae12d4bSDaniel P. Berrangé        IFS="%"
242aae12d4bSDaniel P. Berrangé        for ev in $CURRENT_QEMU_EVENTS
243aae12d4bSDaniel P. Berrangé        do
244aae12d4bSDaniel P. Berrangé            grep -q "$match" < <(echo "${ev}")
245aae12d4bSDaniel P. Berrangé            if [ $? -eq 0 ] && [ $matched = 0 ]; then
246aae12d4bSDaniel P. Berrangé                echo "${ev}" | _filter_testdir | _filter_qemu \
247aae12d4bSDaniel P. Berrangé                           | _filter_qemu_io | _filter_qmp | _filter_hmp
248aae12d4bSDaniel P. Berrangé                matched=1
249aae12d4bSDaniel P. Berrangé            else
250aae12d4bSDaniel P. Berrangé                QEMU_EVENTS="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
251aae12d4bSDaniel P. Berrangé            fi
252aae12d4bSDaniel P. Berrangé        done
253aae12d4bSDaniel P. Berrangé        IFS=$old_IFS
254aae12d4bSDaniel P. Berrangé    fi
255aae12d4bSDaniel P. Berrangé}
256aae12d4bSDaniel P. Berrangé
257aae12d4bSDaniel P. Berrangé# Wait for a named QMP event
258aae12d4bSDaniel P. Berrangé#
259aae12d4bSDaniel P. Berrangé# Input parameters:
260aae12d4bSDaniel P. Berrangé# $1:       QEMU handle to use
261aae12d4bSDaniel P. Berrangé# $2:       Name of the QMP event to wait for
262aae12d4bSDaniel P. Berrangé#
263aae12d4bSDaniel P. Berrangé# Checks if the named QMP even was previously captured
264aae12d4bSDaniel P. Berrangé# into $QEMU_EVENTS. If none are present, then waits for the
265aae12d4bSDaniel P. Berrangé# event to arrive on the QMP channel. When matched, the QMP
266aae12d4bSDaniel P. Berrangé# event will be echoed
267aae12d4bSDaniel P. Berrangé_wait_event()
268aae12d4bSDaniel P. Berrangé{
269aae12d4bSDaniel P. Berrangé    local h=${1}
270aae12d4bSDaniel P. Berrangé    local evname=${2}
271aae12d4bSDaniel P. Berrangé
272aae12d4bSDaniel P. Berrangé    while true
273aae12d4bSDaniel P. Berrangé    do
274aae12d4bSDaniel P. Berrangé        _check_cached_events $evname
275aae12d4bSDaniel P. Berrangé
276aae12d4bSDaniel P. Berrangé        if [ $matched = 1 ];
277aae12d4bSDaniel P. Berrangé        then
278aae12d4bSDaniel P. Berrangé            return
279aae12d4bSDaniel P. Berrangé        fi
280aae12d4bSDaniel P. Berrangé
281aae12d4bSDaniel P. Berrangé        only_capture_events=1 qemu_error_no_exit=1 _timed_wait_for ${h}
282aae12d4bSDaniel P. Berrangé
283aae12d4bSDaniel P. Berrangé        if [ ${QEMU_STATUS[$h]} -ne 0 ] ; then
284aae12d4bSDaniel P. Berrangé            echo "Timeout waiting for event ${evname} on handle ${h}"
285aae12d4bSDaniel P. Berrangé            exit 1 #Timeout means the test failed
286aae12d4bSDaniel P. Berrangé        fi
287aae12d4bSDaniel P. Berrangé    done
288aae12d4bSDaniel P. Berrangé}
289aae12d4bSDaniel P. Berrangé
290e940bc13SJeff Cody# Launch a QEMU process.
291e940bc13SJeff Cody#
292e940bc13SJeff Cody# Input parameters:
293e940bc13SJeff Cody# $qemu_comm_method: set this variable to 'monitor' (case insensitive)
294e940bc13SJeff Cody#                    to use the QEMU HMP monitor for communication.
295e940bc13SJeff Cody#                    Otherwise, the default of QMP is used.
29672538537SKevin Wolf# $qmp_pretty: Set this variable to 'y' to enable QMP pretty printing.
29715cfba69SMax Reitz# $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on stderr.
29815cfba69SMax Reitz#               If this variable is empty, stderr will be redirected to stdout.
299e940bc13SJeff Cody# Returns:
300e940bc13SJeff Cody# $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance.
301e940bc13SJeff Cody#
3028cedcffdSEric Blake_launch_qemu()
303e940bc13SJeff Cody{
304e940bc13SJeff Cody    local comm=
305e940bc13SJeff Cody    local fifo_out=
306e940bc13SJeff Cody    local fifo_in=
307e940bc13SJeff Cody
308e940bc13SJeff Cody    if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor" ]])
309e940bc13SJeff Cody    then
310e940bc13SJeff Cody        comm="-monitor stdio"
311e940bc13SJeff Cody    else
312e940bc13SJeff Cody        local qemu_comm_method="qmp"
31372538537SKevin Wolf        if [ "$qmp_pretty" = "y" ]; then
31472538537SKevin Wolf            comm="-monitor none -qmp-pretty stdio"
31572538537SKevin Wolf        else
316e940bc13SJeff Cody            comm="-monitor none -qmp stdio"
317e940bc13SJeff Cody        fi
31872538537SKevin Wolf    fi
319e940bc13SJeff Cody
320e940bc13SJeff Cody    fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE}
321e940bc13SJeff Cody    fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE}
322e940bc13SJeff Cody    mkfifo "${fifo_out}"
323e940bc13SJeff Cody    mkfifo "${fifo_in}"
324e940bc13SJeff Cody
32513a1d4a7SDaniel P. Berrange    object_options=
32613a1d4a7SDaniel P. Berrange    if [ -n "$IMGKEYSECRET" ]; then
32713a1d4a7SDaniel P. Berrange        object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
32813a1d4a7SDaniel P. Berrange    fi
32913a1d4a7SDaniel P. Berrange
33015cfba69SMax Reitz    if [ -z "$keep_stderr" ]; then
331f6c8c2e0SJeff Cody        QEMU_NEED_PID='y'\
33213a1d4a7SDaniel P. Berrange        ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
333d71a8b06SKevin Wolf                                                       2>&1 \
334e940bc13SJeff Cody                                                       <"${fifo_in}" &
33515cfba69SMax Reitz    elif [ "$keep_stderr" = "y" ]; then
33615cfba69SMax Reitz        QEMU_NEED_PID='y'\
33713a1d4a7SDaniel P. Berrange        ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
33815cfba69SMax Reitz                                                       <"${fifo_in}" &
33915cfba69SMax Reitz    else
34015cfba69SMax Reitz        exit 1
34115cfba69SMax Reitz    fi
342e940bc13SJeff Cody
343e940bc13SJeff Cody    if [[ "${BASH_VERSINFO[0]}" -ge "5" ||
344e940bc13SJeff Cody        ("${BASH_VERSINFO[0]}" -ge "4"  &&  "${BASH_VERSINFO[1]}" -ge "1") ]]
345e940bc13SJeff Cody    then
346e940bc13SJeff Cody        # bash >= 4.1 required for automatic fd
347e940bc13SJeff Cody        exec {_out_fd}<"${fifo_out}"
348e940bc13SJeff Cody        exec {_in_fd}>"${fifo_in}"
349e940bc13SJeff Cody    else
350e940bc13SJeff Cody        let _out_fd++
351e940bc13SJeff Cody        let _in_fd++
352e940bc13SJeff Cody        eval "exec ${_out_fd}<'${fifo_out}'"
353e940bc13SJeff Cody        eval "exec ${_in_fd}>'${fifo_in}'"
354e940bc13SJeff Cody    fi
355e940bc13SJeff Cody
356e940bc13SJeff Cody    QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd}
357e940bc13SJeff Cody    QEMU_IN[${_QEMU_HANDLE}]=${_in_fd}
358e940bc13SJeff Cody    QEMU_STATUS[${_QEMU_HANDLE}]=0
359e940bc13SJeff Cody
360e940bc13SJeff Cody    if [ "${qemu_comm_method}" == "qmp" ]
361e940bc13SJeff Cody    then
362e940bc13SJeff Cody        # Don't print response, since it has version information in it
363e940bc13SJeff Cody        silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities"
36472538537SKevin Wolf        if [ "$qmp_pretty" = "y" ]; then
36572538537SKevin Wolf            silent=yes _timed_wait_for ${_QEMU_HANDLE} "^}"
36672538537SKevin Wolf        fi
367e940bc13SJeff Cody    fi
368e940bc13SJeff Cody    QEMU_HANDLE=${_QEMU_HANDLE}
369e940bc13SJeff Cody    let _QEMU_HANDLE++
370e940bc13SJeff Cody}
371e940bc13SJeff Cody
372e940bc13SJeff Cody
373e50a6121SStefan Weil# Silently kills the QEMU process
374ea82aa42SMax Reitz#
375ea82aa42SMax Reitz# If $wait is set to anything other than the empty string, the process will not
376ea82aa42SMax Reitz# be killed but only waited for, and any output will be forwarded to stdout. If
377ea82aa42SMax Reitz# $wait is empty, the process will be killed and all output will be suppressed.
3788cedcffdSEric Blake_cleanup_qemu()
379e940bc13SJeff Cody{
380e940bc13SJeff Cody    # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices
381e940bc13SJeff Cody    for i in "${!QEMU_OUT[@]}"
382e940bc13SJeff Cody    do
383f6c8c2e0SJeff Cody        local QEMU_PID
384846a1d11SJeff Cody        if [ -f "${QEMU_TEST_DIR}/qemu-${i}.pid" ]; then
385846a1d11SJeff Cody            read QEMU_PID < "${QEMU_TEST_DIR}/qemu-${i}.pid"
386846a1d11SJeff Cody            rm -f "${QEMU_TEST_DIR}/qemu-${i}.pid"
387f6c8c2e0SJeff Cody            if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then
388f6c8c2e0SJeff Cody                kill -KILL ${QEMU_PID} 2>/dev/null
389ea82aa42SMax Reitz            fi
390f6c8c2e0SJeff Cody            if [ -n "${QEMU_PID}" ]; then
391f6c8c2e0SJeff Cody                wait ${QEMU_PID} 2>/dev/null # silent kill
392f6c8c2e0SJeff Cody            fi
393f6c8c2e0SJeff Cody        fi
394f6c8c2e0SJeff Cody
395ea82aa42SMax Reitz        if [ -n "${wait}" ]; then
396ea82aa42SMax Reitz            cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \
39769404d9eSKevin Wolf                                  | _filter_qemu_io | _filter_qmp | _filter_hmp
398ea82aa42SMax Reitz        fi
399e940bc13SJeff Cody        rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}"
400e940bc13SJeff Cody        eval "exec ${QEMU_IN[$i]}<&-"   # close file descriptors
401e940bc13SJeff Cody        eval "exec ${QEMU_OUT[$i]}<&-"
402ecaf8c8aSKevin Wolf
403ecaf8c8aSKevin Wolf        unset QEMU_IN[$i]
404ecaf8c8aSKevin Wolf        unset QEMU_OUT[$i]
405e940bc13SJeff Cody    done
406e940bc13SJeff Cody}
407