thermal: kill heater program if test is terminated by signal

if the thermal test is terminated by signals such as an Ctrl+C operation, the
heat_cpu and also glmark(if there is any) should be terminated too.

Signed-off-by: hongbo.zhang <hongbo.zhang@linaro.com>
diff --git a/thermal/thermal_03.sh b/thermal/thermal_03.sh
index 86bdffd..5790320 100755
--- a/thermal/thermal_03.sh
+++ b/thermal/thermal_03.sh
@@ -30,14 +30,23 @@
 
 CPU_HEAT_BIN=../utils/heat_cpu
 GPU_HEAT_BIN=/usr/bin/glmark2
+cpu_pid=0
+gpu_pid=0
+
+heater_kill() {
+    if [ $cpu_pid != 0 ]; then
+	kill -9 $cpu_pid
+    fi
+    if [ $gpu_pid != 0 ]; then
+	kill -9 $gpu_pid
+    fi
+}
 
 check_temperature_change() {
     local dirpath=$THERMAL_PATH/$1
     local zone_name=$1
     shift 1
 
-    local cpu_pid=0
-    local gpu_pid=0
     local init_temp=$(cat $dirpath/temp)
     $CPU_HEAT_BIN &
     cpu_pid=$(ps | grep heat_cpu| awk '{print $1}')
@@ -55,13 +64,10 @@
 
     sleep 5
     local final_temp=$(cat $dirpath/temp)
-    if [ $cpu_pid != 0 ]; then
-	kill -9 $cpu_pid
-    fi
-    if [ $gpu_pid != 0 ]; then
-	kill -9 $gpu_pid
-    fi
+    heater_kill
     check "temperature variation with load" "test $final_temp -gt $init_temp"
 }
 
+trap "heater_kill; sigtrap" SIGHUP SIGINT SIGTERM
+
 for_each_thermal_zone check_temperature_change
diff --git a/thermal/thermal_04.sh b/thermal/thermal_04.sh
index 52882e2..8f0dee7 100755
--- a/thermal/thermal_04.sh
+++ b/thermal/thermal_04.sh
@@ -28,6 +28,13 @@
 source ../include/functions.sh
 source ../include/thermal_functions.sh
 HEAT_CPU_MODERATE=../utils/heat_cpu
+pid=0
+
+heater_kill() {
+    if [ $pid != 0 ]; then
+	kill -9 $pid
+    fi
+}
 
 verify_cooling_device_temp_change() {
     local dirpath=$THERMAL_PATH/$1
@@ -65,9 +72,11 @@
 					"test $cool_temp -ge 0"
 	count=$((count+1))
     done
-    kill -9 $pid
+    heater_kill
     echo $prev_mode_val > $tzonepath/mode
     echo $prev_state_val > $dirpath/cur_state
 }
 
+trap "heater_kill; sigtrap" SIGHUP SIGINT SIGTERM
+
 for_each_cooling_device verify_cooling_device_temp_change
diff --git a/thermal/thermal_06.sh b/thermal/thermal_06.sh
index 1497bcc..8013b82 100755
--- a/thermal/thermal_06.sh
+++ b/thermal/thermal_06.sh
@@ -32,6 +32,17 @@
 GPU_HEAT_BIN=/usr/bin/glmark2
 TEST_LOOP=100
 TRIP_CROSSED_COUNT=
+cpu_pid=0
+gpu_pid=0
+
+heater_kill() {
+    if [ $cpu_pid != 0 ]; then
+	kill -9 $cpu_pid
+    fi
+    if [ $gpu_pid != 0 ]; then
+	kill -9 $gpu_pid
+    fi
+}
 
 check_trip_point_change() {
     local dirpath=$THERMAL_PATH/$1
@@ -43,8 +54,6 @@
     local trip_temp=0
     local trip_cross=
     local trip_id=
-    local cpu_pid=0
-    local gpu_pid=0
     local trip_type=0
     local trip_type_path=0
     $CPU_HEAT_BIN &
@@ -94,12 +103,9 @@
 	index=$((index + 1))
     done
 
-    if [ $cpu_pid != 0 ]; then
-	kill -9 $cpu_pid
-    fi
-    if [ $gpu_pid != 0 ]; then
-	kill -9 $gpu_pid
-    fi
+    heater_kill
 }
 
+trap "heater_kill; sigtrap" SIGHUP SIGINT SIGTERM
+
 for_each_thermal_zone check_trip_point_change