These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / tests / qemu-iotests / common.qemu
1 #!/bin/bash
2 #
3 # This allows for launching of multiple QEMU instances, with independent
4 # communication possible to each instance.
5 #
6 # Each instance can choose, at launch, to use either the QMP or the
7 # HMP (monitor) interface.
8 #
9 # All instances are cleaned up via _cleanup_qemu, including killing the
10 # running qemu instance.
11 #
12 # Copyright (C) 2014 Red Hat, Inc.
13 #
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.
18 #
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.
23 #
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/>.
26 #
27
28 QEMU_COMM_TIMEOUT=10
29
30 QEMU_FIFO_IN="${TEST_DIR}/qmp-in-$$"
31 QEMU_FIFO_OUT="${TEST_DIR}/qmp-out-$$"
32
33 QEMU_HANDLE=0
34
35 # If bash version is >= 4.1, these will be overwritten and dynamic
36 # file descriptor values assigned.
37 _out_fd=3
38 _in_fd=4
39
40 # Wait for expected QMP response from QEMU.  Will time out
41 # after 10 seconds, which counts as failure.
42 #
43 # Override QEMU_COMM_TIMEOUT for a timeout different than the
44 # default 10 seconds
45 #
46 # $1: The handle to use
47 # $2+ All remaining arguments comprise the string to search for
48 #    in the response.
49 #
50 # If $silent is set to anything but an empty string, then
51 # response is not echoed out.
52 function _timed_wait_for()
53 {
54     local h=${1}
55     shift
56
57     QEMU_STATUS[$h]=0
58     while read -t ${QEMU_COMM_TIMEOUT} resp <&${QEMU_OUT[$h]}
59     do
60         if [ -z "${silent}" ]; then
61             echo "${resp}" | _filter_testdir | _filter_qemu \
62                            | _filter_qemu_io | _filter_qmp
63         fi
64         grep -q "${*}" < <(echo ${resp})
65         if [ $? -eq 0 ]; then
66             return
67         fi
68     done
69     QEMU_STATUS[$h]=-1
70     if [ -z "${qemu_error_no_exit}" ]; then
71         echo "Timeout waiting for ${*} on handle ${h}"
72         exit 1  # Timeout means the test failed
73     fi
74 }
75
76
77 # Sends QMP or HMP command to QEMU, and waits for the expected response
78 #
79 # $1:       QEMU handle to use
80 # $2:       String of the QMP command to send
81 # ${@: -1}  (Last string passed)
82 #             String that the QEMU response should contain. If it is a null
83 #             string, do not wait for a response
84 #
85 # Set qemu_cmd_repeat to the number of times to repeat the cmd
86 # until either timeout, or a response.  If it is not set, or <=0,
87 # then the command is only sent once.
88 #
89 # If $qemu_error_no_exit is set, then even if the expected response
90 # is not seen, we will not exit.  $QEMU_STATUS[$1] will be set it -1 in
91 # that case.
92 function _send_qemu_cmd()
93 {
94     local h=${1}
95     local count=1
96     local cmd=
97     local use_error=${qemu_error_no_exit}
98     shift
99
100     if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev/null; then
101         count=${qemu_cmd_repeat}
102         use_error="no"
103     fi
104     # This array element extraction is done to accommodate pathnames with spaces
105     cmd=${@: 1:${#@}-1}
106     shift $(($# - 1))
107
108     while [ ${count} -gt 0 ]
109     do
110         echo "${cmd}" >&${QEMU_IN[${h}]}
111         if [ -n "${1}" ]; then
112             qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}"
113             if [ ${QEMU_STATUS[$h]} -eq 0 ]; then
114                 return
115             fi
116         fi
117         let count--;
118     done
119     if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then
120         echo "Timeout waiting for ${1} on handle ${h}"
121         exit 1 #Timeout means the test failed
122     fi
123 }
124
125
126 # Launch a QEMU process.
127 #
128 # Input parameters:
129 # $qemu_comm_method: set this variable to 'monitor' (case insensitive)
130 #                    to use the QEMU HMP monitor for communication.
131 #                    Otherwise, the default of QMP is used.
132 # $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on stderr.
133 #               If this variable is empty, stderr will be redirected to stdout.
134 # Returns:
135 # $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance.
136 #
137 function _launch_qemu()
138 {
139     local comm=
140     local fifo_out=
141     local fifo_in=
142
143     if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor" ]])
144     then
145         comm="-monitor stdio"
146     else
147         local qemu_comm_method="qmp"
148         comm="-monitor none -qmp stdio"
149     fi
150
151     fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE}
152     fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE}
153     mkfifo "${fifo_out}"
154     mkfifo "${fifo_in}"
155
156     if [ -z "$keep_stderr" ]; then
157         QEMU_NEED_PID='y'\
158         ${QEMU} -nographic -serial none ${comm} -machine accel=qtest "${@}" \
159                                                                 >"${fifo_out}" \
160                                                                 2>&1 \
161                                                                 <"${fifo_in}" &
162     elif [ "$keep_stderr" = "y" ]; then
163         QEMU_NEED_PID='y'\
164         ${QEMU} -nographic -serial none ${comm} -machine accel=qtest "${@}" \
165                                                                 >"${fifo_out}" \
166                                                                 <"${fifo_in}" &
167     else
168         exit 1
169     fi
170
171     if [[ "${BASH_VERSINFO[0]}" -ge "5" ||
172         ("${BASH_VERSINFO[0]}" -ge "4"  &&  "${BASH_VERSINFO[1]}" -ge "1") ]]
173     then
174         # bash >= 4.1 required for automatic fd
175         exec {_out_fd}<"${fifo_out}"
176         exec {_in_fd}>"${fifo_in}"
177     else
178         let _out_fd++
179         let _in_fd++
180         eval "exec ${_out_fd}<'${fifo_out}'"
181         eval "exec ${_in_fd}>'${fifo_in}'"
182     fi
183
184     QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd}
185     QEMU_IN[${_QEMU_HANDLE}]=${_in_fd}
186     QEMU_STATUS[${_QEMU_HANDLE}]=0
187
188     if [ "${qemu_comm_method}" == "qmp" ]
189     then
190         # Don't print response, since it has version information in it
191         silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities"
192     fi
193     QEMU_HANDLE=${_QEMU_HANDLE}
194     let _QEMU_HANDLE++
195 }
196
197
198 # Silenty kills the QEMU process
199 #
200 # If $wait is set to anything other than the empty string, the process will not
201 # be killed but only waited for, and any output will be forwarded to stdout. If
202 # $wait is empty, the process will be killed and all output will be suppressed.
203 function _cleanup_qemu()
204 {
205     # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices
206     for i in "${!QEMU_OUT[@]}"
207     do
208         local QEMU_PID
209         if [ -f "${TEST_DIR}/qemu-${i}.pid" ]; then
210             read QEMU_PID < "${TEST_DIR}/qemu-${i}.pid"
211             rm -f "${TEST_DIR}/qemu-${i}.pid"
212             if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then
213                 kill -KILL ${QEMU_PID} 2>/dev/null
214             fi
215             if [ -n "${QEMU_PID}" ]; then
216                 wait ${QEMU_PID} 2>/dev/null # silent kill
217             fi
218         fi
219
220         if [ -n "${wait}" ]; then
221             cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \
222                                   | _filter_qemu_io | _filter_qmp
223         fi
224         rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}"
225         eval "exec ${QEMU_IN[$i]}<&-"   # close file descriptors
226         eval "exec ${QEMU_OUT[$i]}<&-"
227     done
228 }