]> git.pld-linux.org Git - projects/rc-scripts.git/commitdiff
move inner body of daemon() into separate func
authorElan Ruusamäe <glen@delfi.ee>
Thu, 8 Oct 2015 14:46:41 +0000 (17:46 +0300)
committerElan Ruusamäe <glen@delfi.ee>
Thu, 8 Oct 2015 14:46:44 +0000 (17:46 +0300)
this will ease understanding it's logic and avoid bugs

lib/functions

index 8a6d9c985fce2204cad7e9bac1a32dfdd44a2502..ecd8e8d95063990fee18912ccedeec7dcce7fec1 100644 (file)
@@ -628,16 +628,92 @@ _daemon_set_ulimits() {
        done
 }
 
+# inner function used by daemon()
+# do not call this directly, as it expects variables being inherited
+# it expects options parsed by daemon() and command to be executed in "$@"
+_daemon_exec() {
+       local prog=""
+       umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
+       export USER=root HOME=/tmp TMPDIR=/tmp
+
+       nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
+       nice=${nice:-0}
+
+       # make nice level absolute, not to be dependant of nice level of shell where service started
+       nice=$(($nice - $(nice)))
+
+       if [ "$closefds" = 1 ]; then
+               exec 1>&-
+               exec 2>&-
+               exec 0<&-
+       elif [ "$redirfds" = 1 ]; then
+               exec 1>/dev/null
+               exec 2>/dev/null
+               exec 0</dev/null
+       else
+               exec 2>&1
+               exec 0</dev/null
+       fi
+
+       if is_no "$RC_LOGGING"; then
+               prog=$1; shift
+               if [ ! -x $prog ]; then
+                       logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
+                       local a o=$IFS
+                       IFS=:
+                       for a in $PATH; do
+                               if [ -x $a/$prog ]; then
+                                       prog=$a/$prog
+                                       break
+                               fi
+                       done
+                       IFS=$o
+               fi
+               set -- "$prog" "$@"
+
+               # use setsid to detach from terminal,
+               # needs pidfile or ssd would check setsid program instead of real program
+               if [ "$pidfile" ]; then
+                       set -- /usr/bin/setsid "$@"
+               fi
+
+               prog=$1; shift
+               /sbin/start-stop-daemon -q --start \
+                       --nicelevel $nice \
+                       ${pidfile:+--pidfile $pidfile} \
+                       ${makepid:+--make-pidfile} \
+                       ${user:+--chuid $user} \
+                       ${chdir:+--chdir "$chdir"} \
+                       ${fork:+--background} \
+                       ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
+                       --exec "$prog" \
+                       -- "$@"
+       else
+               if [ -n "$user" -a "$user" != "root" ]; then
+                       prog="/bin/su $user -s /bin/sh -c \""
+               fi
+               if [ "$fork" = "1" ]; then
+                       prog="/usr/bin/setsid ${prog:-sh -c \"}"
+               fi
+               # If command to execute ends with quotation mark, add remaining
+               # arguments and close quotation.
+               if [ "$prog" != "${prog%\"}" ]; then
+                       prog="$prog $*$end\""
+               else
+                       prog="$prog $*$end"
+               fi
+
+               nice -n $nice initlog -c "$prog" 2>&1 </dev/null
+       fi
+}
+
 # A function to start a program (now it's useful on read-only filesystem too)
 daemon() {
-       local errors="" prog="" end="" waitname="" waittime=""
+       local errors="" end="" waitname="" waittime=""
        local exit_code=0
        local nice=$SERVICE_RUN_NICE_LEVEL
        local fork user closefds redirfds pidfile makepid chdir=/
 
-       # NOTE: if you wonder how the shellish (by syntax) $prog works in ssd mode,
-       # then the answer is: it totally ignores $prog and uses "$@" itself.
-
        while [ $# -gt 0 ]; do
                case $1 in
                '')
@@ -708,81 +784,7 @@ daemon() {
        busy
        cd $chdir
        [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
-       if errors=$(
-               umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
-               export USER=root HOME=/tmp TMPDIR=/tmp
-
-               nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
-               nice=${nice:-0}
-
-               # make nice level absolute, not to be dependant of nice level of shell where service started
-               nice=$(($nice - $(nice)))
-
-               if [ "$closefds" = 1 ]; then
-                       exec 1>&-
-                       exec 2>&-
-                       exec 0<&-
-               elif [ "$redirfds" = 1 ]; then
-                       exec 1>/dev/null
-                       exec 2>/dev/null
-                       exec 0</dev/null
-               else
-                       exec 2>&1
-                       exec 0</dev/null
-               fi
-
-               if is_no "$RC_LOGGING"; then
-                       prog=$1; shift
-                       if [ ! -x $prog ]; then
-                               logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
-                               local a o=$IFS
-                               IFS=:
-                               for a in $PATH; do
-                                       if [ -x $a/$prog ]; then
-                                               prog=$a/$prog
-                                               break
-                                       fi
-                               done
-                               IFS=$o
-                       fi
-                       set -- "$prog" "$@"
-
-                       # use setsid to detach from terminal,
-                       # needs pidfile or ssd would check setsid program instead of real program
-                       if [ "$pidfile" ]; then
-                               set -- /usr/bin/setsid "$@"
-                       fi
-
-                       prog=$1; shift
-                       /sbin/start-stop-daemon -q --start \
-                               --nicelevel $nice \
-                               ${pidfile:+--pidfile $pidfile} \
-                               ${makepid:+--make-pidfile} \
-                               ${user:+--chuid $user} \
-                               ${chdir:+--chdir "$chdir"} \
-                               ${fork:+--background} \
-                               ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
-                               --exec "$prog" \
-                               -- "$@"
-               else
-                       if [ -n "$user" -a "$user" != "root" ]; then
-                               prog="/bin/su $user -s /bin/sh -c \""
-                       fi
-                       if [ "$fork" = "1" ]; then
-                               prog="/usr/bin/setsid ${prog:-sh -c \"}"
-                       fi
-                       # If command to execute ends with quotation mark, add remaining
-                       # arguments and close quotation.
-                       if [ "$prog" != "${prog%\"}" ]; then
-                               prog="$prog $*$end\""
-                       else
-                               prog="$prog $*$end"
-                       fi
-
-                       nice -n $nice initlog -c "$prog" 2>&1 </dev/null
-               fi
-               ); then
-
+       if errors=$(_daemon_exec "$@"); then
                # wait for process (or pidfile) to be created
                if [ "$waittime" -gt 0 ]; then
                        # waitname can be empty, as if pidfile is in use, it is not relevant
This page took 0.267567 seconds and 4 git commands to generate.