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.49 2000/05/01 18:43:55 zagrodzki 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>
12 # First set up a default search path.
13 export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
18 # Source configuration if available - may override default values
19 [ -f /etc/sysconfig/system ] && . /etc/sysconfig/system
21 [ -z "$COLUMNS" ] && COLUMNS=80
26 if [ ! -d /usr/share/terminfo ] || \
27 [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
30 echo -ne "\033[$(($2+1))G"
39 [ is_yes "$COLOR_INIT" ] && echo -ne "\033[0;3${2}m"
55 while [ $# -gt 0 ]; do
60 awk "BEGIN {printf \"$text\", \"$m\"; }"
63 # National language support function
66 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
67 OLD_NLS_DOMAIN="$NLS_DOMAIN"
68 if [ "$1" = "--nls-domain" ]; then
73 if [ -z "$NLS_DOMAIN" ]; then
74 NLS_DOMAIN="rc-scripts"
77 # avoid translating empty text. --misiek
78 if [ -n "$MESSAGE" ]; then
79 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$MESSAGE"`"
86 NLS_DOMAIN="$OLD_NLS_DOMAIN"
94 nls "ERROR: Networking is down. %s can't be run.\n" "$1"
99 show "Starting %s service" "$1"
102 msg_Already_Running()
104 nls "%s service is already running.\n" "$1"
109 show "Stopping %s service" "$1"
114 nls "%s service is not running.\n" "$1"
119 show "Reloading %s service" "$1"
124 nls "Usage: %s" "$*\n"
127 # Some functions to handle PLD-style messages
130 what="`nls --nls-domain rc-scripts "DONE"`"; typeset -i offset=${#what}
133 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
139 echo -n "`termput setaf 6`[`termput setaf 5` `nls --nls-domain rc-scripts "BUSY"` `termput setaf 6`]`termput setaf 7`"
145 echo "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "DONE"` `termput setaf 6`]`termput setaf 7`"
151 echo "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "WORK"` `termput setaf 6`]`termput setaf 7`"
157 echo "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "FAIL"` `termput setaf 6`]`termput setaf 7`"
163 echo "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "DIED"` `termput setaf 6`]`termput setaf 7`"
168 termput hpa $INIT_COL
171 # Usage run_cmd Message command_to_run
177 show "$MESSAGE"; busy
179 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
182 fail; [ -n "$_ERRORS" ] && echo $_ERRORS
189 # compatibility functions
194 run_cmd "$STRING" "$*"
197 # A function to start a program (now it's usefull on read-only filesystem too)
203 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
206 '') msg_Usage " daemon [+/-nicelevel] {program}\n"
208 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
212 # make sure it doesn't core dump anywhere; while this could mask
213 # problems with the daemon, it also closes some security problems
218 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
223 [ -n "$_ERRORS" ] && echo $_ERRORS
229 # A function to stop a program.
234 msg_Usage " killproc {program} [signal]\n"
241 # check for second arg to be kill level
242 if [ "$2" != "" ] ; then
253 pid=`pidofproc $base`
256 if [ "$pid" != "" ] ; then
257 if [ "$notset" = "1" ] ; then
258 if ps h $pid>/dev/null 2>&1; then
259 # TERM first, then KILL if not dead
260 kill -TERM $pid >/dev/null 2>&1
262 if ps h $pid >/dev/null 2>&1 ; then
264 if ps h $pid >/dev/null 2>&1 ; then
266 if ps h $pid >/dev/null 2>&1 ; then
267 kill -KILL $pid >/dev/null 2>&1
271 ps h $pid >/dev/null 2>&1 && fail || ok
275 # use specified level only
277 if ps h $pid >/dev/null 2>&1; then
278 kill $killlevel $pid && ok || fail
287 # Remove pid file if any.
288 if [ "$notset" = "1" ]; then
289 rm -f /var/run/$base.pid
293 # A function to find the pid of a program.
298 msg_Usage " pidofproc {program}\n"
302 # First try "/var/run/*.pid" files
303 if [ -f /var/run/$1.pid ] ; then
304 pid=`head -1 /var/run/$1.pid`
305 if [ "$pid" != "" ] ; then
313 if [ "$pid" != "" ] ; then
318 # Finally try to extract it from ps
319 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
320 { if ((prog == $5) || (("(" prog ")") == $5) ||
321 (("[" prog "]") == $5) ||
322 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
329 msg_Usage " status {program}\n"
335 if [ "$pid" != "" ] ; then
336 nls "%s (pid %s) is running...\n" "$1" "$pid"
339 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
340 { if ((prog == $5) || (("(" prog ")") == $5) ||
341 (("[" prog "]") == $5) ||
342 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
343 if [ "$pid" != "" ] ; then
344 nls "%s (pid %s) is running...\n" "$1" "$pid"
349 # Next try "/var/run/*.pid" files
350 if [ -f /var/run/$1.pid ] ; then
351 pid=`head -1 /var/run/$1.pid`
352 if [ "$pid" != "" ] ; then
353 nls "%s dead but pid file exists\n" "$1"
357 # See if /var/lock/subsys/$1 exists
358 if [ -f /var/lock/subsys/$1 ]; then
359 nls "%s dead but subsys locked\n" "$1"
362 nls "%s is stopped\n" "$1"
366 # Confirm whether we really want to run this service
368 echo -n "`nls "Start service"` $1 `nls "(Y)es/(N)o/(C)ontinue? [Y] "`"
391 msg_Usage " is_yes {value}\n"
399 [ "$1" = "true" ] ||\
400 [ "$1" = "True" ] ||\
401 [ "$1" = "TRUE" ] ||\
413 msg_Usage " is_no {value}\n"
421 [ "$1" = "false" ] ||\
422 [ "$1" = "False" ] ||\
423 [ "$1" = "FALSE" ] ||\