From 64ec60428d6d123dbc63926e14355b931a74c57c Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Fri, 1 May 2020 09:51:09 +0200 Subject: [PATCH] error() function accepts an optional return code --- lib/bkctld-ip | 2 +- lib/bkctld-is-on | 2 +- lib/bkctld-key | 2 +- lib/bkctld-port | 2 +- lib/bkctld-reload | 2 +- lib/bkctld-remove | 2 +- lib/bkctld-restart | 2 +- lib/bkctld-start | 2 +- lib/bkctld-stop | 2 +- lib/bkctld-sync | 2 +- lib/bkctld-update | 2 +- lib/includes | 8 ++++++-- 12 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/bkctld-ip b/lib/bkctld-ip index cefcedb..cb0e60c 100755 --- a/lib/bkctld-ip +++ b/lib/bkctld-ip @@ -15,7 +15,7 @@ if [ ! -n "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 jail_sshd_config="${jail_path}/${SSHD_CONFIG}" diff --git a/lib/bkctld-is-on b/lib/bkctld-is-on index 7722f13..6a80569 100755 --- a/lib/bkctld-is-on +++ b/lib/bkctld-is-on @@ -14,7 +14,7 @@ if [ -z "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 jail_pid_file="${jail_path}/${SSHD_PID}" diff --git a/lib/bkctld-key b/lib/bkctld-key index e0b8c94..8384bf7 100755 --- a/lib/bkctld-key +++ b/lib/bkctld-key @@ -15,7 +15,7 @@ if [ ! -n "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 if [ -z "${keyfile}" ]; then if [ -f "${jail_path}/${AUTHORIZED_KEYS}" ]; then diff --git a/lib/bkctld-port b/lib/bkctld-port index 8ed125c..e2bcf66 100755 --- a/lib/bkctld-port +++ b/lib/bkctld-port @@ -15,7 +15,7 @@ if [ ! -n "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 jail_sshd_config="${jail_path}/${SSHD_CONFIG}" diff --git a/lib/bkctld-reload b/lib/bkctld-reload index 711405a..310b23c 100755 --- a/lib/bkctld-reload +++ b/lib/bkctld-reload @@ -13,7 +13,7 @@ if [ -z "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 "${LIBDIR}/bkctld-is-on" "${jail_name}" || exit 0 diff --git a/lib/bkctld-remove b/lib/bkctld-remove index 0580e16..6303ac8 100755 --- a/lib/bkctld-remove +++ b/lib/bkctld-remove @@ -14,7 +14,7 @@ fi jail_path=$(jail_path "${jail_name}") incs_path=$(incs_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 "${LIBDIR}/bkctld-is-on" "${jail_name}" && "${LIBDIR}/bkctld-stop" "${jail_name}" diff --git a/lib/bkctld-restart b/lib/bkctld-restart index e9e3dae..f3f2e87 100755 --- a/lib/bkctld-restart +++ b/lib/bkctld-restart @@ -15,7 +15,7 @@ if [ -z "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 "${LIBDIR}/bkctld-is-on" "${jail_name}" && "${LIBDIR}/bkctld-stop" "${jail_name}" "${LIBDIR}/bkctld-start" "${jail_name}" diff --git a/lib/bkctld-start b/lib/bkctld-start index 81c1f92..e25ff0b 100755 --- a/lib/bkctld-start +++ b/lib/bkctld-start @@ -13,7 +13,7 @@ if [ -z "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 "${LIBDIR}/bkctld-is-on" "${jail_name}" && exit 0 diff --git a/lib/bkctld-stop b/lib/bkctld-stop index 7f02394..50438c5 100755 --- a/lib/bkctld-stop +++ b/lib/bkctld-stop @@ -13,7 +13,7 @@ if [ -z "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 "${LIBDIR}/bkctld-is-on" "${jail_name}" || exit 0 diff --git a/lib/bkctld-sync b/lib/bkctld-sync index 90250f9..74dea8c 100755 --- a/lib/bkctld-sync +++ b/lib/bkctld-sync @@ -14,7 +14,7 @@ fi jail_path=$(jail_path "${jail_name}") jail_config_dir=$(jail_config_dir "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 [ -n "${NODE}" ] || error "Sync need config of \$NODE in /etc/default/bkctld !" diff --git a/lib/bkctld-update b/lib/bkctld-update index ba15687..c5b4ec2 100755 --- a/lib/bkctld-update +++ b/lib/bkctld-update @@ -13,7 +13,7 @@ if [ ! -n "${jail_name}" ]; then fi jail_path=$(jail_path "${jail_name}") -test -d "${jail_path}" || error "${jail_name}: jail not found" +test -d "${jail_path}" || error "${jail_name}: jail not found" 2 "${LIBDIR}/bkctld-is-on" "${jail_name}" && "${LIBDIR}/bkctld-stop" "${jail_name}" diff --git a/lib/includes b/lib/includes index 805df0b..6ba6fec 100755 --- a/lib/includes +++ b/lib/includes @@ -59,15 +59,19 @@ warning() { logger -t bkctld -p daemon.warning "$(process_name) ${msg}" fi } - +# Return codes +# 1 : generic error +# 2 : jail not found +# > 100 : subcommands specific errors error() { msg="${1:-$(cat /dev/stdin)}" + rc="${2:-1}" tty -s && echo "$(log_date) ERROR $(process_name) ${msg}" >&2 if [ "${LOGLEVEL}" -ge 5 ]; then tty -s || echo "$(log_date) ERROR $(process_name) ${msg}" >&2 logger -t bkctld -p daemon.error "$(process_name) ${msg}" fi - exit 1 + exit ${rc} } dry_run() {