]> git.pld-linux.org Git - projects/rc-scripts.git/blobdiff - lib/functions
rename option to RC_UPTIME and add to default config
[projects/rc-scripts.git] / lib / functions
index a997d3517c4b5d6e8f0515edba95985f322d01b0..4658d6f08a178fc51c35b1573d5b9972b626a1bf 100644 (file)
@@ -62,6 +62,17 @@ env_upstart=$USE_UPSTART
 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
 [ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
 
+# if initscript is invoked via bash, enable RedHat/Fedora compatibility
+# RC_FEDORA is "set" if enabled and "unset" when not, but it's "value" is always empty
+# this is useful for inline constructs
+if [ "${BASH_VERSION+set}" = "set" ]; then
+       RC_LOGGING=yes
+       FASTRC=no
+       RC_FEDORA=
+else
+       unset RC_FEDORA || :
+fi
+
 [ "$env_upstart" ] && USE_UPSTART=$env_upstart
 
 if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
@@ -381,12 +392,16 @@ msg_usage() {
 
 # Some functions to handle PLD Linux-style messages
 show() {
-       local text len
+       local text len time
+
+       if is_yes "$RC_UPTIME"; then
+               time=$(awk '{printf("[%8.2f] ", $1)}' /proc/uptime)
+       fi
 
        if is_no "$FASTRC" && is_yes "$GETTEXT"; then
-               text=$(nls -n "$@")
+               text=$time$(nls -n "$@")
        else
-               text=$(printf "$@")
+               text=$time$(printf "$@")
        fi
        len=${#text}
        while [ $((len++)) -lt $INIT_COL ]; do
@@ -424,7 +439,7 @@ busy() {
 }
 
 ok() {
-       echo "$_ok"
+       echo -ne "$_ok${RC_FEDORA+\r}${RC_FEDORA-\n}"
 }
 
 started() {
@@ -432,7 +447,7 @@ started() {
 }
 
 fail() {
-       echo "$_fail"
+       echo -ne "$_fail${RC_FEDORA+\r}${RC_FEDORA-\n}"
        return 1
 }
 
@@ -573,12 +588,10 @@ daemon() {
                        ;;
                --user)
                        shift
-                       [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
                        user=$1
                        ;;
                --fork)
                        fork=1
-                       prog="/usr/bin/setsid sh -c \""
                        end='&'
                        ;;
                --chdir)
@@ -599,6 +612,10 @@ daemon() {
                        shift
                        waittime="$1"
                        ;;
+               --pidfile=?*)
+                       pidfile="${1#--pidfile=}"
+                       case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
+                       ;;
                --pidfile)
                        shift
                        pidfile="$1"
@@ -618,6 +635,12 @@ daemon() {
                esac
                shift
        done
+       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
@@ -643,7 +666,7 @@ daemon() {
                nice=${nice:-0}
 
                # make nice level absolute, not to be dependant of nice level of shell where service started
-               nice=$((nice-$(nice)))
+               nice=$(($nice - $(nice)))
 
                if [ "$closefds" = 1 ]; then
                        exec 1>&-
@@ -683,7 +706,7 @@ daemon() {
                                --exec "$prog" \
                                -- ${1:+"$@"}
                else
-                       nice -n $nice initlog -c "$prog" 2>&1
+                       nice -n $nice initlog -c "$prog" 2>&1 </dev/null
                fi
                ); then
 
@@ -712,15 +735,19 @@ daemon() {
 
 # A function to stop a program.
 killproc() {
-       local notset killlevel base pid pidfile result
+       local notset killlevel base pid pidfile result delay=3 try
        # Test syntax.
        if [ $# = 0 ]; then
-               msg_usage " killproc [--pidfile PIDFILE] {program} [-SIGNAME]"
+               msg_usage " killproc [--pidfile|-p PIDFILE] [-d DELAY] {program} [-SIGNAME]"
                return 2
        fi
 
        while [ "$1" != "${1##-}" ]; do
                case $1 in
+               -d)
+                       delay="$2"
+                       shift 2
+                       ;;
                --pidfile|-p)
                        pidfile="$2"
                        case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
@@ -750,9 +777,17 @@ killproc() {
        # experimental start-stop-daemon based killing.
        # works only with pidfile
        if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
-               local sig=${killlevel:--TERM}
+               local sig=${killlevel:--TERM} retry
+               # retry only if signal is not specified,
+               # as otherwise impossible to send HUP if process pid stays in pidfile.
+               if [ "${killlevel+set}" = "set" ]; then
+                       # if we send HUP it's ok if process does not die
+                       retry="--oknodo"
+               else
+                       retry="--retry ${sig#-}/10/${sig#-}/60/KILL/10"
+               fi
                /sbin/start-stop-daemon -q --stop \
-                       --retry ${sig#-}/10/${sig#-}/60/KILL/10 \
+                       $retry \
                        -s ${sig#-} \
                        ${pidfile:+--pidfile $pidfile}
                result=$?
@@ -778,14 +813,19 @@ killproc() {
                        if checkpid $pid 2>&1; then
                                # TERM first, then KILL if not dead
                                kill -TERM $pid
-                               usleep 100000
-                               if checkpid $pid && sleep 1 &&
-                                       checkpid $pid && sleep 3 &&
-                                       checkpid $pid; then
+                               usleep 50000
+
+                               try=0
+                               while [ $try -lt $delay ]; do
+                                       checkpid $pid || break
+                                       sleep 1
+                                       try=$((try+1))
+                               done
+                               if checkpid $pid; then
                                        # XXX: SIGKILL is sent already on 4th second!
                                        # HARMFUL for example to mysqld (which is already workarounded)
                                        kill -KILL $pid
-                                       usleep 100000
+                                       usleep 50000
                                fi
                        fi
                        checkpid $pid
This page took 0.081899 seconds and 4 git commands to generate.