TT#82051 Shellcheck fixes + fix failure handling of init scripts

shellcheck v0.7.1 complains about a bunch of issues:

SC2154: status is referenced but not assigned.
SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).
SC2235: Use { ..; } instead of (..) to avoid subshell overhead.
SC2236: Use -n instead of ! -z.

The "$status" variable disappeared in d4763aba14.

The init scripts of ngcp-rtpengine-daemon and
ngcp-rtpengine-recording-daemon had a logic bug, where a failing stop
action didn't properly return, but continued execution of the following
firewalling code (ngcp-rtpengine-iptables-setup). Thanks to Guillem
Gover for spotting this one. While at it, no longer execute under
'set -e'.

Change-Id: Ia50e76f615564a288627e6e42ec8f7eb082de74c
changes/01/40201/9
Michael Prokop 6 years ago
parent 6e77bebb35
commit 0bf07827df

@ -9,8 +9,6 @@
# Description: Proxy for RTP and other media streams # Description: Proxy for RTP and other media streams
### END INIT INFO ### END INIT INFO
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=ngcp-rtpengine-daemon NAME=ngcp-rtpengine-daemon
DESC="RTP/media proxy" DESC="RTP/media proxy"
@ -37,14 +35,14 @@ fi
OPTIONS="" OPTIONS=""
START_OPTIONS="" START_OPTIONS=""
if [ ! -z "$INTERFACES" ]; then if [ -n "$INTERFACES" ]; then
for interface in $INTERFACES; do for interface in $INTERFACES; do
OPTIONS="$OPTIONS --interface=$interface" OPTIONS="$OPTIONS --interface=$interface"
done done
fi fi
if [ ! -z "$SUBSCRIBE_KEYSPACES" ]; then if [ -n "$SUBSCRIBE_KEYSPACES" ]; then
for ks in $SUBSCRIBE_KEYSPACES; do for ks in $SUBSCRIBE_KEYSPACES; do
OPTIONS="$OPTIONS --subscribe-keyspace=$ks" OPTIONS="$OPTIONS --subscribe-keyspace=$ks"
done done
@ -74,9 +72,9 @@ fi
[ -z "$REDIS_NUM_THREADS" ] || OPTIONS="$OPTIONS --redis-num-threads=$REDIS_NUM_THREADS" [ -z "$REDIS_NUM_THREADS" ] || OPTIONS="$OPTIONS --redis-num-threads=$REDIS_NUM_THREADS"
[ -z "$REDIS_EXPIRES" ] || OPTIONS="$OPTIONS --redis-expires=$REDIS_EXPIRES" [ -z "$REDIS_EXPIRES" ] || OPTIONS="$OPTIONS --redis-expires=$REDIS_EXPIRES"
[ -z "$REDIS_MULTIKEY" ] || OPTIONS="$OPTIONS --redis-multikey=$REDIS_MULTIKEY" [ -z "$REDIS_MULTIKEY" ] || OPTIONS="$OPTIONS --redis-multikey=$REDIS_MULTIKEY"
[ -z "$NO_REDIS_REQUIRED" ] || ( [ "$NO_REDIS_REQUIRED" != "1" ] && [ "$NO_REDIS_REQUIRED" != "yes" ] ) || OPTIONS="$OPTIONS --no-redis-required" [ -z "$NO_REDIS_REQUIRED" ] || { [ "$NO_REDIS_REQUIRED" != "1" ] && [ "$NO_REDIS_REQUIRED" != "yes" ] ; } || OPTIONS="$OPTIONS --no-redis-required"
[ -z "$B2B_URL" ] || OPTIONS="$OPTIONS --b2b-url=$B2B_URL" [ -z "$B2B_URL" ] || OPTIONS="$OPTIONS --b2b-url=$B2B_URL"
[ -z "$NO_FALLBACK" ] || ( [ "$NO_FALLBACK" != "1" ] && [ "$NO_FALLBACK" != "yes" ] ) || OPTIONS="$OPTIONS --no-fallback" [ -z "$NO_FALLBACK" ] || { [ "$NO_FALLBACK" != "1" ] && [ "$NO_FALLBACK" != "yes" ] ; } || OPTIONS="$OPTIONS --no-fallback"
OPTIONS="$OPTIONS --table=$TABLE" OPTIONS="$OPTIONS --table=$TABLE"
[ -z "$LOG_LEVEL" ] || OPTIONS="$OPTIONS --log-level=$LOG_LEVEL" [ -z "$LOG_LEVEL" ] || OPTIONS="$OPTIONS --log-level=$LOG_LEVEL"
[ -z "$LOG_FACILITY" ] || OPTIONS="$OPTIONS --log-facility=$LOG_FACILITY" [ -z "$LOG_FACILITY" ] || OPTIONS="$OPTIONS --log-facility=$LOG_FACILITY"
@ -91,7 +89,7 @@ OPTIONS="$OPTIONS --table=$TABLE"
[ -z "$HOMER" ] || OPTIONS="$OPTIONS --homer=$HOMER" [ -z "$HOMER" ] || OPTIONS="$OPTIONS --homer=$HOMER"
[ -z "$HOMER_PROTOCOL" ] || OPTIONS="$OPTIONS --homer-protocol=$HOMER_PROTOCOL" [ -z "$HOMER_PROTOCOL" ] || OPTIONS="$OPTIONS --homer-protocol=$HOMER_PROTOCOL"
[ -z "$HOMER_ID" ] || OPTIONS="$OPTIONS --homer-id=$HOMER_ID" [ -z "$HOMER_ID" ] || OPTIONS="$OPTIONS --homer-id=$HOMER_ID"
if [ ! -z "$RECORDING_DIR" ]; then if [ -n "$RECORDING_DIR" ]; then
OPTIONS="$OPTIONS --recording-dir=$RECORDING_DIR" OPTIONS="$OPTIONS --recording-dir=$RECORDING_DIR"
if [ ! -d "$RECORDING_DIR" ]; then if [ ! -d "$RECORDING_DIR" ]; then
mkdir "$RECORDING_DIR" 2> /dev/null mkdir "$RECORDING_DIR" 2> /dev/null
@ -100,7 +98,7 @@ if [ ! -z "$RECORDING_DIR" ]; then
fi fi
[ -z "$RECORDING_METHOD" ] || OPTIONS="$OPTIONS --recording-method=$RECORDING_METHOD" [ -z "$RECORDING_METHOD" ] || OPTIONS="$OPTIONS --recording-method=$RECORDING_METHOD"
[ -z "$RECORDING_FORMAT" ] || OPTIONS="$OPTIONS --recording-format=$RECORDING_FORMAT" [ -z "$RECORDING_FORMAT" ] || OPTIONS="$OPTIONS --recording-format=$RECORDING_FORMAT"
[ -z "$DTLS_PASSIVE" ] || ( [ "$DTLS_PASSIVE" != "yes" ] && [ "$DTLS_PASSIVE" != "1" ] ) || OPTIONS="$OPTIONS --dtls-passive" [ -z "$DTLS_PASSIVE" ] || { [ "$DTLS_PASSIVE" != "yes" ] && [ "$DTLS_PASSIVE" != "1" ] ; } || OPTIONS="$OPTIONS --dtls-passive"
if test "$FORK" = "no" ; then if test "$FORK" = "no" ; then
OPTIONS="$OPTIONS --foreground" OPTIONS="$OPTIONS --foreground"
@ -135,20 +133,26 @@ fi
case "$1" in case "$1" in
start) start)
set +e
if [ -x "$(which ngcp-check-active)" ]; then if [ -x "$(which ngcp-check-active)" ]; then
case "$(ngcp-check-active -v)" in case "$(ngcp-check-active -v)" in
active|transition) active|transition)
log_action_msg "Active node or transition." log_action_msg "Active node or transition."
;; ;;
*) *)
log_action_msg "Ignored start action in inactive node ($status)" log_action_msg "Ignored start action in inactive node"
exit 0 exit 0
;; ;;
esac esac
fi fi
ngcp-rtpengine-iptables-setup start
set -e RC=0
ngcp-rtpengine-iptables-setup start || RC=$?
if [ "$RC" -ne 0 ]; then
log_action_msg "Failed to start ngcp-rtpengine-iptables-setup"
log_end_msg "$RC"
exit 1
fi
log_daemon_msg "Starting $DESC" "$NAME" log_daemon_msg "Starting $DESC" "$NAME"
# shellcheck disable=SC2086 # shellcheck disable=SC2086
start-stop-daemon --start --quiet --pidfile "$PIDFILE" \ start-stop-daemon --start --quiet --pidfile "$PIDFILE" \
@ -157,16 +161,15 @@ case "$1" in
;; ;;
stop) stop)
log_daemon_msg "Stopping $DESC" "$NAME" log_daemon_msg "Stopping $DESC" "$NAME"
RC=0
start-stop-daemon --oknodo --stop --quiet --pidfile $PIDFILE \ start-stop-daemon --oknodo --stop --quiet --pidfile $PIDFILE \
--retry 5 --exec "$DAEMON" --retry 5 --exec "$DAEMON" || RC=$?
if [ "$?" -ne 0 ]; then if [ "$RC" -eq 0 ]; then
return $? ngcp-rtpengine-iptables-setup stop || true
fi fi
set +e rm -f "$PIDFILE"
ngcp-rtpengine-iptables-setup stop log_end_msg "$RC"
set -e exit "$RC"
rm -f $PIDFILE
log_end_msg $?
;; ;;
restart|force-reload) restart|force-reload)
$0 stop $0 stop

