#!/system/bin/sh # Driver Manager v2 - Main Service # Runs after boot_completed MODDIR=${0%/*} LOGFILE="$MODDIR/logs/service.log" CONFIGDIR="$MODDIR/config" . "$MODDIR/scripts/core.sh" log_init "$LOGFILE" log "Service starting" # Wait for boot to complete while [ "$(getprop sys.boot_completed)" != "1" ]; do sleep 1 done sleep 3 log "Boot completed, initializing" # --- Run driver scan if registry doesn't exist --- if [ ! -f "$CONFIGDIR/drivers.json" ] || [ ! -s "$CONFIGDIR/drivers.json" ]; then log "No driver registry found, running initial scan" sh "$MODDIR/scripts/driver_registry.sh" scan fi # --- Create protection baseline if missing --- if [ ! -f "$CONFIGDIR/baseline.json" ] || [ ! -s "$CONFIGDIR/baseline.json" ]; then log "No protection baseline found, creating" sh "$MODDIR/scripts/protect.sh" baseline fi # --- Load kernel modules from autoload list --- log "Loading kernel modules" sh "$MODDIR/scripts/ko_manager.sh" autoload # --- Apply driver scopes --- log "Applying driver scopes" sh "$MODDIR/scripts/scope_manager.sh" apply # --- Start protection monitor --- SETTINGS=$(cat "$CONFIGDIR/settings.json" 2>/dev/null) PROT_MODE=$(echo "$SETTINGS" | grep -o '"protection_mode"[[:space:]]*:[[:space:]]*"[^"]*"' | grep -o '"[^"]*"$' | tr -d '"') if [ "$PROT_MODE" = "monitor" ] || [ "$PROT_MODE" = "enforce" ]; then log "Starting protection monitor (mode: $PROT_MODE)" sh "$MODDIR/scripts/protect.sh" watch & echo $! > "$MODDIR/run/protect.pid" fi # --- Monitor Zygote for new app launches (apply per-app scopes) --- MONITOR_ZYGOTE=$(echo "$SETTINGS" | grep -o '"monitor_zygote"[[:space:]]*:[[:space:]]*[a-z]*' | grep -o '[a-z]*$') if [ "$MONITOR_ZYGOTE" = "true" ]; then log "Starting Zygote process monitor for per-app scoping" sh "$MODDIR/scripts/scope_manager.sh" monitor & echo $! > "$MODDIR/run/scope_monitor.pid" fi log "Service initialization complete" # --- Keep service alive for API calls --- while true; do sleep 86400 done