Tweak exit codes

* Add exit code to status action
* Improve relevance of exit codes for various errors
This commit is contained in:
LouisTakePILLz 2015-07-28 17:19:10 -04:00
parent 0e3cecf975
commit 4778965f21

@ -81,8 +81,7 @@ get_pid() {
fi fi
} }
case "$1" in do_start() {
start)
# Ensure that the PIDFILE is still valid. # Ensure that the PIDFILE is still valid.
pid=$(get_pid) pid=$(get_pid)
if [ ! -d "/proc/$pid" ]; then if [ ! -d "/proc/$pid" ]; then
@ -116,18 +115,18 @@ start)
# Check whether the specified user exists # Check whether the specified user exists
user_exists=$(id -u "${PS_USER:-root}" &> /dev/null && echo $?) user_exists=$(id -u "${PS_USER:-root}" &> /dev/null && echo $?)
[ -z "$user_exists" ] \ [ -z "$user_exists" ] \
&& log_failure_msg "User '${PS_USER:-root}' does not exist" && exit 2 && log_failure_msg "User '${PS_USER:-root}' does not exist" && exit 124
# Check whether the user can execute the daemon # Check whether the user can execute the daemon
has_permission=$(sh -c "sudo -n -u \"${PS_USER:-root}\" test -x $DAEMON && echo y" 2> /dev/null) has_permission=$(sh -c "sudo -n -u \"${PS_USER:-root}\" test -x $DAEMON && echo y" 2> /dev/null)
[ -z "$has_permission" ] \ [ -z "$has_permission" ] \
&& log_failure_msg "User '${PS_USER}' cannot execute $DAEMON" && exit 2 && log_failure_msg "User '${PS_USER}' cannot execute $DAEMON" && exit 126
# Check whether config files can be read # Check whether config files can be read
[ ! -r "$CONFIG" ] \ [ ! -r "$CONFIG" ] \
&& log_failure_msg "Can't read configuration file" "$CONFIG" && exit 2 && log_failure_msg "Can't read configuration file" "$CONFIG" && exit 126
[ ! -r "$SIGNATURES" ] \ [ ! -r "$SIGNATURES" ] \
&& log_failure_msg "Can't read signatures file" "$SIGNATURES" && exit 2 && log_failure_msg "Can't read signatures file" "$SIGNATURES" && exit 126
if [ -z "$PS_USER" ]; then if [ -z "$PS_USER" ]; then
log_daemon_msg "Starting portspoof" log_daemon_msg "Starting portspoof"
@ -152,9 +151,9 @@ start)
exit 2 exit 2
fi fi
fi fi
;; }
stop) do_stop() {
pid=$(get_pid) pid=$(get_pid)
if [ ! -z "$pid" ]; then if [ ! -z "$pid" ]; then
kill $pid kill $pid
@ -167,22 +166,28 @@ stop)
else else
log_daemon_msg "portspoof not running" log_daemon_msg "portspoof not running"
fi fi
;; }
reload) do_reload() {
log_daemon_msg "Reloading iptables rules" log_daemon_msg "Reloading iptables rules"
setup_iptables setup_iptables
;; }
restart) do_restart() {
$0 stop do_stop
$0 start [ "$?" -eq 0 ] && do_start
;; }
case "$1" in
start) do_start ;;
stop) do_stop ;;
reload) do_reload ;;
restart) do_restart ;;
status) status)
status_of_proc -p "${PIDFILE}" $DAEMON portspoof retval=0
status_of_proc -p "${PIDFILE}" $DAEMON portspoof || retval=$?
exit $retval
;; ;;
*) *)
log_action_msg "Usage: $0 {start|stop|reload|restart|status}" log_action_msg "Usage: $0 {start|stop|reload|restart|status}"
exit 1 exit 1