]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/init.d/functions
- calculate default INIT_COL from terminal size
[projects/rc-scripts.git] / rc.d / init.d / functions
1 #!/bin/sh - keep it for file(1) to get bourne shell script result
2 # functions     This file contains functions to be used by most or all
3 #               shell scripts in the /etc/init.d directory.
4 #
5 # $Id$
6 #
7 # Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
8 # Hacked by:    Greg Galloway and Marc Ewing
9 # Modified for PLD Linux by:
10 #               Marek Obuchowicz <elephant@pld-linux.org>
11 #               Arkadiusz Miśkiewicz <misiek@pld-linux.org>
12 #               Michał Kochanowicz <mkochano@pld-linux.org>
13 #               Łukasz Pawelczyk <havner@pld-linux.org>
14
15 # First set up a default search path.
16 export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
17
18 # Set defaults
19 if [ -z "$COLUMNS" -o -z "$LINES" ]; then
20         _setterm() {
21                 set -- $(stty size 2>/dev/null)
22                 LINES=${LINES:-$1}
23                 COLUMNS=${COLUMNS:-$2}
24         }
25         _setterm
26         unset _setterm
27 fi
28 [ -z "$COLUMNS" ] && COLUMNS=80
29 export LINES COLUMNS
30 INIT_COL=$((COLUMNS - 13))
31
32 # Set colors
33 RED=1
34 GREEN=2
35 YELLOW=3
36 BLUE=4
37 MAGENTA=5
38 CYAN=6
39 WHITE=7
40 NORMAL=9
41 # Bold definition (second parameter to termput setaf)
42 BOLD=1
43 NOBOLD=0
44 # Default colors
45 CBRACKETS="$CYAN"       # brackets [ ] color
46 CDONE="$GREEN"          # DONE and WORK color
47 CBUSY="$MAGENTA"        # BUSY color
48 CFAIL="$RED"            # FAIL and DIED color
49 CPOWEREDBY="$CYAN"      # "Powered by" color
50 CPLD="$GREEN"           # "PLD Linux Distribution" color
51 CI="$RED"               # Capital I color (press I to enter interactive startup)
52 CRESMAN="$GREEN"        # "Resource Manager" color
53 CHARS=""                # Characters displayed on the beginning of show line
54 CCHARS="$NORMAL"        # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
55
56 # Source configuration if available - may override default values
57 [ -r /etc/sysconfig/init-colors ] && . /etc/sysconfig/init-colors
58 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
59 [ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
60
61 if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
62         {
63                 while read _f _ctx; do
64                         [ "$_f" = "VxID:" -o "$_f" = "s_context:" ] && break
65                 done </proc/self/status
66         } 2>/dev/null
67         if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
68                 VSERVER=no
69         else
70                 VSERVER=yes
71         fi
72         unset _f _ctx
73 fi
74
75 # we need to know in functions if we were called from a terminal
76 if [ -z "$ISATTY" ]; then
77         [ -t ] && ISATTY=yes || ISATTY=no
78         export ISATTY
79 fi
80
81 is_yes() {
82         # Test syntax
83         if [ $# = 0 ] ; then
84                 msg_usage " is_yes {value}"
85                 return 2
86         fi
87
88         # Check value
89         case "$1" in
90         yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
91                 # true returns zero
92                 return 0
93                 ;;
94         *)
95                 # false returns one
96                 return 1
97                 ;;
98         esac
99 }
100
101 is_no() {
102         # Test syntax
103         if [ $# = 0 ] ; then
104                 msg_usage " is_no {value}"
105                 return 2
106         fi
107
108         case "$1" in
109         no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
110                 # true returns zero
111                 return 0
112                 ;;
113         *)
114                 # false returns one
115                 return 1
116                 ;;
117         esac
118 }
119
120 # checks if file is empty
121 # empty lines and lines beginning with hash are ignored
122 is_empty_file() {
123         [ -s "$1" ] || return 0
124         egrep -vq "^(#|[[:blank:]]*$)" "$1" && return 1 || return 0
125 }
126
127 # returns OK if $1 contains $2
128 strstr() {
129         local a=$2
130         [ "${1#*$a*}" = "$1" ] && return 1
131         return 0
132 }
133
134 if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
135         RC_LOGGING=no
136 fi
137
138 if is_no "$RC_LOGGING"; then
139         initlog() {
140                 RESULT=0
141                 while [ "$1" != "${1##-}" ]; do
142                         case $1 in
143                         -c)
144                                 shift
145                                 $1
146                                 RESULT=$?
147                                 break
148                                 ;;
149                         *)
150                                 shift
151                                 ;;
152                         esac
153                 done
154                 return $RESULT
155         }
156 fi
157
158 kernelver() {
159         typeset _x _y _z v old_IFS ver
160         {
161                 read _x _y v _z
162                 old_IFS=$IFS
163                 IFS='.'
164                 set -- $v
165                 IFS=$old_IFS
166
167                 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
168                 ver=${3%%[-_]*}
169
170                 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
171                 ver="$2$ver"
172                 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
173                 ver="$1$ver"
174                 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
175                 echo $ver
176         } < /proc/version
177 }
178
179 kernelverser() {
180         typeset _x _y _z v old_IFS ver
181         {
182                 read _x _y v _z
183                 old_IFS=$IFS
184                 IFS='.'
185                 set -- $v
186                 IFS=$old_IFS
187                 ver=$2
188                 while [ ${#ver} -lt 3 ] ; do ver="0$ver" ; done
189                 ver="$1$ver"
190                 while [ ${#ver} -lt 6 ] ; do ver="0$ver" ; done
191                 echo $ver
192         } </proc/version
193 }
194
195 kernelvermser() {
196         typeset _x _y _z v old_IFS ver
197         {
198                 read _x _y v _z
199                 old_IFS=$IFS
200                 IFS='.'
201                 set -- $v
202                 IFS=$old_IFS
203                 ver="$1$ver"
204                 while [ ${#ver} -lt 3 ] ; do ver="0$ver" ; done
205                 echo $ver
206         } </proc/version
207 }
208
209 # Colors workaround
210 termput() {
211         is_yes "$ISATTY" || return
212
213         if is_yes "$FASTRC" || is_no "$TPUT"; then
214                 case "$1" in
215                 hpa)
216                         echo -ne "\033[$(($2+1))G"
217                         ;;
218                 cuu*)
219                         echo -ne "\033[${2}A"
220                         ;;
221                 el)
222                         echo -ne "\033[0K"
223                         ;;
224                 setaf)
225                         typeset ISBOLD
226                         if [ -n "$3" ]; then
227                                 ISBOLD="$3"
228                         else
229                                 ISBOLD="$NOBOLD";
230                         fi
231                         is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
232                         ;;
233                 op)
234                         termput setaf $NORMAL
235                         ;;
236                 esac
237         else
238                 case "$1" in
239                 hpa | cuu* | el)
240                         tput "$@"
241                         ;;
242                 setaf)
243                         if [ "$3" == "1" ]; then tput bold; else tput sgr0; fi
244                         is_yes "$COLOR_INIT" && tput setaf "$2"
245                         ;;
246                 op)
247                         termput setaf $NORMAL
248                         ;;
249                 esac
250         fi
251 }
252
253 if [ ! -x /bin/printf ]; then
254         # printf equivalent
255         # FIXME: buggy when single or double quotes in message!
256         printf() {
257                 typeset text m
258                 text="$1"
259                 shift
260                 if [ $# -gt 0 ]; then
261                         m="$1"
262                         shift
263                         while [ $# -gt 0 ]; do
264                                 m="$m\",\"$1"
265                                 shift
266                         done
267                 fi
268                 awk "BEGIN {printf \"$text\", \"$m\"; }"
269         }
270 fi
271
272 # National language support function
273 nls() {
274         typeset msg_echo nls_domain text message
275         msg_echo='\n'
276         nls_domain="$NLS_DOMAIN"
277         while [ "$1" != "${1##-}" ]; do
278                 case "$1" in
279                 --nls-domain)
280                         shift
281                         nls_domain="$1"
282                         shift
283                         ;;
284                 -n)
285                         msg_echo=''
286                         shift
287                         ;;
288                 esac
289         done
290         message="$1"
291         shift
292
293         # empty message, so we return --misiek
294         if [ -z "$message" ]; then
295                 echo -en "$msg_echo"
296                 return
297         fi
298
299         if is_yes "$GETTEXT"; then
300                 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
301         fi
302
303         printf "$message" "$@"
304         echo -en "$msg_echo"
305 }
306
307 rc_splash() {
308         typeset action
309
310         action="$1"
311
312         if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
313                 [ -x /bin/splash ] && /bin/splash "$action"
314         fi
315
316         : $((progress++))
317 }
318
319 msg_network_down() {
320         nls "ERROR: Networking is down. %s can't be run." "$1" >&2
321 }
322
323 msg_starting() {
324         show "Starting %s service" "$1"
325 }
326
327 msg_already_running() {
328         nls "%s service is already running." "$1"
329 }
330
331 msg_stopping() {
332         show "Stopping %s service" "$1"
333 }
334
335 msg_not_running() {
336         nls "%s service is not running." "$1"
337 }
338
339 msg_reloading() {
340         show "Reloading %s service" "$1"
341 }
342
343 msg_usage() {
344         nls "Usage: %s" "$*"
345 }
346
347 # Some functions to handle PLD Linux-style messages
348 show() {
349         typeset text
350         typeset -i len
351
352         if is_no "$FASTRC" && is_yes "$GETTEXT"; then
353                 text=$(nls -n "$@")
354         else
355                 text=$(printf "$@")
356         fi
357         len=${#text}
358         while [ $((len++)) -lt $INIT_COL ]; do
359                 text="$text."
360         done
361         if [ -n "$CHARS" ]; then
362                 termput setaf $CCHARS
363                 echo -n "$CHARS"
364                 termput op
365         fi
366         echo -n "$text"
367 }
368
369 deltext() {
370         termput hpa $INIT_COL
371 }
372
373 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
374 # First is the text to display, second is color number to use (argument to
375 # tput setaf). If second argument is not given, default (2, green) will be
376 # used).
377 progress() {
378         typeset COLOR
379         if [ -n "$2" ]; then COLOR="$2"; else COLOR="$CDONE"; fi
380         deltext
381         echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
382 }
383
384 busy() {
385         echo -n "$_busy"
386 }
387
388 ok() {
389         echo "$_ok"
390 }
391
392 started() {
393         echo "$_started"
394 }
395
396 fail() {
397         echo "$_fail"
398         return 1
399 }
400
401 died() {
402         echo "$_died"
403         return 1
404 }
405
406 # Check if $pid (could be plural) are running
407 checkpid() {
408         while [ "$1" ]; do
409                 [ -d "/proc/$1" ] && return 0
410                 shift
411         done
412         return 1
413 }
414
415 # - outside chroot get only those processes, which are outside chroot.
416 # - inside chroot get only those processes, which are inside chroot.
417 # - don't filter out pids which do not have corresponding running processes (process died etc)
418 # (note: some processes like named are chrooted but run outside chroot)
419 # - do nothing inside vserver
420 filter_chroot() {
421         if is_yes "$VSERVER"; then
422                 echo $@
423                 return
424         fi
425         if [ $# -lt 1 -o ! -d /proc/1 ] ; then
426                 echo $@
427                 return
428         fi
429         local root_dir good_pids="" good_add_pid
430         for root_pid in $@; do
431                 root_dir=$(resolvesymlink /proc/${root_pid}/root)
432                 if [ -n "$root_dir" ]; then
433                         good_add_pid=1
434                         if [ -n "${SYSTEM_CHROOTS}" ]; then
435                                 for r_dir in ${SYSTEM_CHROOTS}; do
436                                         echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
437                                 done
438                         fi
439                         [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
440                 elif [ ! -d "/proc/$root_pid" ]; then
441                         good_pids="$good_pids $root_pid"
442                 fi
443         done
444         echo $good_pids
445 }
446
447 # Usage run_cmd Message command_to_run
448 run_cmd() {
449         typeset exit_code errors message force_err
450         typeset -i force_err=0
451         typeset -i exit_code=0
452         case "$1" in
453         -a)
454                 force_err=1
455                 shift
456                 ;;
457         esac
458         message=$1
459         show "$message"; busy
460         shift
461         cd /
462         if errors=$(
463                 export HOME=/tmp TMPDIR=/tmp
464                 if is_no "$RC_LOGGING"; then
465                         "$@" 2>&1
466                 else
467                         initlog -c "$*" 2>&1
468                 fi
469                 ); then
470                 ok
471                 log_success "$1 $message"
472         else
473                 fail
474                 log_failed "$1 $message"
475                 exit_code=1
476         fi
477         [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
478         return $exit_code
479 }
480
481 _daemon_set_ulimits() {
482         local opt val ksh=${KSH_VERSION:+1}
483         set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
484         while [ $# -gt 0 ]; do
485                 opt=$1
486                 val=$2
487                 if [ "$ksh" ]; then
488                         case "$opt" in
489                         -Hu)
490                                 opt=-Hp
491                         ;;
492                         -Su)
493                                 opt=-Sp
494                         ;;
495                         -u)
496                                 opt=-p
497                         ;;
498                         esac
499                 fi
500                 ulimit $opt $val
501                 shift 2
502         done
503 }
504
505 # A function to start a program (now it's useful on read-only filesystem too)
506 daemon() {
507         typeset errors="" prog="" end="" waitname="" waittime=""
508         typeset -i exit_code=0
509         local nice=$SERVICE_RUN_NICE_LEVEL
510         local fork user closefds pidfile makepid chdir=/
511
512         while [ $# -gt 0 ]; do
513                 case $1 in
514                 '')
515                 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
516                         return 2
517                         ;;
518                 --check)
519                         # for compatibility with redhat/mandrake
520                         nls "warning: --check option is ignored!"
521                         shift
522                         ;;
523                 --user)
524                         shift
525                         [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
526                         user=$1
527                         ;;
528                 --fork)
529                         fork=1
530                         prog="/usr/bin/setsid sh -c \""
531                         end='&'
532                         ;;
533                 --chdir)
534                         shift
535                         chdir=$1
536                         ;;
537                 --closefds)
538                         closefds=1
539                         ;;
540                 --waitforname)
541                         shift
542                         waitname="$1"
543                         ;;
544                 --waitfortime)
545                         shift
546                         waittime="$1"
547                         ;;
548                 --pidfile)
549                         shift
550                         pidfile="$1"
551                         ;;
552                 --makepid)
553                         makepid=1
554                         ;;
555                 -*|+*)
556                         nice=$1
557                         shift
558                         break
559                         ;;
560                 *)
561                         break
562                         ;;
563                 esac
564                 shift
565         done
566         # If command to execute ends with quotation mark, add remaining
567         # arguments and close quotation.
568         if [ "$prog" != "${prog%\"}" ]; then
569                 prog="$prog $*$end\""
570         else
571                 prog="$prog $*$end"
572         fi
573
574         _daemon_set_ulimits
575
576         [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
577         [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
578
579         # And start it up.
580         busy
581         cd $chdir
582         [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
583         if errors=$(
584                 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
585                 export USER=root HOME=/tmp TMPDIR=/tmp
586                 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
587                 nice=${nice:-0}
588
589                 if [ "$closefds" = 1 ]; then
590                         exec 1>&-
591                         exec 2>&-
592                         exec 0>&-
593                 else
594                         exec 2>&1
595                 fi
596
597                 if is_no "$RC_LOGGING" ; then
598                         prog=$1; shift
599                         if [ ! -x $prog ]; then
600                                 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
601                                 local a o=$IFS
602                                 IFS=:
603                                 for a in $PATH; do
604                                         if [ -x $a/$prog ]; then
605                                                 prog=$a/$prog
606                                                 break
607                                         fi
608                                 done
609                                 IFS=$o
610                         fi
611                         /sbin/start-stop-daemon -q --start \
612                                 --nicelevel $nice \
613                                 ${pidfile:+--pidfile $pidfile} \
614                                 ${user:+-c $user} \
615                                 ${chdir:+--chdir "$chdir"} \
616                                 ${fork:+-b} \
617                                 ${makepid:+-m} \
618                                 --exec "$prog" \
619                                 -- ${1:+"$@"}
620                 else
621                         nice -n $nice initlog -c "$prog" 2>&1
622                 fi
623                 ); then
624
625                 if [ -n "$waitname" -a -n "$waittime" ]; then
626                         # Save basename.
627                         base=${waitname##*/}
628                         # Find pid.
629                         pid=$(pidofproc "$waitname" "$pidfile")
630                         [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
631                         i=0
632                         while [ "$i" -lt "$waittime" ]; do
633                                 i=$((i + 1))
634                                 checkpid $pid && sleep 1 || break
635                         done
636                 fi
637                 log_success "$1 startup"
638                 ok
639         else
640                 exit_code=1
641                 fail
642                 log_failed "$1 startup"
643                 [ -n "$errors" ] && echo "$errors"
644         fi
645         return $exit_code
646 }
647
648 # A function to stop a program.
649 killproc() {
650         typeset notset killlevel base pid pidfile result
651         # Test syntax.
652         if [ $# = 0 ]; then
653                 msg_usage " killproc [--pidfile PIDFILE] {program} [-SIGNAME]"
654                 return 2
655         fi
656
657         while [ "$1" != "${1##-}" ]; do
658                 case $1 in
659                 --pidfile)
660                         pidfile="$2"
661                         shift 2
662                         ;;
663                 --waitforname)
664                         waitname="$2"
665                         shift 2
666                         ;;
667                 --waitfortime)
668                         waittime="$2"
669                         shift 2
670                         ;;
671                 esac
672         done
673
674         busy
675
676         typeset -i notset=0
677         # check for second arg to be kill level
678         if [ -n "$2" ] ; then
679                 killlevel=$2
680         else
681                 notset=1
682                 killlevel="-KILL"
683         fi
684
685         # experimental start-stop-daemon based killing.
686         # works only with pidfile
687         if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
688                 local sig=${killlevel:--TERM}
689                 /sbin/start-stop-daemon --stop \
690                         --retry ${sig#-}/10/${sig#-}/60/KILL/10 \
691                         -s ${sig#-} \
692                         ${pidfile:+--pidfile $pidfile}
693                 result=$?
694                 if [ "$result" -eq 0 ]; then
695                         ok
696                 else
697                         fail
698                 fi
699                 return $result
700         fi
701
702
703         # Save basename.
704         base=${1##*/}
705
706         # Find pid.
707         pid=$(pidofproc "$1" "$pidfile")
708         [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
709
710         # Kill it.
711         if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
712                 if [ "$notset" = "1" ] ; then
713                         if checkpid $pid 2>&1; then
714                                 # TERM first, then KILL if not dead
715                                 kill -TERM $pid
716                                 usleep 100000
717                                 if checkpid $pid && sleep 1 &&
718                                         checkpid $pid && sleep 3 &&
719                                         checkpid $pid; then
720                                         # XXX: SIGKILL is sent already on 4th second!
721                                         # HARMFUL for example to mysqld (which is already workarounded)
722                                         kill -KILL $pid
723                                         usleep 100000
724                                 fi
725                         fi
726                         checkpid $pid
727                         result=$?
728                         if [ "$result" -eq 0 ]; then
729                                 fail
730                                 log_failed "$1 shutdown"
731                         else
732                                 ok
733                                 log_success "$1 shutdown"
734                         fi
735                         result=$(( ! $result ))
736                 else
737                         # use specified level only
738                         if checkpid $pid > /dev/null 2>&1; then
739                                 kill $killlevel $pid
740                                 result=$?
741                                 if [ "$result" -eq 0 ]; then
742                                         ok
743                                         log_success "$1 got $killlevel"
744                                 else
745                                         result=7
746                                         fail
747                                         log_failed "$1 didn't get $killlevel"
748                                 fi
749                         else
750                                 result=7
751                                 died
752                                 log_failed "$1 shutdown"
753                         fi
754                 fi
755         else
756                 died
757                 log_failed "$1 shutdown"
758                 result=7
759         fi
760
761         if [ -n "$waitname" -a -n "$waittime" ]; then
762                 # Save basename.
763                 base=${waitname##*/}
764                 # Find pid.
765                 pid=$(pidofproc "$waitname" "$pidfile")
766                 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
767                 i=0
768                 while [ "$i" -lt "$waittime" ]; do
769                         i=$(( i + 1 ))
770                         checkpid $pid && sleep 1 || break
771                 done
772         fi
773
774         # Remove pid file if any.
775         if [ "$notset" = "1" ]; then
776                 rm -f /var/run/${base}.pid
777         fi
778
779         return $result
780 }
781
782 # A function to find the pid of a program.
783 pidofproc() {
784         typeset pid pidfile base=${1##*/}
785         pidfile="$base.pid"
786         [ -n "$2" ] && pidfile="$2"
787
788         # Test syntax.
789         if [ $# = 0 ] ; then
790                 msg_usage " pidofproc {program}"
791                 return 2
792         fi
793
794         # First try pidfile or "/var/run/*.pid"
795         if [[ "$pidfile" = /* ]]; then
796                 pidfile="${pidfile}"
797         else
798                 pidfile="/var/run/${pidfile}";
799         fi
800         if [ -f "${pidfile}" ] ; then
801                 typeset p pid
802                 pid=
803                 for p in $(< "${pidfile}"); do
804                         [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
805                 done
806         fi
807
808         # Next try "pidof"
809         [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
810         pid=$(filter_chroot "$pid")
811         echo $pid
812 }
813
814 status() {
815         typeset pid subsys daemon cpuset_msg pidfile
816         if [ "$1" = "--pidfile" ]; then
817                 pidfile=$2
818                 shift 2
819         fi
820
821         subsys=$1
822         daemon=${2:-$subsys}
823
824         # Test syntax.
825         if [ $# = 0 ] ; then
826                 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
827                 return 2
828         fi
829
830         # if pidfile specified, pid must be there
831         if [ "$pidfile" -a -f "$pidfile" ]; then
832                 read pid < $pidfile
833         else
834                 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
835         fi
836         pid=$(filter_chroot "$pid")
837
838         if [ "$pid" ]; then
839                 cpuset_msg="..."
840                 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" ]; then
841                         if $(grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"); then
842                                 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
843                         else
844                                 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
845                         fi
846                 fi
847                 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
848                 return 0
849         fi
850
851         # Next try "/var/run/*.pid" files
852         local base=${daemon##*/}
853         if [ -f /var/run/${base}.pid ]; then
854                 read pid < /var/run/${base}.pid
855                 pid=$(filter_chroot "$pid")
856                 if [ "$pid" != "" ]; then
857                         nls "%s dead but pid file exists" "$subsys"
858                         return 1
859                 fi
860         fi
861
862         # See if /var/lock/subsys/$subsys exists
863         if [ -f /var/lock/subsys/$subsys ]; then
864                 nls "%s dead but subsys locked" "$subsys"
865                 return 2
866         fi
867         nls "%s is stopped" "$subsys"
868         return 3
869 }
870
871 # Confirm whether we really want to run this service
872 confirm() {
873         typeset answer
874         nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
875         read answer
876         case $answer in
877         y|Y|t|T|j|J|"")
878                 return 0
879                 ;;
880         c|C|k|K|w|W)
881                 return 2
882                 ;;
883         n|N)
884                 return 1
885                 ;;
886         *)
887                 confirm $1
888                 return $?
889                 ;;
890         esac
891 }
892
893 # module is needed (ie. is requested, is available and isn't loaded already)
894 is_module() {
895         # module name without .o at end
896         if ! lsmod | grep -q "$1"; then
897                 if ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"; then
898                         # true
899                         return 0
900                 fi
901         fi
902         # false
903         return 1
904 }
905
906 _modprobe() {
907         typeset parsed single die args foo result
908         parsed=no
909         while is_no "$parsed" ; do
910                 case "$1" in
911                 "single")
912                         single=yes
913                         shift
914                         ;;
915                 "die")
916                         die=yes
917                         shift
918                         ;;
919                 -*)
920                         args="$args $1"
921                         shift
922                         ;;
923                 *)
924                         parsed=yes
925                         ;;
926                 esac
927         done
928         if is_yes "${single}" ; then
929                 foo="$@"
930                 show "Loading %s kernel module(s)" "$foo"
931                 busy
932         fi
933         if [ -x /sbin/modprobe ] ; then
934                 /sbin/modprobe -s $args "$@"
935                 result=$?
936         else
937                 deltext ; fail
938                 result=1
939         fi
940         if is_yes "${single}" ; then
941                 deltext
942                 if [ $result == "0" ] ; then
943                         is_yes "$single" && ok
944                 else
945                         fail
946                         if is_yes "$die" ; then
947                                 nls "Could not load %s kernel module(s)" "$@"
948                                 exit 1
949                         fi
950                 fi
951         fi
952 }
953
954 if is_no "$RC_LOGGING"; then
955         log_success() {
956                 :
957         }
958
959         log_failed() {
960                 :
961         }
962 else
963         log_success() {
964                 initlog -n $0 -s "$1 $2" -e 1
965         }
966
967         log_failed() {
968                 initlog -n $0 -s "$1 $2" -e 2
969         }
970 fi
971
972 # Check if any flavor of portmapper is running
973 check_portmapper() {
974         typeset RPCINFO
975
976         if [ -x /usr/sbin/rpcinfo ]; then
977                 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
978                         return 0
979                 else
980                         return 1
981                 fi
982         elif [ -x /sbin/pidof ]; then
983                 [ -z "$(/sbin/pidof portmap)" -a \
984                         -z "$(/sbin/pidof rpcbind)" ] && return 1
985         fi
986         return 0
987 }
988
989 rc_cache_init() {
990         # If we have cachefile, use it.
991         # If we don't, create memory variables and try to save silently,
992         local cachefile='/var/cache/rc-scripts/msg.cache'
993
994         local term
995         if is_yes "$ISATTY"; then
996                 term=$TERM
997         else
998                 term=dumb
999         fi
1000
1001         # We create $check variable which is used to invalidate the cache.
1002         # The $check contains user locale and terminal.
1003         local check="$term.$LC_MESSAGES.$INIT_COL"
1004
1005         if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1006                 if . "$cachefile" 2>/dev/null; then
1007                         if [ "$check" = "$_check" ]; then
1008                                 return
1009                         fi
1010                 fi
1011         fi
1012
1013         # primitive caching
1014         _busy=$(progress "BUSY" "$CBUSY")
1015         _ok=$(progress "DONE")
1016         _started=$(progress "WORK")
1017         _fail=$(progress "FAIL" "$CFAIL")
1018         _died=$(progress "DIED" "$CFAIL")
1019
1020         # we don't use heredoc, as ksh attempts to create tempfile then
1021         (> "$cachefile" ) 2>/dev/null || return
1022         echo "_busy='$_busy';" >> "$cachefile"
1023         echo "_ok='$_ok';" >> "$cachefile"
1024         echo "_started='$_started';" >> "$cachefile"
1025         echo "_fail='$_fail';" >> "$cachefile"
1026         echo "_died='$_died';" >> "$cachefile"
1027         echo "_check='$check';" >> "$cachefile"
1028 }
1029
1030 rc_gettext_init() {
1031         if [ -z "$GETTEXT" ]; then
1032                 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1033                         GETTEXT=yes
1034                 else
1035                         GETTEXT=no
1036                 fi
1037         fi
1038
1039         if [ -z "$TPUT" ]; then
1040                 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ] ; then
1041                         TPUT=yes
1042                         # check if we are on proper terminal
1043                         tput longname >/dev/null 2>&1 || TPUT=no
1044                 else
1045                         TPUT=no
1046                 fi
1047         fi
1048 }
1049
1050 rc_gettext_init
1051 rc_cache_init
1052
1053 #/*
1054 # * Local variables:
1055 # * mode: sh
1056 # * indent-tabs-mode: notnil
1057 # * End:
1058 # *
1059 # */
This page took 0.289324 seconds and 3 git commands to generate.