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
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# Changelog

## Unreleased

- **A root service can read its own settings** (`HmgApi.settings()`, `saveSettings()`): the
daemon sees such a service as root, so the client names its mod (`HMGAPI_MOD`).
- **A failed apply is rolled back at once.** When a rename, a dexopt, a remount or a copy
failed part-way, /system stayed half-applied (one library new, the next stock, an app's
odex deleted) until the next start, and every other engine command was refused. Now the
same run puts every file and setting back, last changed first, deletes its temp files
(`*.hmgapi-new`, `stage/`), and starts the apps it stopped. Only a power cut, a killed
engine or a rollback that fails too leaves the journal for the next start (`engine recover`).
- **Apps stopped during an apply always come back.** The apps whose files change (GPM,
DiPO, iAP2, navigation, BtSettings) are `pm disable`d for the swap, and Android keeps them
off across restarts. The list lived only in memory, so a power cut (the driver's door, the
battery saver) or a killed engine left them off for good. It's now written to the journal
and `state/held.txt` before any is stopped; `engine recover` starts them again, and at boot,
once Android is up, `engine release-held` does (only with no unfinished apply left, so they
never run on half-changed files). Android itself (`framework-res.apk`), persistent apps
(`packages.xml`), OTAClient and apps that were off already are never stopped; an app
`pm` won't stop keeps running, as before.
- **File modes, owners and SELinux labels are kept.** Every file was written, restored and
rolled back as 644 root:root, whatever it was: uninstalling a mod that patched a binary in
`/system/bin` put the stock one back unable to run. Each file's own mode, owner and label
are now read before HMG-API first writes it and put back on restore and rollback; a change
with no `"mode"` keeps them. A new file under `/system/bin` or `/system/xbin` with no `"mode"`
is refused. Files recorded by an earlier HMG-API get Android's defaults on restore (755
root:shell in `/system/bin` and `/system/xbin`, 644 root:root elsewhere).
Tested off-car only.

## 1.1.0 (2026-11-28)

1.1.0-rc.10's code: only the version, this heading and the release notes changed. It is the
Expand Down
193 changes: 193 additions & 0 deletions core/runtime/engine-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,197 @@ for i in 1 2 3; do
done
echo "ok three plans at once on a fresh mod, three rounds"
check "... the feature file written once, no .tmp left" "android.hardware.wifi.xml" "$(ls -A $R/data/hmgapi/mods/wa/.features)"

