Make test/smack_rule_test/checksmackrule.sh more robust 68/314268/2
authorKrzysztof Malysa <k.malysa@samsung.com>
Tue, 9 Jul 2024 11:16:32 +0000 (13:16 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 15 Jul 2024 15:55:06 +0000 (15:55 +0000)
Change-Id: I88a9c0a756264d1676768b33ea3c3c9236545053

test/smack_rule_test/checksmackrule.sh

index 32bff94af6381c1f6d139678ffb04dd47ba543e0..b01bbfcc5bd38662bd5753916ed7ba32fd90445c 100755 (executable)
@@ -19,10 +19,50 @@ then
        rm $log_file
 fi
 
+function kill_all_apps {
+       echo "Killing all apps..."
+       sqlite3 /opt/dbspace/.security-manager.db "select name from app" | xargs -i sh -c 'app_launcher -k {} | true'
+       echo "... done."
+}
+
+function run_apps {
+       apps_to_run=("$@")
+       # Applications may depend on other apps, so try starting unstarted applications until no new application starts
+       while true; do
+               next_apps_to_run=()
+               for app in "${apps_to_run[@]}"; do
+                       printf "Trying to run app ${app}: "
+                       if app_launcher --is-running="${app}" > /dev/null; then
+                               echo "... already running."
+                       elif ! app_launcher --start="${app}"; then
+                               next_apps_to_run+=("${app}")
+                       fi
+               done
+               if [ "${#next_apps_to_run[@]}" == "${#apps_to_run[@]}" ]; then
+                       break; # No new app started, we reached a point where we assume that no new application will start
+               fi
+               apps_to_run=("${next_apps_to_run[@]}")
+       done
+}
+
+function run_all_apps {
+       run_apps $(sqlite3 /opt/dbspace/.security-manager.db "select name from app")
+}
+
+function get_running_apps {
+       for app in $(sqlite3 /opt/dbspace/.security-manager.db "select name from app"); do
+               if app_launcher --is-running="${app}" > /dev/null; then
+                       echo "${app}"
+               fi
+       done
+}
+
+# Restore apps upon script exit or termination by a signal
+apps_running_before_the_test=($(get_running_apps))
+trap 'kill_all_apps; run_apps "${apps_running_before_the_test[@]}"' EXIT
+
 # Run all apps to load all SMACK rules
-# Some applications may have dependencies on other apps, so double execution is required at first.
-sqlite3 /opt/dbspace/.security-manager.db "select name from app" | xargs -i sh -c 'app_launcher -s {} | true'
-sqlite3 /opt/dbspace/.security-manager.db "select name from app" | xargs -i sh -c 'app_launcher -s {} | true'
+run_all_apps
 
 # Copy original SMACK rules
 cat /sys/fs/smackfs/load2 | sort > $tmp_current_rule
@@ -42,8 +82,8 @@ smackctl apply
 security-manager-rules-loader
 
 # Re-run all apps
-sqlite3 /opt/dbspace/.security-manager.db "select name from app" | xargs -i sh -c 'app_launcher -k {} | true'
-sqlite3 /opt/dbspace/.security-manager.db "select name from app" | xargs -i sh -c 'app_launcher -s {} | true'
+kill_all_apps
+run_all_apps
 
 # Copy calculated SMACK rules
 cat /sys/fs/smackfs/load2 | sort > $tmp_calculated_rule