From 3315228a908ccd8d0aab126711e2fc9bc852e3c4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 6 Jan 2017 11:20:16 +0800 Subject: [PATCH] Properly handle loading prop files 1. Add new trigger "load_magisk_props_action" in init.magisk.rc 2. Patch init*.rc with new trigger 3. Update resetprop to handle prop value with spaces 4. Handle the case when modules contains file/folder names with spaces --- build.cmd | 2 + build.sh | 4 +- jni/resetprop | 2 +- .../custom_ramdisk_patch.sh | 25 ++-- scripts/flash_script.sh | 31 ++-- scripts/magic_mask.sh | 136 +++++++++--------- zip_static/common/init.magisk.rc | 11 ++ 7 files changed, 126 insertions(+), 85 deletions(-) rename {zip_static/common => scripts}/custom_ramdisk_patch.sh (71%) diff --git a/build.cmd b/build.cmd index 041a0b125..c84ee257f 100644 --- a/build.cmd +++ b/build.cmd @@ -74,6 +74,7 @@ EXIT /B %ERRORLEVEL% forfiles /P zip_static\x86 /C "cmd /C IF NOT @file == \"busybox\" DEL @file" forfiles /P zip_static\x64 /C "cmd /C IF NOT @file == \"busybox\" DEL @file" 2>NUL DEL zip_static\META-INF\com\google\android\update-binary + 2>NUL DEL zip_static\common\custom_ramdisk_patch.sh 2>NUL DEL zip_static\common\magic_mask.sh 2>NUL RMDIR /S /Q uninstaller\arm 2>NUL RMDIR /S /Q uninstaller\arm64 @@ -98,6 +99,7 @@ EXIT /B %ERRORLEVEL% ECHO ************************ ECHO * Zipping Magisk v%~1 ECHO ************************ + COPY /Y scripts\custom_ramdisk_patch.sh zip_static\common\custom_ramdisk_patch.sh CD zip_static 2>NUL DEL "..\Magisk-v%~1.zip" ..\ziptools\win_bin\zip "..\Magisk-v%~1.zip" -r . diff --git a/build.sh b/build.sh index 0cbd8d9f9..2dcb2ba4c 100755 --- a/build.sh +++ b/build.sh @@ -24,7 +24,8 @@ cleanup() { ls zip_static/arm64/* | grep -v "busybox" | xargs rm -rfv ls zip_static/x86/* | grep -v "busybox" | xargs rm -rfv ls zip_static/x64/* | grep -v "busybox" | xargs rm -rfv - rm -rfv zip_static/META-INF/com/google/android/update-binary + rm -rfv zip_static/META-INF/com/google/android/update-binary + rm -rfv zip_static/common/custom_ramdisk_patch.sh rm -rfv zip_static/common/magic_mask.sh rm -rfv uninstaller/arm rm -rfv uninstaller/arm64 @@ -71,6 +72,7 @@ zip_package() { echo "************************" echo "* Zipping Magisk v$1" echo "************************" + cp -afv scripts/custom_ramdisk_patch.sh zip_static/common/custom_ramdisk_patch.sh cd zip_static find . -type f -exec chmod 644 {} \; find . -type d -exec chmod 755 {} \; diff --git a/jni/resetprop b/jni/resetprop index 5bafa01da..3a6db7dba 160000 --- a/jni/resetprop +++ b/jni/resetprop @@ -1 +1 @@ -Subproject commit 5bafa01da8ea6c82742f96673a89dd6ca668372d +Subproject commit 3a6db7dbafdfb46fd09705ba1ab5477cacb87155 diff --git a/zip_static/common/custom_ramdisk_patch.sh b/scripts/custom_ramdisk_patch.sh similarity index 71% rename from zip_static/common/custom_ramdisk_patch.sh rename to scripts/custom_ramdisk_patch.sh index d38cce655..f0cb5f5de 100644 --- a/zip_static/common/custom_ramdisk_patch.sh +++ b/scripts/custom_ramdisk_patch.sh @@ -30,24 +30,33 @@ cpio_rm() { fi } +file_contain() { + grep "$1" "$2" >/dev/null 2>&1 + return $? +} + rm -rf /tmp/magisk/ramdisk 2>/dev/null mkdir -p /tmp/magisk/ramdisk cd /tmp/magisk/ramdisk cat $RAMDISK | cpio -i -# Patch ramdisk -echo "- Patching ramdisk" - # Cleanup SuperSU backups cpio_rm -r .subackup # Add magisk entrypoint -for INIT in init*.rc; do - if [ `grep -c "import /init.environ.rc" $INIT` -ne "0" ] && [ `grep -c "import /init.magisk.rc" $INIT` -eq "0" ]; then - sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" $INIT - cpio_add $INIT 750 - break +for RC in init*.rc; do + if file_contain "import /init.environ.rc" $RC && ! file_contain "import /init.magisk.rc" $RC; then + sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" $RC + cpio_add $RC 750 + fi + if file_contain "trigger load_persist_props_action" $RC && ! file_contain "trigger load_magisk_props_action" $RC; then + sed -i "/trigger load_persist_props_action/a\ \ \ \ trigger load_magisk_props_action" $RC + cpio_add $RC 750 + fi + if file_contain "selinux.reload_policy"; then + sed -i "/selinux.reload_policy/d" $RC + cpio_add $RC 750 fi done diff --git a/scripts/flash_script.sh b/scripts/flash_script.sh index b34c06cd3..4437e08be 100644 --- a/scripts/flash_script.sh +++ b/scripts/flash_script.sh @@ -149,6 +149,11 @@ grep_prop() { cat $FILES 2>/dev/null | sed -n $REGEX | head -n 1 } +file_contain() { + grep "$1" "$2" >/dev/null 2>&1 + return $? +} + unpack_boot() { rm -rf $UNPACKDIR $RAMDISK 2>/dev/null mkdir -p $UNPACKDIR @@ -491,23 +496,31 @@ else mkdir -p /magisk/phh/su.d 2>/dev/null cp -af $INSTALLER/common/phh/. /magisk/phh cp -af $BINDIR/su $BINDIR/sepolicy-inject /magisk/phh/bin - chmod -R 755 /magisk/phh/bin + chmod -R 755 /magisk/phh + chown -R 0.0 /magisk/phh fi # Patch ramdisk ui_print "- Patching ramdisk" - # Add magisk entrypoint - for INIT in init*.rc; do - if [ `grep -c "import /init.environ.rc" $INIT` -ne "0" ] && [ `grep -c "import /init.magisk.rc" $INIT` -eq "0" ]; then - cp $INIT .backup - sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" $INIT - break + # Add magisk entrypoints + for RC in init*.rc; do + if file_contain "import /init.environ.rc" $RC && ! file_contain "import /init.magisk.rc" $RC; then + [ ! -f .backup/$RC ] && cp -af $RC .backup + sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" $RC + fi + if file_contain "trigger load_persist_props_action" $RC && ! file_contain "trigger load_magisk_props_action" $RC; then + [ ! -f .backup/$RC ] && cp -af $RC .backup + sed -i "/trigger load_persist_props_action/a\ \ \ \ trigger load_magisk_props_action" $RC + fi + if file_contain "selinux.reload_policy"; then + [ ! -f .backup/$RC ] && cp -af $RC .backup + sed -i "/selinux.reload_policy/d" $RC fi done - sed -i "/selinux.reload_policy/d" init.rc - find . -type f -name "*fstab*" 2>/dev/null | while read FSTAB ; do + for FSTAB in *fstab*; do + [ -L $FSTAB ] && continue if (! $KEEPVERITY); then sed -i "s/,support_scfs//g" $FSTAB sed -i 's;,\{0,1\}verify\(=[^,]*\)\{0,1\};;g' $FSTAB diff --git a/scripts/magic_mask.sh b/scripts/magic_mask.sh index 797182b5e..c47e44743 100644 --- a/scripts/magic_mask.sh +++ b/scripts/magic_mask.sh @@ -28,7 +28,7 @@ log_print() { } mktouch() { - mkdir -p ${1%/*} 2>/dev/null + mkdir -p "${1%/*}" 2>/dev/null if [ -z "$2" ]; then touch "$1" 2>/dev/null else @@ -54,7 +54,7 @@ run_scripts() { if [ ! -f $MOD/disable ]; then if [ -f $MOD/$1.sh ]; then chmod 755 $MOD/$1.sh - chcon "u:object_r:system_file:s0" "$MOD/$1.sh" + chcon u:object_r:system_file:s0 $MOD/$1.sh log_print "$1: $MOD/$1.sh" sh $MOD/$1.sh fi @@ -73,7 +73,7 @@ loopsetup() { } target_size_check() { - e2fsck -p -f $1 + e2fsck -p -f "$1" curBlocks=`e2fsck -n $1 2>/dev/null | cut -d, -f3 | cut -d\ -f2`; curUsedM=$((`echo "$curBlocks" | cut -d/ -f1` * 4 / 1024)); curSizeM=$((`echo "$curBlocks" | cut -d/ -f2` * 4 / 1024)); @@ -84,39 +84,39 @@ travel() { # Ignore /system/vendor, we will handle it differently [ "$1" = "system/vendor" ] && return - cd $TRAVEL_ROOT/$1 + cd "$TRAVEL_ROOT/$1" if [ -f .replace ]; then - rm -rf $MOUNTINFO/$1 - mktouch $MOUNTINFO/$1 $TRAVEL_ROOT + rm -rf "$MOUNTINFO/$1" + mktouch "$MOUNTINFO/$1" "$TRAVEL_ROOT" else for ITEM in * ; do - if [ ! -e /$1/$ITEM ]; then + if [ ! -e "/$1/$ITEM" ]; then # New item found # If we are in a higher level, delete the lower levels - rm -rf $MOUNTINFO/dummy/$1 + rm -rf "$MOUNTINFO/dummy/$1" # Mount the dummy parent - mktouch $MOUNTINFO/dummy/$1 + mktouch "$MOUNTINFO/dummy/$1" - if [ -d $ITEM ]; then + if [ -d "$ITEM" ]; then # Create new dummy directory and mount it - mkdir -p $DUMMDIR/$1/$ITEM - mktouch $MOUNTINFO/$1/$ITEM $TRAVEL_ROOT - elif [ -L $ITEM ]; then + mkdir -p "$DUMMDIR/$1/$ITEM" + mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT" + elif [ -L "$ITEM" ]; then # Symlinks are small, copy them - mkdir -p $DUMMDIR/$1 2>/dev/null - cp -afc $ITEM $DUMMDIR/$1/$ITEM + mkdir -p "$DUMMDIR/$1" 2>/dev/null + cp -afc "$ITEM" $"DUMMDIR/$1/$ITEM" else # Create new dummy file and mount it - mktouch $DUMMDIR/$1/$ITEM - mktouch $MOUNTINFO/$1/$ITEM $TRAVEL_ROOT + mktouch "$DUMMDIR/$1/$ITEM" + mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT" fi else - if [ -d $ITEM ]; then + if [ -d "$ITEM" ]; then # It's an directory, travel deeper - (travel $1/$ITEM) - elif [ ! -L $ITEM ]; then + (travel "$1/$ITEM") + elif [ ! -L "$ITEM" ]; then # Mount this file - mktouch $MOUNTINFO/$1/$ITEM $TRAVEL_ROOT + mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT" fi fi done @@ -125,28 +125,28 @@ travel() { clone_dummy() { LINK=false - in_list $1 "$WHITELIST" && LINK=true + in_list "$1" "$WHITELIST" && LINK=true for ITEM in $MIRRDIR$1/* ; do - REAL=${ITEM#$MIRRDIR} - if [ -d $MOUNTINFO$REAL ]; then + REAL="${ITEM#$MIRRDIR}" + if [ -d "$MOUNTINFO$REAL" ]; then # Need to clone deeper - mkdir -p $DUMMDIR$REAL - (clone_dummy $REAL) + mkdir -p "$DUMMDIR$REAL" + (clone_dummy "$REAL") else - if [ -L $ITEM ]; then + if [ -L "$ITEM" ]; then # Copy original symlink - cp -afc $ITEM $DUMMDIR$REAL + cp -afc "$ITEM" "$DUMMDIR$REAL" else - if $LINK && [ ! -e $MOUNTINFO$REAL ]; then - ln -s $MIRRDIR$REAL $DUMMDIR$REAL + if $LINK && [ ! -e "$MOUNTINFO$REAL" ]; then + ln -s "$MIRRDIR$REAL" "$DUMMDIR$REAL" else - if [ -d $ITEM ]; then - mkdir -p $DUMMDIR$REAL + if [ -d "$ITEM" ]; then + mkdir -p "$DUMMDIR$REAL" else - mktouch $DUMMDIR$REAL + mktouch "$DUMMDIR$REAL" fi - [ ! -e $MOUNTINFO$REAL ] && mktouch $MOUNTINFO/mirror$REAL + [ ! -e "$MOUNTINFO$REAL" ] && mktouch "$MOUNTINFO/mirror$REAL" fi fi fi @@ -154,8 +154,8 @@ clone_dummy() { } bind_mount() { - if [ -e $1 -a -e $2 ]; then - mount -o bind $1 $2 + if [ -e "$1" -a -e "$2" ]; then + mount -o bind "$1" "$2" if [ $? -eq 0 ]; then log_print "Mount: $1" else @@ -248,10 +248,10 @@ case $1 in if [ -d /cache/magisk_mount ]; then log_print "* Mounting cache files" find /cache/magisk_mount -type f 2>/dev/null | while read ITEM ; do - chmod 644 $ITEM - chcon "u:object_r:system_file:s0" $ITEM - TARGET=${ITEM#/cache/magisk_mount} - bind_mount $ITEM $TARGET + chmod 644 "$ITEM" + chcon u:object_r:system_file:s0 "$ITEM" + TARGET="${ITEM#/cache/magisk_mount}" + bind_mount "$ITEM" "$TARGET" done fi @@ -276,7 +276,6 @@ case $1 in rm -rf $BINPATH $TOOLPATH mkdir -p $TOOLPATH mv /cache/data_bin $BINPATH - chmod -R 755 $BINPATH $TOOLPATH $BINPATH/busybox --install -s $TOOLPATH ln -s $BINPATH/busybox $TOOLPATH/busybox # Prevent issues @@ -286,9 +285,10 @@ case $1 in mv /cache/stock_boot.img /data/stock_boot.img 2>/dev/null mv /cache/magisk.apk /data/magisk.apk 2>/dev/null - find $BINPATH -exec chcon -h "u:object_r:system_file:s0" {} \; - find $TOOLPATH -exec chcon -h "u:object_r:system_file:s0" {} \; + # Set up environment chmod -R 755 $BINPATH $TOOLPATH + chown -R 0.0 $BINPATH $TOOLPATH + find $BINPATH $TOOLPATH -exec chcon -h u:object_r:system_file:s0 {} \; # Live patch sepolicy $BINPATH/sepolicy-inject --live -s su @@ -314,12 +314,12 @@ case $1 in # Remove empty directories, legacy paths, symlinks, old temporary images find $MOUNTPOINT -type d -depth ! -path "*core*" -exec rmdir {} \; 2>/dev/null - rm -rf $COREDIR/bin $COREDIR/dummy $COREDIR/mirror /data/magisk/*.img 2>/dev/null + rm -rf $MOUNTPOINT/zzsupersu $COREDIR/bin $COREDIR/dummy $COREDIR/mirror /data/magisk/*.img 2>/dev/null # Remove modules that is labeled to be removed for MOD in $MOUNTPOINT/* ; do rm -f $MOD/system/placeholder 2>/dev/null - if [ -f $MOD/remove ] || [ $MOD = zzsupersu ]; then + if [ -f $MOD/remove ]; then log_print "Remove module: $MOD" rm -rf $MOD fi @@ -327,7 +327,7 @@ case $1 in # Unmount, shrink, remount if umount $MOUNTPOINT; then - losetup -d $LOOPDEVICE + losetup -d $LOOPDEVICE 2>/dev/null target_size_check $IMG NEWDATASIZE=$(((curUsedM / 32 + 2) * 32)) if [ "$curSizeM" -gt "$NEWDATASIZE" ]; then @@ -365,8 +365,6 @@ case $1 in # Travel through all mods for MOD in $MOUNTPOINT/* ; do - # Read in defined system props - [ -f $MOD/system.prop ] && /data/magisk/resetprop --file $MOD/system.prop if [ -f $MOD/auto_mount -a -d $MOD/system -a ! -f $MOD/disable ]; then TRAVEL_ROOT=$MOD (travel system) @@ -379,7 +377,7 @@ case $1 in done # Proper permissions for generated items - find $TMPDIR -exec chcon -h "u:object_r:system_file:s0" {} \; + find $TMPDIR -exec chcon -h u:object_r:system_file:s0 {} \; # linker(64), t*box required for bin if [ -f $MOUNTINFO/dummy/system/bin ]; then @@ -411,10 +409,10 @@ case $1 in mv -f $MOUNTINFO/dummy/system/vendor $MOUNTINFO/dummy/vendor 2>/dev/null mv -f $DUMMDIR/system/vendor $DUMMDIR/vendor 2>/dev/null find $MOUNTINFO/dummy -type f 2>/dev/null | while read ITEM ; do - TARGET=${ITEM#$MOUNTINFO/dummy} - ORIG=$DUMMDIR$TARGET - (clone_dummy $TARGET) - bind_mount $ORIG $TARGET + TARGET="${ITEM#$MOUNTINFO/dummy}" + ORIG="$DUMMDIR$TARGET" + (clone_dummy "$TARGET") + bind_mount "$ORIG" "$TARGET" done # Check if the dummy /system/bin is empty, it shouldn't @@ -422,15 +420,10 @@ case $1 in # Stage 3 log_print "* Stage 3: Mount module items" - find $MOUNTINFO/system -type f 2>/dev/null | while read ITEM ; do - TARGET=${ITEM#$MOUNTINFO} - ORIG=`cat $ITEM`$TARGET - bind_mount $ORIG $TARGET - done - find $MOUNTINFO/vendor -type f 2>/dev/null | while read ITEM ; do - TARGET=${ITEM#$MOUNTINFO} - ORIG=`cat $ITEM`$TARGET - bind_mount $ORIG $TARGET + find $MOUNTINFO/system $MOUNTINFO/vendor -type f 2>/dev/null | while read ITEM ; do + TARGET="${ITEM#$MOUNTINFO}" + ORIG="`cat "$ITEM"`$TARGET" + bind_mount "$ORIG" "$TARGET" done # Stage 4 @@ -440,9 +433,9 @@ case $1 in # Stage 5 log_print "* Stage 5: Mount mirrored items back to dummy" find $MOUNTINFO/mirror -type f 2>/dev/null | while read ITEM ; do - TARGET=${ITEM#$MOUNTINFO/mirror} - ORIG=$MIRRDIR$TARGET - bind_mount $ORIG $TARGET + TARGET="${ITEM#$MOUNTINFO/mirror}" + ORIG="$MIRRDIR$TARGET" + bind_mount "$ORIG" "$TARGET" done # Bind hosts for Adblock apps @@ -457,7 +450,7 @@ case $1 in cp -afc /data/busybox/. $COREDIR/busybox cp -afc /system/xbin/. $COREDIR/busybox chmod -R 755 $COREDIR/busybox - chcon -hR "u:object_r:system_file:s0" $COREDIR/busybox + chcon -hR u:object_r:system_file:s0 $COREDIR/busybox bind_mount $COREDIR/busybox /system/xbin fi @@ -482,6 +475,17 @@ case $1 in unblock ;; + load_magisk_props ) + for MOD in $MOUNTPOINT/* ; do + # Read in defined system props + if [ -f $MOD/system.prop ]; then + log_print "* Reading props from $MOD/system.prop" + /data/magisk/resetprop --file $MOD/system.prop + fi + done + unblock + ;; + service ) # Version info MAGISK_VERSION_STUB diff --git a/zip_static/common/init.magisk.rc b/zip_static/common/init.magisk.rc index a99f8b4bb..16791fd21 100644 --- a/zip_static/common/init.magisk.rc +++ b/zip_static/common/init.magisk.rc @@ -10,6 +10,11 @@ on post-fs-data wait /dev/.magisk.unblock 60 rm /dev/.magisk.unblock +on load_magisk_props_action + start magisk_props + wait /dev/.magisk.unblock 5 + rm /dev/.magisk.unblock + on property:magisk.restart_pfsd=1 trigger post-fs-data @@ -27,6 +32,12 @@ service magisk_pfsd /sbin/magic_mask.sh post-fs-data seclabel u:r:su:s0 oneshot +# Load module props +service magisk_props /sbin/magic_mask.sh load_magisk_props + user root + seclabel u:r:su:s0 + oneshot + # launch late_start script service magisk_service /sbin/magic_mask.sh service class late_start