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