[PATCH 3/3] init-d-script: handle daemon errors returned by start-stop-daemon

Trek trek00 at inbox.ru
Tue Apr 26 22:33:33 BST 2022


When start-stop-daemon is called without --background, it returns
the exit status of the daemon instead of its own, that could mean
different things.

To catch the start-stop-daemon errors, it needs to be called at
first with --test and its error code, if any, is returned by the
do_start_cmd function. Otherwise it is called again to start the
daemon and the function returns 2 in case of errors.
---
 debian/init-d-script         | 19 ++++++++++++++++++-
 debian/init-d-script-test.sh |  8 ++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/debian/init-d-script b/debian/init-d-script
index 7b0b1c0c..6b6985f8 100755
--- a/debian/init-d-script
+++ b/debian/init-d-script
@@ -43,11 +43,23 @@ call() {
 # Function that starts the daemon/service
 #
 
+# Return
+#   0 if daemon has been started
+#   1 if daemon was already running
+#   other if a failure occurred
 do_start_cmd() {
+	# When start-stop-daemon is called without --background, it
+	# returns the exit status of the daemon instead of its own,
+	# so test start-stop-daemon for errors before starting
+	start-stop-daemon --test --start --quiet \
+	    ${PIDFILE:+--pidfile "$PIDFILE"} \
+	    ${COMMAND_NAME:+--name "$COMMAND_NAME"} \
+	    ${DAEMON:+--exec "$DAEMON"} $START_ARGS || return $?
 	start-stop-daemon --start --quiet \
 	    ${PIDFILE:+--pidfile "$PIDFILE"} \
 	    ${COMMAND_NAME:+--name "$COMMAND_NAME"} \
-	    ${DAEMON:+--exec "$DAEMON"} $START_ARGS -- $DAEMON_ARGS
+	    ${DAEMON:+--exec "$DAEMON"} $START_ARGS -- $DAEMON_ARGS ||
+	    return 2
 }
 
 do_start()
@@ -73,6 +85,11 @@ do_start()
 # Function that stops the daemon/service
 #
 
+# Return
+#   0 if daemon has been stopped
+#   1 if daemon was already stopped
+#   2 if daemon is still running
+#   other if a failure occurred
 do_stop_cmd() {
 	start-stop-daemon --stop --quiet \
 	    --retry=TERM/0/CONT/30/KILL/5 \
diff --git a/debian/init-d-script-test.sh b/debian/init-d-script-test.sh
index 8e83b9f3..392b8c0c 100644
--- a/debian/init-d-script-test.sh
+++ b/debian/init-d-script-test.sh
@@ -85,6 +85,14 @@ EOF
 
 try start 0 1
 
+# daemon returns error
+cat >$dir/script <<-EOF
+	DAEMON=/bin/sh
+	DAEMON_ARGS=/nonexistent
+EOF
+
+try start 1 1
+
 
 [ $err -eq 0 ] && log OK || log FAILED!
 
-- 
2.20.1



More information about the Debian-init-diversity mailing list