@ -20,7 +20,7 @@ else
if [ -n "$TABLE" ] && [ "$TABLE" -ge 0 ] && \ if [ -n "$TABLE" ] && [ "$TABLE" -ge 0 ] && \
[ -n "$NO_FALLBACK" ] && \ [ -n "$NO_FALLBACK" ] && \
([ "$NO_FALLBACK" = "1" ] || [ "$NO_FALLBACK" = "yes" ]) { [ "$NO_FALLBACK" = "1" ] || [ "$NO_FALLBACK" = "yes" ] ; }
then then
if lsmod | grep -q $modname || modinfo $modname > /dev/null 2> /dev/null; then if lsmod | grep -q $modname || modinfo $modname > /dev/null 2> /dev/null; then
true true

@ -9,8 +9,6 @@
# Description: Recording daemon for RTP and other media streams # Description: Recording daemon for RTP and other media streams
### END INIT INFO ### END INIT INFO
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=ngcp-rtpengine-recording-daemon NAME=ngcp-rtpengine-recording-daemon
DESC="RTP/media recording daemon" DESC="RTP/media recording daemon"
@ -66,21 +64,25 @@ fi
case "$1" in case "$1" in
start) start)
set +e
if [ -x "$(which ngcp-check-active)" ]; then if [ -x "$(which ngcp-check-active)" ]; then
case "$(ngcp-check-active -v)" in case "$(ngcp-check-active -v)" in
active|transition) active|transition)
log_action_msg "Active node or transition." log_action_msg "Active node or transition."
;; ;;
*) *)
log_action_msg "Ignored start action in inactive node ($status)" log_action_msg "Ignored start action in inactive node"
exit 0 exit 0
;; ;;
esac esac
fi fi
set -e
ngcp-rtpengine-recording-nfs-setup start RC=0
ngcp-rtpengine-recording-nfs-setup start || RC=$?
if [ "$RC" -ne 0 ] ; then
log_action_msg "Failed to start ngcp-rtpengine-recording-nfs-setup"
log_end_msg "$RC"
exit "$RC"
fi
log_daemon_msg "Starting $DESC" "$NAME" log_daemon_msg "Starting $DESC" "$NAME"
@ -91,13 +93,12 @@ case "$1" in
;; ;;
stop) stop)
log_daemon_msg "Stopping $DESC" "$NAME" log_daemon_msg "Stopping $DESC" "$NAME"
RC=0
start-stop-daemon --oknodo --stop --quiet --pidfile $PIDFILE \ start-stop-daemon --oknodo --stop --quiet --pidfile $PIDFILE \
--retry 5 --exec "$DAEMON" --retry 5 --exec "$DAEMON" || RC=$?
if [ "$?" -ne 0 ]; then rm -f "$PIDFILE"
return $? log_end_msg "$RC"
fi exit "$RC"
rm -f $PIDFILE
log_end_msg $?
;; ;;
force-reload|restart) force-reload|restart)
$0 stop $0 stop

