From patchwork Tue Oct 17 16:31:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Cody X-Patchwork-Id: 827124 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yGgmd1wWkz9t2Q for ; Wed, 18 Oct 2017 03:36:01 +1100 (AEDT) Received: from localhost ([::1]:40467 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4UqV-0000ge-7S for incoming@patchwork.ozlabs.org; Tue, 17 Oct 2017 12:35:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41583) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Un3-0006mf-8T for qemu-devel@nongnu.org; Tue, 17 Oct 2017 12:32:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Un1-0006I9-Ss for qemu-devel@nongnu.org; Tue, 17 Oct 2017 12:32:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:20881) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Umy-0006CJ-VL; Tue, 17 Oct 2017 12:32:21 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CC350C04AC5A; Tue, 17 Oct 2017 16:32:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CC350C04AC5A Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jcody@redhat.com Received: from localhost (ovpn-117-62.phx2.redhat.com [10.3.117.62]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8C15061783; Tue, 17 Oct 2017 16:32:19 +0000 (UTC) From: Jeff Cody To: qemu-devel@nongnu.org Date: Tue, 17 Oct 2017 12:31:50 -0400 Message-Id: <85b170e6abd3f92bee7449d0970f6ba24e589c72.1508257445.git.jcody@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 17 Oct 2017 16:32:19 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v5 05/10] qemu-iotests: change qemu pid and fd tracking / cleanup X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jsnow@redhat.com, stefanha@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" So that later patches can cleanup running qemu processes called from different subshells, track resources to cleanup in pid and fd list files. In subsquent patches, ./check will kill all QEMU processes launched with the common.qemu framework, and the tests will not need to cleanup manually (unless they want to do so as part of the test, e.g. wait for a process to end such as migration). Signed-off-by: Jeff Cody --- tests/qemu-iotests/common.qemu | 82 ++++++++++++++++++++++++++++++++---------- tests/qemu-iotests/common.rc | 3 +- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/tests/qemu-iotests/common.qemu b/tests/qemu-iotests/common.qemu index 7b3052d..35a08a6 100644 --- a/tests/qemu-iotests/common.qemu +++ b/tests/qemu-iotests/common.qemu @@ -33,6 +33,10 @@ QEMU_FIFO_OUT="${QEMU_TEST_DIR}/qmp-out-$$" QEMU_HANDLE=0 export _QEMU_HANDLE=0 +QEMU_PID_LIST="${QEMU_TEST_DIR}"/qemu-pid.lst +QEMU_OUT_FD_LIST="${QEMU_TEST_DIR}"/qemu-out-fd.lst +QEMU_IN_FD_LIST="${QEMU_TEST_DIR}"/qemu-in-fd.lst +QEMU_FIFO_LIST="${QEMU_TEST_DIR}"/qemu-fifo.lst # If bash version is >= 4.1, these will be overwritten and dynamic # file descriptor values assigned. _out_fd=3 @@ -193,6 +197,10 @@ function _launch_qemu() QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd} QEMU_IN[${_QEMU_HANDLE}]=${_in_fd} QEMU_STATUS[${_QEMU_HANDLE}]=0 + echo ${_out_fd} >> "$QEMU_OUT_FD_LIST" + echo ${_in_fd} >> "$QEMU_IN_FD_LIST" + echo ${fifo_in} >> "$QEMU_FIFO_LIST" + echo ${fifo_out} >> "$QEMU_FIFO_LIST" if [ "${qemu_comm_method}" == "qmp" ] then @@ -209,35 +217,71 @@ function _launch_qemu() # Silenty kills the QEMU process # +# This is able to kill and clean up after QEMU processes without the +# need for any subshell-specific variables, so long as the qemu-pidlist +# and qemu-out-fd.list and qemu-in-fd.list files are properly maintained. +# # If $wait is set to anything other than the empty string, the process will not # be killed but only waited for, and any output will be forwarded to stdout. If # $wait is empty, the process will be killed and all output will be suppressed. function _cleanup_qemu() { - # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices - for i in "${!QEMU_OUT[@]}" + local fifo_path= + local testdir_path= + + if [ ! -e "${QEMU_PID_LIST}" ]; then + return + fi + + # get line count, and therefore number of processes to kill + numproc=$(wc -l "${QEMU_PID_LIST}" | sed 's/\s.*//') + + for i in $(seq 1 $numproc) do local QEMU_PID - if [ -f "${QEMU_TEST_DIR}/qemu-${i}.pid" ]; then - read QEMU_PID < "${QEMU_TEST_DIR}/qemu-${i}.pid" - rm -f "${QEMU_TEST_DIR}/qemu-${i}.pid" - if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then - kill -KILL ${QEMU_PID} 2>/dev/null - fi - if [ -n "${QEMU_PID}" ]; then - wait ${QEMU_PID} 2>/dev/null # silent kill - fi + local OUT_FD + local IN_FD + j=$(expr $i - 1) + + QEMU_PID=$(tail -n+${i} "${QEMU_PID_LIST}" | head -n1) + OUT_FD=$(tail -n+${i} "${QEMU_OUT_FD_LIST}" | head -n1) + IN_FD=$(tail -n+${i} "${QEMU_IN_FD_LIST}" | head -n1) + + if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then + kill -KILL ${QEMU_PID} 2>/dev/null + fi + if [ -n "${QEMU_PID}" ]; then + wait ${QEMU_PID} 2>/dev/null # silent kill fi - if [ -n "${wait}" ]; then - cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \ - | _filter_qemu_io | _filter_qmp | _filter_hmp + if [ -n "${wait}" ] && [ -n "${OUT_FD}" ]; then + cat <&${OUT_FD} | _filter_testdir | _filter_qemu \ + | _filter_qemu_io | _filter_qmp | _filter_hmp + fi + + if [ -n "${IN_FD}" ]; then + eval "exec ${IN_FD}<&-" # close file descriptors + fi + if [ -n "${OUT_FD}" ]; then + eval "exec ${OUT_FD}<&-" fi - rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}" - eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors - eval "exec ${QEMU_OUT[$i]}<&-" - unset QEMU_IN[$i] - unset QEMU_OUT[$i] + unset QEMU_IN[$j] + unset QEMU_OUT[$j] done + + # The FIFOs do not correspond to process entry in the pidlist, so + # just clean them up afterwards + while read fifo_name + do + # make sure entries are inside the $TEST_DIR + fifo_path=$(dirname -z $(realpath "$fifo_name")) + testdir_path=$(realpath "$QEMU_TEST_DIR") + if [ "$fifo_path" == "$testdir_path" ] + then + rm -f "$fifo_name" + fi + done < "${QEMU_FIFO_LIST}" + + rm -f "${QEMU_PID_LIST}" "${QEMU_OUT_FD_LIST}" "${QEMU_IN_FD_LIST}" "$QEMU_FIFO_LIST}" } diff --git a/tests/qemu-iotests/common.rc b/tests/qemu-iotests/common.rc index a345ffd..b26b02f 100644 --- a/tests/qemu-iotests/common.rc +++ b/tests/qemu-iotests/common.rc @@ -40,7 +40,6 @@ poke_file() printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null } - if ! . ./common.config then echo "$0: failed to source common.config" @@ -51,7 +50,7 @@ _qemu_wrapper() { ( if [ -n "${QEMU_NEED_PID}" ]; then - echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid" + echo $BASHPID >> "${QEMU_TEST_DIR}/qemu-pid.lst" fi exec "$QEMU_PROG" $QEMU_OPTIONS "$@" )