Improved signal handling, removing result ISO if xorriso exits with error, redirected a few messages from stdout to stderr

This commit is contained in:
Thomas Schmitt 2022-06-18 17:14:11 +02:00
parent 34981b1278
commit 5897c706a4
2 changed files with 103 additions and 37 deletions

View File

@ -32,6 +32,8 @@ usage() {
echo "Exported non-empty variable MERGE_FOR_DIST enforces the use of a" >&2
echo "particular directory in /dists of iso1. Normally only one" >&2
echo "such directory is found and thus no need to set MERGE_FOR_DIST." >&2
echo "Exported non-empty variable MERGE_KEEP_ISO prevents the removal" >&2
echo "of result_iso after xorriso indicated failure of production." >&2
echo "Exported non-empty variable XORRISO overrides command xorriso." >&2
echo "This may be needed if installed xorriso is older than 1.4.2." >&2
echo 'If XORRISO is set to "dummy" then no new ISO will emerge.' >&2
@ -82,39 +84,34 @@ check_single_word() {
MOUNT_LIST=
UMOUNT_LIST=
RMDIR_LIST=
TEMPFILE_LIST="merged_dists merged_md5sum.txt merged_README.txt temp_file"
TEMPFILE_LIST=
EMERGING_ISO=
finish_cleanup() {
if test -n "$1"
# Cleanup temporary files, mount points and made directories
cleanup_ignore_counter=0
cleanup_ignore_handler() {
cleanup_ignore_counter=$(expr "$cleanup_ignore_counter" + 1)
if test "$cleanup_ignore_counter" -ge 4
then
echo >&2
if test "$1" -gt 0
then
echo "--- Merge run aborted !" >&2
else
echo "Merge run ended with success indication." >&2
fi
exit "$1"
# Reset traps to default
trap - INT TERM QUIT
echo "--- Ignored several INT, TERM, or QUIT signals during cleanup." >&2
echo "--- Will give in to next signal." >&2
return 0
fi
echo "--- Ignored INT, TERM, or QUIT signal during cleanup." >&2
return 0
}
# Cleanup temporary files, mount points and made directories
cleanup_ran=0
cleanup() {
cleanup_and_end() {
# Remove trap
trap - INT TERM QUIT EXIT
if test "$cleanup_ran" = 1
then
finish_cleanup "$1"
return 0
fi
cleanup_ran=1
# Make sure to be in cleanup state of traps
trap cleanup_ignore_handler INT TERM QUIT
echo >&2
echo "Cleaning up temporary files and mount points ..." 2>&1
echo "Cleaning up temporary files and mount points ..." >&2
ret=0
@ -153,6 +150,28 @@ cleanup_and_end() {
ret=1
fi
done
if test -n "$EMERGING_ISO" && test -e "$EMERGING_ISO"
then
if test -n "$MERGE_KEEP_ISO"
then
echo "Removal of incomplete result ISO suppressed by MERGE_KEEP_ISO." >&2
echo "Remaining: $EMERGING_ISO" >&2
else
echo "Removing incomplete result ISO." >&2
echo \
" (This can be suppressed by exporting non-empty variable MERGE_KEEP_ISO.)" \
>&2
if rm "$EMERGING_ISO"
then
dummy=dummy
else
echo "--- Note: Cannot remove incomplete result: $EMERGING_ISO" >&2
ret=1
fi
fi
fi
if test "$ret" = 0
then
echo "Cleanup completed." >&2
@ -160,14 +179,49 @@ cleanup_and_end() {
echo "--- Cleanup could not be fully completed." >&2
fi
# Might exit
finish_cleanup "$1"
# Do not come back via trap again
trap - INT TERM QUIT
return $ret
}
# Handler for INT TERM QUIT events if "$1" is empty
# Procedural program exit if "$1" is not empty
cleanup_and_end() {
# +++ No cleanup_and_end before TEMPFILE_LIST is verified to be missing +++
# Early assume cleanup state of traps
trap cleanup_ignore_handler INT TERM QUIT
trap - EXIT
if test -z "$1"
then
exit_value=6
echo >&2
echo "--- Encountered INT, TERM, or QUIT signal." >&2
else
exit_value="$1"
fi
cleanup
echo >&2
if test "$exit_value" -gt 0
then
echo "--- Merge run aborted !" >&2
else
echo "Merge run ended with success indication." >&2
fi
exit "$exit_value"
}
# Handler for rogue EXIT
cleanup_for_exit() {
# Early assume cleanup state of traps
trap cleanup_ignore_handler INT TERM QUIT
trap - EXIT
cleanup
echo >&2
echo "--- Merge run ended by unexpected EXIT event !" >&2
}
## Check arguments and dependencies
@ -191,7 +245,7 @@ do
then
dummy=dummy
else
echo "--- Missing a helper program: $i" 2>&1
echo "--- Missing a helper program: $i" >&2
missing=1
fi
done
@ -210,16 +264,16 @@ then
dummy=dummy
else
echo '--- Help text of xorriso program '"$XORRISO"' lacks word "replay".' \
2>&1
echo "--- It will fail when trying to make the new ISO bootable." 2>&1
echo " Consider to get and compile GNU xorriso from" 2>&1
echo " https://www.gnu.org/software/xorriso" 2>&1
echo " and to export variable XORRISO with the binary's path." 2>&1
>&2
echo "--- It will fail when trying to make the new ISO bootable." >&2
echo " Consider to get and compile GNU xorriso from" >&2
echo " https://www.gnu.org/software/xorriso" >&2
echo " and to export variable XORRISO with the binary's path." >&2
echo >&2
missing=1
fi
else
echo "--- Test run of xorriso program failed: $XORRISO -no_rc -version" 2>&1
echo "--- Test run of xorriso program failed: $XORRISO -no_rc -version" >&2
missing=1
fi
if test "$missing" = 1
@ -229,11 +283,13 @@ then
fi
existing_tempfiles=
for i in $TEMPFILE_LIST
for i in merged_dists merged_md5sum.txt merged_README.txt temp_file
do
if test -e "$i"
then
existing_tempfiles="$existing_tempfiles $i"
else
TEMPFILE_LIST="$TEMPFILE_LIST $i"
fi
done
if test -n "$existing_tempfiles"
@ -245,8 +301,12 @@ then
exit 1
fi
# +++ From here on: Always call cleanup_and_end to perform exit +++
trap cleanup_and_end INT TERM QUIT EXIT
trap cleanup_and_end INT TERM QUIT
trap cleanup_for_exit EXIT
RESULT_ISO="$1"
check_single_word "$2" "result_iso" || cleanup_and_end 1
@ -683,6 +743,9 @@ else
fi
echo >&2
# Mark the result path for possible removal by cleanup
EMERGING_ISO="$RESULT_ISO"
if "$XORRISO" \
-no_rc \
-indev "$iso_1" \
@ -700,6 +763,9 @@ if "$XORRISO" \
-padding included \
-compliance no_emul_toc
then
# Revoke mark for possible removal by cleanup
EMERGING_ISO=
test "$XORRISO" = echo || \
echo "Run of xorriso program ended without error indication." >&2
else

Binary file not shown.