From 80993c4e1b9aa68651a026dd7416ba3d5e96c50c Mon Sep 17 00:00:00 2001
From: Jiajun Xu <jiajun.xu@intel.com>
Date: Wed, 1 Sep 2010 23:38:53 +0800
Subject: qemuimage-testlib: kill qemu process according to its pid, instead of
 process name

poky-qemu-internal will set up a tap lockfile when creating tap device. The lockfile
will be released when a TERM signal is received. In previous code, function
Test_Kill_Qemu uses pkill to kill all process named "qemu". This may cause lockfile
release function not work in poky-qemu-internal. Then poky-qemu-internal will be
hang when user start QEMU the second time. To prevent the issue, the new function
Test_Kill_Qemu kills all child pid with a given parent process ID.

Signed-off-by Jiajun Xu <jiajun.xu@intel.com>
---
 scripts/qemuimage-testlib | 53 +++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 4 deletions(-)

diff --git a/scripts/qemuimage-testlib b/scripts/qemuimage-testlib
index 6dc219da2..733cd12c0 100644
--- a/scripts/qemuimage-testlib
+++ b/scripts/qemuimage-testlib
@@ -16,6 +16,9 @@
 
 TYPE="ext3"
 
+# Global variable for process id
+PID=0
+
 # common function for information print
 Test_Error()
 {
@@ -92,11 +95,50 @@ Test_Print_Result()
 	echo -e "\t$1\t\t$PASS\t$FAIL\t$NORESULT" >> $TEST_RESULT/testresult.log
 }
 
-# function to kill qemu
+# Test_Kill_Qemu to kill child pid with parent pid given
+# $1 is qemu process id, which needs to be killed
 Test_Kill_Qemu()
 {
-	sudo pkill -9 qemu
-	return $?
+	local ret=0
+	local ppid=0
+	local i=0
+	declare local pid
+
+	# Check if $1 pid exists and is a qemu process
+	ps -fp $PID | grep -iq "qemu"
+
+	# Find all children pid of the pid $1
+	if [ $? -eq 0 ]; then
+
+		# Check if there is any child pid of the pid $PID
+		ppid=$PID
+		ps -f --ppid $ppid
+		ret=$?
+
+		while [ $ret -eq 0 ]
+		do
+			# If yes, get the child pid and check if the child pid has other child pid
+			# Continue the while loop until there is no child pid found
+			pid[$i]=`ps -f --ppid $ppid | awk '{if ($2 != "PID") print $2}'`
+			ppid=${pid[$i]}
+			i=$((i+1))
+			ps -f --ppid $ppid
+			ret=$?
+		done
+
+		# Kill these children pids from the last one
+		while [ $i -ne 0 ]
+		do
+			i=$((i-1))
+			kill ${pid[$i]}
+			sleep 2
+		done
+
+		# Kill the parent id
+		kill $PID
+	fi
+
+	return
 }
 
 # function to check if there is any qemu process
@@ -224,10 +266,13 @@ Test_Create_Qemu()
 	$CP $ROOTFS_IMAGE $TEST_ROOTFS_IMAGE
 
 	export MACHINE=$QEMUARCH
-	# Create Qemu in localhost VNC Port 1
 
+	# Create Qemu in localhost VNC Port 1
     	xterm -display ${DISPLAY} -e "${RUNQEMU} ${KERNEL} ${TEST_ROOTFS_IMAGE}" &
 
+	# Get the pid of the xterm processor, which will be used in Test_Kill_Qemu
+	PID=$!
+
 	sleep 5
 	
 	while [ ${up_time} -lt ${timeout} ]
-- 
cgit v1.2.3