Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions module/service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ d="${0%/*}"; [ "$d" = "$0" ] && d="."; MODDIR="$(cd "$d" 2>/dev/null && pwd || e
STATE_DIR=/data/adb/vdbackup
LOGS="$STATE_DIR/logs"
LOOPPID="$LOGS/scheduler.loop.pid"
LOOPBOOT="$LOGS/scheduler.loop.boot"
BOOTID="$(cat /proc/sys/kernel/random/boot_id 2>/dev/null)"
INTERVAL=900

# wait for boot to complete before touching the framework
Expand All @@ -48,11 +50,15 @@ mkdir -p "$LOGS"
# honour the skip flag
[ -f "$MODDIR/.scheduler.skip" ] && exit 0

# single-instance guard: bail out if a scheduler loop is already alive
if [ -f "$LOOPPID" ] && kill -0 "$(cat "$LOOPPID" 2>/dev/null)" 2>/dev/null; then exit 0; fi
# single-instance guard: bail out if a scheduler loop is already alive. The pid file survives a
# reboot and Android reuses pids quickly, so a bare kill -0 can match an unrelated process and make
# this script exit before the scheduler and the VDLU boot update; only trust a pid recorded in the
# current boot (boot_id).
if [ -f "$LOOPPID" ] && [ "$(cat "$LOOPBOOT" 2>/dev/null)" = "$BOOTID" ] && kill -0 "$(cat "$LOOPPID" 2>/dev/null)" 2>/dev/null; then exit 0; fi

# recurring, detached heartbeat (survives boot, never blocks it): the loop
# records its own pid for the guard, then runs scheduler-tick every INTERVAL.
echo "$BOOTID" > "$LOOPBOOT"
setsid nohup sh -c '
echo "$$" > "$1"
while :; do
Expand Down
8 changes: 6 additions & 2 deletions module/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ STATE_DIR=/data/adb/vdbackup
# stop the detached scheduler heartbeat first, so it does not keep spinning every 900s
# against a helper.sh that uninstall is about to remove (harmless but noisy until reboot).
_lp="$(cat "$STATE_DIR/logs/scheduler.loop.pid" 2>/dev/null)"
case "$_lp" in ''|*[!0-9]*) : ;; *) kill "$_lp" 2>/dev/null ;; esac
# only signal the pid when it was recorded in this boot (scheduler.loop.boot == boot_id): the pid file
# survives a reboot and Android reuses pids, so a stale pid could belong to an unrelated process.
_lb="$(cat "$STATE_DIR/logs/scheduler.loop.boot" 2>/dev/null)"
_bid="$(cat /proc/sys/kernel/random/boot_id 2>/dev/null)"
case "$_lp" in ''|*[!0-9]*) : ;; *) [ "$_lb" = "$_bid" ] && kill "$_lp" 2>/dev/null ;; esac
rm -rf "$STATE_DIR/staging" "$STATE_DIR/iconcache" 2>/dev/null
rm -f "$STATE_DIR/logs"/*.pid "$STATE_DIR/logs"/*.out "$STATE_DIR/logs"/progress-*.json 2>/dev/null
rm -f "$STATE_DIR/logs"/*.pid "$STATE_DIR/logs"/scheduler.loop.boot "$STATE_DIR/logs"/*.out "$STATE_DIR/logs"/progress-*.json 2>/dev/null
exit 0