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 28QEMU_COMM_TIMEOUT=10 29 30QEMU_FIFO_IN="${TEST_DIR}/qmp-in-$$" 31QEMU_FIFO_OUT="${TEST_DIR}/qmp-out-$$" 32 33QEMU_PID= 34_QEMU_HANDLE=0 35QEMU_HANDLE=0 36 37# If bash version is >= 4.1, these will be overwritten and dynamic 38# file descriptor values assigned. 39_out_fd=3 40_in_fd=4 41 42# Wait for expected QMP response from QEMU. Will time out 43# after 10 seconds, which counts as failure. 44# 45# Override QEMU_COMM_TIMEOUT for a timeout different than the 46# default 10 seconds 47# 48# $1: The handle to use 49# $2+ All remaining arguments comprise the string to search for 50# in the response. 51# 52# If $silent is set to anything but an empty string, then 53# response is not echoed out. 54function _timed_wait_for() 55{ 56 local h=${1} 57 shift 58 59 QEMU_STATUS[$h]=0 60 while read -t ${QEMU_COMM_TIMEOUT} resp <&${QEMU_OUT[$h]} 61 do 62 if [ -z "${silent}" ]; then 63 echo "${resp}" | _filter_testdir | _filter_qemu \ 64 | _filter_qemu_io | _filter_qmp 65 fi 66 grep -q "${*}" < <(echo ${resp}) 67 if [ $? -eq 0 ]; then 68 return 69 fi 70 done 71 QEMU_STATUS[$h]=-1 72 if [ -z "${qemu_error_no_exit}" ]; then 73 echo "Timeout waiting for ${*} on handle ${h}" 74 exit 1 # Timeout means the test failed 75 fi 76} 77 78 79# Sends QMP or HMP command to QEMU, and waits for the expected response 80# 81# $1: QEMU handle to use 82# $2: String of the QMP command to send 83# ${@: -1} (Last string passed) 84# String that the QEMU response should contain. If it is a null 85# string, do not wait for a response 86# 87# Set qemu_cmd_repeat to the number of times to repeat the cmd 88# until either timeout, or a response. If it is not set, or <=0, 89# then the command is only sent once. 90# 91# If $qemu_error_no_exit is set, then even if the expected response 92# is not seen, we will not exit. $QEMU_STATUS[$1] will be set it -1 in 93# that case. 94function _send_qemu_cmd() 95{ 96 local h=${1} 97 local count=1 98 local cmd= 99 local use_error=${qemu_error_no_exit} 100 shift 101 102 if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev/null; then 103 count=${qemu_cmd_repeat} 104 use_error="no" 105 fi 106 # This array element extraction is done to accommodate pathnames with spaces 107 cmd=${@: 1:${#@}-1} 108 shift $(($# - 1)) 109 110 while [ ${count} -gt 0 ] 111 do 112 echo "${cmd}" >&${QEMU_IN[${h}]} 113 if [ -n "${1}" ]; then 114 qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}" 115 if [ ${QEMU_STATUS[$h]} -eq 0 ]; then 116 return 117 fi 118 fi 119 let count--; 120 done 121 if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then 122 echo "Timeout waiting for ${1} on handle ${h}" 123 exit 1 #Timeout means the test failed 124 fi 125} 126 127 128# Launch a QEMU process. 129# 130# Input parameters: 131# $qemu_comm_method: set this variable to 'monitor' (case insensitive) 132# to use the QEMU HMP monitor for communication. 133# Otherwise, the default of QMP is used. 134# Returns: 135# $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance. 136# 137function _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 "${QEMU}" -nographic -serial none ${comm} -machine accel=qtest "${@}" 2>&1 \ 157 >"${fifo_out}" \ 158 <"${fifo_in}" & 159 QEMU_PID[${_QEMU_HANDLE}]=$! 160 161 if [[ "${BASH_VERSINFO[0]}" -ge "5" || 162 ("${BASH_VERSINFO[0]}" -ge "4" && "${BASH_VERSINFO[1]}" -ge "1") ]] 163 then 164 # bash >= 4.1 required for automatic fd 165 exec {_out_fd}<"${fifo_out}" 166 exec {_in_fd}>"${fifo_in}" 167 else 168 let _out_fd++ 169 let _in_fd++ 170 eval "exec ${_out_fd}<'${fifo_out}'" 171 eval "exec ${_in_fd}>'${fifo_in}'" 172 fi 173 174 QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd} 175 QEMU_IN[${_QEMU_HANDLE}]=${_in_fd} 176 QEMU_STATUS[${_QEMU_HANDLE}]=0 177 178 if [ "${qemu_comm_method}" == "qmp" ] 179 then 180 # Don't print response, since it has version information in it 181 silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities" 182 fi 183 QEMU_HANDLE=${_QEMU_HANDLE} 184 let _QEMU_HANDLE++ 185} 186 187 188# Silenty kills the QEMU process 189function _cleanup_qemu() 190{ 191 # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices 192 for i in "${!QEMU_OUT[@]}" 193 do 194 kill -KILL ${QEMU_PID[$i]} 2>/dev/null 195 wait ${QEMU_PID[$i]} 2>/dev/null # silent kill 196 rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}" 197 eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors 198 eval "exec ${QEMU_OUT[$i]}<&-" 199 done 200} 201