# ---- a failed apply: rolled back in the same run; a cut-off one: by recover. Apps held while
# their files change are journaled first and always started again; modes, owners, labels kept.
# Off the unit, owners and labels live in $R/meta.txt (FileMeta), the mode is the file's own.
rm -rf $R/data/hmgapi/mods/* $R/data/hmgapi/state $R/data/hmgapi/stock
check "app stock before" "$STOCK_APK $STOCK_ODEX" "$(md5 $R/system/app/Test.apk) $(md5 $R/system/app/Test.odex)"
mkdir -p $R/system/bin $R/system/framework $R/data/system
. core/runtime/test-meta.sh
for f in lib/a.so lib/z.so bin/netd framework/framework-res.apk app/Pers.apk app/Off.apk app/Two.apk; do
head -c 4096 /dev/urandom > $R/system/$f; head -c 4096 /dev/urandom > $T/m/$(basename $f).new
done
chmod 640 $R/system/lib/a.so; chmod 644 $R/system/lib/z.so; chmod 755 $R/system/bin/netd
printf '%s\n' "/system/lib/a.so 1000 1000 u:object_r:system_file:s0" "/system/bin/netd 0 2000 u:object_r:netd_exec:s0" > $R/meta.txt
cp -p $R/meta.txt $T/meta.stock
OWN_A=$(own /system/lib/a.so); OWN_Z=$(own /system/lib/z.so); OWN_N=$(own /system/bin/netd)
check "stock modes and owners set up" "640 1000 1000 u:object_r:system_file:s0|644 0 0 -|755 0 2000 u:object_r:netd_exec:s0" "$OWN_A|$OWN_Z|$OWN_N"
sums() { for f in lib/a.so lib/z.so bin/netd app/Test.apk app/Test.odex framework/framework-res.apk app/Pers.apk app/Off.apk app/Two.apk; do md5 $R/system/$f; done | tr '\n' ' '; }
STOCK_ALL=$(sums)
# a package manager: Test.apk's and Two.apk's apps run (PM_REFUSE names one it won't stop), Off.apk's is off already, Pers.apk's is persistent, framework-res is android
cat > $T/pm <<EOF
#!/bin/sh
echo "\$*" >> $T/pm.calls
case "\$1 \$2" in
"list packages") if [ "\$3" = -f ]; then cat $T/pm.pkgs; else sed 's/^/package:/' $T/pm.off; fi ;;
"disable "*) if [ "\$2" = "\$PM_REFUSE" ]; then echo "Error: refused"; exit 1; fi
echo "\$2" >> $T/pm.off; echo "Package \$2 new state: disabled" ;;
"enable "*) grep -vx "\$2" $T/pm.off > $T/pm.off.n || true; mv $T/pm.off.n $T/pm.off; echo "Package \$2 new state: enabled" ;;
esac
EOF
chmod +x $T/pm
printf 'package:%s\n' /system/app/Test.apk=com.test.app /system/app/Two.apk=com.test.two /system/app/Off.apk=com.test.off /system/app/Pers.apk=com.test.pers \
/system/framework/framework-res.apk=android > $T/pm.pkgs
cat > $R/data/system/packages.xml <<'X'
<packages>
<package name="android" codePath="/system/framework/framework-res.apk" nativeLibraryPath="/data/app-lib/framework-res" flags="573005" ft="1" version="19" sharedUserId="1000">
<package name="com.test.app" codePath="/system/app/Test.apk" nativeLibraryPath="/data/app-lib/Test" flags="572997" ft="1" version="1" userId="10010">
<package name="com.test.two" codePath="/system/app/Two.apk" flags="572997" ft="1" version="1" userId="10013">
<package name="com.test.off" codePath="/system/app/Off.apk" flags="572997" ft="1" version="1" userId="10011">
<package name="com.test.pers" codePath="/system/app/Pers.apk" flags="573005" ft="1" version="1" userId="10012">
<updated-package name="com.test.app" codePath="/system/app/Test.apk" flags="8" ft="1" version="1" />
</packages>
X
pmreset() { echo com.test.off > $T/pm.off; rm -f $T/pm.calls; }
# engf "<step>:<path> ...": eng with those steps failing (Engine.testFail); exported and unset again, since
# sh keeps a variable put before a function call
engf() { HMGAPI_TEST_FAIL=$1; export HMGAPI_TEST_FAIL; shift; eng "$@"; unset HMGAPI_TEST_FAIL; }
pmreset
export HMGAPI_PM=$T/pm HMGAPI_PROPS="sys.boot_completed=1"
mod rb 1.0.0 '' '
{"type": "add", "path": "/system/lib/a.so", "src": "a.so.new", "stock": "any"},
{"type": "add", "path": "/system/lib/z.so", "src": "z.so.new", "stock": "any"},
{"type": "add", "path": "/system/bin/netd", "src": "netd.new", "stock": "any", "mode": "755"},
{"type": "app", "path": "/system/app/Test.apk", "delta": "Test.lpd"},
{"type": "add", "path": "/system/framework/framework-res.apk", "src": "framework-res.apk.new", "stock": "any"},
{"type": "add", "path": "/system/app/Pers.apk", "src": "Pers.apk.new", "stock": "any"},
{"type": "add", "path": "/system/app/Off.apk", "src": "Off.apk.new", "stock": "any"},
{"type": "add", "path": "/system/app/Two.apk", "src": "Two.apk.new", "stock": "any"}'
cp $T/m/*.new $T/m/Test.lpd $R/data/hmgapi/mods/rb/
J=$R/data/hmgapi/state/journal.json; HELD=$R/data/hmgapi/state/held.txt
left() { find $R/system -name '*.hmgapi-new' | tr '\n' ' '; }
allstock() { # allstock <what>: every file, mode, owner and label as before, nothing left over, apps as before
check "$1: every file as it was" "$STOCK_ALL" "$(sums)"
check "$1: modes, owners, labels as they were" "$OWN_A|$OWN_Z|$OWN_N" "$(own /system/lib/a.so)|$(own /system/lib/z.so)|$(own /system/bin/netd)"
check "$1: no journal, no prev/, no held list, no temp files" "no no no " "$([ -f $J ] && echo yes || echo no) $([ -d $R/data/hmgapi/state/prev ] && echo yes || echo no) $([ -f $HELD ] && echo yes || echo no) $(left)"
check "$1: the apps it held started again; the one off before still off" "com.test.off" "$(cat $T/pm.off)"
}

engf "swap:/system/lib/z.so" apply
check "a swap failing part-way (a.so written, z.so not): the apply fails" 1 $rc
has "... saying why" "apply failed: java.io.IOException: test: swap fails at /system/lib/z.so" "$out"
has "... rolled back in the same run, last written first" "rolled back /system/lib/a.so" "$out"
has "... all of it" "rolled back: every file is as it was before this apply" "$out"
allstock "swap failure"
has "... the app was stopped first, then started again" "disable com.test.app|" "$(tr '\n' '|' < $T/pm.calls)"
hasnt "... Android itself (framework-res.apk) never stopped" "disable android" "$(cat $T/pm.calls)"
hasnt "... nor a persistent app" "disable com.test.pers" "$(cat $T/pm.calls)"
hasnt "... nor one that was off already (or started)" "com.test.off" "$(grep -v '^list' $T/pm.calls)"
has "... said" "note com.test.pers keeps running while /system/app/Pers.apk changes (persistent)" "$out"
check "... no record of the failed apply" "" "$(grep -o 'a\.so' $R/data/hmgapi/state/files.json 2>/dev/null || true)"
eng plan
check "... other commands aren't refused after it" 0 $rc

pmreset
engf "dexopt:/system/app/Test.apk" apply
check "dexopt failing (the odex deleted already): the apply fails" 1 $rc
allstock "dexopt failure"
check "... the odex back" "$STOCK_ODEX" "$(md5 $R/system/app/Test.odex)"

pmreset
engf "put:/system/lib/z.so" apply
check "a copy failing: the apply fails" 1 $rc
allstock "copy failure (its temp file deleted)"

pmreset
engf "remount:rw" apply
check "remount rw failing: the apply fails" 1 $rc
allstock "remount failure"
hasnt "... nothing had changed, so nothing rolled back" "rolled back /system" "$out"

# pm throwing at the second app it stops: the first, off already, is still started again
pmreset
engf "disable:com.test.two" apply
check "pm failing at the second app: the apply fails" 1 $rc
has "... saying why" "apply failed: java.io.IOException: test: disable fails at com.test.two" "$out"
allstock "pm failing mid-hold"
has "... the first app stopped, then started again" "disable com.test.app|enable com.test.app|" "$(grep -v '^list' $T/pm.calls | tr '\n' '|')"

# pm refusing to stop an app: it keeps running (as before), isn't listed as held, isn't started "again"
pmreset
PM_REFUSE=com.test.two; export PM_REFUSE
engf "swap:/system/lib/z.so" apply
unset PM_REFUSE
check "pm refusing one app (then a swap failing): the apply fails" 1 $rc
has "... the refusal said" "WARNING: couldn't stop com.test.two (Error: refused); it keeps running while its files change" "$out"
has "... only the app that stopped listed" "stopped [com.test.app] while their files change" "$out"
hasnt "... nor started again" "enable com.test.two" "$(cat $T/pm.calls)"
allstock "pm refusing an app"

# pm failing as an app starts again: the apply's own failure is what's said, the other app starts,
# the one that didn't stays in held.txt for release-held
pmreset
engf "swap:/system/lib/z.so enable:com.test.app" apply
check "pm failing at release: the apply fails" 1 $rc
has "... with its own reason" "apply failed: java.io.IOException: test: swap fails at /system/lib/z.so" "$out"
has "... rolled back" "rolled back: every file is as it was before this apply" "$out"
has "... the other app started" "started [com.test.two] again" "$out"
check "... the one that didn't: in held.txt, still off" "com.test.app|com.test.app com.test.off" "$(cat $HELD)|$(sort $T/pm.off | tr '\n' ' ' | sed 's/ $//')"
eng release-held
check "... release-held starts it" 0 $rc
allstock "pm failing at release, then release-held"

# a power cut (or a killed engine) in the middle: a.so new, z.so stock, the app off
pmreset
engf "halt:/system/lib/z.so" apply
check "cut off mid-swap: the journal stays" yes "$([ -f $J ] && echo yes)"
check "... a.so new, z.so stock" "$(md5 $T/m/a.so.new) $(md5 $R/data/hmgapi/state/prev/_system_lib_z.so)" "$(md5 $R/system/lib/a.so) $(md5 $R/system/lib/z.so)"
check "... the held apps journaled before they were stopped, and still off" "com.test.app com.test.two|com.test.app com.test.two" "$(tr '\n' ' ' < $HELD | sed 's/ $//')|$(python3 -c 'import json,sys; print(" ".join(json.load(open(sys.argv[1]))["held"]))' $J)"
has "... (off in the package manager)" "com.test.app" "$(cat $T/pm.off)"
eng plan
check "... other commands wait for recover" 1 $rc
eng status
has "... status names the held apps" "held [com.test.app, com.test.two]" "$out"
export HMGAPI_PROPS="" # recover at boot runs before Android is up
eng recover
check "recover before Android is up" 0 $rc
has "... rolled back" "unfinished apply rolled back" "$out"
check "... files, modes, owners back" "$STOCK_ALL|$OWN_A|$OWN_Z|$OWN_N" "$(sums)|$(own /system/lib/a.so)|$(own /system/lib/z.so)|$(own /system/bin/netd)"
has "... the app left for boot.sh" "start again once Android is up (boot.sh: engine release-held)" "$out"
check "... still in held.txt" "com.test.app com.test.two" "$(tr '\n' ' ' < $HELD | sed 's/ $//')"
eng release-held
check "release-held with Android not up: waits" 1 $rc
export HMGAPI_PROPS="sys.boot_completed=1"
eng release-held
check "release-held once Android is up" 0 $rc
allstock "power cut, then recover and release-held"

# the rollback fails too: the journal stays, the apps stay off with their files half-changed, recover finishes later
pmreset
engf "swap:/system/lib/z.so rollback:/system/lib/a.so" apply
check "a rollback that fails too: the apply fails" 1 $rc
has "... said" "NOT rolled back: [/system/lib/a.so (test: rollback fails at /system/lib/a.so)]" "$out"
check "... the journal kept" yes "$([ -f $J ] && echo yes)"
has "... the app stays off" "com.test.app" "$(cat $T/pm.off)"
eng release-held
check "... release-held won't start it over half-changed files" 1 $rc
eng recover
check "... recover finishes it" 0 $rc
allstock "failed rollback, then recover"

# a good apply: modes and owners kept, a mod's mode used, stock ones recorded; restore puts them back
pmreset
eng apply
check "apply" 0 $rc
check "a file with no mode keeps its stock mode, owner and label" "$OWN_A" "$(own /system/lib/a.so)"
check "netd: the mod's mode, the stock owner and label" "$OWN_N" "$(own /system/bin/netd)"
check "the held app started again" "com.test.off" "$(cat $T/pm.off)"
check "no held list left" no "$([ -f $HELD ] && echo yes || echo no)"
check "stock modes recorded" "640 1000 1000 u:object_r:system_file:s0" "$(python3 -c 'import json, sys; m = json.load(open(sys.argv[1]))["files"]["/system/lib/a.so"]["stockMeta"]["/system/lib/a.so"]; print(m["mode"], m["uid"], m["gid"], m["context"])' $R/data/hmgapi/state/files.json)"
eng restore
check "restore" 0 $rc
check "restore: files, modes, owners, labels stock" "$STOCK_ALL|$OWN_A|$OWN_Z|$OWN_N" "$(sums)|$(own /system/lib/a.so)|$(own /system/lib/z.so)|$(own /system/bin/netd)"
# a record from before modes were kept, and netd as that HMG-API wrote it (644 0:0): restore uses /system/bin's 755 0:2000
eng apply
python3 - $R/data/hmgapi/state/files.json <<'PY'
import json, sys
s = json.load(open(sys.argv[1]))
for f in s["files"].values(): f.pop("stockMeta", None)
json.dump(s, open(sys.argv[1], "w"))
PY
chmod 644 $R/system/bin/netd; sed -i.bak '/^\/system\/bin\/netd /d' $R/meta.txt
eng restore
check "restore of an old record: /system/bin's defaults" "755 0 2000 -" "$(own /system/bin/netd)"
unset HMGAPI_PM HMGAPI_PROPS
exit $fail
Loading
Loading