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