From 4db2a4c408c72db7287e854ce1550ed1e2b80981 Mon Sep 17 00:00:00 2001 From: RikkaW Date: Thu, 9 May 2019 11:38:31 +0800 Subject: [PATCH] use Magisk installer & support Android Q beta 3 --- build.gradle | 8 +- jni/main/main.cpp | 95 +++++++++--------- template_override/config.sh | 150 --------------------------- template_override/install.sh | 184 ++++++++++++++++++++++++++++++++++ template_override/module.prop | 3 +- 5 files changed, 237 insertions(+), 203 deletions(-) delete mode 100644 template_override/config.sh create mode 100644 template_override/install.sh diff --git a/build.gradle b/build.gradle index 693f9aa..e116201 100644 --- a/build.gradle +++ b/build.gradle @@ -24,13 +24,13 @@ def moduleId = "location_report_enabler" def moduleName = "Location Report Enabler" def moduleAuthor = "Rikka" def moduleDescription = "Enable Google location report by hook system_property_get (and android::base::GetProperty for API 28+)." -def versionName = "v8" -def versionCode = 8 -def riruApi = 3 +def versionName = "v9" +def versionCode = 6 +def riruApi = 4 def moduleIdMagisk = "riru_location_report_enabler" def moduleNameMagisk = "Riru - $moduleName" -def moduleDescriptionMagisk = "$moduleDescription Requires Riru - Core v18 or above installed." +def moduleDescriptionMagisk = "$moduleDescription Requires Riru - Core v19 or above installed." def zipNameMagisk = "magisk-${project.name}-${versionName}.zip" def isWindows = org.gradle.internal.os.OperatingSystem.current().isWindows() diff --git a/jni/main/main.cpp b/jni/main/main.cpp index 4050d90..9917f99 100644 --- a/jni/main/main.cpp +++ b/jni/main/main.cpp @@ -2,13 +2,10 @@ #include #include #include -#include #include -#include #include #include #include -#include #include #include @@ -21,54 +18,44 @@ static char package_name[256]; static int uid; static int enable_hook; -static std::vector packages = { - "com.google.android.gms", - "com.google.android.gsf", - "com.google.android.apps.maps" -}; -int is_app_need_hook(JNIEnv *env, jstring appDataDir) { - if (!appDataDir) - return 0; - - - const char *app_data_dir = env->GetStringUTFChars(appDataDir, nullptr); - - int user = 0; - if (sscanf(app_data_dir, "/data/%*[^/]/%d/%s", &user, package_name) != 2) { - if (sscanf(app_data_dir, "/data/%*[^/]/%s", package_name) != 1) { - package_name[0] = '\0'; - LOGW("can't parse %s", app_data_dir); - return 0; +static bool is_app_need_hook(JNIEnv *env, jstring jAppDataDir, jstring jPackageName) { + if (jAppDataDir) { + const char *appDataDir = env->GetStringUTFChars(jAppDataDir, nullptr); + int user = 0; + if (sscanf(appDataDir, "/data/%*[^/]/%d/%s", &user, package_name) != 2) { + if (sscanf(appDataDir, "/data/%*[^/]/%s", package_name) != 1) { + package_name[0] = '\0'; + LOGW("can't parse %s", appDataDir); + return false; + } } + env->ReleaseStringUTFChars(jAppDataDir, appDataDir); + } else if (jPackageName) { + const char *packageName = env->GetStringUTFChars(jPackageName, nullptr); + sprintf(package_name, "%s", packageName); + env->ReleaseStringUTFChars(jPackageName, packageName); + } else { + return false; } - env->ReleaseStringUTFChars(appDataDir, app_data_dir); - if (access(CONFIG_PATH "/packages", R_OK) != 0) { - for (auto &s : packages) { - if (strcmp(s.c_str(), package_name) == 0) - return 1; - } - } else { - char path[PATH_MAX]; - snprintf(path, PATH_MAX, CONFIG_PATH "/packages/%s", package_name); - return access(path, F_OK) == 0; - } - return 0; + char path[PATH_MAX]; + snprintf(path, PATH_MAX, CONFIG_PATH "/packages/%s", package_name); + return access(path, F_OK) == 0; } -void load_config() { +static void load_config() { char buf[PROP_VALUE_MAX + 1]; int fd; - fd = open(CONFIG_PATH "/gsm.sim.operator.numeric", O_RDONLY); + fd = open(CONFIG_PATH "/gsm.sim.operator.numeric", O_RDONLY | O_CLOEXEC); if (fd > 0 && fdgets(buf, sizeof(buf), fd) > 0) set_sim_operator_numeric(buf); if (fd > 0) close(fd); - fd = open(CONFIG_PATH "/gsm.sim.operator.iso-country", O_RDONLY); + fd = open(CONFIG_PATH "/gsm.sim.operator.iso-country", O_RDONLY | O_CLOEXEC); if (fd > 0 && fdgets(buf, sizeof(buf), fd) > 0) set_sim_operator_country(buf); @@ -76,30 +63,44 @@ void load_config() { close(fd); } -void nativeForkAndSpecialize(int res, int enable_hook, const char *package_name, jint uid) { - if (res == 0 && enable_hook) { - install_hook(package_name, uid / 100000); - } -} - extern "C" { __attribute__((visibility("default"))) void nativeForkAndSpecializePre( JNIEnv *env, jclass clazz, jint *_uid, jint *gid, jintArray *gids, jint *runtime_flags, jobjectArray *rlimits, jint *_mount_external, jstring *se_info, jstring *se_name, jintArray *fdsToClose, jintArray *fdsToIgnore, jboolean *is_child_zygote, jstring *instructionSet, jstring *appDataDir, jstring *packageName, - jobjectArray *packagesForUID, jobjectArray *visibleVolIDs) { + jobjectArray *packagesForUID, jstring *sandboxId) { uid = *_uid; - enable_hook = is_app_need_hook(env, *appDataDir); + enable_hook = is_app_need_hook(env, *appDataDir, nullptr); if (enable_hook) load_config(); } -__attribute__((visibility("default"))) int nativeForkAndSpecializePost(JNIEnv *env, jclass clazz, - jint res) { - nativeForkAndSpecialize(res, enable_hook, package_name, uid); +__attribute__((visibility("default"))) int nativeForkAndSpecializePost( + JNIEnv *env, jclass clazz, jint res) { + if (res == 0 && enable_hook) install_hook(package_name, uid / 100000); + return !enable_hook; +} + +__attribute__((visibility("default"))) void specializeAppProcessPre( + JNIEnv *env, jclass clazz, jint *_uid, jint *gid, jintArray *gids, jint *runtimeFlags, + jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName, + jboolean *startChildZygote, jstring *instructionSet, jstring *appDataDir, + jstring *packageName, jobjectArray *packagesForUID, jstring *sandboxId) { + + uid = *_uid; + enable_hook = is_app_need_hook(env, nullptr, *packageName); + + if (enable_hook) + load_config(); +} + +__attribute__((visibility("default"))) int specializeAppProcessPost( + JNIEnv *env, jclass clazz) { + + if (enable_hook) install_hook(package_name, uid / 100000); return !enable_hook; } } diff --git a/template_override/config.sh b/template_override/config.sh deleted file mode 100644 index 58f9c4d..0000000 --- a/template_override/config.sh +++ /dev/null @@ -1,150 +0,0 @@ -########################################################################################## -# -# Magisk Module Template Config Script -# by topjohnwu -# -########################################################################################## -########################################################################################## -# -# Instructions: -# -# 1. Place your files into system folder (delete the placeholder file) -# 2. Fill in your module's info into module.prop -# 3. Configure the settings in this file (config.sh) -# 4. If you need boot scripts, add them into common/post-fs-data.sh or common/service.sh -# 5. Add your additional or modified system properties into common/system.prop -# -########################################################################################## - -########################################################################################## -# Configs -########################################################################################## - -# Set to true if you need to enable Magic Mount -# Most mods would like it to be enabled -AUTOMOUNT=true - -# Set to true if you need to load system.prop -PROPFILE=false - -# Set to true if you need post-fs-data script -POSTFSDATA=false - -# Set to true if you need late_start service script -LATESTARTSERVICE=false - -########################################################################################## -# Installation Message -########################################################################################## - -# Set what you want to show when installing your mod - -print_modname() { - ui_print "********************************" - ui_print "Riru - Location Report Enabler " - ui_print "********************************" -} - -########################################################################################## -# Replace list -########################################################################################## - -# List all directories you want to directly replace in the system -# Check the documentations for more info about how Magic Mount works, and why you need this - -# This is an example -REPLACE=" -/system/app/Youtube -/system/priv-app/SystemUI -/system/priv-app/Settings -/system/framework -" - -# Construct your own list here, it will override the example above -# !DO NOT! remove this if you don't need to replace anything, leave it empty as it is now -REPLACE=" -" - -########################################################################################## -# Permissions -########################################################################################## - -set_permissions() { - # Only some special files require specific permissions - # The default permissions should be good enough for most cases - - # Here are some examples for the set_perm functions: - - # set_perm_recursive (default: u:object_r:system_file:s0) - # set_perm_recursive $MODPATH/system/lib 0 0 0755 0644 - - # set_perm (default: u:object_r:system_file:s0) - # set_perm $MODPATH/system/bin/app_process32 0 2000 0755 u:object_r:zygote_exec:s0 - # set_perm $MODPATH/system/bin/dex2oat 0 2000 0755 u:object_r:dex2oat_exec:s0 - # set_perm $MODPATH/system/lib/libart.so 0 0 0644 - - # The following is default permissions, DO NOT remove - set_perm_recursive $MODPATH 0 0 0755 0644 -} - -########################################################################################## -# Custom Functions -########################################################################################## - -# This file (config.sh) will be sourced by the main flash script after util_functions.sh -# If you need custom logic, please add them here as functions, and call these functions in -# update-binary. Refrain from adding code directly into update-binary, as it will make it -# difficult for you to migrate your modules to newer template versions. -# Make update-binary as clean as possible, try to only do function calls in it. -fail() { - echo "$1" - exit 1 -} - -check_riru_version() { - [[ ! -f "/data/misc/riru/api_version" ]] && fail "! Please Install Riru - Core v18 or above" - VERSION=$(cat "/data/misc/riru/api_version") - ui_print "- Riru API version is $VERSION" - [[ "$VERSION" -ge 3 ]] || fail "! Please Install Riru - Core v18 or above" -} - -check_architecture() { - if [[ "$ARCH" != "arm" && "$ARCH" != "arm64" ]]; then - ui_print "- Unsupported platform: $ARCH" - exit 1 - else - ui_print "- Device platform: $ARCH" - fi - - check_riru_version -} - -copy_files() { - if [[ "$ARCH" == "x86" || "$ARCH" == "x64" ]]; then - ui_print "- Removing arm/arm64 libraries" - rm -rf "$MODPATH/system/lib" - rm -rf "$MODPATH/system/lib64" - ui_print "- Extracting x86/64 libraries" - unzip -o "$ZIP" 'system_x86/*' -d $MODPATH >&2 - mv "$MODPATH/system_x86/lib" "$MODPATH/system/lib" - mv "$MODPATH/system_x86/lib64" "$MODPATH/system/lib64" - fi - - if [[ "$IS64BIT" = false ]]; then - ui_print "- Removing 64-bit libraries" - rm -rf "$MODPATH/system/lib64" - fi - - ui_print "- Extracting extra files" - unzip -o "$ZIP" 'data/*' -d $MODPATH >&2 - - TARGET="/data/misc/riru/modules" - - # TODO: do not overwrite if file exists - [[ -d "$TARGET" ]] || mkdir -p "$TARGET" || fail "! Can't mkdir -p $TARGET" - cp -af "$MODPATH$TARGET/." "$TARGET" || fail "! Can't cp -af $MODPATH$TARGET/. $TARGET" - - rm -rf "$MODPATH/data" 2>/dev/null - - ui_print "- Files copied" -} \ No newline at end of file diff --git a/template_override/install.sh b/template_override/install.sh new file mode 100644 index 0000000..5b0317d --- /dev/null +++ b/template_override/install.sh @@ -0,0 +1,184 @@ +########################################################################################## +# +# Magisk Module Installer Script +# +########################################################################################## +########################################################################################## +# +# Instructions: +# +# 1. Place your files into system folder (delete the placeholder file) +# 2. Fill in your module's info into module.prop +# 3. Configure and implement callbacks in this file +# 4. If you need boot scripts, add them into common/post-fs-data.sh or common/service.sh +# 5. Add your additional or modified system properties into common/system.prop +# +########################################################################################## + +########################################################################################## +# Config Flags +########################################################################################## + +# Set to true if you do *NOT* want Magisk to mount +# any files for you. Most modules would NOT want +# to set this flag to true +SKIPMOUNT=false + +# Set to true if you need to load system.prop +PROPFILE=false + +# Set to true if you need post-fs-data script +POSTFSDATA=false + +# Set to true if you need late_start service script +LATESTARTSERVICE=false + +########################################################################################## +# Replace list +########################################################################################## + +# List all directories you want to directly replace in the system +# Check the documentations for more info why you would need this + +# Construct your list in the following format +# This is an example +REPLACE_EXAMPLE=" +/system/app/Youtube +/system/priv-app/SystemUI +/system/priv-app/Settings +/system/framework +" + +# Construct your own list here +REPLACE=" +" + +########################################################################################## +# +# Function Callbacks +# +# The following functions will be called by the installation framework. +# You do not have the ability to modify update-binary, the only way you can customize +# installation is through implementing these functions. +# +# When running your callbacks, the installation framework will make sure the Magisk +# internal busybox path is *PREPENDED* to PATH, so all common commands shall exist. +# Also, it will make sure /data, /system, and /vendor is properly mounted. +# +########################################################################################## +########################################################################################## +# +# The installation framework will export some variables and functions. +# You should use these variables and functions for installation. +# +# ! DO NOT use any Magisk internal paths as those are NOT public API. +# ! DO NOT use other functions in util_functions.sh as they are NOT public API. +# ! Non public APIs are not guranteed to maintain compatibility between releases. +# +# Available variables: +# +# MAGISK_VER (string): the version string of current installed Magisk +# MAGISK_VER_CODE (int): the version code of current installed Magisk +# BOOTMODE (bool): true if the module is currently installing in Magisk Manager +# MODPATH (path): the path where your module files should be installed +# TMPDIR (path): a place where you can temporarily store files +# ZIPFILE (path): your module's installation zip +# ARCH (string): the architecture of the device. Value is either arm, arm64, x86, or x64 +# IS64BIT (bool): true if $ARCH is either arm64 or x64 +# API (int): the API level (Android version) of the device +# +# Availible functions: +# +# ui_print +# print to console +# Avoid using 'echo' as it will not display in custom recovery's console +# +# abort +# print error message to console and terminate installation +# Avoid using 'exit' as it will skip the termination cleanup steps +# +# set_perm [context] +# if [context] is empty, it will default to "u:object_r:system_file:s0" +# this function is a shorthand for the following commands +# chown owner.group target +# chmod permission target +# chcon context target +# +# set_perm_recursive [context] +# if [context] is empty, it will default to "u:object_r:system_file:s0" +# for all files in , it will call: +# set_perm file owner group filepermission context +# for all directories in (including itself), it will call: +# set_perm dir owner group dirpermission context +# +########################################################################################## +########################################################################################## +# If you need boot scripts, DO NOT use general boot scripts (post-fs-data.d/service.d) +# ONLY use module scripts as it respects the module status (remove/disable) and is +# guaranteed to maintain the same behavior in future Magisk releases. +# Enable boot scripts by setting the flags in the config section above. +########################################################################################## + +RIRU_PATH="/data/misc/riru" + +print_modname() { + ui_print "*******************************" + ui_print "Riru - Location Report Enabler " + ui_print "*******************************" +} + +check_riru_version() { + [[ ! -f "$RIRU_PATH/api_version" ]] && abort "! Please Install Riru - Core v19 or above" + VERSION=$(cat "$RIRU_PATH/api_version") + ui_print "- Riru API version is $VERSION" + [[ "$VERSION" -ge 4 ]] || abort "! Please Install Riru - Core v19 or above" +} + +check_architecture() { + if [[ "$ARCH" != "arm" && "$ARCH" != "arm64" && "$ARCH" != "x86" && "$ARCH" != "x64" ]]; then + abort "! Unsupported platform: $ARCH" + else + ui_print "- Device platform: $ARCH" + fi +} + +on_install() { + check_architecture + check_riru_version + + if [[ "$ARCH" == "x86" || "$ARCH" == "x64" ]]; then + ui_print "- Extracting x86/64 libraries" + unzip -o "$ZIPFILE" 'system_x86/*' -d $MODPATH >&2 + mv "$MODPATH/system_x86/lib" "$MODPATH/system/lib" + mv "$MODPATH/system_x86/lib64" "$MODPATH/system/lib64" + else + ui_print "- Extracting arm/arm64 libraries" + unzip -o "$ZIPFILE" 'system/*' -d $MODPATH >&2 + fi + + if [[ "$IS64BIT" = false ]]; then + ui_print "- Removing 64-bit libraries" + rm -rf "$MODPATH/system/lib64" + fi + + TARGET="$RIRU_PATH/modules" + + ui_print "- Extracting extra files" + unzip -o "$ZIPFILE" 'data/*' -d "$TMPDIR" >&2 + + [[ -d "$TARGET" ]] || mkdir -p "$TARGET" || abort "! Can't mkdir -p $TARGET" + cp -af "$TMPDIR$TARGET/." "$TARGET" || abort "! Can't cp -af $TMPDIR$TARGET/. $TARGET" + + ui_print "- Files copied" +} + +set_permissions() { + # The following is the default rule, DO NOT remove + set_perm_recursive $MODPATH 0 0 0755 0644 + + # Here are some examples: + # set_perm_recursive $MODPATH/system/lib 0 0 0755 0644 + # set_perm $MODPATH/system/bin/app_process32 0 2000 0755 u:object_r:zygote_exec:s0 + # set_perm $MODPATH/system/bin/dex2oat 0 2000 0755 u:object_r:dex2oat_exec:s0 + # set_perm $MODPATH/system/lib/libart.so 0 0 0644 +} \ No newline at end of file diff --git a/template_override/module.prop b/template_override/module.prop index cccd0c6..ed76484 100644 --- a/template_override/module.prop +++ b/template_override/module.prop @@ -3,5 +3,4 @@ name=%%%NAME%%% version=%%%VERSION_NAME%%% versionCode=%%%VERSION_CODE%%% author=%%%AUTHOR%%% -description=%%%DESCRIPTION%%% -minMagisk=17000 +description=%%%DESCRIPTION%%% \ No newline at end of file