From ccb40ae635e2bf5b5d414f2e4fb50d1ea5ce678f Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Fri, 27 Mar 2026 16:04:50 +0100 Subject: [PATCH] fix: phone focus mode daemon survival across reboots - Fix PID reuse bug: validate /proc/cmdline before assuming daemon is running - Add log rotation using existing LOG_MAX_LINES config (was 7MB/70K lines) - chmod 666 state files on init and deploy to prevent permission drift --- phone_focus_mode/deploy.sh | 5 +++-- phone_focus_mode/focus_daemon.sh | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/phone_focus_mode/deploy.sh b/phone_focus_mode/deploy.sh index 2bee1fb..6605df8 100755 --- a/phone_focus_mode/deploy.sh +++ b/phone_focus_mode/deploy.sh @@ -143,8 +143,9 @@ do_deploy() { echo "[5/6] Setting permissions..." adb_root "chmod 755 $REMOTE_DIR/config.sh $REMOTE_DIR/focus_daemon.sh $REMOTE_DIR/focus_ctl.sh" || true adb_root "chmod 755 /data/adb/service.d/99-focus-mode.sh" - adb_root "touch $REMOTE_DIR/disabled_by_focus.txt" - adb_root "touch $REMOTE_DIR/focus_mode.log" + adb_root "touch $REMOTE_DIR/disabled_by_focus.txt $REMOTE_DIR/focus_mode.log" + # State files need 666 so the daemon can write regardless of SELinux context drift + adb_root "chmod 666 $REMOTE_DIR/disabled_by_focus.txt $REMOTE_DIR/focus_mode.log" || true echo "[6/6] Starting daemon..." # Stop existing daemon, then start fresh diff --git a/phone_focus_mode/focus_daemon.sh b/phone_focus_mode/focus_daemon.sh index 1a05526..6c2ceb5 100755 --- a/phone_focus_mode/focus_daemon.sh +++ b/phone_focus_mode/focus_daemon.sh @@ -18,10 +18,15 @@ acquire_lock() { local old_pid old_pid="$(cat "$PIDFILE")" if kill -0 "$old_pid" 2>/dev/null; then - echo "Daemon already running (PID $old_pid), exiting." - exit 0 + # Verify the PID is actually a focus_daemon, not a reused PID + local cmdline + cmdline="$(cat /proc/$old_pid/cmdline 2>/dev/null | tr '\0' ' ')" + if echo "$cmdline" | grep -q "focus_daemon"; then + echo "Daemon already running (PID $old_pid), exiting." + exit 0 + fi fi - # Stale pidfile + # Stale or reused pidfile rm -f "$PIDFILE" fi echo $$ > "$PIDFILE" @@ -34,6 +39,16 @@ log() { echo "[$ts] $1" >> "$LOG_FILE" } +rotate_log() { + local lines + lines="$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0)" + if [ "$lines" -gt "$LOG_MAX_LINES" ]; then + local tmp="$LOG_FILE.tmp" + tail -n "$LOG_MAX_LINES" "$LOG_FILE" > "$tmp" + mv "$tmp" "$LOG_FILE" + fi +} + # ---- Build helper files for fast package checks ---- build_whitelist_file() { @@ -73,6 +88,8 @@ init() { mkdir -p "$STATE_DIR" touch "$LOG_FILE" touch "$DISABLED_APPS_FILE" + # Ensure state files are writable (survives reboot / permission drift) + chmod 666 "$LOG_FILE" "$DISABLED_APPS_FILE" "$PIDFILE" 2>/dev/null if [ "$HOME_LAT" = "0.000000" ] && [ "$HOME_LON" = "0.000000" ]; then log "ERROR: Home coordinates not set! Edit config.sh first." @@ -81,6 +98,7 @@ init() { build_whitelist_file build_sysprotect_file + rotate_log if [ -f "$MODE_FILE" ]; then CURRENT_MODE="$(cat "$MODE_FILE")" @@ -261,6 +279,8 @@ main() { else sleep "$CHECK_INTERVAL_NORMAL" fi + + rotate_log done }