[PATCH 2/9] init-d-script: allow to disable --exec or --name options

Trek trek00 at inbox.ru
Fri Aug 28 07:21:07 BST 2020


This is useful in conjunction with --startas or when other matching
options are specified, like --user and --pidfile, or when --exec
or --name cannot be easily guessed at packaging time.

The variables START_ARGS and STOP_ARGS are moved at the end of the
start-stop-daemon command line, to allow the override of any option.
---
 debian/init-d-script   | 29 +++++++++++++++++++----------
 debian/init-d-script.5 | 24 +++++++++++++++++++++---
 2 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/debian/init-d-script b/debian/init-d-script
index a977d7e2..21be2804 100755
--- a/debian/init-d-script
+++ b/debian/init-d-script
@@ -45,8 +45,9 @@ call() {
 
 do_start_cmd() {
 	start-stop-daemon --start --quiet --oknodo \
-	    ${PIDFILE:+--pidfile ${PIDFILE}} $START_ARGS \
-	    --startas $DAEMON --name ${COMMAND_NAME} --exec $DAEMON -- $DAEMON_ARGS
+	    ${PIDFILE:+--pidfile "$PIDFILE"} \
+	    ${COMMAND_NAME:+--name "$COMMAND_NAME"} \
+	    ${DAEMON:+--exec "$DAEMON"} $START_ARGS -- $DAEMON_ARGS
 }
 
 do_start()
@@ -74,8 +75,9 @@ do_start()
 
 do_stop_cmd() {
 	start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 \
-	    $STOP_ARGS \
-	    ${PIDFILE:+--pidfile ${PIDFILE}} --name ${COMMAND_NAME} --exec $DAEMON
+	    ${PIDFILE:+--pidfile "$PIDFILE"} \
+	    ${COMMAND_NAME:+--name "$COMMAND_NAME"} \
+	    ${DAEMON:+--exec "$DAEMON"} $STOP_ARGS
 	RETVAL="$?"
 	[ "$RETVAL" = 2 ] && return 2
 	# Wait for children to finish too if this is a daemon that forks
@@ -84,10 +86,11 @@ do_stop_cmd() {
 	# that waits for the process to drop all resources that could be
 	# needed by services started subsequently.  A last resort is to
 	# sleep for some time.
-	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \
-	    $STOP_ARGS \
-	    --exec $DAEMON
-	[ "$?" = 2 ] && return 2
+	if [ -n "$DAEMON" ] ; then
+		start-stop-daemon --stop --quiet --oknodo \
+		    --retry=0/30/KILL/5 --exec "$DAEMON" $STOP_ARGS
+		[ "$?" = 2 ] && return 2
+	fi
 	# Many daemons don't delete their pidfiles when they exit.
 	rm -f $PIDFILE
 	return $RETVAL
@@ -147,7 +150,8 @@ do_reload_sigusr1() {
         fi
         log_daemon_msg "Reloading $DESC configuration files" "$NAME"
         start-stop-daemon --oknodo --stop --signal 1 --quiet \
-          --pidfile "$PIDFILE" --exec "$DAEMON"
+	    ${PIDFILE:+--pidfile "$PIDFILE"} \
+	    ${DAEMON:+--exec "$DAEMON"}
         log_end_msg $?
         if is_call_implemented do_reload_cleanup ; then
             call do_reload_cleanup
@@ -164,7 +168,8 @@ fi
 
 # Unset configuration variables to make sure that if variable is not assigned a
 # value in init script, it does not use one from environment. See #822918.
-unset DAEMON DAEMON_ARGS NAME COMMAND_NAME PIDFILE
+unset DAEMON DAEMON_ARGS DESC NAME COMMAND_NAME PIDFILE \
+    START_ARGS STOP_ARGS
 
 SCRIPTNAME="$__init_d_script_name"
 scriptbasename="$(basename "$__init_d_script_name")"
@@ -194,6 +199,10 @@ if [ none != "$DAEMON" ] && [ ! -x "$DAEMON" ] ; then
 	exit 0
 fi
 
+# Do not use DAEMON or COMMAND_NAME if they are set to 'none'.
+[ none = "$DAEMON" ] && DAEMON=
+[ none = "$COMMAND_NAME" ] && COMMAND_NAME=
+
 # Load the VERBOSE setting and other rcS variables
 . /lib/init/vars.sh
 if [ -t 0 ] ; then # Be verbose when called from a terminal
diff --git a/debian/init-d-script.5 b/debian/init-d-script.5
index d683e78a..e372b515 100644
--- a/debian/init-d-script.5
+++ b/debian/init-d-script.5
@@ -32,7 +32,10 @@ The following variables affect behaviour of an init script:
 .Bl -tag -width "DAEMON_ARGS"
 .It Ev DAEMON
 Path to daemon being started.
-If the init script is not supposed to start any kind of daemon, the functions
+If the init script is not supposed to start any kind of daemon,
+it should be set to
+.Dq Li none
+and the functions
 .Fn do_start_override ,
 .Fn do_stop_override
 and
@@ -52,8 +55,15 @@ If this variable is set, it is used as argument to the
 option of
 .Xr start\-stop\-daemon 8 .
 It may be useful if the value of the
-.Ev NAME
-variable is too long.
+.Dq Ev NAME
+variable is longer than the command name length supported by
+the running kernel.
+If the value is verbatim
+.Dq Li none ,
+the command name will not be used to match the processes.
+If unset, this variable defaults to the
+.Dq Ev NAME
+value.
 .It Ev PIDFILE
 Path to file where the process identifier of the started daemon
 will be stored during start.
@@ -64,6 +74,14 @@ If this variable is not set, it gets a sensible default value,
 so it is rarely necessary to set this variable explicitly.
 .El
 .Pp
+The variables
+.Ev START_ARGS
+and
+.Ev STOP_ARGS
+are additional arguments, passed to
+.Xr start\-stop\-daemon 8
+during start and stop actions, to override the default options.
+.Pp
 Additionally, it is possible to change the behaviour of the resulting
 shell script by overriding some of the internal functions.
 To do so, define function with an
-- 
2.20.1




More information about the Debian-init-diversity mailing list