From ea16c8141eb64dc28b56a0e7fb536689ef465429 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 20 Oct 2026 22:32:15 +0000 Subject: [PATCH] Fix package management and user targeting on HSUM - Use --user current for pm install, adb install, and appops set across install_script, adb_pm_install, and test/setup scripts. - Skip headless user 0 in get_users() on HSUM devices. - Use --user current in app_notify for non-app UIDs instead of calling content call against all users. - Clear path buffer on each iteration in setup_magisk_env. - Use --user current for am instrument in app migration tests. Assisted-by: Gemini 4 Argon --- .../topjohnwu/magisk/test/AppMigrationTest.kt | 2 +- native/src/core/bootstages.rs | 3 ++- native/src/core/package.rs | 10 +++++++--- native/src/core/scripting.cpp | 4 ++-- native/src/core/su/connect.rs | 19 +++++++++---------- scripts/app_functions.sh | 5 +++-- scripts/avd.sh | 4 ++-- scripts/avd_setup.sh | 2 +- scripts/test_common.sh | 6 +++--- 9 files changed, 30 insertions(+), 25 deletions(-) diff --git a/app/test/src/main/java/com/topjohnwu/magisk/test/AppMigrationTest.kt b/app/test/src/main/java/com/topjohnwu/magisk/test/AppMigrationTest.kt index 2e1cdc637..56faf300a 100644 --- a/app/test/src/main/java/com/topjohnwu/magisk/test/AppMigrationTest.kt +++ b/app/test/src/main/java/com/topjohnwu/magisk/test/AppMigrationTest.kt @@ -64,7 +64,7 @@ class AppMigrationTest { // Trigger the test to run migration val pfd = uiAutomation.executeShellCommand( - "am instrument -w --user 0 -e class .Environment#$method " + + "am instrument -w --user current -e class .Environment#$method " + "$pkg.test/${AppTestRunner::class.java.name}" ) val output = AutoCloseInputStream(pfd).reader().use { it.readText() } diff --git a/native/src/core/bootstages.rs b/native/src/core/bootstages.rs index 2bf1d33f6..9df44d2c8 100644 --- a/native/src/core/bootstages.rs +++ b/native/src/core/bootstages.rs @@ -11,7 +11,7 @@ use crate::mount::{clean_mounts, setup_preinit_dir}; use crate::resetprop::get_prop; use crate::selinux::restorecon; use base::const_format::concatcp; -use base::{BufReadExt, FsPathBuilder, ResultExt, cstr, error, info}; +use base::{BufReadExt, FsPathBuilder, ResultExt, Utf8CStrBuf, cstr, error, info}; use bitflags::bitflags; use nix::fcntl::OFlag; use std::io::BufReader; @@ -36,6 +36,7 @@ impl MagiskD { let mut buf = cstr::buf::default(); for user in self.get_users() { + buf.clear(); buf.append_path(APP_DATA_DIR) .append_path_fmt(user) .append_path(APP_PACKAGE_NAME) diff --git a/native/src/core/package.rs b/native/src/core/package.rs index c2a78d528..1cf95ad2c 100644 --- a/native/src/core/package.rs +++ b/native/src/core/package.rs @@ -506,14 +506,18 @@ impl MagiskD { while let Some(e) = app_data_dir.read()? { if e.is_dir() && let Ok(user) = e.name().parse::() - && user >= 0 { - users.push(user); + if self.is_headless && user == 0 { + continue; + } + if user >= 0 { + users.push(user); + } } } Ok(()) }(); - if !users.contains(&0) { + if users.is_empty() { users.push(0); } users.sort_unstable(); diff --git a/native/src/core/scripting.cpp b/native/src/core/scripting.cpp index 7b7929380..ad2356a09 100644 --- a/native/src/core/scripting.cpp +++ b/native/src/core/scripting.cpp @@ -235,8 +235,8 @@ void exec_module_scripts(Utf8CStr stage, const rust::Vec &module_lis constexpr char install_script[] = R"EOF( APK=%s log -t Magisk "pm_install: $APK" -log -t Magisk "pm_install: $(pm install --user all -g -r $APK 2>&1)" -appops set --user all %s REQUEST_INSTALL_PACKAGES allow +log -t Magisk "pm_install: $(pm install --user current -g -r $APK 2>&1)" +appops set --user current %s REQUEST_INSTALL_PACKAGES allow rm -f $APK )EOF"; diff --git a/native/src/core/su/connect.rs b/native/src/core/su/connect.rs index acee435ef..94f832cbb 100644 --- a/native/src/core/su/connect.rs +++ b/native/src/core/su/connect.rs @@ -70,9 +70,7 @@ impl Extra<'_> { } impl MagiskD { - fn exec_cmd(&self, user: i32, mgr_pkg: &str, action: &'static str, extras: &[Extra]) { - let user = user.to_string(); - + fn exec_cmd(&self, user: &str, mgr_pkg: &str, action: &'static str, extras: &[Extra]) { let provider = format!("content://{mgr_pkg}.provider"); let mut cmd = app_process(); cmd.args([ @@ -82,7 +80,7 @@ impl MagiskD { "--uri", &provider, "--user", - &user, + user, "--method", action, ]); @@ -164,13 +162,14 @@ impl MagiskD { value: Bool(info.settings.log), }, ]; - if to_app_id(info.eval_uid) < AID_APP_START { - for user in self.get_users() { - self.exec_cmd(user, &info.mgr_pkg, "notify", &extras); - } + let user_id; + let user = if to_app_id(info.eval_uid) < AID_APP_START { + "current" } else { - self.exec_cmd(to_user_id(info.eval_uid), &info.mgr_pkg, "notify", &extras); - } + user_id = to_user_id(info.eval_uid).to_string(); + &user_id + }; + self.exec_cmd(user, &info.mgr_pkg, "notify", &extras); } pub(super) fn notify_app(&self, cred: &UCred, info: &SuInfo, request: &SuRequest) { diff --git a/scripts/app_functions.sh b/scripts/app_functions.sh index 6763ea757..7edec9bdb 100644 --- a/scripts/app_functions.sh +++ b/scripts/app_functions.sh @@ -123,11 +123,12 @@ adb_pm_install() { local tmp=/data/local/tmp/temp.apk cp -f "$1" $tmp chmod 644 $tmp - su 2000 -c pm install -g $tmp || pm install -g $tmp || su 1000 -c pm install -g $tmp + local cmd="pm install --user current -g $tmp" + su 2000 -c $cmd || $cmd || su 1000 -c $cmd local res=$? rm -f $tmp if [ $res = 0 ]; then - appops set "$2" REQUEST_INSTALL_PACKAGES allow + appops set --user current "$2" REQUEST_INSTALL_PACKAGES allow fi return $res } diff --git a/scripts/avd.sh b/scripts/avd.sh index 0f8ee1d8b..41ae46266 100755 --- a/scripts/avd.sh +++ b/scripts/avd.sh @@ -322,8 +322,8 @@ app_test_main() { for apk in "${apks[@]}"; do # Cleanup adb shell pm uninstall com.topjohnwu.magisk || true - adb install -r -g $apk - adb install -r -g out/test.apk + adb install --user current -r -g $apk + adb install --user current -r -g out/test.apk am_instrument '.AppTest' $app done } diff --git a/scripts/avd_setup.sh b/scripts/avd_setup.sh index 377600885..f90b43443 100644 --- a/scripts/avd_setup.sh +++ b/scripts/avd_setup.sh @@ -51,7 +51,7 @@ if [ -z "$FIRST_STAGE" ]; then fi fi -pm install -r -g $(pwd)/magisk.apk +pm install --user current -r -g $(pwd)/magisk.apk # Stop zygote (and previous setup if exists) magisk --stop 2>/dev/null diff --git a/scripts/test_common.sh b/scripts/test_common.sh index 0a5e57be1..62cf30957 100644 --- a/scripts/test_common.sh +++ b/scripts/test_common.sh @@ -44,7 +44,7 @@ ensure_android_cli() { # $2 = component am_instrument() { set +x - local out=$(adb shell am instrument -w --user 0 -e class "$1" "$2") + local out=$(adb shell am instrument -w --user current -e class "$1" "$2") echo "$out" if grep -q 'OK (' <<< "$out"; then set -x @@ -60,10 +60,10 @@ run_setup() { adb shell 'PATH=$PATH:/debug_ramdisk magisk -v' # Install the Magisk app - adb install -r -g $apk + adb install --user current -r -g $apk # Install the test app - adb install -r -g out/test.apk + adb install --user current -r -g out/test.apk local app='com.topjohnwu.magisk.test/com.topjohnwu.magisk.test.AppTestRunner'