1 # functions This file contains functions to be used by most or all
2 # shell scripts in the /etc/init.d directory.
6 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by: Greg Galloway and Marc Ewing
9 # Marek Obuchowicz <elephant@pld-linux.org>
10 # Arkadiusz Mi¶kiewicz <misiek@pld-linux.org>
11 # Micha³ Kochanowicz <mkochano@pld-linux.org>
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 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
22 [ -z "$COLUMNS" ] && COLUMNS=80
28 msg_usage " is_yes {value}"
34 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
49 msg_usage " is_no {value}"
54 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
65 if is_yes "$FASTRC"; then
66 INIT_DOTS=$(awk "BEGIN{for(\$i=0;\$i<$INIT_COL;\$i++)printf(\".\");}")
69 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
83 awk '{print $3}' /proc/version | awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}'
88 awk '{print $3}' /proc/version | awk -F. '{print sprintf("%03d%03d",$1,$2)}'
93 awk '{print $3}' /proc/version | awk -F. '{print sprintf("%03d",$1)}'
99 if is_yes "$FASTRC" || [ ! -d /usr/share/terminfo ] || \
100 [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
103 echo -ne "\033[$(($2+1))G"
106 echo -ne "\033[${2}A"
112 is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
119 # check if we are on proper terminal
120 tput longname > /dev/null 2>&1 || return
127 is_yes "$COLOR_INIT" && tput "$@"
142 if [ $# -gt 0 ]; then
145 while [ $# -gt 0 ]; do
150 awk "BEGIN {printf \"$text\", \"$m\"; }"
153 # National language support function
156 typeset msg_echo old_nls_domain text message
158 old_nls_domain="$NLS_DOMAIN"
160 # don't use -o instead || here - this will break ksh --misiek
161 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
176 # empty message, so we return --misiek
177 if [ -z "$message" ]; then
178 NLS_DOMAIN="$old_nls_domain"
183 if is_yes "$FASTRC"; then
184 printf "$message" "$@"
185 elif [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
186 text=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${NLS_DOMAIN:-rc-scripts}" "$message")
189 printf_ "$message" "$@"
193 NLS_DOMAIN="$old_nls_domain"
198 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
203 show "Starting %s service" "$1"
206 msg_already_running()
208 nls "%s service is already running." "$1"
213 show "Stopping %s service" "$1"
218 nls "%s service is not running." "$1"
223 show "Reloading %s service" "$1"
231 # Some functions to handle PLD-style messages
236 if is_yes "$FASTRC"; then
240 termput hpa $INIT_COL
244 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
248 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
249 # First is the text to display, second is color number to use (argument to
250 # tput setaf). If second argument is not given, default (2, green) will be
255 if [ -n "$2" ]; then COLOR="$2"; else COLOR="2"; fi
257 echo -n "$(termput setaf 6)[$(termput setaf "$COLOR") $(nls --nls-domain rc-scripts "$1") $(termput setaf 6)]$(termput op)"
293 termput hpa $INIT_COL
296 # Check if $pid (could be plural) are running
300 [ -d "/proc/$1" ] && return 0
306 # Usage run_cmd Message command_to_run
309 typeset exit_code errors message force_err
310 typeset -i force_err=0
311 typeset -i exit_code=0
319 show "$message"; busy
321 if errors=$(HOME=/tmp TMPDIR=/tmp initlog -c "$*" 2>&1); then
323 log_success "$1 $message"
326 log_failed "$1 $message"
329 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
333 # A function to start a program (now it's usefull on read-only filesystem too)
336 typeset errors="" prog=""
337 typeset -i exit_code=0
338 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
339 # Test syntax. Don't use -o instead || here - this will broke ksh --misiek
340 while [ "$1" != "${1##-}" ] || [ "$1" != "${1##+}" ]; do
343 msg_usage " daemon [--user user] [+/-nicelevel] {program}"
347 # for compatibility with redhat/mandrake
348 nls "warning: --check option is ignored!"
354 [ "$1" != "root" ] && prog="/bin/su $1 -c \""
357 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
361 # If command to execute ends with quotation mark, add remaining
362 # arguments and close quotation.
363 if [ "$prog" != "${prog%\"}" ]; then
369 # make sure it doesn't core dump anywhere; while this could mask
370 # problems with the daemon, it also closes some security problems
373 # Make sure daemons will be able to fork.
374 # Limits are set via pam_limits.
375 if [ -n "$KSH_VERSION" ]; then
377 elif [ -n "$ZSH_VERSION" ]; then
379 elif [ -n "$BASH_VERSION" ]; then
381 # elif [ -n "`$SH -c 'echo ${.sh.version}' 2>/dev/null`" ]; then
386 if errors=$(USER=root HOME=/tmp TMPDIR=/tmp nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$prog" 2>&1); then
387 log_success "$1 startup"
392 log_failed "$1 startup"
393 [ -n "$errors" ] && echo "$errors"
398 # A function to stop a program.
401 typeset notset killlevel base pid result
404 msg_usage " killproc {program} [signal]"
411 # check for second arg to be kill level
412 if [ -n "$2" ] ; then
420 base=$(basename "$1")
423 pid=$(pidofproc "$1")
424 [ -z "$pid" ] && pid=$(pidofproc "$base")
427 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
428 if [ "$notset" = "1" ] ; then
429 if checkpid $pid 2>&1; then
430 # TERM first, then KILL if not dead
433 if checkpid $pid && sleep 1 &&
434 checkpid $pid && sleep 3 &&
442 if [ "$result" -eq 0 ]; then
444 log_failed "$1 shutdown"
447 log_success "$1 shutdown"
449 result=$(( ! $result ))
451 # use specified level only
452 if checkpid $pid > /dev/null 2>&1; then
455 if [ "$result" -eq 0 ]; then
457 log_success "$1 got $killlevel"
461 log_failed "$1 didn't get $killlevel"
466 log_failed "$1 shutdown"
471 log_failed "$1 shutdown"
475 # Remove pid file if any.
476 if [ "$notset" = "1" ]; then
477 rm -f /var/run/${base}.pid
483 # A function to find the pid of a program.
487 base=$(basename "$1")
491 msg_usage " pidofproc {program}"
495 # First try "/var/run/*.pid" files
496 if [ -f /var/run/${base}.pid ] ; then
499 read line < /var/run/${base}.pid
501 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,""); print $0;}')" ] && pid="$pid $p"
503 if [ -n "$pid" ]; then
510 pidof -o $$ -o $PPID -o %PPID -x "$1"
515 typeset base pid subsys daemon
518 base=$(basename $daemon)
522 msg_usage " status {subsys} [{daemon}]"
527 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
529 if [ "$pid" != "" ]; then
530 nls "%s (pid %s) is running..." "$daemon" "$pid"
533 # pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
534 # { if ((prog == $5) || (("(" prog ")") == $5) ||
535 # (("[" prog "]") == $5) ||
536 # ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
537 # if [ "$pid" != "" ]; then
538 # nls "%s (pid %s) is running..." "$daemon" "$pid"
543 # Next try "/var/run/*.pid" files
544 if [ -f /var/run/${base}.pid ]; then
545 read pid < /var/run/${base}.pid
546 if [ "$pid" != "" ]; then
547 nls "%s dead but pid file exists" "$subsys"
552 # See if /var/lock/subsys/$subsys exists
553 if [ -f /var/lock/subsys/$subsys ]; then
554 nls "%s dead but subsys locked" "$subsys"
557 nls "%s is stopped" "$subsys"
561 # Confirm whether we really want to run this service
564 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
583 # module is needed (ie. is requested, is available and isn't loaded already)
586 # module name without .o at end
587 if ! (lsmod | grep -q "$1"); then
588 if (ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"); then
599 typeset parsed single die args foo result
601 while is_no "$parsed" ; do
620 if is_yes "${single}" ; then
622 show "Loading %s kernel module(s)" "$foo"
625 if [ -x /sbin/modprobe ] ; then
626 /sbin/modprobe -s $args "$@"
632 if is_yes "${single}" ; then
634 if [ $result == "0" ] ; then
635 is_yes "$single" && ok
638 if is_yes "$die" ; then
639 nls "Could not load %s kernel module(s)" "$@"
648 initlog -n $0 -s "$1 $2" -e 1
653 initlog -n $0 -s "$1 $2" -e 2
656 # RedHat/Mandrake specific functions
657 action () { STRING=$1; shift; run_cmd "$STRING" "$*"; }
658 success () { return 0; }
659 failure () { return 1; }
661 # TO BE REMOVED IN AC. --ankry
662 msg_Network_Down () { msg_network_down "$*"; }
663 msg_Already_Running () { msg_already_running "$*"; }
664 msg_Not_Running () { msg_not_running "$*"; }
665 msg_Usage () { msg_usage "$*"; }
670 # * indent-tabs-mode: notnil
674 # vi: syntax=sh:shiftwidth=8: