Skip to content

Engine: roll back a failed apply at once, journal held apps, keep file modes - #434

Open
AwsomeFox wants to merge 4 commits into
release/2.0from
fix/apply-recovery
Open

AwsomeFox wants to merge 4 commits into
release/2.0from
fix/apply-recovery

Conversation

@AwsomeFox

Copy link
Copy Markdown
Contributor

Fixes the three problems from the 2026-11-26 review of Engine.java.

Held apps

  • The apps hold() stops (pm disable) are written to the journal and state/held.txt before any is stopped.
  • engine recover starts them again. At boot, boot.sh waits for sys.boot_completed and runs the new engine release-held, which refuses while an unfinished apply's journal is there (apps never run on half-changed files).
  • uninstall.sh runs recover when held.txt is there too.
  • Never stopped: android / anything in /system/framework, persistent apps (flags in /data/system/packages.xml), OTAClient, and apps that were off already (before, release() turned those on).

File modes

  • New FileMeta: real mode, uid, gid and SELinux label (libcore stat and android.os.SELinux on the unit, toolbox as fallback). Recorded before HMG-API's first write (files.json stockMeta, each journal entry) and put back on apply, rollback and restore.
  • chown, then chmod (chown clears setuid), then label; any failing fails the step instead of being ignored.
  • A change under /system/bin or /system/xbin with no "mode" is refused.
  • Records from an earlier HMG-API get Android's defaults on restore (755 root:shell in bin/xbin, 644 root:root elsewhere).

Failed apply

  • Any failure after the journal is written (rename, copy, dexopt, remount, settings, state write) rolls back in the same run, last file first, with settings and binds.txt; *.hmgapi-new and stage/ are deleted; held apps are started after the rollback.
  • The journal stays only after a power cut, a killed engine, or a rollback that fails too.
  • recover says "rolled back" only when a file changed, so boot.sh doesn't restart the unit for nothing.

Tests

  • engine-test.sh: failures at swap, dexopt, copy, remount; a simulated power cut, then recover (Android not up) and release-held; a rollback that fails, finished by recover; modes, owners and labels through apply/restore; an old record restored with the defaults. Fake pm; owners/labels off the unit in $R/meta.txt; failure injection with HMGAPI_TEST_FAIL (off the unit only).
  • safety-test.sh: the bin/xbin "mode" rule, stock mode/owner/label back after restore.
  • overlap-test.sh: mode/owner/label kept over a NEMESIS package, after its uninstall (rebase), and on restore.
  • install/common/test.sh: boot.sh runs engine release-held when held.txt is left.

tools/test-all.sh --fast: 31 ok, 1 skipped (no ImageMagick), 0 failed. Full run on macOS: the 6 tests that need qemu-arm, mksh or ./build.sh (hmgapid, zygote, lpatch, install-test, usb, sdk) fail the same way on untouched main; CI should run them.

Tested off-car only, on no car. Worth checking on a car:

  • whether any of GPM, DiPO, iAP2, navigation or BtSettings is persistent (then it's no longer stopped during the swap);
  • that libcore stat and android.os.SELinux work from the engine's app_process (if stat fails, the apply stops before anything changes).

🤖 Generated with Claude Code

…e modes

A failed apply (rename, dexopt, remount, copy, settings, state write) now
puts every file and setting back in the same run, last changed first,
deletes its temp files and starts the apps it stopped. Only a power cut,
a killed engine or a failed rollback leaves the journal for recover.

Apps stopped (pm disable) while their files change are written to the
journal and state/held.txt before they're stopped. recover starts them
again, and boot.sh runs engine release-held once Android is up (not
while an unfinished apply is waiting). Android itself, persistent apps,
OTAClient and apps already off are never stopped.

Each file's mode, owner and SELinux label are read before HMG-API first
writes it (FileMeta, files.json stockMeta, journal entries) and put back
on apply, rollback and restore. A change under /system/bin or
/system/xbin must give a "mode".

Tests: engine, safety, overlap and boot.sh, with a fake pm and failure
injection (HMGAPI_TEST_FAIL, off the unit only). Tested off-car only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AwsomeFox
AwsomeFox requested review from a team and VysseCodes as code owners November 27, 2026 14:34
AwsomeFox added a commit that referenced this pull request Nov 28, 2026
stagedIn puts them in mods/ right before an apply plans, and they went back only when the plan
was refused. An apply that fails part-way (an exception, the files rolled back or left for recover)
left them in mods/, where their code loads at the next boot, on /system files built for the
versions before them. Found combining the open PRs: with #433's gear check and #434's in-run
rollback there are more such paths; whichever lands after this puts them back there too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CHANGELOG: keep main's root-service settings entry and this branch's
three engine entries under Unreleased.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@VysseCodes VysseCodes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. This PR keeps the held-apps journal (state/held.txt); I've asked #433 to drop its version.

On the new rule refusing /system/bin/xbin changes with no "mode": I checked the mod repos. hmgapi-adb only sets props, hmgapi-adbd binds /sbin/adbd, and hmgapi-nemesis-shortcut has no patches.json, so none is affected.

Merge order: first of #434 → #430 → #433.

AwsomeFox added a commit that referenced this pull request Nov 30, 2026
#434 writes down the apps an apply stops before pm disable and turns
them on again at recover, more fully than this PR did. Take state/held.txt
out of the engine, boot.sh, the tests, the changelog and the car test,
so this PR stays about the gear check. The rest of the cut-off-install
hardening (ignoring a closed pipe, syncing before each rename) stays.

Tested off-car: tools/test-all.sh --fast, 32 ok, 1 skipped (no ImageMagick).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AwsomeFox added a commit that referenced this pull request Nov 30, 2026
Engine.recover: #434's version (startHeld, rollback) with the staged ids taken from the journal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AwsomeFox added a commit that referenced this pull request Nov 30, 2026
Kept #434's held-apps journal where #433 dropped its own version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AwsomeFox
AwsomeFox changed the base branch from main to release/2.0 December 1, 2026 16:39
CHANGELOG: release/2.0 turned the old Unreleased heading into 1.1.0,
which is rc.10's code. This branch's three engine entries and the
root-service settings entry came after it, so they go under a new
Unreleased heading above 1.1.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@VysseCodes VysseCodes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normal apply path looks good, and it works on a car. These are all in the failure paths:

  1. The new /system/bin / /system/xbin mode rule is too broad (Engine.java ~1284). It refuses any change there with no "mode", including a lines or add edit to an existing file, whose stock mode (755 root:shell) this PR already keeps (targetMeta). The refusal has no override. safety-test.sh (~101) asserts it for netd, which already exists, and docs/mod-reference.md says both "keeps the mode, owner and SELinux label" and "must give a mode". Suggest: require a mode only for a new file in those folders.
  2. hold() can leave apps stopped (~1917). held = hold(changed, j) is only assigned when hold() returns. If pm fails on the second app, the first stays disabled: the rollback succeeds, the finally sees an empty held and releases nothing, and the app stays off until the next boot. That breaks the changelog's "always come back". Suggest filling the caller's set as each app is disabled (or releasing from held.txt in the finally).
  3. hold() ignores pm disable's exit code (~2288). The old code held only apps whose disable worked. Now an app that never stopped keeps running on half-changed files, is reported as "stopped" and written to held.txt. If its later pm enable reply lacks "new state", it stays in held.txt, and boot.sh warns about it every boot.
  4. release() can throw from the finally (~2037). If pm can't start, the caller gets a bare IOException instead of "apply failed: …" and its lines, and the remaining held apps are skipped until the next boot. Suggest catching per app inside release() and leaving the rest in held.txt.
  5. Minor: the own() helper (mode + meta.txt) is copied into engine-test.sh, overlap-test.sh and safety-test.sh. One shared copy would keep them from drifting.

From the review of this change:

- A file in /system/bin or /system/xbin that has a stock copy keeps
  that copy's mode, owner and label, so a change to it no longer needs
  a "mode". Only a new file there does (it would get 644 and not run).
  The check moved from guard() to build(), where the stock is known.
- hold() fills the caller's held set as it goes, each app before its
  pm disable, so if pm throws at the second app the first is still
  started again by apply's finally.
- hold() checks pm disable's exit code and reply. An app pm won't stop
  keeps running as before, with a warning; it's dropped from the held
  set, the journal and held.txt.
- release() never throws: a pm that fails for one app leaves that app
  in held.txt and goes on with the rest, so apply's own failure is what
  gets reported. The held apps are released even if remount ro throws.
- The tests share one own() helper (core/runtime/test-meta.sh).

engine-test.sh adds a second app and covers pm failing mid-hold, pm
refusing to stop an app, and pm failing at release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@VysseCodes VysseCodes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every point from the review is fixed: the mode rule only for new files in /system/bin and /system/xbin (moved to build(), where the stock is known), hold() filling the held set before each pm disable and dropping apps pm won't stop, release() never throwing, and one shared own() helper. engine-test (224) and safety-test (56) pass here. Thanks.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants