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