[Android Software/Hacking General [Developers Only]] gu5t3r:
IFS=$'\n' WORKS TESTED

#!/system/bin/sh
#
# Fix Emissions Script (Fix Permissions) created by -=zeppelinrox=-
#
ver=V6U9RC12BlackDog63457Fix
#
# When using scripting tricks, ideas, or code snippets from here, GIVE PROPER CREDIT.
# There are many things may look simple, but actually took a lot of time, trial, and error to get perfected.
# So DO NOT remove credits, put your name on top, and pretend it's your creation.
# That's called kanging and makes you a dumbass.
#
# This script can be used freely and can even be modified for PERSONAL USE ONLY.
# It can be freely incorporated into ROMs - provided that proper credit is given WITH a link back to the XDA SuperCharger thread.
# If you want to share it or make a thread about it, just provide a link to the main thread.
# - This ensures that users will always be getting the latest versions.
# Prohibited: Any modification (excluding personal use), repackaging, redistribution, or mirrors of my work are NOT PERMITTED.
# Thanks, zeppelinrox.
#
# Sets permissions for android data directories and apks.
# This should fix app force closes (FCs).
#
# The code is HIGHLY optimized... so it's REALLY FAST! - setting permissions for 300 apps in approximately 1 minute.
#
# Get full stats from the log file!
#
# Tweaks & Enhancements by zeppelinrox...
# - Added BRAINS: Only applies permissions if they are not what they're supposed to be!
# - Removed the usage of the "pm list packages" command - it didn't work on boot.
# - Added support for /vendor/app (POST-ICS).
# - Added support for /mnt/asec.
# - No longer excludes framework-res.apk or com.htc.resources.apk
# - Added support for more data directories ie. dbdata, datadata, etc.
# - Added debugging.
# - Enhanced logging!
# - Tweaked interface... just a wittle bit ;^]
#
# Props: Originally and MOSTLY (erm... something like 75% of it lol) by Jared Rummler (JRummy16).
# However, I actually meshed together 3 different Fix Permissions scripts and added my own spices ;^]
#
# For debugging, delete the # at the beginning of the following 2 lines, and check /data/Debug_FixEmissions.log file to see what may have fubarred.
# set -x
# exec > /data/Debug_FixEmissions.log 2>&1
#
PATH=$PATH:/system/xbin:/sbin:/vendor/bin:/system/sbin:/system/bin
if [ -d "/data/local/busybox*/xbin" ]; then PATH=$PATH:`ls -d /data/local/busybox*/xbin 2>/dev/null`; fi
line=================================================
cd "${0%/*}" 2>/dev/null
mount -o remount,rw /data 2>/dev/null
busybox mount -o remount,rw /data 2>/dev/null
# Next line only applies to the init.d boot script. "bootfixemissions=1" means run on boot. Anything else disables it.
bootfixemissions=0
bootupdelay="sleep 300"
if [ -t 0 ]; then interactive=yes; fi
title(){
echo $line
echo " -=Fix Emissions=- script by -=zeppelinrox=-"
echo $line
echo ""
sleep 2
echo " Version: V6U9RC12BlackDog63457Fix"
echo ""
sleep 2
}
remount(){
mount -o remount,$1 /system 2>/dev/null
busybox mount -o remount,$1 /system 2>/dev/null
busybox mount -o remount,$1 $(busybox mount | awk '/system /{print $1,$3}') 2>/dev/null
}
Configure(){
echo " Fix Emissions On Boot..."
echo " ========================"
echo ""
sleep 1
echo $line
echo -n " Status: Fix Emissions "
if [ "$bootfixemissions" -eq 1 ]; then echo "DOES Run On Boot!"
else echo "DOES NOT Run On Boot!"
fi
echo $line
echo ""
sleep 1
echo " Just check /data/BootLog_FixEmissions.log!"
echo ""
sleep 1
echo " If desired, you can change boot options..."
echo ""
sleep 1
echo " ...this script is VERY sophisticated..."
echo ""
sleep 1
echo " ...so boot time would be unaffected!"
echo ""
sleep 1
echo " You can also configure this in Driver Options!"
echo ""
sleep 1
echo $line
echo " Also READ THE COMMENTS inside this script!"
echo $line
echo ""
sleep 1
echo " Change Fix Emissions Options?"
echo ""
sleep 1
echo -n " Enter Y for Yes, any key for No: "
read changeoptions
echo ""
echo $line
case $changeoptions in
y|Y)remount rw
echo ""
sleep 1
echo " Run Fix Emissions on boot?"
echo ""
sleep 1
echo " Note: If you say Yes, Fix Alignment On Boot..."
echo " ...gets disabled (avoids overlapping mods)"
echo ""
sleep 1
echo -n " Enter Y for Yes, any key for No: "
read bootfixemissions
echo ""
echo $line
case $bootfixemissions in
y|Y)fixemissions=1; fixalign=0
echo " Fix Emissions Set To Run On Boot!";;
*)fixemissions=0
echo " No FCing Fix On Boot For You!";;
esac
sed -i '/^bootfixemissions=/s/=.*/='$fixemissions'/' $0
for cfxif in /data/V6_SuperCharger/!FixEmissions.sh /system/xbin/fixfc /system/etc/init.d/94fixfc; do
if [ "$0" != "$cfxif" ]; then sed -i '/^bootfixemissions=/s/=.*/='$fixemissions'/' $cfxif; fi
done 2>/dev/null
if [ "$fixalign" ]; then
for ngilaxif in /data/V6_SuperCharger/!FixAlignment.sh /system/xbin/fixalign /system/etc/init.d/95fixalign; do
if [ -f "$ngilaxif" ]; then sed -i '/^bootfixalign=/s/=.*/='$fixalign'/' $ngilaxif; fi
done
fi
if [ -f "/data/V6_SuperCharger/SuperChargerOptions" ]; then
if [ "$fixalign" ]; then awk 'BEGIN{OFS=FS=","}{$14='$fixemissions',$15='$fixalign';print}' /data/V6_SuperCharger/SuperChargerOptions > /data/V6_SuperCharger/SuperChargerOptions.tmp
else awk 'BEGIN{OFS=FS=","}{$14='$fixemissions';print}' /data/V6_SuperCharger/SuperChargerOptions > /data/V6_SuperCharger/SuperChargerOptions.tmp
fi
mv /data/V6_SuperCharger/SuperChargerOptions.tmp /data/V6_SuperCharger/SuperChargerOptions
fi
remount ro;;
*)echo " No Change For You!";;
esac
echo $line
echo ""
sleep 1
}
install(){
remount rw
for i in /system/etc/init.d/*; do
if [ -f "$i" ] && [ "`grep -E "packages\.xml|pm list packages" "$i"`" ] && [ "`grep "771" "$i"`" ] && [ "`echo "$i" | grep -Ev "Alignment|Emission|fixfc|fixalign|disabled"`" ]; then
sed -i '1 a\
#\
# Fix Emissions (aka "fixfc") script disabled this script for additional optimization.\
# To Re-Enable this script, delete the fix permissions stuff in here and rename this file.\
#' "$i"
mv "$i" "$i.disabled_due_to_slow_fix_permissions Read_Comment_Inside_For_Fix"
fi
done
if [ ! -f "$1" ] && [ ! "`grep $ver "$1"`" ]; then
echo ""
echo " Installing myself to ${1#*etc}"
sleep 2
dd if=$0 of=$1
chown 0.0 $1; chmod 777 $1
elif [ "`echo $1 | grep xbin`" ]; then
echo ""
echo " $1 is already up to date..."
sleep 2
fi 2>/dev/null
if [ "`echo $1 | grep xbin`" ]; then
echo ""
echo " To run, type in Terminal: \"su -c ${1##*/}\""
sleep 2
fi
remount ro
}
Get_PWN (){ PWN=`busybox stat $1 | sed 's/ //g' | awk -F [\(\/] '/Uid/{print $4,$6,substr($2,2,3)}'`; }
apply_permissions(){
chown $1:$2 $4
echo " chown $1:$2 $4 ( was `echo $PWN | awk '{print $1":"$2}'` )" | tee -a $LOG_FILE
chmod $3 $4
echo " chmod $3 $4 ( was `echo $PWN | awk '{print $3}'` )" | tee -a $LOG_FILE
echo ""
}
Fix_Emissions(){
while [ ! "`ps | grep -m 1 [a]ndroid`" ]; do sleep 10; done
if [ "`echo $0 | grep -v xbin`" ]; then install /system/xbin/fixfc; fi
if [ "`echo $0 | grep "init\.d"`" ] && [ ! "$interactive" ]; then echo ""
if [ "$bootfixemissions" -ne 1 ]; then
title
echo " Current Status: Fix Emissions DOES NOT Run On Boot!"
echo ""
echo " But Hey! Init.d Start Up Scripts Are Working!"
echo ""
exit 0
else echo " Waiting for `echo $bootupdelay | awk '{print $NF}'` seconds..."; $bootupdelay
fi
elif [ -d "/system/etc/init.d" ]; then install /system/etc/init.d/94fixfc
fi
LOG_FILE=/data/Ran_FixEmissions.log
if [ "easyloglulz" ]; then
echo ""
title
id=$(id); id=${id#*=}; id=${id%%[\( ]*}
if [ "$id" != "0" ] && [ "$id" != "root" ]; then
sleep 1
echo " You are NOT running this script as root..."
echo ""
sleep 3
echo $line
echo " ...No SuperUser For You!!"
echo $line
echo ""
sleep 3
echo " ...Please Run as Root and try again..."
echo ""
echo $line
echo ""
sleep 3
exit 69
fi
fi | tee $LOG_FILE
remount rw
START=`busybox date +%s`
BEGAN=`date`
TOTAL=`grep -c "package name" /d*/system/packages.xml`
INCREMENT=3
PROGRESS=0
PROGRESS_BAR=""
echo " Start Fix Emissions: $BEGAN" >> $LOG_FILE
echo "" >> $LOG_FILE
echo " Setting & Fixing Permissions For..." >> $LOG_FILE
echo "" >> $LOG_FILE
busybox sync; SIFS=$IFS; HIFS=$'\n';
grep "package name" /d*/system/packages.xml | while read pkgline; do
PROGRESS=$(($PROGRESS+1))
PERCENT=$(( $PROGRESS * 100 / $TOTAL ))
if [ "$PERCENT" -eq "$INCREMENT" ]; then
INCREMENT=$(( $INCREMENT + 3 ))
PROGRESS_BAR="$PROGRESS_BAR="
fi
clear
echo ""
echo -n " >"
echo -e "\r $PROGRESS_BAR>"
echo " \"Fix Emissions\" by -=zeppelinrox=-"
echo -n " >"
echo -e "\r $PROGRESS_BAR>"
echo ""
echo " Processing Apps - $PERCENT% ($PROGRESS of $TOTAL)"
echo ""
PKGNAME=`echo $pkgline | sed 's/.* name="\|".*//g'`
CODEPATH=`echo $pkgline | sed 's/.* codePath="\|".*//g'`
DATAPATH=/d*/d*/$PKGNAME
PKGID=`echo $pkgline | sed 's/.*serId="\|".*//g'`
echo " Setting & Fixing Permissions For..."
echo ""
echo " ...$PKGNAME"
echo " $PKGNAME ($CODEPATH)" >> $LOG_FILE
if [ -e "$CODEPATH" ]; then Get_PWN $CODEPATH
APPDIR=`busybox dirname $CODEPATH`
if [ "$APPDIR" = "/system/app" ] || [ "$APPDIR" = "/vendor/app" ] || [ "$APPDIR" = "/system/framework" ] && [ "$PWN" != "0 0 644" ]; then apply_permissions 0 0 644 $CODEPATH
elif [ "$APPDIR" = /d*/app ] && [ "$PWN" != "1000 1000 644" ]; then apply_permissions 1000 1000 644 $CODEPATH
elif [ "$APPDIR" = /d*/app-private ] || [ "`echo $APPDIR | grep asec`" ] && [ "$PWN" != "1000 $PKGID 640" ]; then apply_permissions 1000 $PKGID 640 $CODEPATH
fi
if [ -d $DATAPATH ]; then Get_PWN $DATAPATH
if [ "$PWN" != "$PKGID $PKGID 755" ]; then apply_permissions $PKGID $PKGID 755 $DATAPATH; fi
DIRS=`busybox find $DATAPATH -mindepth 1 -type d`
IFS=$HIFS; for file in $DIRS; do Get_PWN $file
FNAME=`busybox basename $file`
FPERM=771
NEWID=$PKGID
case $FNAME in
lib)PERM=755; NEWID=1000; FPERM=755;;
shared_prefs|databases)PERM=660;;
cache)PERM=600;;
files)PERM=775;;
*)PERM=771;;
esac
if [ "$PWN" != "$NEWID $NEWID $FPERM" ]; then apply_permissions $NEWID $NEWID $FPERM $file; fi
busybox find $file -type f -maxdepth 2 ! -perm $PERM -exec busybox chmod $PERM {} ';'
busybox find $file -type f -maxdepth 1 ! -user $NEWID -exec busybox chown $NEWID {} ';'
busybox find $file -type f -maxdepth 1 ! -group $NEWID -exec busybox chown :$NEWID {} ';'
done; IFS=$SIFS;
fi
fi | tee -a $LOG_FILE
done
busybox sync
remount ro
STOP=`busybox date +%s`
ENDED=`date`
RUNTIME=$(($STOP-$START))
HOURS=$(($RUNTIME/3600))
MINS=$(($RUNTIME/60%60))
SECS=$(($RUNTIME%60))
RUNTIME=`busybox printf "%02d:%02d:%02d\n" "$HOURS" "$MINS" "$SECS"`
echo "" | tee -a $LOG_FILE
echo $line | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
sleep 1
echo " FIXED Permissions For ALL $TOTAL Apps..." | tee -a $LOG_FILE
echo ""| tee -a $LOG_FILE
sleep 1
echo " ...Say Buh Bye To Force Close Errors!"
echo ""
echo $line
echo ""
sleep 1
echo " Start Time: $BEGAN" | tee -a $LOG_FILE
echo " Stop Time: $ENDED" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo " Completion Time: $RUNTIME" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
sleep 1
echo " See $LOG_FILE for more details!"
echo ""
sleep 1
echo " ==============================" | tee -a $LOG_FILE
echo " ) Fix Emissions Completed! (" | tee -a $LOG_FILE
echo " ==============================" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
sleep 1
if [ -d "/system/etc/init.d" ] && [ "$interactive" ]; then Configure; fi
}
if [ "`ps | grep -m 1 [a]ndroid`" ]; then Fix_Emissions
else exec > /data/BootLog_FixEmissions.log 2>&1
Fix_Emissions &
fi
exit 0

Changes are in lines 239, 274 & 289 =)

Applied same to FixAlign and it works perfect...

One error left and it is not shown in log, error: operation not permitted on /mnt/asec/*/pkg.apk applications that are on sdcard...

Setting & Fixing Permissions For...

...com.imangi.templerun2
chown: /mnt/asec/com.imangi.templerun2-2/pkg.apk: Operation not permitted
chown 1000:10255 /mnt/asec/com.imangi.templerun2-2/pkg.apk ( was 1000:0 )
chmod 640 /mnt/asec/com.imangi.templerun2-2/pkg.apk ( was 555 )