summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2024-03-27 01:19:39 +0000
committerLeah Rowe <leah@libreboot.org>2024-03-27 01:50:31 +0000
commit6ebab10caa5be6fc1cfd244e745851687d4bd70d (patch)
tree0b983459c0cbc1b8998186975f9a571decb05592 /include
parent6b11f1b055466982747f65665506173a0b22557b (diff)
safer, simpler error handling in lbmk
in shell scripts, a function named the same as a program included in the $PATH will override that program. for example, you could make a function called ls() and this would override the standand "ls". in lbmk, a part of it was first trying to run the "fail" command, deferring to "err", because some scripts call fail() which does some minor cleanup before calling err. in most cases, fail() is not defined, and it's possible that the user could have a program called "fail" in their $PATH, the behaviour of which we could not determine, and it could have disastrous effects. lbmk error handling has been re-engineered in such a way that the err function is defined in a variable, which defaults to err_ which calls err_, so defined under include/err.sh. in functions that require cleanup prior to error handling, a fail() function is still defined, and err is overridden, thus: err="fail" this change has made xx_() obsolete, so now only x_ is used. the x_ function is a wrapper that can be used to run a command and exit with non-zero status (from lbmk) if the command fails. the xx_ command did the same thing, but called fail() which would have called err(); now everything is $err example: rm -f "$filename" || err "could not delete file" this would now be: rm -f "$filename" || $err "could not delete file" overriding of err= must be done *after* including err.sh. for example: err="fail" . "include/err.sh" ^ this is wrong. instead, one must do: . "include/err.sh" err="fail" this is because err is set as a global variable under err.sh the new error handling is much cleaner, and safer. it also reduces the chance of mistakes such as: calling err when you meant to call fail. this is because the standard way is now to call $err, so you set err="fail" at the top of the script and all is well. Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'include')
-rwxr-xr-xinclude/err.sh25
-rwxr-xr-xinclude/git.sh42
-rwxr-xr-xinclude/mrc.sh22
-rwxr-xr-xinclude/option.sh9
4 files changed, 43 insertions, 55 deletions
diff --git a/include/err.sh b/include/err.sh
index cab81d02..5b66c999 100755
--- a/include/err.sh
+++ b/include/err.sh
@@ -2,19 +2,10 @@
# SPDX-FileCopyrightText: 2022, 2023 Leah Rowe <leah@libreboot.org>
version=""; versiondate=""; projectname=""; _nogit=""
+err="err_"
x_() {
- [ $# -lt 1 ] || ${@} || err_exit err ${@}
-}
-xx_() {
- [ $# -lt 1 ] || ${@} || err_exit fail ${@}
-}
-
-err_exit()
-{
- _fail="${1}" && shift 1
- echo "Non-zero exit: $@"
- $_fail "Unhandled error"
+ [ $# -lt 1 ] || ${@} || $err "Unhandled non-zero exit: $@"; return 0
}
check_git()
@@ -30,8 +21,7 @@ check_git()
git_err()
{
printf "You need to set git name/email, like so:\n%s\n\n" "$1" 1>&2
- fail "Git name/email not configured" || \
- err "Git name/email not configured"
+ $err "Git name/email not configured"
}
check_project()
@@ -49,9 +39,8 @@ check_project()
--pretty='%ct' HEAD)" || versiondate="${versiondate_}"
for p in projectname version versiondate; do
- eval "[ -n \"\$$p\" ] || fail \"$p unset\" || err \"$p unset\""
- p_="x_ printf \"%s\\n\" \"\$$p\" > $p"
- eval "x$p_ || $p_"
+ eval "[ -n \"\$$p\" ] || $err \"$p unset\""
+ eval "x_ printf \"%s\\n\" \"\$$p\" > $p"
done
export LOCALVERSION="-${projectname}-${version%%-*}"
}
@@ -59,7 +48,7 @@ check_project()
setvars()
{
_setvars=""
- [ $# -lt 2 ] && err "setvars: too few arguments"
+ [ $# -lt 2 ] && $err "setvars: too few arguments"
val="${1}" && shift 1
for var in $@; do
_setvars="${var}=\"${val}\"; ${_setvars}"
@@ -67,7 +56,7 @@ setvars()
printf "%s\n" "${_setvars% }"
}
-err()
+err_()
{
printf "ERROR %s: %s\n" "${0}" "${1}" 1>&2
exit 1
diff --git a/include/git.sh b/include/git.sh
index 1fb8046d..5c456706 100755
--- a/include/git.sh
+++ b/include/git.sh
@@ -29,7 +29,7 @@ fetch_from_upstream()
fetch_config()
{
- rm -f "${cfgsdir}/"*/seen || err "fetch_config ${cfgsdir}: !rm seen"
+ rm -f "${cfgsdir}/"*/seen || $err "fetch_config ${cfgsdir}: !rm seen"
eval "$(setvars "" xtree tree_depend)"
while true; do
eval "$(setvars "" rev tree)"
@@ -44,12 +44,12 @@ fetch_config()
load_target_config()
{
- [ -f "$cfgsdir/$1/target.cfg" ] || err "$1: target.cfg missing"
+ [ -f "$cfgsdir/$1/target.cfg" ] || $err "$1: target.cfg missing"
[ -f "${cfgsdir}/${1}/seen" ] && \
- err "${_xm} check: infinite loop in tree definitions"
+ $err "${_xm} check: infinite loop in tree definitions"
- . "$cfgsdir/$1/target.cfg" || err "load_target_config !$cfgsdir/$1"
- touch "$cfgsdir/$1/seen" || err "load_config $cfgsdir/$1: !mk seen"
+ . "$cfgsdir/$1/target.cfg" || $err "load_target_config !$cfgsdir/$1"
+ touch "$cfgsdir/$1/seen" || $err "load_config $cfgsdir/$1: !mk seen"
}
prepare_new_tree()
@@ -57,7 +57,7 @@ prepare_new_tree()
printf "Creating %s tree %s (%s)\n" "$project" "$tree" "$_target"
cp -R "src/${project}/${project}" "${tmpgit}" || \
- err "prepare_new_tree ${project}/${tree}: can't make tmpclone"
+ $err "prepare_new_tree ${project}/${tree}: can't make tmpclone"
git_prep "$PWD/$cfgsdir/$tree/patches" "src/$project/$tree" "update"
}
@@ -65,15 +65,15 @@ fetch_project_repo()
{
eval "$(setvars "" xtree tree_depend)"
- scan_config "${project}" "config/git" "err"
- [ -z "${loc+x}" ] && err "fetch_project_repo $project: loc not set"
- [ -z "${url+x}" ] && err "fetch_project_repo $project: url not set"
+ scan_config "${project}" "config/git"
+ [ -z "${loc+x}" ] && $err "fetch_project_repo $project: loc not set"
+ [ -z "${url+x}" ] && $err "fetch_project_repo $project: url not set"
clone_project
[ -z "${depend}" ] || for d in ${depend} ; do
x_ ./update trees -f ${d}
done
- rm -Rf "${tmpgit}" || err "fetch_repo: !rm -Rf ${tmpgit}"
+ rm -Rf "${tmpgit}" || $err "fetch_repo: !rm -Rf ${tmpgit}"
}
clone_project()
@@ -86,7 +86,7 @@ clone_project()
fi
git clone $url "$tmpgit" || git clone $bkup_url "$tmpgit" \
- || err "clone_project: could not download ${project}"
+ || $err "clone_project: could not download ${project}"
git_prep "$PWD/config/$project/patches" "$loc"
}
@@ -95,26 +95,26 @@ git_prep()
_patchdir="$1"
_loc="$2"
- [ -z "${rev+x}" ] && err "git_prep $_loc: rev not set"
- git -C "$tmpgit" reset --hard $rev || err "git -C $_loc: !reset $rev"
- git_am_patches "$tmpgit" "$_patchdir" || err "!am $_loc $_patchdir"
+ [ -z "${rev+x}" ] && $err "git_prep $_loc: rev not set"
+ git -C "$tmpgit" reset --hard $rev || $err "git -C $_loc: !reset $rev"
+ git_am_patches "$tmpgit" "$_patchdir" || $err "!am $_loc $_patchdir"
if [ "$project" != "coreboot" ] || [ $# -gt 2 ]; then
[ ! -f "$tmpgit/.gitmodules" ] || git -C "$tmpgit" submodule \
- update --init --checkout || err "git_prep $_loc: !submod"
+ update --init --checkout || $err "git_prep $_loc: !submod"
if [ "$project" = "coreboot" ] && [ -n "$xtree" ] && \
[ "$xtree" != "$tree" ]; then
(
- cd "$tmpgit/util" || err "prep $_loc: !cd $tmpgit/util"
- rm -Rf crossgcc || err "prep $_loc: !rm xgcc"
+ cd "$tmpgit/util" || $err "prep $_loc: !cd $tmpgit/util"
+ rm -Rf crossgcc || $err "prep $_loc: !rm xgcc"
ln -s "../../$xtree/util/crossgcc" crossgcc || \
- err "prep $_loc: can't create xgcc symlink"
- ) || err "prep $_loc: can't create xgcc symlink"
+ $err "prep $_loc: can't create xgcc symlink"
+ ) || $err "prep $_loc: can't create xgcc symlink"
fi
fi
[ "$_loc" = "${_loc%/*}" ] || x_ mkdir -p "${_loc%/*}"
- mv "$tmpgit" "$_loc" || err "git_prep: !mv $tmpgit $_loc"
+ mv "$tmpgit" "$_loc" || $err "git_prep: !mv $tmpgit $_loc"
[ -n "$xtree" ] && [ ! -d "src/coreboot/$xtree" ] && \
x_ ./update project trees -f coreboot "$xtree"; return 0
}
@@ -123,7 +123,7 @@ git_am_patches()
{
for _patch in "$2/"*; do
[ -L "$_patch" ] || [ ! -f "$_patch" ] || git -C "$1" am \
- "$_patch" || err "git_am $1 $2: !git am $_patch"; continue
+ "$_patch" || $err "git_am $1 $2: !git am $_patch"; continue
done
for _patches in "$2/"*; do
[ ! -L "$_patches" ] && [ -d "$_patches" ] && \
diff --git a/include/mrc.sh b/include/mrc.sh
index 32c68a83..901a3c3b 100755
--- a/include/mrc.sh
+++ b/include/mrc.sh
@@ -14,7 +14,7 @@ extract_ref()
# but refcode is downloaded alongside mrc. in cases where lbmk
# erred, downloading only mrc, we must ensure downloading refcode
[ -n "$CONFIG_MRC_FILE" ] || \
- err "extract_ref $board: CONFIG_MRC_FILE not defined"
+ $err "extract_ref $board: CONFIG_MRC_FILE not defined"
# the extract_mrc function actually downloads the refcode
fetch "mrc" "$MRC_url" "$MRC_url_bkup" "$MRC_hash" "$CONFIG_MRC_FILE"
@@ -22,9 +22,9 @@ extract_ref()
extract_mrc()
{
- [ -z "$MRC_board" ] && err "extract_mrc $MRC_hash: MRC_board not set"
+ [ -z "$MRC_board" ] && $err "extract_mrc $MRC_hash: MRC_board not set"
[ -z "${CONFIG_MRC_FILE}" ] && \
- err "extract_mrc $MRC_hash: CONFIG_MRC_FILE not set"
+ $err "extract_mrc $MRC_hash: CONFIG_MRC_FILE not set"
SHELLBALL="chromeos-firmwareupdate-${MRC_board}"
@@ -32,10 +32,10 @@ extract_mrc()
x_ cd "${appdir}"
extract_partition "${MRC_url##*/}"
extract_archive "${SHELLBALL}" .
- ) || err "mrc download/extract failure"
+ ) || $err "mrc download/extract failure"
"${cbfstool}" "${appdir}/"bios.bin extract -n mrc.bin \
- -f "$_dest" -r RO_SECTION || err "extract_mrc: cbfstool $_dest"
+ -f "$_dest" -r RO_SECTION || $err "extract_mrc: cbfstool $_dest"
[ -n "$CONFIG_REFCODE_BLOB_FILE" ] && extract_refcode; return 0
}
@@ -51,10 +51,10 @@ extract_partition()
dd if="${1%.zip}" of="root-a.ext2" bs=1024 \
skip=$(( ${START} / 1024 )) count=$(( ${SIZE} / 1024 )) || \
- err "extract_partition, dd ${1%.zip}, root-a.ext2"
+ $err "extract_partition, dd ${1%.zip}, root-a.ext2"
printf "cd /usr/sbin\ndump chromeos-firmwareupdate ${SHELLBALL}\nquit" \
- | debugfs "root-a.ext2" || err "can't extract shellball"
+ | debugfs "root-a.ext2" || $err "can't extract shellball"
}
extract_refcode()
@@ -66,16 +66,16 @@ extract_refcode()
# incompatible with older versions before coreboot 4.14,
# so we need coreboot 4.13 cbfstool for certain refcode files
[ -n "$cbfstoolref" ] || \
- err "extract_refcode $board: MRC_refcode_cbtree not set"
+ $err "extract_refcode $board: MRC_refcode_cbtree not set"
mkdir -p "${_refdest%/*}" || \
- err "extract_refcode $board: !mkdir -p ${_refdest%/*}"
+ $err "extract_refcode $board: !mkdir -p ${_refdest%/*}"
"$cbfstoolref" "$appdir/bios.bin" extract \
-m x86 -n fallback/refcode -f "$_refdest" -r RO_SECTION \
- || err "extract_refcode $board: !cbfstoolref $_refdest"
+ || $err "extract_refcode $board: !cbfstoolref $_refdest"
# enable the Intel GbE device, if told by offset MRC_refcode_gbe
[ -z "$MRC_refcode_gbe" ] || dd if="config/ifd/hp820g2/1.bin" \
of="$_refdest" bs=1 seek=$MRC_refcode_gbe count=1 conv=notrunc || \
- err "extract_refcode $_refdest: byte $MRC_refcode_gbe"; return 0
+ $err "extract_refcode $_refdest: byte $MRC_refcode_gbe"; return 0
}
diff --git a/include/option.sh b/include/option.sh
index 2284fb0b..64442d15 100755
--- a/include/option.sh
+++ b/include/option.sh
@@ -43,10 +43,9 @@ scan_config()
{
awkstr=" /\{.*${1}.*}{/ {flag=1;next} /\}/{flag=0} flag { print }"
confdir="${2}"
- _fail="${3}"
revfile="$(mktemp -t sources.XXXXXXXXXX)"
cat "${confdir}/"* > "${revfile}" || \
- "${_fail}" "scan_config ${confdir}: Cannot concatenate files"
+ $err "scan_config ${confdir}: Cannot concatenate files"
while read -r line ; do
set ${line} 1>/dev/null 2>/dev/null || :
if [ "${1%:}" = "depend" ]; then
@@ -57,7 +56,7 @@ scan_config()
done << EOF
$(eval "awk '${awkstr}' \"${revfile}\"")
EOF
- rm -f "$revfile" || "$_fail" "scan_config: Cannot remove tmpfile"
+ rm -f "$revfile" || $err "scan_config: Cannot remove tmpfile"
}
check_defconfig()
@@ -81,6 +80,6 @@ handle_coreboot_utils()
remkdir()
{
- rm -Rf "${1}" || err "remkdir: !rm -Rf \"${1}\""
- mkdir -p "${1}" || err "remkdir: !mkdir -p \"${1}\""
+ rm -Rf "${1}" || $err "remkdir: !rm -Rf \"${1}\""
+ mkdir -p "${1}" || $err "remkdir: !mkdir -p \"${1}\""
}