@ -109,7 +109,7 @@ build_opts() {
[ -z "$REDIS_NUM_THREADS" ] || OPTS+=" --redis-num-threads=$REDIS_NUM_THREADS" [ -z "$REDIS_NUM_THREADS" ] || OPTS+=" --redis-num-threads=$REDIS_NUM_THREADS"
[ -z "$REDIS_EXPIRES" ] || OPTS+=" --redis-expires=$REDIS_EXPIRES" [ -z "$REDIS_EXPIRES" ] || OPTS+=" --redis-expires=$REDIS_EXPIRES"
[ -z "$REDIS_MULTIKEY" ] || OPTS+=" --redis-multikey=$REDIS_MULTIKEY" [ -z "$REDIS_MULTIKEY" ] || OPTS+=" --redis-multikey=$REDIS_MULTIKEY"
[ -z "$NO_REDIS_REQUIRED" ] || ( [ "$NO_REDIS_REQUIRED" != "1" ] && [ "$NO_REDIS_REQUIRED" != "yes" ] ) || OPTS+=" --no-redis-required" [ -z "$NO_REDIS_REQUIRED" ] || { [ "$NO_REDIS_REQUIRED" != "1" ] && [ "$NO_REDIS_REQUIRED" != "yes" ] ; } || OPTS+=" --no-redis-required"
[ -z "$B2B_URL" ] || OPTS+=" --b2b-url=$B2B_URL" [ -z "$B2B_URL" ] || OPTS+=" --b2b-url=$B2B_URL"
[ -z "$LOG_LEVEL" ] || OPTS+=" --log-level=$LOG_LEVEL" [ -z "$LOG_LEVEL" ] || OPTS+=" --log-level=$LOG_LEVEL"
[ -z "$LOG_FACILITY" ] || OPTS+=" --log-facility=$LOG_FACILITY" [ -z "$LOG_FACILITY" ] || OPTS+=" --log-facility=$LOG_FACILITY"
@ -126,10 +126,10 @@ build_opts() {
[ -z "$HOMER_ID" ] || OPTS+=" --homer-id=$HOMER_ID" [ -z "$HOMER_ID" ] || OPTS+=" --homer-id=$HOMER_ID"
[ -z "$RECORDING_METHOD" ] || OPTS+=" --recording-method=$RECORDING_METHOD" [ -z "$RECORDING_METHOD" ] || OPTS+=" --recording-method=$RECORDING_METHOD"
[ -z "$RECORDING_FORMAT" ] || OPTS+=" --recording-format=$RECORDING_FORMAT" [ -z "$RECORDING_FORMAT" ] || OPTS+=" --recording-format=$RECORDING_FORMAT"
[ -z "$DTLS_PASSIVE" ] || ( [ "$DTLS_PASSIVE" != "yes" ] && [ "$DTLS_PASSIVE" != "1" ] ) || OPTS+=" --dtls-passive" [ -z "$DTLS_PASSIVE" ] || { [ "$DTLS_PASSIVE" != "yes" ] && [ "$DTLS_PASSIVE" != "1" ] ; } || OPTS+=" --dtls-passive"
# recording dir # recording dir
if [ ! -z "$RECORDING_DIR" ];then if [ -n "$RECORDING_DIR" ];then
OPTS+=" --recording-dir=$RECORDING_DIR" OPTS+=" --recording-dir=$RECORDING_DIR"
if [ ! -d "$RECORDING_DIR" ]; then if [ ! -d "$RECORDING_DIR" ]; then
mkdir "$RECORDING_DIR" 2>/dev/null mkdir "$RECORDING_DIR" 2>/dev/null
@ -154,12 +154,10 @@ start() {
else else
modprobe xt_RTPENGINE modprobe xt_RTPENGINE
fi fi
firewall-cmd --state 2>/dev/null if firewall-cmd --state 2>/dev/null ; then
if [[ $? == 0 ]];then
# Using firewalld # Using firewalld
# Need to check if the INPUT_prefilter chain is present (permanently) # Need to check if the INPUT_prefilter chain is present (permanently)
firewall-cmd --permanent --direct --query-chain ipv4 filter INPUT_prefilter > /dev/null if ! firewall-cmd --permanent --direct --query-chain ipv4 filter INPUT_prefilter > /dev/null; then
if [[ $? != 0 ]];then
firewall-cmd --permanent --direct --add-chain ipv4 filter INPUT_prefilter firewall-cmd --permanent --direct --add-chain ipv4 filter INPUT_prefilter
firewall-cmd --permanent --direct --passthrough ipv4 -t filter -I INPUT -j INPUT_prefilter firewall-cmd --permanent --direct --passthrough ipv4 -t filter -I INPUT -j INPUT_prefilter
firewall-cmd --reload firewall-cmd --reload
@ -210,8 +208,7 @@ stop() {
. "$cachefile" . "$cachefile"
echo "Unloading module for in-kernel packet forwarding" echo "Unloading module for in-kernel packet forwarding"
echo "del $TABLE" > /proc/rtpengine/control echo "del $TABLE" > /proc/rtpengine/control
firewall-cmd --state 2>/dev/null if firewall-cmd --state 2>/dev/null; then
if [[ $? == 0 ]];then
firewall-cmd --direct --remove-rules ipv4 filter rtpengine firewall-cmd --direct --remove-rules ipv4 filter rtpengine
firewall-cmd --direct --remove-rules ipv6 filter rtpengine firewall-cmd --direct --remove-rules ipv6 filter rtpengine
firewall-cmd --direct --remove-rule ipv4 filter INPUT_prefilter 0 -j rtpengine firewall-cmd --direct --remove-rule ipv4 filter INPUT_prefilter 0 -j rtpengine

Loading…
Cancel
Save