# Error management functions # Exit with error function die() { local errorcode=$1 # Don't trigger the ERR signal as we already managed the error trap - ERR if [[ "$KEEPGOING" != "true" || "$2" == "--force" ]]; then prnt E "Sortie prématuré avec erreur (code #$errorcode)." exit $errorcode else prnt W "On continue malgrés l'erreur #$errorcode." fi # Put the trigger back (only executed with --keepgoing) trap "error ${LINENO}" ERR } export -f die function terminate() { prnt E "$1 reçu, sortie immédiate." die 128 --force } trap "terminate 'Ctrl + C'" SIGINT trap "terminate 'SIGTERM'" SIGTERM function error() { # Check the call is actually the result of an error local error_code="$?" [[ $error_code == 0 ]] && return; # Local variables local i=0 local regex='' local mem='' local error_file='' local error_lineno='' local lineno=$1 && shift # Print error header prnt E "${BIWhite}*** ${BIRed}Une erreur fatale est intervenue, \ le script va s'arrêter immédiatement !$DEFAULTCOL" # Print backtrace backtrace 2 # Exit the script die $error_code } # Trigger error function on error trap "error ${LINENO}" ERR # Print a backtrace function backtrace { local _start_from_=0 local params=( "$@" ) if (( "${#params[@]}" >= "1" )); then _start_from_="$1" fi local i=0 local first=false local last=false while caller $i > /dev/null; do if [[ -n "$_start_from_" ]] && (( "$i" + 1 >= "$_start_from_" )); then if [[ "$first" == false ]]; then echo "==== Pile d'appel ====" first=true fi caller $i fi (( i=i++ )) done } check_root() { if [[ $EUID -ne 0 ]]; then prnt E "Ce script doit être démarré en tant que root. Arrêt." die 4 --force fi }