scripts: fix sloppy/unpredictable cmd && this || that statements

- be a bit more POSIX to avoid any potential issues when full shell stdout/err are redirected
- actual logic chains remain unchanged
This commit is contained in:
osm0sis 2020-11-29 20:35:34 -04:00 committed by John Wu
parent a687d1347b
commit ce84f1762c
5 changed files with 52 additions and 16 deletions

View File

@ -13,7 +13,8 @@ trampoline() {
exec sh $MAGISKBIN/addon.d.sh "$@" exec sh $MAGISKBIN/addon.d.sh "$@"
exit $? exit $?
elif [ "$1" = post-restore ]; then elif [ "$1" = post-restore ]; then
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false BOOTMODE=false
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true $BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
if ! $BOOTMODE; then if ! $BOOTMODE; then
@ -24,7 +25,13 @@ trampoline() {
[ -z $OUTFD ] && OUTFD=$(ps | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2) [ -z $OUTFD ] && OUTFD=$(ps | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2)
[ -z $OUTFD ] && OUTFD=$(ps -Af | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2) [ -z $OUTFD ] && OUTFD=$(ps -Af | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2)
fi fi
ui_print() { $BOOTMODE && log -t Magisk -- "$1" || echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD; } ui_print() {
if $BOOTMODE; then
log -t Magisk -- "$1"
else
echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD
fi
}
ui_print "***********************" ui_print "***********************"
ui_print " Magisk addon.d failed" ui_print " Magisk addon.d failed"
@ -88,7 +95,12 @@ main() {
if $backuptool_ab; then if $backuptool_ab; then
# Swap the slot for addon.d-v2 # Swap the slot for addon.d-v2
if [ ! -z $SLOT ]; then [ $SLOT = _a ] && SLOT=_b || SLOT=_a; fi if [ ! -z $SLOT ]; then
case $SLOT in
_a) SLOT=_b;;
_b) SLOT=_a;;
esac
fi
fi fi
find_boot_image find_boot_image
@ -128,7 +140,8 @@ case "$1" in
post-restore) post-restore)
initialize initialize
if $backuptool_ab; then if $backuptool_ab; then
$BOOTMODE && su=su || su=sh su=sh
$BOOTMODE && su=su
exec $su -c "sh $0 addond-v2" exec $su -c "sh $0 addond-v2"
else else
# Run in background, hack for addon.d-v1 # Run in background, hack for addon.d-v1

View File

