From d4251ea5ca51c2f61a786ee513d2f6936363f0c5 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sun, 27 Dec 2020 16:09:07 +0100 Subject: [PATCH] Exit from script instead of subshell on missing user When executing `false || (echo bad; exit 1)`, the call to `exit` won't exit the script, it will only exit the subshell and the exit code will be stored in the return code `$?`. Since this is an error, we have to exit the script properly. --- etc/initsystem/icinga2.init.d.cmake | 17 ++++++++++++++--- etc/initsystem/prepare-dirs.cmake | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/etc/initsystem/icinga2.init.d.cmake b/etc/initsystem/icinga2.init.d.cmake index 49a6ee290..c50dc9865 100644 --- a/etc/initsystem/icinga2.init.d.cmake +++ b/etc/initsystem/icinga2.init.d.cmake @@ -53,9 +53,20 @@ if [ ! -e $ICINGA2_CONFIG_FILE ]; then exit 6 fi -getent passwd $ICINGA2_USER >/dev/null 2>&1 || (echo "Icinga user '$ICINGA2_USER' does not exist. Exiting." && exit 6) -getent group $ICINGA2_GROUP >/dev/null 2>&1 || (echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting." && exit 6) -getent group $ICINGA2_COMMAND_GROUP >/dev/null 2>&1 || (echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting." && exit 6) +if ! getent passwd "$ICINGA2_USER" >/dev/null 2>&1; then + echo "Icinga user '$ICINGA2_USER' does not exist. Exiting." + exit 6 +fi + +if ! getent group "$ICINGA2_GROUP" >/dev/null 2>&1; then + echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting." + exit 6 +fi + +if ! getent group "$ICINGA2_COMMAND_GROUP" >/dev/null 2>&1; then + echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting." + exit 6 +fi # Start Icinga 2 start() { diff --git a/etc/initsystem/prepare-dirs.cmake b/etc/initsystem/prepare-dirs.cmake index 4cef83193..2b5126d98 100644 --- a/etc/initsystem/prepare-dirs.cmake +++ b/etc/initsystem/prepare-dirs.cmake @@ -21,9 +21,20 @@ fi : ${ICINGA2_LOG_DIR:="@ICINGA2_FULL_LOGDIR@"} : ${ICINGA2_CACHE_DIR:="@ICINGA2_FULL_CACHEDIR@"} -getent passwd $ICINGA2_USER >/dev/null 2>&1 || (echo "Icinga user '$ICINGA2_USER' does not exist. Exiting." && exit 6) -getent group $ICINGA2_GROUP >/dev/null 2>&1 || (echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting." && exit 6) -getent group $ICINGA2_COMMAND_GROUP >/dev/null 2>&1 || (echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting." && exit 6) +if ! getent passwd "$ICINGA2_USER" >/dev/null 2>&1; then + echo "Icinga user '$ICINGA2_USER' does not exist. Exiting." + exit 6 +fi + +if ! getent group "$ICINGA2_GROUP" >/dev/null 2>&1; then + echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting." + exit 6 +fi + +if ! getent group "$ICINGA2_COMMAND_GROUP" >/dev/null 2>&1; then + echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting." + exit 6 +fi if [ ! -e "$ICINGA2_INIT_RUN_DIR" ]; then mkdir -m 755 "$ICINGA2_INIT_RUN_DIR"