]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/init.d/functions
- i don't think cat output is localized
[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"
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=15
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         grep -vqE "^(#|[[: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         local _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         local _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         local _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                         local 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                 local 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         local 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         local action="$1"
309
310         if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
311                 [ -x /bin/splash ] && /bin/splash "$action"
312         fi
313
314         : $((progress++))
315 }
316
317 msg_network_down() {
318         nls "ERROR: Networking is down. %s can't be run." "$1" >&2
319 }
320
321 msg_starting() {
322         show "Starting %s service" "$1"
323 }
324
325 msg_already_running() {
326         nls "%s service is already running." "$1"
327 }
328
329 msg_stopping() {
330         show "Stopping %s service" "$1"
331 }
332
333 msg_not_running() {
334         nls "%s service is not running." "$1"
335 }
336
337 msg_reloading() {
338         show "Reloading %s service" "$1"
339 }
340
341 msg_usage() {
342         nls "Usage: %s" "$*"
343 }
344
345 # Some functions to handle PLD Linux-style messages
346 show() {
347         local text len
348
349         if is_no "$FASTRC" && is_yes "$GETTEXT"; then
350                 text=$(nls -n "$@")
351         else
352                 text=$(printf "$@")
353         fi
354         len=${#text}
355         while [ $((len++)) -lt $INIT_COL ]; do
356                 text="$text."
357         done
358         if [ -n "$CHARS" ]; then
359                 termput setaf $CCHARS
360                 echo -n "$CHARS"
361                 termput op
362         fi
363         echo -n "$text"
364 }
365
366 deltext() {
367         termput hpa $INIT_COL
368 }
369
370 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
371 # First is the text to display, second is color number to use (argument to
372 # tput setaf). If second argument is not given, default (2, green) will be
373 # used).
374 progress() {
375         local COLOR
376         if [ -n "$2" ]; then
377                 COLOR="$2"
378         else
379                 COLOR="$CDONE"
380         fi
381         deltext
382         echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
383 }
384
385 busy() {
386         echo -n "$_busy"
387 }
388
389 ok() {
390         echo "$_ok"
391 }
392
393 started() {
394         echo "$_started"
395 }
396
397 fail() {
398         echo "$_fail"
399         return 1
400 }
401
402 died() {
403         echo "$_died"
404         return 1
405 }
406
407 # Check if $pid (could be plural) are running
408 checkpid() {
409         while [ "$1" ]; do
410                 [ -d "/proc/$1" ] && return 0
411                 shift
412         done
413         return 1
414 }
415
416 # - outside chroot get only those processes, which are outside chroot.
417 # - inside chroot get only those processes, which are inside chroot.
418 # - don't filter out pids which do not have corresponding running processes (process died etc)
419 # (note: some processes like named are chrooted but run outside chroot)
420 # - do nothing inside vserver
421 filter_chroot() {
422         if is_yes "$VSERVER"; then
423                 echo $@
424                 return
425         fi
426         if [ $# -lt 1 -o ! -d /proc/1 ]; then
427                 echo $@
428                 return
429         fi
430         local root_dir good_pids="" good_add_pid
431         for root_pid in $@; do
432                 root_dir=$(resolvesymlink /proc/${root_pid}/root)
433                 if [ -n "$root_dir" ]; then
434                         good_add_pid=1
435                         if [ -n "${SYSTEM_CHROOTS}" ]; then
436                                 for r_dir in ${SYSTEM_CHROOTS}; do
437                                         echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
438                                 done
439                         fi
440                         [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
441                 elif [ ! -d "/proc/$root_pid" ]; then
442                         good_pids="$good_pids $root_pid"
443                 fi
444         done
445         echo $good_pids
446 }
447
448 # Usage run_cmd Message command_to_run
449 run_cmd() {
450         local exit_code errors message force_err
451         local force_err=0 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         local errors="" prog="" end="" waitname="" waittime=""
508         local 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                         [[ "$pidfile" != /* ]] && pidfile="/var/run/$pidfile"
552                         ;;
553                 --makepid)
554                         makepid=1
555                         ;;
556                 -*|+*)
557                         nice=$1
558                         shift
559                         break
560                         ;;
561                 *)
562                         break
563                         ;;
564                 esac
565                 shift
566         done
567         # If command to execute ends with quotation mark, add remaining
568         # arguments and close quotation.
569         if [ "$prog" != "${prog%\"}" ]; then
570                 prog="$prog $*$end\""
571         else
572                 prog="$prog $*$end"
573         fi
574
575         _daemon_set_ulimits
576
577         [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
578         [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
579
580         # And start it up.
581         busy
582         cd $chdir
583         [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
584         if errors=$(
585                 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
586                 export USER=root HOME=/tmp TMPDIR=/tmp
587                 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
588                 nice=${nice:-0}
589
590                 if [ "$closefds" = 1 ]; then
591                         exec 1>&-
592                         exec 2>&-
593                         exec 0>&-
594                 else
595                         exec 2>&1
596                 fi
597
598                 if is_no "$RC_LOGGING"; then
599                         prog=$1; shift
600                         if [ ! -x $prog ]; then
601                                 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
602                                 local a o=$IFS
603                                 IFS=:
604                                 for a in $PATH; do
605                                         if [ -x $a/$prog ]; then
606                                                 prog=$a/$prog
607                                                 break
608                                         fi
609                                 done
610                                 IFS=$o
611                         fi
612                         /sbin/start-stop-daemon -q --start \
613                                 --nicelevel $nice \
614                                 ${pidfile:+--pidfile $pidfile} \
615                                 ${makepid:+--make-pidfile} \
616                                 ${user:+--chuid $user} \
617                                 ${chdir:+--chdir "$chdir"} \
618                                 ${fork:+--background} \
619                                 ${waitname:+--name $waitname} \
620                                 --exec "$prog" \
621                                 -- ${1:+"$@"}
622                 else
623                         nice -n $nice initlog -c "$prog" 2>&1
624                 fi
625                 ); then
626
627                 if [ -n "$waitname" -a -n "$waittime" ]; then
628                         # Save basename.
629                         base=${waitname##*/}
630                         # Find pid.
631                         pid=$(pidofproc "$waitname" "$pidfile")
632                         [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
633                         i=0
634                         while [ "$i" -lt "$waittime" ]; do
635                                 i=$((i + 1))
636                                 checkpid $pid && sleep 1 || break
637                         done
638                 fi
639                 log_success "$1 startup"
640                 ok
641         else
642                 exit_code=1
643                 fail
644                 log_failed "$1 startup"
645                 [ -n "$errors" ] && echo >&2 "$errors"
646         fi
647         return $exit_code
648 }
649
650 # A function to stop a program.
651 killproc() {
652         local notset killlevel base pid pidfile result
653         # Test syntax.
654         if [ $# = 0 ]; then
655                 msg_usage " killproc [--pidfile PIDFILE] {program} [-SIGNAME]"
656                 return 2
657         fi
658
659         while [ "$1" != "${1##-}" ]; do
660                 case $1 in
661                 --pidfile)
662                         pidfile="$2"
663                         [[ "$pidfile" != /* ]] && pidfile="/var/run/$pidfile"
664                         shift 2
665                         ;;
666                 --waitforname)
667                         waitname="$2"
668                         shift 2
669                         ;;
670                 --waitfortime)
671                         waittime="$2"
672                         shift 2
673                         ;;
674                 esac
675         done
676
677         busy
678
679         local notset=0
680         # check for second arg to be kill level
681         if [ -n "$2" ]; then
682                 killlevel=$2
683         else
684                 notset=1
685         fi
686
687         # experimental start-stop-daemon based killing.
688         # works only with pidfile
689         if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
690                 local sig=${killlevel:--TERM}
691                 /sbin/start-stop-daemon --stop \
692                         --retry ${sig#-}/10/${sig#-}/60/KILL/10 \
693                         -s ${sig#-} \
694                         ${pidfile:+--pidfile $pidfile}
695                 result=$?
696                 if [ "$result" -eq 0 ]; then
697                         ok
698                 else
699                         fail
700                 fi
701                 return $result
702         fi
703
704
705         # Save basename.
706         base=${1##*/}
707
708         # Find pid.
709         pid=$(pidofproc "$1" "$pidfile")
710         [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
711
712         # Kill it.
713         if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
714                 if [ "$notset" = "1" ]; then
715                         if checkpid $pid 2>&1; then
716                                 # TERM first, then KILL if not dead
717                                 kill -TERM $pid
718                                 usleep 100000
719                                 if checkpid $pid && sleep 1 &&
720                                         checkpid $pid && sleep 3 &&
721                                         checkpid $pid; then
722                                         # XXX: SIGKILL is sent already on 4th second!
723                                         # HARMFUL for example to mysqld (which is already workarounded)
724                                         kill -KILL $pid
725                                         usleep 100000
726                                 fi
727                         fi
728                         checkpid $pid
729                         result=$?
730                         if [ "$result" -eq 0 ]; then
731                                 fail
732                                 log_failed "$1 shutdown"
733                         else
734                                 ok
735                                 log_success "$1 shutdown"
736                         fi
737                         result=$(( ! $result ))
738                 else
739                         # use specified level only
740                         if checkpid $pid > /dev/null 2>&1; then
741                                 kill $killlevel $pid
742                                 result=$?
743                                 if [ "$result" -eq 0 ]; then
744                                         ok
745                                         log_success "$1 got $killlevel"
746                                 else
747                                         result=7
748                                         fail
749                                         log_failed "$1 didn't get $killlevel"
750                                 fi
751                         else
752                                 result=7
753                                 died
754                                 log_failed "$1 shutdown"
755                         fi
756                 fi
757         else
758                 died
759                 log_failed "$1 shutdown"
760                 result=7
761         fi
762
763         if [ -n "$waitname" -a -n "$waittime" ]; then
764                 # Save basename.
765                 base=${waitname##*/}
766                 # Find pid.
767                 pid=$(pidofproc "$waitname" "$pidfile")
768                 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
769                 i=0
770                 while [ "$i" -lt "$waittime" ]; do
771                         i=$(( i + 1 ))
772                         checkpid $pid && sleep 1 || break
773                 done
774         fi
775
776         # Remove pid file if any.
777         if [ "$notset" = "1" ]; then
778                 rm -f /var/run/${base}.pid
779         fi
780
781         return $result
782 }
783
784 # A function to find the pid of a program.
785 pidofproc() {
786         local pid pidfile base=${1##*/}
787         pidfile="$base.pid"
788         [ -n "$2" ] && pidfile="$2"
789
790         # Test syntax.
791         if [ $# = 0 ]; then
792                 msg_usage " pidofproc {program}"
793                 return 2
794         fi
795
796         # First try pidfile or "/var/run/*.pid"
797         if [[ "$pidfile" = /* ]]; then
798                 pidfile="${pidfile}"
799         else
800                 pidfile="/var/run/${pidfile}";
801         fi
802         if [ -f "${pidfile}" ]; then
803                 local p pid=""
804                 for p in $(< "${pidfile}"); do
805                         [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
806                 done
807         fi
808
809         # Next try "pidof"
810         [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
811         pid=$(filter_chroot "$pid")
812         echo $pid
813 }
814
815 status() {
816         local pid subsys daemon cpuset_msg pidfile
817         if [ "$1" = "--pidfile" ]; then
818                 pidfile=$2
819                 [[ "$pidfile" != /* ]] && pidfile="/var/run/$pidfile"
820                 shift 2
821         fi
822
823         subsys=$1
824         daemon=${2:-$subsys}
825
826         # Test syntax.
827         if [ $# = 0 ]; then
828                 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
829                 return 2
830         fi
831
832         # if pidfile specified, pid must be there
833         if [ "$pidfile" -a -f "$pidfile" ]; then
834                 read pid < $pidfile
835         else
836                 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
837         fi
838         pid=$(filter_chroot "$pid")
839
840         if [ "$pid" ]; then
841                 cpuset_msg="..."
842                 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" ]; then
843                         if $(grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"); then
844                                 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
845                         else
846                                 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
847                         fi
848                 fi
849                 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
850                 return 0
851         fi
852
853         # Next try "/var/run/*.pid" files
854         local base=${daemon##*/}
855         if [ -f /var/run/${base}.pid ]; then
856                 read pid < /var/run/${base}.pid
857                 pid=$(filter_chroot "$pid")
858                 if [ "$pid" != "" ]; then
859                         nls "%s dead but pid file exists" "$subsys"
860                         return 1
861                 fi
862         fi
863
864         # See if /var/lock/subsys/$subsys exists
865         if [ -f /var/lock/subsys/$subsys ]; then
866                 nls "%s dead but subsys locked" "$subsys"
867                 return 2
868         fi
869         nls "%s is stopped" "$subsys"
870         return 3
871 }
872
873 # Confirm whether we really want to run this service
874 confirm() {
875         local answer
876         nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
877         read answer
878         case $answer in
879         y|Y|t|T|j|J|"")
880                 return 0
881                 ;;
882         c|C|k|K|w|W)
883                 return 2
884                 ;;
885         n|N)
886                 return 1
887                 ;;
888         *)
889                 confirm $1
890                 return $?
891                 ;;
892         esac
893 }
894
895 # module is needed (ie. is requested, is available and isn't loaded already)
896 is_module() {
897         # module name without .o at end
898         if ! lsmod | grep -q "$1"; then
899                 if ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"; then
900                         # true
901                         return 0
902                 fi
903         fi
904         # false
905         return 1
906 }
907
908 _modprobe() {
909         local parsed single die args foo result
910         parsed=no
911         while is_no "$parsed"; do
912                 case "$1" in
913                 "single")
914                         single=yes
915                         shift
916                         ;;
917                 "die")
918                         die=yes
919                         shift
920                         ;;
921                 -*)
922                         args="$args $1"
923                         shift
924                         ;;
925                 *)
926                         parsed=yes
927                         ;;
928                 esac
929         done
930         if is_yes "${single}"; then
931                 foo="$@"
932                 show "Loading %s kernel module(s)" "$foo"
933                 busy
934         fi
935         if [ -x /sbin/modprobe ]; then
936                 /sbin/modprobe -s $args "$@"
937                 result=$?
938         else
939                 deltext; fail
940                 result=1
941         fi
942         if is_yes "${single}"; then
943                 deltext
944                 if [ $result = "0" ]; then
945                         is_yes "$single" && ok
946                 else
947                         fail
948                         if is_yes "$die"; then
949                                 nls "Could not load %s kernel module(s)" "$@"
950                                 exit 1
951                         fi
952                 fi
953         fi
954 }
955
956 if is_no "$RC_LOGGING"; then
957         log_success() {
958                 :
959         }
960
961         log_failed() {
962                 :
963         }
964 else
965         log_success() {
966                 initlog -n $0 -s "$1 $2" -e 1
967         }
968
969         log_failed() {
970                 initlog -n $0 -s "$1 $2" -e 2
971         }
972 fi
973
974 # Check if any flavor of portmapper is running
975 check_portmapper() {
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 # is_fsmounted fstype mntpoint
990 # Check if filesystem fstype is mounted on mntpoint
991 is_fsmounted() {
992         local fstype=$1
993         local mntpoint=$2
994
995         [ -n "$fstype" -a -n "$mntpoint" ] || return 1
996
997         if [ -r /proc/mounts ]; then
998                 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
999                 return $?
1000         else
1001                 if [ "$(/bin/stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
1002                         return 0
1003                 else
1004                         return 1
1005                 fi
1006         fi
1007 }
1008
1009 rc_cache_init() {
1010         # If we have cachefile, use it.
1011         # If we don't, create memory variables and try to save silently,
1012         local cachefile='/var/cache/rc-scripts/msg.cache'
1013
1014         local term
1015         if is_yes "$ISATTY"; then
1016                 term=$TERM
1017         else
1018                 term=dumb
1019         fi
1020
1021         # We create $check variable which is used to invalidate the cache.
1022         # The $check contains user locale and terminal.
1023         local check="$term.$LC_MESSAGES.$INIT_COL"
1024
1025         if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1026                 if . "$cachefile" 2>/dev/null; then
1027                         if [ "$check" = "$_check" ]; then
1028                                 return
1029                         fi
1030                 fi
1031         fi
1032
1033         # primitive caching
1034         _busy=$(progress "BUSY" "$CBUSY")
1035         _ok=$(progress "DONE")
1036         _started=$(progress "WORK")
1037         _fail=$(progress "FAIL" "$CFAIL")
1038         _died=$(progress "DIED" "$CFAIL")
1039
1040         # we don't use heredoc, as ksh attempts to create tempfile then
1041         (> "$cachefile" ) 2>/dev/null || return
1042         echo "_busy='$_busy';" >> "$cachefile"
1043         echo "_ok='$_ok';" >> "$cachefile"
1044         echo "_started='$_started';" >> "$cachefile"
1045         echo "_fail='$_fail';" >> "$cachefile"
1046         echo "_died='$_died';" >> "$cachefile"
1047         echo "_check='$check';" >> "$cachefile"
1048 }
1049
1050 rc_gettext_init() {
1051         if [ -z "$GETTEXT" ]; then
1052                 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1053                         GETTEXT=yes
1054                 else
1055                         GETTEXT=no
1056                 fi
1057         fi
1058
1059         if [ -z "$TPUT" ]; then
1060                 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
1061                         TPUT=yes
1062                         # check if we are on proper terminal
1063                         tput longname >/dev/null 2>&1 || TPUT=no
1064                 else
1065                         TPUT=no
1066                 fi
1067         fi
1068 }
1069
1070 rc_gettext_init
1071 rc_cache_init
1072
1073 #/*
1074 # * Local variables:
1075 # * mode: sh
1076 # * indent-tabs-mode: notnil
1077 # * End:
1078 # *
1079 # */
This page took 0.17712 seconds and 4 git commands to generate.