#!/system/bin/sh # SlopRoot SuManager installer. Runs as root through an existing root shell or helper. # Arguments: FILES TARGET BACKUP OWNER GROUP MODE # # Uses only shell syntax plus cat, chmod, chown, mkdir, mount, mv, rm, and sync. # Some Android 2.x ROMs ship no [ or test, no touch, no cp, no ls -d, and no mkdir -p. PATH=/system/bin:/system/xbin:/sbin:/vendor/bin:/system/sbin export PATH FILES="$1" TARGET="$2" BACKUP="$3" OWNER="$4" GROUP="$5" MODE="$6" LOG="$FILES/install.log" : > "$LOG" 2>/dev/null say() { echo "$1" echo "$1" >> "$LOG" 2>/dev/null } # Some 2.x toolbox builds ship no cp. Copy through cat instead. copy_file() { cat "$1" > "$2" 2>>"$LOG" } # Map numeric IDs to symbolic names for old toolbox builds whose # android_name_to_id only accepts hardcoded names and rejects digits. to_name() { case "$1" in 0) echo "root" ;; 1000) echo "system" ;; 2000) echo "shell" ;; 1015) echo "sdcard_rw" ;; *) echo "$1" ;; esac } # toolbox chown on older Android requires USER.GROUP with symbolic names; # busybox/toybox also accepts numeric USER:GROUP or USER.GROUP. set_owner() { F_USER="$2" F_GRP="$3" N_USER=$(to_name "$F_USER") N_GRP=$(to_name "$F_GRP") chown "$F_USER:$F_GRP" "$1" 2>>"$LOG" \ || chown "$F_USER.$F_GRP" "$1" 2>>"$LOG" \ || chown "$N_USER.$N_GRP" "$1" 2>>"$LOG" \ || chown "$N_USER:$N_GRP" "$1" 2>>"$LOG" } path_exists() { ls "$1" >/dev/null 2>&1 } # Pure-shell directory check; 2.x toolbox ls does not support -d and has no [ -d ]. is_dir() { (cd "$1") >/dev/null 2>&1 } # 2.x toolbox mkdir does not support -p and would create a directory named "-p". make_dir() { is_dir "$1" && return 0 mkdir "$1" 2>>"$LOG" } # Atomically replace a file via a temp file + mv so replacing a running binary # (e.g. upgrading /system/xbin/su from inside su) does not fail with ETXTBSY. install_file() { SRC="$1" DST="$2" F_OWNER="$3" F_GROUP="$4" F_MODE="$5" TMP="$DST.sloproot.tmp" if copy_file "$SRC" "$TMP"; then if set_owner "$TMP" "$F_OWNER" "$F_GROUP" \ && chmod "$F_MODE" "$TMP" 2>>"$LOG" \ && mv "$TMP" "$DST" 2>>"$LOG"; then return 0 fi rm "$TMP" 2>/dev/null fi copy_file "$SRC" "$DST" || return 1 set_owner "$DST" "$F_OWNER" "$F_GROUP" || return 1 chmod "$F_MODE" "$DST" 2>>"$LOG" || return 1 return 0 } restore() { case "$TARGET" in "") return 0 ;; esac case "$BACKUP" in "") return 0 ;; esac path_exists "$BACKUP" || return 0 install_file "$BACKUP" "$TARGET" "$OWNER" "$GROUP" "$MODE" || return 1 path_exists /system/bin/restorecon && /system/bin/restorecon "$TARGET" 2>/dev/null say "restored $TARGET" return 0 } fail() { say "FAIL: $1" restore sync exit 1 } # Writing a probe file is the only reliable test: 2.x toolbox rejects both # -o rw,remount and -o remount,rw depending on the vendor build. probe_rw() { : > /system/.sloproot_rw 2>/dev/null || return 1 rm /system/.sloproot_rw 2>/dev/null return 0 } ensure_rw() { probe_rw && return 0 mount -o remount,rw /system >>"$LOG" 2>&1 probe_rw && return 0 mount -o rw,remount /system >>"$LOG" 2>&1 probe_rw && return 0 # Old Android toolbox mount requires the explicit block/mtd device from /proc/mounts. SYS_DEV="" SYS_FS="" while read m_dev m_mnt m_fs m_rest; do case "$m_mnt" in /system) SYS_DEV="$m_dev" SYS_FS="$m_fs" ;; esac done < /proc/mounts case "$SYS_DEV" in "") ;; *) mount -o remount,rw "$SYS_DEV" /system >>"$LOG" 2>&1 probe_rw && return 0 mount -o rw,remount "$SYS_DEV" /system >>"$LOG" 2>&1 probe_rw && return 0 case "$SYS_FS" in "") ;; *) mount -o remount,rw -t "$SYS_FS" "$SYS_DEV" /system >>"$LOG" 2>&1 probe_rw && return 0 mount -o rw,remount -t "$SYS_FS" "$SYS_DEV" /system >>"$LOG" 2>&1 probe_rw && return 0 ;; esac ;; esac return 1 } say "SlopRoot installer" say "files: $FILES" ensure_rw || fail "system is still read-only" make_dir /system/xbin make_dir /system/app make_dir /system/etc install_file "$FILES/su" /system/xbin/su 0 0 6755 || fail "cannot install /system/xbin/su" say "installed /system/xbin/su" install_file "$FILES/su" /system/xbin/daemonsu 0 0 755 || fail "cannot install /system/xbin/daemonsu" install_file "$FILES/sumanager.apk" /system/app/sumanager.apk 0 0 644 || fail "cannot install /system/app/sumanager.apk" say "installed /system/app/sumanager.apk" # Android 4.3 through 6.0 runs this script at boot. The daemon covers # devices where /system is mounted nosuid and setuid su cannot run. if path_exists /system/etc/install-recovery.sh; then : else cat > /system/etc/install-recovery.sh << 'EOF' #!/system/bin/sh /system/xbin/su --daemon & EOF chmod 755 /system/etc/install-recovery.sh 2>>"$LOG" set_owner /system/etc/install-recovery.sh 0 0 fi if is_dir /system/etc/init.d; then cat > /system/etc/init.d/99sumanager << 'EOF' #!/system/bin/sh /system/xbin/su --daemon & EOF chmod 755 /system/etc/init.d/99sumanager 2>>"$LOG" set_owner /system/etc/init.d/99sumanager 0 0 fi path_exists /system/bin/restorecon && /system/bin/restorecon /system/xbin/su /system/xbin/daemonsu /system/app/sumanager.apk 2>/dev/null restore sync say "SlopRoot installed" say "Reboot once so Android scans /system/app." exit 0