DivestOS/Scripts/Common/Post.sh
steadfasterX 7724c3effd
make divest functions error aware
this commit adds (the currently non-existing) error handling when
using divest's functions and scripts.
all the magic here gets activated when `source ../../Scripts/init.sh`
gets executed which is already a mandatory step before starting any
of the divest functions.

when something fails during patching, resetting or building
each error will be catched + printed including an error code now.
last but not least the executed file and the line number causing that
failure will be shown, too.

as all divest functions get source'd and so not a single build
script gets executed all ERR's needs to be trapped to catch issues.
I am not aware of another way to handle that properly as sourcing
means we cannot track a script or smth while this approach here
just works.

Example for an error thrown in a function call:

> ERROR: $DOS_WORKSPACE_ROOT/Scripts/Common/Functions.sh -> verifyAllPlatformTags() ended with status >1< at line >49<

Final SUCCESS result message after using `patchWorkspace`:

> [FINAL RESULT] No error detected (please check the above output nevertheless!)

Final ERROR result message after using `patchWorkspace`:

> [FINAL RESULT] Serious error(s) found!!!
> Summary error code was: 126. Check & fix all error lines above

Some notes:

- when an error occurs the process continues until the end (like it is now)
  i.e. an error will not stop the current and following tasks
- when multiple errors occur the exit codes will be summed
- buildDevice: a (summed) end result gets printed (SUCCESS or ERROR) at the very end
- the trap used to catch any error will also be active for any command executed
  on the cli. that means: type "false" -> ENTER and you will get an error, too
  same for any script exectued after source init.sh
- when all goes well the trap will be resetted at the end but there are cases
  where this might not happen -> that is why `resetEnv` can be executed to
  reset the trap, i.e. all becomes as it was before sourcing init.sh
- `resetEnv` gets called automatically:
  - after a successful `patchWorkspace` run
  - whenever CTRL+C is used (during a running task or just on the cli)
  - a process get killed (SIGHUP, TERM)
- the whole implementation might not catch all errors though - it highly depends
  on how the function or the script/program called actually handles errors or better
  said: if they return a proper exit code on failures.
  For example some tools (like some git cmds) might print an error but don't return
  a non-zero exit code. This cannot be tracked other then with your eyes or these
  must be replaced by other methods returning a non-zero exit code on failures.

Signed-off-by: steadfasterX <steadfasterX@gmail.com>
2024-04-22 10:24:18 +02:00

63 lines
3.5 KiB
Bash

#!/bin/bash
#DivestOS: A privacy focused mobile distribution
#Copyright (c) 2017-2022 Divested Computing Group
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <https://www.gnu.org/licenses/>.
umask 0022;
set -euo pipefail;
source "$DOS_SCRIPTS_COMMON/Shell.sh";
echo "Post tweaks...";
#Workaround broken MSM_DLOAD_MODE=y+PANIC_ON_OOPS=y for devices that oops on shutdown
#MSM_DLOAD_MODE can't be disabled as it breaks compile
sed -i 's/set_dload_mode(in_panic)/set_dload_mode(0)/' kernel/*/*/arch/arm/mach-msm/restart.c &>/dev/null || true;
#Increase power efficiency of KSM
sed -i 's/bool use_deferred_timer;/bool use_deferred_timer = true;/' kernel/*/*/mm/ksm.c &>/dev/null || true;
sed -i 's/unsigned int ksm_thread_sleep_millisecs = 20;/unsigned int ksm_thread_sleep_millisecs = 500;/' kernel/*/*/mm/ksm.c &>/dev/null || true;
if [ "$DOS_USE_KSM" = true ]; then
#Enable slub/slab merging
sed -i 's/static int slub_nomerge;/static int slub_nomerge = 0;/' kernel/*/*/mm/slub.c &>/dev/null || true; #2.6.22-3.17
sed -i 's/static int slab_nomerge;/static int slab_nomerge = 0;/' kernel/*/*/mm/slab_common.c &>/dev/null || true; #3.18-4.12
sed -i 's/static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);/static bool slab_nomerge = false;/' kernel/*/*/mm/slab_common.c &>/dev/null || true; #4.13+
sed -i 's/static bool slab_nomerge __ro_after_init = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);/static bool slab_nomerge __ro_after_init = false;/' kernel/*/*/mm/slab_common.c &>/dev/null || true; #4.13+
else
#Disable slub/slab merging
sed -i 's/static int slub_nomerge;/static int slub_nomerge = 1;/' kernel/*/*/mm/slub.c &>/dev/null || true; #2.6.22-3.17
sed -i 's/static int slab_nomerge;/static int slab_nomerge = 1;/' kernel/*/*/mm/slab_common.c &>/dev/null || true; #3.18-4.12
sed -i 's/static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);/static bool slab_nomerge = true;/' kernel/*/*/mm/slab_common.c &>/dev/null || true; #4.13+
sed -i 's/static bool slab_nomerge __ro_after_init = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);/static bool slab_nomerge __ro_after_init = true;/' kernel/*/*/mm/slab_common.c &>/dev/null || true; #4.13+
fi;
#Enable page poisoning
#Commented as set by defconfig
#sed -i 's/= IS_ENABLED(CONFIG_PAGE_POISONING_ENABLE_DEFAULT);/= true;/' kernel/*/*/mm/page_poison.c &>/dev/null || true; #4.4+ #XXX: shouldn't be enabled past 5.3
#Build speedup
[[ -f build/soong/cc/tidy.go ]] && sed -i 's/flags.Tidy = true/flags.Tidy = false/' build/soong/cc/tidy.go; #Disable clang-tidy (kdrag0n)
#Reduce memory usage
awk -i inplace '!/persist.device_config.runtime_native.usap_pool_enabled=true/' device/*/*/*.prop &>/dev/null || true;
awk -i inplace '!/config_pinnerCameraApp/' device/*/*/overlay/frameworks/base/core/res/res/values/config.xml &>/dev/null || true;
#Allow Work Profiles in low_ram mode
sed -i '/android.software.managed_users/s/notLowRam="true"//' frameworks/native/data/etc/handheld_core_hardware.xml || true;
cd "$DOS_BUILD_BASE";
echo -e "\e[0;32m[SCRIPT COMPLETE] Post tweaks complete\e[0m";
kill -USR2 $TR_PID