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.48 2000/04/08 21:41:31 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 [ "$COLOR_INIT" = "no" ]; then
28 elif [ ! -d /usr/share/terminfo ] || \
29 ! ( [ -x /usr/bin/tput ] || [ -x /bin/tput ] ) ; then
32 echo -ne "\033[$(($2+1))G"
41 echo -ne "\033[0;3${2}m"
62 awk "BEGIN {printf \"$text\", \"$m\"; }"
65 # National language support function
68 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
69 OLD_NLS_DOMAIN="$NLS_DOMAIN"
70 if [ "$1" = "--nls-domain" ]; then
75 if [ -z "$NLS_DOMAIN" ]; then
76 NLS_DOMAIN="rc-scripts"
79 # avoid translating empty text. --misiek
80 if [ -n "$MESSAGE" ]; then
81 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$MESSAGE"`"
88 NLS_DOMAIN="$OLD_NLS_DOMAIN"
96 nls "ERROR: Networking is down. %s can't be run.\n" "$1"
101 show "Starting %s service" "$1"
104 msg_Already_Running()
106 nls "%s service is already running.\n" "$1"
111 show "Stopping %s service" "$1"
116 nls "%s service is not running.\n" "$1"
121 show "Reloading %s service" "$1"
126 nls "Usage: %s" "$*\n"
129 # Some functions to handle PLD-style messages
132 what="`nls --nls-domain rc-scripts "DONE"`"; typeset -i offset=${#what}
135 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
140 termput hpa $INIT_COL
141 echo -n "`termput setaf 6`[`termput setaf 5` `nls --nls-domain rc-scripts "BUSY"` `termput setaf 6`]`termput setaf 7`"
146 termput hpa $INIT_COL
147 echo "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "DONE"` `termput setaf 6`]`termput setaf 7`"
152 termput hpa $INIT_COL
153 echo "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "WORK"` `termput setaf 6`]`termput setaf 7`"
158 termput hpa $INIT_COL
159 echo "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "FAIL"` `termput setaf 6`]`termput setaf 7`"
164 termput hpa $INIT_COL
165 echo "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "DIED"` `termput setaf 6`]`termput setaf 7`"
170 echo -ne "`nls "\b\b\b\b\b\b\b\b"`"
173 # Usage run_cmd Message command_to_run
179 show "$MESSAGE"; busy
181 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
184 deltext; fail; [ -n "$_ERRORS" ] && echo $_ERRORS
191 # compatibility functions
196 run_cmd "$STRING" "$*"
199 # A function to start a program (now it's usefull on read-only filesystem too)
205 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
208 '') msg_Usage " daemon [+/-nicelevel] {program}\n"
210 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
214 # make sure it doesn't core dump anywhere; while this could mask
215 # problems with the daemon, it also closes some security problems
220 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
227 [ -n "$_ERRORS" ] && echo $_ERRORS
233 # A function to stop a program.
238 msg_Usage " killproc {program} [signal]\n"
245 # check for second arg to be kill level
246 if [ "$2" != "" ] ; then
257 pid=`pidofproc $base`
260 if [ "$pid" != "" ] ; then
261 if [ "$notset" = "1" ] ; then
262 if ps h $pid>/dev/null 2>&1; then
263 # TERM first, then KILL if not dead
264 kill -TERM $pid >/dev/null 2>&1
266 if ps h $pid >/dev/null 2>&1 ; then
268 if ps h $pid >/dev/null 2>&1 ; then
270 if ps h $pid >/dev/null 2>&1 ; then
271 kill -KILL $pid >/dev/null 2>&1
275 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
279 # use specified level only
281 if ps h $pid >/dev/null 2>&1; then
282 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
292 # Remove pid file if any.
293 if [ "$notset" = "1" ]; then
294 rm -f /var/run/$base.pid
298 # A function to find the pid of a program.
303 msg_Usage " pidofproc {program}\n"
307 # First try "/var/run/*.pid" files
308 if [ -f /var/run/$1.pid ] ; then
309 pid=`head -1 /var/run/$1.pid`
310 if [ "$pid" != "" ] ; then
318 if [ "$pid" != "" ] ; then
323 # Finally try to extract it from ps
324 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
325 { if ((prog == $5) || (("(" prog ")") == $5) ||
326 (("[" prog "]") == $5) ||
327 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
334 msg_Usage " status {program}\n"
340 if [ "$pid" != "" ] ; then
341 nls "%s (pid %s) is running...\n" "$1" "$pid"
344 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
345 { if ((prog == $5) || (("(" prog ")") == $5) ||
346 (("[" prog "]") == $5) ||
347 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
348 if [ "$pid" != "" ] ; then
349 nls "%s (pid %s) is running...\n" "$1" "$pid"
354 # Next try "/var/run/*.pid" files
355 if [ -f /var/run/$1.pid ] ; then
356 pid=`head -1 /var/run/$1.pid`
357 if [ "$pid" != "" ] ; then
358 nls "%s dead but pid file exists\n" "$1"
362 # See if /var/lock/subsys/$1 exists
363 if [ -f /var/lock/subsys/$1 ]; then
364 nls "%s dead but subsys locked\n" "$1"
367 nls "%s is stopped\n" "$1"
371 # Confirm whether we really want to run this service
373 echo -n "`nls "Start service"` $1 `nls "(Y)es/(N)o/(C)ontinue? [Y] "`"
396 msg_Usage " is_yes {value}\n"
404 [ "$1" = "true" ] ||\
405 [ "$1" = "True" ] ||\
406 [ "$1" = "TRUE" ] ||\
418 msg_Usage " is_no {value}\n"
426 [ "$1" = "false" ] ||\
427 [ "$1" = "False" ] ||\
428 [ "$1" = "FALSE" ] ||\