fix(ci): always shut the model down, even if it can exit itself

Some runs can send an EOT character when they are done, and the model
can look for that and turn itself off when it sees it (with
shutdown_on_eot=1). We currently use that to skip the automatic cleanup
(shutting down) of the model. However, that doesn't work well as we
don't have an exact way of specifying which jobs can rely on that
parameter and which can't. Some runs have the parameter but never use
it. Whenever that happens, the cleanup will be skipped and we get a
zombie model, preventing the CI run from finishing (after showing
success).

So never skip the cleanup. Rely on the fact that we will get correct
signals (from expect and the exit status of terminals) to cleanup at the
correct time. Do check that the model exited before we could kill it so
that we don't fail a command (due to set +e) and produce a backtrace.

This is effectively a revert of 359b3ff6c.

Signed-off-by: Boyan Karatotev <boyan.karatotev@arm.com>
Change-Id: I3ec193a6d3d59f818ab8fed8d9b09821076ad0f5
diff --git a/script/run_package.sh b/script/run_package.sh
index 7827e76..340b3a5 100755
--- a/script/run_package.sh
+++ b/script/run_package.sh
@@ -18,10 +18,6 @@
 run_root="$workspace/run"
 pid_dir="$workspace/pids"
 
-# This variable avoids graceful termination of the model when
-# launched with the parameter 'bp.pl011_uart0.shutdown_on_eot=1'
-exit_on_model_param=0
-
 # Model exit parameter string
 model_exit_param_string="bp.pl011_uart0.shutdown_on_eot=1"
 
@@ -60,30 +56,30 @@
 	sig=${1:-SIGINT}
 	echo "signal received: $sig"
 
-	# Avoid the model termination gracefully when the parameter 'exit_on_model_param'
-	# is set and test if exited successfully.
-	if [ "$exit_on_model_param" -eq 0 ] || [ "$sig" != "EXIT" ]; then
-		# Kill all background processes so far and wait for them
-		while read pid; do
-			pid="$(cat $pid)"
-			echo $pid
-			# Forcefully killing model process does not show statistical
-			# data (Host CPU time spent running in User and System). Safely
-			# kill the model by using SIGINT(^C) that helps in printing
-			# statistical data.
-			if [ "$pid" == "$model_pid" ] && [ "${COVERAGE_ON}" != "1" ]; then
-				model_cid=$(pgrep -P "$model_pid" | xargs)
-				# ignore errors
-				kill -SIGINT "$model_cid" &>/dev/null || true
-				# Allow some time to print data, we can't use wait since the process is
-				# a child of the daemonized launch process.
-				sleep 5
+	while read pid; do
+		pid="$(cat $pid)"
+		echo $pid
+		# Forcefully killing model process does not show statistical
+		# data (Host CPU time spent running in User and System). Safely
+		# kill the model by using SIGINT(^C) that helps in printing
+		# statistical data.
+		if [ "$pid" == "$model_pid" ] && [ "${COVERAGE_ON}" != "1" ]; then
+			model_cid=$(pgrep -P "$model_pid" | xargs || true)
+			if [ -z "$model_cid" ]; then
+				echo "Model quit by itself. Not killing!"
+				continue
 			fi
 
-			kill_and_reap "$pid"
+			# ignore errors
+			kill -SIGINT "$model_cid" &>/dev/null || true
+			# Allow some time to print data, we can't use wait since the process is
+			# a child of the daemonized launch process.
+			sleep 5
+		fi
 
-		done < <(find -name '*.pid')
-	fi
+		kill_and_reap "$pid"
+
+	done < <(find -name '*.pid')
 
 	popd
 }
@@ -450,9 +446,6 @@
 
 popd
 
-# Capture whether the model is running with the 'exit model parameter' or not.
-exit_on_model_param=$(grep -wc "$model_exit_param_string" "$run_cwd/model_params")
-
 if [ "$result" -eq 0 ]; then
 	echo "Test success!"
 else