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