1 # functions This file contains functions to be used by most or all
2 # shell scripts in the /etc/init.d directory.
4 # $Id: functions,v 1.54 2000/07/28 18:37:00 baggins Exp $
6 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by: Greg Galloway and Marc Ewing
9 # Marek Obuchowicz <elephant@pld.org.pl>
10 # Arkadiusz Mi¶kiewicz <misiek@pld.org.pl>
11 # Micha³ Kochanowicz <mkochano@pld.org.pl>
13 # First set up a default search path.
14 export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
19 # Source configuration if available - may override default values
20 [ -f /etc/sysconfig/system ] && . /etc/sysconfig/system
22 [ -z "$COLUMNS" ] && COLUMNS=80
27 if [ ! -d /usr/share/terminfo ] || \
28 [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
31 echo -ne "\033[$(($2+1))G"
40 is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
56 while [ $# -gt 0 ]; do
61 awk "BEGIN {printf \"$text\", \"$m\"; }"
64 # National language support function
67 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
68 OLD_NLS_DOMAIN="$NLS_DOMAIN"
69 if [ "$1" = "--nls-domain" ]; then
74 if [ -z "$NLS_DOMAIN" ]; then
75 NLS_DOMAIN="rc-scripts"
78 # avoid translating empty text. --misiek
79 if [ -n "$MESSAGE" ]; then
80 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$MESSAGE"`"
87 NLS_DOMAIN="$OLD_NLS_DOMAIN"
95 nls "ERROR: Networking is down. %s can't be run." "$1"
100 show "Starting %s service" "$1"
103 msg_Already_Running()
105 nls "%s service is already running." "$1"
110 show "Stopping %s service" "$1"
115 nls "%s service is not running." "$1"
120 show "Reloading %s service" "$1"
128 # Some functions to handle PLD-style messages
131 what="`nls --nls-domain rc-scripts "DONE"`"; typeset -i offset=${#what}
134 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
137 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
138 # First is the text to display, second is color number to use (argument to
139 # tput setaf). If second argument is not given, default (2, green) will be
143 if [ -n "$2" ]; then COLOR="$2"; else COLOR="2"; fi
145 echo -n "`termput setaf 6`[`termput setaf "$COLOR"` `nls --nls-domain rc-scripts "$1"` `termput setaf 6`]`termput op`"
179 termput hpa $INIT_COL
182 # Usage run_cmd Message command_to_run
188 show "$MESSAGE"; busy
190 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
193 fail; [ -n "$_ERRORS" ] && echo $_ERRORS
200 # compatibility functions
205 run_cmd "$STRING" "$*"
208 # A function to start a program (now it's usefull on read-only filesystem too)
214 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
217 '') msg_Usage " daemon [+/-nicelevel] {program}"
219 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
223 # make sure it doesn't core dump anywhere; while this could mask
224 # problems with the daemon, it also closes some security problems
229 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
234 [ -n "$_ERRORS" ] && echo $_ERRORS
240 # A function to stop a program.
245 msg_Usage " killproc {program} [signal]"
252 # check for second arg to be kill level
253 if [ "$2" != "" ] ; then
264 pid=`pidofproc $base`
267 if [ "$pid" != "" ] ; then
268 if [ "$notset" = "1" ] ; then
269 if ps h $pid>/dev/null 2>&1; then
270 # TERM first, then KILL if not dead
271 kill -TERM $pid >/dev/null 2>&1
273 if ps h $pid >/dev/null 2>&1 ; then
275 if ps h $pid >/dev/null 2>&1 ; then
277 if ps h $pid >/dev/null 2>&1 ; then
278 kill -KILL $pid >/dev/null 2>&1
282 ps h $pid >/dev/null 2>&1 && fail || ok
286 # use specified level only
288 if ps h $pid >/dev/null 2>&1; then
289 kill $killlevel $pid && ok || fail
298 # Remove pid file if any.
299 if [ "$notset" = "1" ]; then
300 rm -f /var/run/$base.pid
304 # A function to find the pid of a program.
309 msg_Usage " pidofproc {program}"
313 # First try "/var/run/*.pid" files
314 if [ -f /var/run/$1.pid ] ; then
315 pid=`head -1 /var/run/$1.pid`
316 if [ "$pid" != "" ] ; then
324 if [ "$pid" != "" ] ; then
329 # Finally try to extract it from ps
330 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
331 { if ((prog == $5) || (("(" prog ")") == $5) ||
332 (("[" prog "]") == $5) ||
333 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
340 msg_Usage " status {program}"
346 if [ "$pid" != "" ] ; then
347 nls "%s (pid %s) is running..." "$1" "$pid"
350 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
351 { if ((prog == $5) || (("(" prog ")") == $5) ||
352 (("[" prog "]") == $5) ||
353 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
354 if [ "$pid" != "" ] ; then
355 nls "%s (pid %s) is running..." "$1" "$pid"
360 # Next try "/var/run/*.pid" files
361 if [ -f /var/run/$1.pid ] ; then
362 pid=`head -1 /var/run/$1.pid`
363 if [ "$pid" != "" ] ; then
364 nls "%s dead but pid file exists" "$1"
368 # See if /var/lock/subsys/$1 exists
369 if [ -f /var/lock/subsys/$1 ]; then
370 nls "%s dead but subsys locked" "$1"
373 nls "%s is stopped" "$1"
377 # Confirm whether we really want to run this service
379 echo -n "`nls "Start service"` $1 `nls "(Y)es/(N)o/(C)ontinue? [Y] "`"
402 msg_Usage " is_yes {value}"
410 [ "$1" = "true" ] ||\
411 [ "$1" = "True" ] ||\
412 [ "$1" = "TRUE" ] ||\
424 msg_Usage " is_no {value}"
432 [ "$1" = "false" ] ||\
433 [ "$1" = "False" ] ||\
434 [ "$1" = "FALSE" ] ||\
446 while is_no $parsed ; do
465 if is_yes "${single}" ; then
467 show "Loading %s kernel module(s)" "$foo"
471 if [ -x /sbin/modprobe ] ; then
472 /sbin/modprobe -s $args "$@"
478 if is_yes "${single}" ; then
480 if [ $result == "0" ] ; then
481 is_yes "${single}" && ok
484 if is_yes "$die" ; then
485 nls "Could not load %s kernel module(s)" "$@"
490 unset single die args result