From fc8da152527e8d36b335b61cfcd29969da79f466 Mon Sep 17 00:00:00 2001 From: VysseCodes <205995085+VysseCodes@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:26:44 -0600 Subject: [PATCH 1/3] One install or uninstall at a time; the USB stick no longer offers the NEMESIS install The stick carried both ota_package.tar (Engineering Mode's DSP OTA) and a NEMESIS.data of type install, so on a unit with NEMESIS both could run the same install.sh at once, each swapping app_process, and install.sh had no lock. install.sh and uninstall.sh take a lock first (lib.sh install_lock): a folder on tmpfs (/dev/.hmgapi-install), so a run cut off by a power loss never blocks the next boot, with the holder's pid; a holder that's gone is taken over. While it's held they refuse and change nothing. The stick no longer has NEMESIS.data or the NEMESIS package; the README points NEMESIS owners to route A or the NEMESIS download. Tested off-car only: install/common/test.sh (lock taken, refused while another install runs, taken over from a holder that's gone), install-test.sh (262), orders-test.sh, install/usb/test.sh (60). --- CHANGELOG.md | 10 ++++++++++ install/common/install.sh | 1 + install/common/lib.sh | 22 ++++++++++++++++++++++ install/common/test.sh | 28 ++++++++++++++++++++++++++++ install/common/uninstall.sh | 1 + install/usb/README.txt | 6 +++--- install/usb/test.sh | 11 +++++------ tools/mkinstallers.sh | 7 +++---- 8 files changed, 73 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f898a92..39aa7cec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## Unreleased + +- **One install or uninstall at a time.** The USB stick carried both the Engineering Mode + install and a `NEMESIS.data`, so on a unit with NEMESIS both could start the same install at + once, each swapping `app_process`. `install.sh` and `uninstall.sh` now take a lock first + (`/dev/.hmgapi-install`, gone at every restart, taken over when its holder has ended) and + refuse with "another HMG-API install or uninstall is running" while it's held. The stick no + longer has a `NEMESIS.data` or the NEMESIS package; NEMESIS owners use the Engineering Mode + route or HMG-API's NEMESIS download. + ## 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 diff --git a/install/common/install.sh b/install/common/install.sh index bc889914..ce72fead 100644 --- a/install/common/install.sh +++ b/install/common/install.sh @@ -74,6 +74,7 @@ backup() { } capcheck +install_lock log "HMG-API $VERSION install $(date)" # the payload is whole diff --git a/install/common/lib.sh b/install/common/lib.sh index 295394e6..e3646692 100644 --- a/install/common/lib.sh +++ b/install/common/lib.sh @@ -9,6 +9,28 @@ HOOK_END="# HMGAPI_HOOK end" log() { echo "$1"; echo "$1" >> "$LOG" 2>/dev/null; } sum() { set -- $(md5 "$1" 2>/dev/null); echo "$1"; } +# One install or uninstall at a time. The USB stick can start one (Engineering Mode's DSP +# OTA) while NEMESIS offers the same package from it, and the app, adb and NEMESIS packages +# all call these scripts too. A folder on tmpfs (/dev): a run cut off by a power loss never +# blocks the next boot. Its pid file names the holder; one that's gone (that pid is no +# longer one of these scripts) is taken over. Given back when the script exits, however. +INSTALL_LOCK=$ROOT/dev/.hmgapi-install +install_lock() { + local p + [ -z "$ROOT" ] || mkdir -p "$ROOT/dev" + if ! mkdir "$INSTALL_LOCK" 2>/dev/null; then + p=$(cat "$INSTALL_LOCK/pid" 2>/dev/null) + if [ -n "$p" ] && [ "$p" != $$ ] && grep -q -e install.sh -e troubleShooter "/proc/$p/cmdline" 2>/dev/null; then + log "FAIL: another HMG-API install or uninstall is running (pid $p). Nothing was changed: wait for it to finish." + exit 1 + fi + rm -rf "$INSTALL_LOCK" + mkdir "$INSTALL_LOCK" 2>/dev/null || { log "FAIL: another HMG-API install or uninstall just started. Nothing was changed: wait for it to finish."; exit 1; } + fi + echo $$ > "$INSTALL_LOCK/pid" + trap 'rm -rf "$INSTALL_LOCK"' EXIT +} + # NEMESIS 1.17's LivePatch puts its own shell script at /system/bin/app_process (its watchdog # writes it again on every boot, LivePatch package or not) and runs app_process.real, which # holds whatever binary it found there first: stock, or HMG-API's wrapper. Under that script diff --git a/install/common/test.sh b/install/common/test.sh index 21bc1355..bf232641 100644 --- a/install/common/test.sh +++ b/install/common/test.sh @@ -108,6 +108,34 @@ echo 999999 > "$D/state/hook-check.pid" HC > /dev/null check "hook-check: a stale lock (after a restart) doesn't stop it" "1" "$(grep -c 'HMGAPI_HOOK begin' "$T/system/etc/install-recovery.sh")" +# install_lock (lib.sh): one install or uninstall at a time +L="$T/dev/.hmgapi-install" +IL() { env ROOT="$T" LOG="$T/lock.log" $SH -c ". $PWD/lib.sh; install_lock; $1"; } +rm -rf "$L" +IL "echo held \$(cat $L/pid)" > "$T/lock.out" && r=0 || r=$? +check "install_lock: free: taken" "0 yes" "$r $(grep -q '^held [0-9]' "$T/lock.out" && echo yes)" +check "... and given back when the script exits" "no" "$([ -d "$L" ] && echo yes || echo no)" +printf 'sleep 5\n' > "$T/install.sh" +$SH "$T/install.sh" & hp=$! +sleep 0.3 +if grep -q install.sh /proc/$hp/cmdline 2>/dev/null; then + mkdir -p "$L"; echo $hp > "$L/pid" + IL "echo got in" > "$T/lock.out" && r=0 || r=$? + check "install_lock: another install running: refused" "1" "$r" + check "... saying so, and nothing ran" "yes no" "$(grep -q 'another HMG-API install or uninstall is running' "$T/lock.out" && echo yes) $(grep -q 'got in' "$T/lock.out" && echo yes || echo no)" + check "... its lock left alone" "$hp" "$(cat "$L/pid")" +else + echo "skip install_lock: another install running (no /proc here)" +fi +kill $hp 2>/dev/null || true; wait $hp 2>/dev/null || true +echo 999999 > "$L/pid" +IL "echo got in" > "$T/lock.out" && r=0 || r=$? +check "install_lock: a holder that's gone (a run cut off): taken over" "0 yes" "$r $(grep -q 'got in' "$T/lock.out" && echo yes)" +mkdir -p "$L"; echo $$ > "$L/pid" +IL "echo got in" > "$T/lock.out" && r=0 || r=$? +check "install_lock: its pid now another process (not an installer): taken over" "0" "$r" +rm -rf "$L" + # boot.sh: an apply that was cut off (or a trial) is rolled back, then one restart. A # recover that fails part-way keeps its journal, so restarting on it would loop. mkdir -p "$T/system/bin" diff --git a/install/common/uninstall.sh b/install/common/uninstall.sh index 4b9ac981..e01d41e0 100644 --- a/install/common/uninstall.sh +++ b/install/common/uninstall.sh @@ -31,6 +31,7 @@ capcheck() { exit 1 } capcheck +install_lock log "HMG-API uninstall $(date)" # the zygote binaries the install wrapped: app_process unless it's NEMESIS 1.17's LivePatch script, diff --git a/install/usb/README.txt b/install/usb/README.txt index e1717ae7..da84aa34 100644 --- a/install/usb/README.txt +++ b/install/usb/README.txt @@ -10,8 +10,8 @@ A. The head unit has nothing installed yet (no NEMESIS): 3. Messages show "HMG-API ... installing", then "HMG-API installed, restarting". The OTA screen then shows error 684. That's expected: nothing is undone. -B. NEMESIS is installed: plug the stick in once Android is up and confirm the - install of "HMG-API". +B. NEMESIS is installed: use route A too, or HMG-API's NEMESIS download + (hmgapi-@VERSION@-nemesis.zip) on a stick of its own. C. HMG-API is installed: the HMG-API app offers the update from HMGAPI/updates. That folder also holds the mods (install them from the app). @@ -23,7 +23,7 @@ backups). Every change HMG-API or its mods made goes back to stock. The result is written to HMGAPI/install.log on the stick. Take the stick out afterwards: with it in, the head unit can try to start -wired Android Auto, and NEMESIS offers the install again. +wired Android Auto. Only for the cars and firmware HMG-API supports (see the compatibility page). On anything else HMG-API stays off and says why. diff --git a/install/usb/test.sh b/install/usb/test.sh index a78605a7..e8c48c90 100755 --- a/install/usb/test.sh +++ b/install/usb/test.sh @@ -21,13 +21,12 @@ has() { case "$3" in *"$2"*) echo "ok $1" ;; *) echo "FAIL $1: wanted [$2] i # whose own NEMESIS.data names them ini() { unzip -p "$1" "$2" | grep "^$3=" | head -1 | cut -d= -f2-; } relok() { case "$1" in ''|/*|*..*|*[\;\|\&\`\$\'\"\(\)\<\>]*) echo no ;; *) echo yes ;; esac; } +# the USB stick: the DSP OTA route only. NEMESIS reading a NEMESIS.data on it would offer the same +# package while the DSP route runs: two installs at once n=${UZ##*/} -check "$n: NEMESIS.data says install" install "$(ini "$UZ" NEMESIS.data type)" -rel=$(ini "$UZ" NEMESIS.data path) -check "$n: path= is one NEMESIS takes ($rel)" yes "$(relok "$rel")" -check "$n: path= is in the zip" "$rel" "$(unzip -Z1 "$UZ" | grep -x "$rel" || true)" -unzip -p "$UZ" "$rel" > $T/inner.zip -check "$n: the package it installs is HMG-API's" hmgapi "$(ini $T/inner.zip NEMESIS.data id)" +check "$n: no NEMESIS.data (NEMESIS doesn't offer a second install)" "" "$(unzip -Z1 "$UZ" | grep -x NEMESIS.data || true)" +check "$n: no NEMESIS package on it" "" "$(unzip -Z1 "$UZ" | grep -x HMGAPI/install/hmgapi.zip || true)" +check "$n: the DSP OTA entry and the payload" "HMGAPI/install/payload/install.sh ota_package.tar" "$(unzip -Z1 "$UZ" | grep -x -e ota_package.tar -e HMGAPI/install/payload/install.sh | LC_ALL=C sort | tr '\n' ' ' | sed 's/ $//')" # the NEMESIS download: a Package Store list (type=list, NEMESIS 1.16; StoreList.parseList): # [package.0], [package.1], ... with no gaps, each a path= to a package on the stick, HMG-API first n=${NB##*/} diff --git a/tools/mkinstallers.sh b/tools/mkinstallers.sh index 179fe9eb..66bee768 100755 --- a/tools/mkinstallers.sh +++ b/tools/mkinstallers.sh @@ -1,7 +1,8 @@ #!/bin/sh # Make HMG-API's install downloads in out/dist/: -# hmgapi--usb.zip a USB stick: DSP OTA (nothing installed yet), NEMESIS -# (NEMESIS.data type=install) and the app (HMGAPI/updates) +# hmgapi--usb.zip a USB stick: DSP OTA (install, update, uninstall) and the app +# (HMGAPI/updates). No NEMESIS.data: NEMESIS offering the same +# package from the stick could start a second install at once # hmgapi--nemesis.zip for NEMESIS 1.16 or newer: unzipped onto a stick, its # NEMESIS.data (type=list) opens NEMESIS's Package Store on # packages/hmgapi.zip (the package itself, NEMESIS.data @@ -92,8 +93,6 @@ with tarfile.open(sys.argv[2], "w", format=tarfile.GNU_FORMAT) as t: t.addfile(i, io.BytesIO(data)) PY cp -r out/payload "$U/HMGAPI/install/payload" -cp "$W/hmgapi.zip" "$U/HMGAPI/install/hmgapi.zip" -printf '[nemesis]\nversion=1.0\ntype=install\npath=HMGAPI/install/hmgapi.zip\n' > "$U/NEMESIS.data" RK=${HMGAPI_RELEASE_KEY:-$HOME/hmg-private/keys/leafia-release.key} if [ -f "$RK" ]; then cp "$(tools/mkrelease.sh "$RK" | tail -1)" "$U/HMGAPI/updates/" From 8bca892539e94da4256050436d04a25a1f32b226 Mon Sep 17 00:00:00 2001 From: VysseCodes <205995085+VysseCodes@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:47:40 -0600 Subject: [PATCH 2/3] USB stick README: don't send NEMESIS owners to HMG-API's NEMESIS download HMG-API isn't installed alongside NEMESIS (2.0), so the stick's route B says so instead of pointing at hmgapi--nemesis.zip. --- CHANGELOG.md | 3 +-- install/usb/README.txt | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39aa7cec..b5de3e97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,7 @@ once, each swapping `app_process`. `install.sh` and `uninstall.sh` now take a lock first (`/dev/.hmgapi-install`, gone at every restart, taken over when its holder has ended) and refuse with "another HMG-API install or uninstall is running" while it's held. The stick no - longer has a `NEMESIS.data` or the NEMESIS package; NEMESIS owners use the Engineering Mode - route or HMG-API's NEMESIS download. + longer has a `NEMESIS.data` or the NEMESIS package (HMG-API isn't installed alongside NEMESIS). ## 1.1.0 (2026-11-28) diff --git a/install/usb/README.txt b/install/usb/README.txt index da84aa34..d7fb3644 100644 --- a/install/usb/README.txt +++ b/install/usb/README.txt @@ -10,8 +10,8 @@ A. The head unit has nothing installed yet (no NEMESIS): 3. Messages show "HMG-API ... installing", then "HMG-API installed, restarting". The OTA screen then shows error 684. That's expected: nothing is undone. -B. NEMESIS is installed: use route A too, or HMG-API's NEMESIS download - (hmgapi-@VERSION@-nemesis.zip) on a stick of its own. +B. NEMESIS is installed: HMG-API isn't installed alongside NEMESIS. This build + has no way to install on a unit with NEMESIS yet. C. HMG-API is installed: the HMG-API app offers the update from HMGAPI/updates. That folder also holds the mods (install them from the app). From 6abb939297626c244d3f26576653a3235a45b474 Mon Sep 17 00:00:00 2001 From: VysseCodes <205995085+VysseCodes@users.noreply.github.com> Date: Wed, 2 Dec 2026 11:18:22 -0600 Subject: [PATCH 3/3] Install lock: read the pid again before taking a lock over; macOS test fix A second install arriving between the first one's mkdir and its pid write saw an empty pid, took the lock over, and both ran (review). An empty pid is now read again after a second before the lock counts as left behind. The test's skip path (no /proc, macOS) never made the lock folder before writing a pid into it, which ended the suite under set -e. Tested off-car: install/common/test.sh (new check: a lock taken a moment ago with no pid yet is refused; it fails without the fix), in sh and bash. --- install/common/lib.sh | 2 ++ install/common/test.sh | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/install/common/lib.sh b/install/common/lib.sh index e3646692..d1f74c13 100644 --- a/install/common/lib.sh +++ b/install/common/lib.sh @@ -20,6 +20,8 @@ install_lock() { [ -z "$ROOT" ] || mkdir -p "$ROOT/dev" if ! mkdir "$INSTALL_LOCK" 2>/dev/null; then p=$(cat "$INSTALL_LOCK/pid" 2>/dev/null) + # no pid yet: its holder may be between its mkdir and writing the pid; read it again + [ -n "$p" ] || { sleep 1; p=$(cat "$INSTALL_LOCK/pid" 2>/dev/null); } if [ -n "$p" ] && [ "$p" != $$ ] && grep -q -e install.sh -e troubleShooter "/proc/$p/cmdline" 2>/dev/null; then log "FAIL: another HMG-API install or uninstall is running (pid $p). Nothing was changed: wait for it to finish." exit 1 diff --git a/install/common/test.sh b/install/common/test.sh index bf232641..33e905bf 100644 --- a/install/common/test.sh +++ b/install/common/test.sh @@ -128,9 +128,19 @@ else echo "skip install_lock: another install running (no /proc here)" fi kill $hp 2>/dev/null || true; wait $hp 2>/dev/null || true -echo 999999 > "$L/pid" +mkdir -p "$L"; echo 999999 > "$L/pid" IL "echo got in" > "$T/lock.out" && r=0 || r=$? check "install_lock: a holder that's gone (a run cut off): taken over" "0 yes" "$r $(grep -q 'got in' "$T/lock.out" && echo yes)" +if [ -d /proc/self ]; then + # just taken by another run that hasn't written its pid yet: read again, then refused + printf 'sleep 5\n' > "$T/install.sh"; $SH "$T/install.sh" & hp=$! + rm -rf "$L"; mkdir -p "$L"; (sleep 0.3; echo $hp > "$L/pid") & + IL "echo got in" > "$T/lock.out" && r=0 || r=$? + check "install_lock: taken a moment ago, no pid yet: read again, refused" "1 no" "$r $(grep -q 'got in' "$T/lock.out" && echo yes || echo no)" + kill $hp 2>/dev/null || true; wait 2>/dev/null || true +else + echo "skip install_lock: taken a moment ago (no /proc here)" +fi mkdir -p "$L"; echo $$ > "$L/pid" IL "echo got in" > "$T/lock.out" && r=0 || r=$? check "install_lock: its pid now another process (not an installer): taken over" "0" "$r"