# functions This file contains functions to be used by most or all # shell scripts in the /etc/init.d directory. # # $Id: functions,v 1.34 2000/01/31 14:30:11 misiek Exp $ # # Author: Miquel van Smoorenburg, # Hacked by: Greg Galloway and Marc Ewing # Modified for PLD by: # Marek Obuchowicz # Arkadiusz Mi¶kiewicz # First set up a default search path. export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin" if [ -f /etc/sysconfig/system ]; then . /etc/sysconfig/system fi if [ -z "$COLUMNS" ]; then COLUMNS=80 fi # Colors workaround termput() { if [ "$COLOR_INIT" = "no" ]; then : elif [ ! -d /usr/share/terminfo ] || \ ! ( [ -x /usr/bin/tput ] || [ -x /bin/tput ] ) ; then case "$1" in hpa) echo -ne "\033[${2}G" ;; cuu*) echo -ne "\033[${2}A" ;; el) echo -ne "\033[0K" ;; setaf) echo -ne "\033[0;3${2}m" ;; esac else tput "$@" fi } # printf equivalent printf_() { text="$1" ; shift ; if [ -n "$1" ]; then m="$1"; shift; while [ -n "$1" ]; do m="$m\",\"$1" ; shift ; done fi awk "BEGIN {printf \"$text\", \"$m\"; }" } # National language support function nls() { if [ -x /bin/gettext ] || [ -x /usr/bin/gettext ]; then if [ -z "$NLS_DOMAIN" ]; then NLS_DOMAIN="rc-scripts" fi MESSAGE="$1" # avoid translating empty text. --misiek if [ -n "$MESSAGE" ]; then text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$MESSAGE"`" else text="$MESSAGE" fi shift printf_ "$text" "$@" echo else echo "$@" fi } # Some functions to handle PLD-style messages show() { what="`nls "DONE"`"; typeset -i offset=${#what} text="`nls "$*"`.................................................................................." awk "BEGIN {printf \"%.$((75 - $offset))s\", \"$text\";}" # move to column number 67 termput hpa 67 } busy() { echo -n "`termput setaf 6`[`termput setaf 5` `nls "BUSY"` `termput setaf 6`]`termput setaf 7`" } ok() { echo "`termput setaf 6`[`termput setaf 2` `nls "DONE"` `termput setaf 6`]`termput setaf 7`" } started() { echo "`termput setaf 6`[`termput setaf 2` `nls "WORK"` `termput setaf 6`]`termput setaf 7`" } fail() { echo "`termput setaf 6`[`termput setaf 1` `nls "FAIL"` `termput setaf 6`]`termput setaf 7`" } died() { echo "`termput setaf 6`[`termput setaf 1` `nls "DIED"` `termput setaf 6`]`termput setaf 7`" } deltext() { echo -ne "`nls "\b\b\b\b\b\b\b\b"`" } # Usage run_cmd Message command_to_run run_cmd() { _ERRORS="" MESSAGE=$1 show "$MESSAGE"; busy shift if _ERRORS="`initlog -c \"$*\" 2>&1`"; then deltext; ok else deltext; fail; [ -n "$_ERRORS" ] && echo $_ERRORS fi exit_code=$? unset _ERRORS return $exit_code } # compatibility functions action() { STRING=$1 shift run_cmd "$STRING" "$*" } # A function to start a program (now it's usefull on read-only filesystem too) daemon() { nicelevel=0 _ERRORS="" [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0 # Test syntax. case $1 in '') echo '$0: Usage: daemon [+/-nicelevel] {program}' return 1;; -*|+*) SERVICE_RUN_NICE_LEVEL=$1 shift;; esac # make sure it doesn't core dump anywhere; while this could mask # problems with the daemon, it also closes some security problems ulimit -c 0 # And start it up. busy if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then deltext ok else deltext fail [ -n "$_ERRORS" ] && echo $_ERRORS fi unset _ERRORS } # A function to stop a program. killproc() { # Test syntax. if [ $# = 0 ]; then nls "Usage: killproc {program} [signal]" return 1 fi busy notset=0 # check for second arg to be kill level if [ "$2" != "" ] ; then killlevel=$2 else notset=1 killlevel="-9" fi # Save basename. base=`basename $1` # Find pid. pid=`pidofproc $base` # Kill it. if [ "$pid" != "" ] ; then if [ "$notset" = "1" ] ; then if ps h $pid>/dev/null 2>&1; then # TERM first, then KILL if not dead kill -TERM $pid >/dev/null 2>&1 usleep 100000 if ps h $pid >/dev/null 2>&1 ; then sleep 1 if ps h $pid >/dev/null 2>&1 ; then sleep 3 if ps h $pid >/dev/null 2>&1 ; then kill -KILL $pid >/dev/null 2>&1 fi fi fi ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok) else deltext; died fi # use specified level only else if ps h $pid >/dev/null 2>&1; then kill $killlevel $pid && (deltext; ok) || (deltext; fail) else deltext; died fi fi else deltext died fi # Remove pid file if any. if [ "$notset" = "1" ]; then rm -f /var/run/$base.pid fi } # A function to find the pid of a program. pidofproc() { # Test syntax. if [ $# = 0 ] ; then nls "Usage: pidofproc {program}\n" return 1 fi # First try "/var/run/*.pid" files if [ -f /var/run/$1.pid ] ; then pid=`head -1 /var/run/$1.pid` if [ "$pid" != "" ] ; then echo $pid return 0 fi fi # Next try "pidof" pid=`pidof $1` if [ "$pid" != "" ] ; then echo $pid return 0 fi # Finally try to extract it from ps ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } { if ((prog == $5) || (("(" prog ")") == $5) || (("[" prog "]") == $5) || ((prog ":") == $5)) { print $1 ; exit 0 } }' $1 } status() { # Test syntax. if [ $# = 0 ] ; then nls "Usage: status {program}\n" return 1 fi # First try "pidof" pid=`pidof $1` if [ "$pid" != "" ] ; then nls "%s (pid %s) is running..." "$1" "$pid" return 0 else pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } { if ((prog == $5) || (("(" prog ")") == $5) || (("[" prog "]") == $5) || ((prog ":") == $5)) { print $1 ; exit 0 } }' $1` if [ "$pid" != "" ] ; then nls "%s (pid %s) is running..." "$1" "$pid" return 0 fi fi # Next try "/var/run/*.pid" files if [ -f /var/run/$1.pid ] ; then pid=`head -1 /var/run/$1.pid` if [ "$pid" != "" ] ; then nls "%s dead but pid file exists" "$1" return 1 fi fi # See if /var/lock/subsys/$1 exists if [ -f /var/lock/subsys/$1 ]; then nls "%s dead but subsys locked" "$1" return 2 fi nls "%s is stopped" "$1" return 3 }