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
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