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
This commit is contained in:
Krzysztof kuhy Rudnicki 2026-03-27 16:04:50 +01:00
parent ead6072eee
commit ccb40ae635
2 changed files with 26 additions and 5 deletions

View File

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

View File

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