@ -32,7 +32,14 @@
# Pure bash dirname implementation # Pure bash dirname implementation
getdir() { getdir() {
case "$1" in case "$1" in
*/*) dir=${1%/*}; [ -z $dir ] && echo "/" || echo $dir ;; */*)
dir=${1%/*}
if [ -z $dir ]; then
echo "/"
else
echo $dir
fi
;;
*) echo "." ;; *) echo "." ;;
esac esac
} }

View File

@ -63,7 +63,8 @@ pgrep magiskd >/dev/null && pkill -9 magiskd
[ -f /system/bin/magisk ] && umount -l /system/bin [ -f /system/bin/magisk ] && umount -l /system/bin
# SELinux stuffs # SELinux stuffs
[ -e /sys/fs/selinux ] && SELINUX=true || SELINUX=false SELINUX=false
[ -e /sys/fs/selinux ] && SELINUX=true
if $SELINUX; then if $SELINUX; then
ln -sf ./magiskinit magiskpolicy ln -sf ./magiskinit magiskpolicy
./magiskpolicy --live --magisk ./magiskpolicy --live --magisk

View File

@ -14,7 +14,8 @@ setup_bb() {
export BBBIN export BBBIN
case "$1" in case "$1" in
"extract"|"-x") "extract"|"-x")
[ -z "$2" ] && BBBIN=./busybox || BBBIN="$2" BBBIN=./busybox
[ -z "$2" ] || BBBIN="$2"
extract_bb extract_bb
;; ;;
"sh") "sh")

View File

@ -12,7 +12,11 @@
################### ###################
ui_print() { ui_print() {
$BOOTMODE && echo "$1" || echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD if $BOOTMODE; then
echo "$1"
else
echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD
fi
} }
toupper() { toupper() {
@ -64,7 +68,8 @@ print_title() {
local len line1len line2len pounds local len line1len line2len pounds
line1len=$(echo -n $1 | wc -c) line1len=$(echo -n $1 | wc -c)
line2len=$(echo -n $2 | wc -c) line2len=$(echo -n $2 | wc -c)
[ $line1len -gt $line2len ] && len=$line1len || len=$line2len len=$line2len
[ $line1len -gt $line2len ] && len=$line1len
len=$((len + 2)) len=$((len + 2))
pounds=$(printf "%${len}s" | tr ' ' '*') pounds=$(printf "%${len}s" | tr ' ' '*')
ui_print "$pounds" ui_print "$pounds"
@ -258,8 +263,8 @@ mount_partitions() {
fi fi
mount -o bind /system_root/system /system mount -o bind /system_root/system /system
else else
grep ' / ' /proc/mounts | grep -qv 'rootfs' || grep -q ' /system_root ' /proc/mounts \ SYSTEM_ROOT=false
&& SYSTEM_ROOT=true || SYSTEM_ROOT=false grep ' / ' /proc/mounts | grep -qv 'rootfs' || grep -q ' /system_root ' /proc/mounts && SYSTEM_ROOT=true
fi fi
# /vendor is used only on some older devices for recovery AVBv1 signing so is not critical if fails # /vendor is used only on some older devices for recovery AVBv1 signing so is not critical if fails
[ -L /system/vendor ] && mount_name vendor$SLOT /vendor '-o ro' [ -L /system/vendor ] && mount_name vendor$SLOT /vendor '-o ro'
@ -426,7 +431,11 @@ install_magisk() {
$BOOTSIGNED && ui_print "- Boot image is signed with AVB 1.0" $BOOTSIGNED && ui_print "- Boot image is signed with AVB 1.0"
fi fi
$IS64BIT && mv -f magiskinit64 magiskinit 2>/dev/null || rm -f magiskinit64 if $IS64BIT; then
mv -f magiskinit64 magiskinit 2>/dev/null
else
rm -f magiskinit64
fi
# Source the boot patcher # Source the boot patcher
SOURCEDMODE=true SOURCEDMODE=true
@ -511,7 +520,8 @@ check_data() {
# Test if DE storage is writable # Test if DE storage is writable
$DATA && [ -d /data/adb ] && touch /data/adb/.rw && rm /data/adb/.rw && DATA_DE=true $DATA && [ -d /data/adb ] && touch /data/adb/.rw && rm /data/adb/.rw && DATA_DE=true
fi fi
$DATA && NVBASE=/data || NVBASE=/cache/data_adb NVBASE=/data
$DATA || NVBASE=/cache/data_adb
$DATA_DE && NVBASE=/data/adb $DATA_DE && NVBASE=/data/adb
resolve_vars resolve_vars
} }
@ -652,14 +662,18 @@ install_module() {
api_level_arch_detect api_level_arch_detect
# Setup busybox and binaries # Setup busybox and binaries
$BOOTMODE && boot_actions || recovery_actions if $BOOTMODE; then
boot_actions
else
recovery_actions
fi
# Extract prop file # Extract prop file
unzip -o "$ZIPFILE" module.prop -d $TMPDIR >&2 unzip -o "$ZIPFILE" module.prop -d $TMPDIR >&2
[ ! -f $TMPDIR/module.prop ] && abort "! Unable to extract zip file!" [ ! -f $TMPDIR/module.prop ] && abort "! Unable to extract zip file!"
local MODDIRNAME local MODDIRNAME=modules
$BOOTMODE && MODDIRNAME=modules_update || MODDIRNAME=modules $BOOTMODE && MODDIRNAME=modules_update
local MODULEROOT=$NVBASE/$MODDIRNAME local MODULEROOT=$NVBASE/$MODDIRNAME
MODID=`grep_prop id $TMPDIR/module.prop` MODID=`grep_prop id $TMPDIR/module.prop`
MODNAME=`grep_prop name $TMPDIR/module.prop` MODNAME=`grep_prop name $TMPDIR/module.prop`