From 29c2d975ec40a8a7d26814b473fa0c44d2b95b6a Mon Sep 17 00:00:00 2001 From: Casper Date: Sat, 8 Jan 2022 00:25:09 +0100 Subject: [PATCH] Fix harmless startup errors (#2357) --- target/bin/delmailuser | 4 ++-- target/bin/sedfile | 25 +++++++++++++++++++++---- target/scripts/start-mailserver.sh | 3 +++ target/scripts/startup/fixes-stack.sh | 10 ++++++++-- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/target/bin/delmailuser b/target/bin/delmailuser index c52668ce..2b98a2d4 100755 --- a/target/bin/delmailuser +++ b/target/bin/delmailuser @@ -101,7 +101,7 @@ do if [[ -f ${DATABASE} ]] then - if ! sedfile -i "/^${EMAIL}|/d" "${DATABASE}" + if ! sedfile --strict -i "/^${EMAIL}|/d" "${DATABASE}" then echo "${UNESCAPED_EMAIL} couldn't be deleted in ${DATABASE}." >&2 ERROR=true @@ -126,7 +126,7 @@ do # remove quota directives if [[ -f ${QUOTA_DATABASE} ]] then - if ! sedfile -i -e "/^${EMAIL}:.*$/d" "${QUOTA_DATABASE}" + if ! sedfile --strict -i -e "/^${EMAIL}:.*$/d" "${QUOTA_DATABASE}" then echo "Quota for ${UNESCAPED_EMAIL} couldn't be deleted in ${QUOTA_DATABASE}." >&2 fi diff --git a/target/bin/sedfile b/target/bin/sedfile index 66e37274..7f8e9c6b 100755 --- a/target/bin/sedfile +++ b/target/bin/sedfile @@ -1,18 +1,34 @@ #!/bin/bash -# wrapper for 'sed -i': fail if file was not modified by sed +# Wrapper for 'sed -i': fail if file was not modified by sed and container was not restarted. +# Error output is surpressed, when container is restarted to avoid harmless error messages. +# Use "--strict" as first parameter, to fail regardless of the container state (fresh or restarted). + +# When to use sedfile? +# Is a file change optional? --> use regular 'sed -i' +# Is a file change expected? --> use 'sedfile --strict -i' +# Is a file change only on the first container run expected? --> use 'sedfile -i' set -ueo pipefail HASHTOOL="sha1sum" +SKIP_ERROR=0 -if [[ $# -lt 1 ]] +if [[ $# -lt 3 ]] then - echo "Error: No file given." + echo "Error: At least, three parameters must be given." + echo "Syntax: sedfile -i " echo exit 1 fi >&2 +[[ -f /CONTAINER_START ]] && SKIP_ERROR=1 # Hide error, if container was restarted. +if [[ "${1}" == "--strict" ]] # Show error every time. +then + SKIP_ERROR=0 + shift +fi + # get last argument FILE=${*: -1} @@ -21,9 +37,10 @@ sed "$@" NEW=$(${HASHTOOL} "${FILE}") # fail if file was not modified -if [[ ${OLD} == "${NEW}" ]] +if [[ ${OLD} == "${NEW}" ]] && [[ ${SKIP_ERROR} -eq 0 ]] then echo "Error: sed $*" exit 1 fi >&2 + exit 0 diff --git a/target/scripts/start-mailserver.sh b/target/scripts/start-mailserver.sh index 0c447bfd..c85e7677 100755 --- a/target/scripts/start-mailserver.sh +++ b/target/scripts/start-mailserver.sh @@ -266,6 +266,9 @@ fix start_misc start_daemons +# marker to check, if container was restarted +date > /CONTAINER_START + _notify 'tasklog' "${HOSTNAME} is up and running" touch /var/log/mail/mail.log diff --git a/target/scripts/startup/fixes-stack.sh b/target/scripts/startup/fixes-stack.sh index 54833519..3d9a58f4 100644 --- a/target/scripts/startup/fixes-stack.sh +++ b/target/scripts/startup/fixes-stack.sh @@ -40,11 +40,17 @@ function _fix_var_amavis_permissions function _fix_cleanup_clamav { _notify 'task' 'Cleaning up disabled ClamAV' - rm /etc/logrotate.d/clamav-* /etc/cron.d/clamav-freshclam || _notify 'err' 'Failed to remove ClamAV configuration' + rm /etc/logrotate.d/clamav-* /etc/cron.d/clamav-freshclam 2>/dev/null || { + # show error only on first container start + [[ ! -f /CONTAINER_START ]] && _notify 'err' 'Failed to remove ClamAV configuration' + } } function _fix_cleanup_spamassassin { _notify 'task' 'Cleaning up disabled SpamAssassin' - rm /etc/cron.daily/spamassassin || _notify 'err' 'Failed to remove SpamAssassin configuration' + rm /etc/cron.daily/spamassassin 2>/dev/null || { + # show error only on first container start + [[ ! -f /CONTAINER_START ]] && _notify 'err' 'Failed to remove SpamAssassin configuration' + } }