# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.
#
-# $Id: functions,v 1.75 2003/05/15 21:37:46 ankry Exp $
+# $Id: functions,v 1.76 2003/05/15 22:39:08 ankry Exp $
#
# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
-# Hacked by: Greg Galloway and Marc Ewing
+# Hacked by: Greg Galloway and Marc Ewing
# Modified for PLD by:
# Marek Obuchowicz <elephant@pld.org.pl>
-# Arkadiusz Mi¶kiewicz <misiek@pld.org.pl>
+# Arkadiusz Mi¶kiewicz <misiek@pld.org.pl>
# Micha³ Kochanowicz <mkochano@pld.org.pl>
# First set up a default search path.
[ -z "$COLUMNS" ] && COLUMNS=80
# Colors workaround
-termput()
+termput()
{
if [ ! -d /usr/share/terminfo ] || \
- [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
+ [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
case "$1" in
hpa)
- echo -ne "\033[$(($2+1))G"
+ echo -ne "\033[$(($2+1))G"
;;
cuu*)
- echo -ne "\033[${2}A"
+ echo -ne "\033[${2}A"
;;
el)
- echo -ne "\033[0K"
+ echo -ne "\033[0K"
;;
setaf)
- is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
- ;;
+ is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
+ ;;
op)
- termput setaf 9
+ termput setaf 9
;;
- esac
+ esac
else
# check if we are on proper terminal
tput longname > /dev/null 2>&1 || return
- case "$1" in
+ case "$1" in
hpa | cuu* | el)
- tput "$@"
+ tput "$@"
;;
setaf)
- is_yes "$COLOR_INIT" && tput "$@"
+ is_yes "$COLOR_INIT" && tput "$@"
;;
op)
- tput setaf 9
+ tput setaf 9
;;
- esac
+ esac
fi
}
shift ;
if [ $# -gt 0 ]; then
m="$1";
- shift;
+ shift;
while [ $# -gt 0 ]; do
m="$m\",\"$1" ;
shift ;
fi
awk "BEGIN {printf \"$text\", \"$m\"; }"
}
-
+
# National language support function
nls()
{
# don't use -o instead || here - this will break ksh --misiek
while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
case "$1" in
- --nls-domain)
- shift
- NLS_DOMAIN="$1"
- shift
- ;;
- -n)
- msg_echo=''
- shift
- ;;
+ --nls-domain)
+ shift
+ NLS_DOMAIN="$1"
+ shift
+ ;;
+ -n)
+ msg_echo=''
+ shift
+ ;;
esac
done
message="$1"
echo -en "$msg_echo"
return
fi
-
+
if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
text=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${NLS_DOMAIN:-rc-scripts}" "$message")
printf_ "$text" "$@"
else
printf_ "$message" "$@"
fi
-
+
echo -en "$msg_echo"
NLS_DOMAIN="$old_nls_domain"
}
}
# Some functions to handle PLD-style messages
-show()
-{
+show()
+{
typeset text
text=$(nls "$@")
echo -n "$text"
echo -n "$(termput setaf 6)[$(termput setaf "$COLOR") $(nls --nls-domain rc-scripts "$1") $(termput setaf 6)]$(termput op)"
}
-busy()
+busy()
{
progress "BUSY" 5
}
-ok()
+ok()
{
progress "DONE"
echo
echo
}
-fail()
+fail()
{
progress "FAIL" 1
echo
return 1
}
-died()
+died()
{
progress "DIED" 1
echo
return 1
}
-deltext()
+deltext()
{
termput hpa $INIT_COL
}
# Check if $pid (could be plural) are running
checkpid()
{
- while [ "$1" ]; do
- [ -d "/proc/$1" ] && return 0
- shift
- done
- return 1
+ while [ "$1" ]; do
+ [ -d "/proc/$1" ] && return 0
+ shift
+ done
+ return 1
}
# Usage run_cmd Message command_to_run
typeset -i force_err=0
typeset -i exit_code=0
case "$1" in
- -a)
- force_err=1
- shift
- ;;
+ -a)
+ force_err=1
+ shift
+ ;;
esac
message=$1
show "$message"; busy
}
# A function to start a program (now it's usefull on read-only filesystem too)
-daemon()
+daemon()
{
typeset errors="" prog=""
typeset -i exit_code=0
# Test syntax. Don't use -o instead || here - this will broke ksh --misiek
while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
case $1 in
- '') msg_usage " daemon [--user user] [+/-nicelevel] {program}"
- return 1
+ '')
+ msg_usage " daemon [--user user] [+/-nicelevel] {program}"
+ return 2
;;
- --check)
+ --check)
# for compatibility with redhat/mandrake
nls "warning: --check option is ignored!"
shift
shift
;;
- --user)
+ --user)
shift
[ "$1" != "root" ] && prog="/bin/su $1 -c \""
shift
;;
- -*|+*) SERVICE_RUN_NICE_LEVEL=$1
+ -*|+*) SERVICE_RUN_NICE_LEVEL=$1
shift;;
esac
done
}
# A function to stop a program.
-killproc()
+killproc()
{
typeset notset killlevel base pid result
# Test syntax.
fi
busy
-
+
typeset -i notset=0
# check for second arg to be kill level
if [ -n "$2" ] ; then
}
# A function to find the pid of a program.
-pidofproc()
+pidofproc()
{
- typeset pid base
- base=$(basename "$1")
+ typeset pid base
+ base=$(basename "$1")
- # Test syntax.
- if [ $# = 0 ] ; then
- msg_usage " pidofproc {program}"
- return 1
- fi
-
- # First try "/var/run/*.pid" files
- if [ -f /var/run/${base}.pid ] ; then
- typeset line p pid
- pid=
- read line < /var/run/${base}.pid
- for p in $line; do
- [ -z "$(echo "$p" | awk ' { gsub(/[0-9]/,""); print $0; } ' )" ] && pid="$pid $p"
- done
- if [ -n "$pid" ]; then
- echo $pid
- return 0
+ # Test syntax.
+ if [ $# = 0 ] ; then
+ msg_usage " pidofproc {program}"
+ return 2
+ fi
+
+ # First try "/var/run/*.pid" files
+ if [ -f /var/run/${base}.pid ] ; then
+ typeset line p pid
+ pid=
+ read line < /var/run/${base}.pid
+ for p in $line; do
+ [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,""); print $0;}')" ] && pid="$pid $p"
+ done
+ if [ -n "$pid" ]; then
+ echo $pid
+ return 0
+ fi
fi
- fi
-
+
# Next try "pidof"
- pidof -o $$ -o $PPID -o %PPID -x "$1" || \
- pidof -o $$ -o $PPID -o %PPID -x "${base}"
+ pidof -o $$ -o $PPID -o %PPID -x "$1"
}
-status()
+status()
{
- typeset base pid
- base=$(basename "$1")
-
- # Test syntax.
- if [ $# = 0 ] ; then
- msg_usage " status {subsys} [{daemon}]"
- return 1
- fi
+ typeset base pid subsys daemon
+ subsys=$1
+ daemon=${2:-$subsys}
+ base=$(basename $daemon)
- # First try "pidof"
- pid=$(pidof -o $$ -o $PPID -o %PPID -x $1 || \
- pidof -o $$ -o $PPID -o %PPID -x ${base})
+ # Test syntax.
+ if [ $# = 0 ] ; then
+ msg_usage " status {subsys} [{daemon}]"
+ return 2
+ fi
- if [ "$pid" != "" ]; then
- nls "%s (pid %s) is running..." "$base" "$pid"
- return 0
- fi
+ # First try "pidof"
+ pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
- # Next try "/var/run/*.pid" files
- if [ -f /var/run/${base}.pid ]; then
- read pid < /var/run/${base}.pid
if [ "$pid" != "" ]; then
- nls "%s dead but pid file exists" "$base"
- return 1
+ nls "%s (pid %s) is running..." "$daemon" "$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..." "$daemon" "$pid"
+# return 0
+# fi
+ fi
+
+ # Next try "/var/run/*.pid" files
+ if [ -f /var/run/${base}.pid ]; then
+ read pid < /var/run/${base}.pid
+ if [ "$pid" != "" ]; then
+ nls "%s dead but pid file exists" "$subsys"
+ return 1
+ fi
+ fi
+
+ # See if /var/lock/subsys/$subsys exists
+ if [ -f /var/lock/subsys/$subsys ]; then
+ nls "%s dead but subsys locked" "$subsys"
+ return 2
fi
- fi
-
- # See if /var/lock/subsys/$subsys exists
- if [ -f /var/lock/subsys/$subsys ]; then
- nls "%s dead but subsys locked" "$base"
- return 2
- fi
- nls "%s is stopped" "$base"
- return 3
+ nls "%s is stopped" "$subsys"
+ return 3
}
# Confirm whether we really want to run this service
nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
read answer
case $answer in
- y|Y|t|T|j|J|"")
- return 0
+ y|Y|t|T|j|J|"")
+ return 0
;;
- c|C|k|K|w|W)
- return 2
+ c|C|k|K|w|W)
+ return 2
;;
- n|N)
- return 1
+ n|N)
+ return 1
;;
- *)
- confirm $1
- return $?
+ *)
+ confirm $1
+ return $?
;;
esac
}
is_yes()
{
- # Test syntax
+ # Test syntax
if [ $# = 0 ] ; then
msg_usage " is_yes {value}"
return 2
# Check value
case "$1" in
- yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
- # true returns zero
- return 0
+ yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
+ # true returns zero
+ return 0
;;
- *)
- # false returns one
- return 1
+ *)
+ # false returns one
+ return 1
;;
esac
}
fi
case "$1" in
- no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
- # true returns zero
- return 0
- ;;
- *)
- # false returns one
- return 1
+ no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
+ # true returns zero
+ return 0
+ ;;
+ *)
+ # false returns one
+ return 1
;;
esac
}
parsed=no
while is_no "$parsed" ; do
case "$1" in
- "single")
- single=yes
- shift
- ;;
- "die")
- die=yes
- shift
- ;;
- -*)
- args="$args $1"
- shift
- ;;
- *)
- parsed=yes
- ;;
+ "single")
+ single=yes
+ shift
+ ;;
+ "die")
+ die=yes
+ shift
+ ;;
+ -*)
+ args="$args $1"
+ shift
+ ;;
+ *)
+ parsed=yes
+ ;;
esac
done
if is_yes "${single}" ; then
success () { return 0; }
failure () { return 1; }
-# TO BE REMOVED SOON. --misiek
+# TO BE REMOVED IN AC. --ankry
msg_Network_Down () { msg_network_down "$*"; }
msg_Already_Running () { msg_already_running "$*"; }
msg_Not_Running () { msg_not_running "$*"; }