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.56 2000/09/18 22:32:04 saq 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"
59 while [ $# -gt 0 ]; do
64 awk "BEGIN {printf \"$text\", \"$m\"; }"
67 # National language support function
70 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
71 OLD_NLS_DOMAIN="$NLS_DOMAIN"
72 if [ "$1" = "--nls-domain" ]; then
77 if [ -z "$NLS_DOMAIN" ]; then
78 NLS_DOMAIN="rc-scripts"
81 # avoid translating empty text. --misiek
82 if [ -n "$MESSAGE" ]; then
83 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$MESSAGE"`"
90 NLS_DOMAIN="$OLD_NLS_DOMAIN"
98 nls "ERROR: Networking is down. %s can't be run." "$1"
103 show "Starting %s service" "$1"
106 msg_Already_Running()
108 nls "%s service is already running." "$1"
113 show "Stopping %s service" "$1"
118 nls "%s service is not running." "$1"
123 show "Reloading %s service" "$1"
131 # Some functions to handle PLD-style messages
134 what="`nls --nls-domain rc-scripts "DONE"`"; typeset -i offset=${#what}
137 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
140 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
141 # First is the text to display, second is color number to use (argument to
142 # tput setaf). If second argument is not given, default (2, green) will be
146 if [ -n "$2" ]; then COLOR="$2"; else COLOR="2"; fi
148 echo -n "`termput setaf 6`[`termput setaf "$COLOR"` `nls --nls-domain rc-scripts "$1"` `termput setaf 6`]`termput op`"
182 termput hpa $INIT_COL
185 # Usage run_cmd Message command_to_run
191 show "$MESSAGE"; busy
193 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
196 fail; [ -n "$_ERRORS" ] && echo $_ERRORS
203 # compatibility functions
208 run_cmd "$STRING" "$*"
211 # A function to start a program (now it's usefull on read-only filesystem too)
217 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
220 '') msg_Usage " daemon [+/-nicelevel] {program}"
222 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
226 # make sure it doesn't core dump anywhere; while this could mask
227 # problems with the daemon, it also closes some security problems
232 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
237 [ -n "$_ERRORS" ] && echo $_ERRORS
243 # A function to stop a program.
248 msg_Usage " killproc {program} [signal]"
255 # check for second arg to be kill level
256 if [ "$2" != "" ] ; then
267 pid=`pidofproc $base`
270 if [ "$pid" != "" ] ; then
271 if [ "$notset" = "1" ] ; then
272 if ps h $pid>/dev/null 2>&1; then
273 # TERM first, then KILL if not dead
274 kill -TERM $pid >/dev/null 2>&1
276 if ps h $pid >/dev/null 2>&1 ; then
278 if ps h $pid >/dev/null 2>&1 ; then
280 if ps h $pid >/dev/null 2>&1 ; then
281 kill -KILL $pid >/dev/null 2>&1
285 ps h $pid >/dev/null 2>&1 && fail || ok
289 # use specified level only
291 if ps h $pid >/dev/null 2>&1; then
292 kill $killlevel $pid && ok || fail
301 # Remove pid file if any.
302 if [ "$notset" = "1" ]; then
303 rm -f /var/run/$base.pid
307 # A function to find the pid of a program.
312 msg_Usage " pidofproc {program}"
316 # First try "/var/run/*.pid" files
317 if [ -f /var/run/$1.pid ] ; then
318 pid=`head -1 /var/run/$1.pid`
319 if [ "$pid" != "" ] ; then
327 if [ "$pid" != "" ] ; then
332 # Finally try to extract it from ps
333 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
334 { if ((prog == $5) || (("(" prog ")") == $5) ||
335 (("[" prog "]") == $5) ||
336 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
343 msg_Usage " status {program}"
349 if [ "$pid" != "" ] ; then
350 nls "%s (pid %s) is running..." "$1" "$pid"
353 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
354 { if ((prog == $5) || (("(" prog ")") == $5) ||
355 (("[" prog "]") == $5) ||
356 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
357 if [ "$pid" != "" ] ; then
358 nls "%s (pid %s) is running..." "$1" "$pid"
363 # Next try "/var/run/*.pid" files
364 if [ -f /var/run/$1.pid ] ; then
365 pid=`head -1 /var/run/$1.pid`
366 if [ "$pid" != "" ] ; then
367 nls "%s dead but pid file exists" "$1"
371 # See if /var/lock/subsys/$1 exists
372 if [ -f /var/lock/subsys/$1 ]; then
373 nls "%s dead but subsys locked" "$1"
376 nls "%s is stopped" "$1"
380 # Confirm whether we really want to run this service
382 echo -n "`nls "Start service"` $1 `nls "(Y)es/(N)o/(C)ontinue? [Y] "`"
405 msg_Usage " is_yes {value}"
413 [ "$1" = "true" ] ||\
414 [ "$1" = "True" ] ||\
415 [ "$1" = "TRUE" ] ||\
427 msg_Usage " is_no {value}"
435 [ "$1" = "false" ] ||\
436 [ "$1" = "False" ] ||\
437 [ "$1" = "FALSE" ] ||\
449 while is_no $parsed ; do
468 if is_yes "${single}" ; then
470 show "Loading %s kernel module(s)" "$foo"
474 if [ -x /sbin/modprobe ] ; then
475 /sbin/modprobe -s $args "$@"
481 if is_yes "${single}" ; then
483 if [ $result == "0" ] ; then
484 is_yes "${single}" && ok
487 if is_yes "$die" ; then
488 nls "Could not load %s kernel module(s)" "$@"
493 unset single die args result