home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / etc / rc.status < prev    next >
Text File  |  2010-05-05  |  10KB  |  412 lines

  1. # /etc/rc.status
  2. # vim: syntax=sh
  3. # Definition of boot script return messages
  4. #
  5. #   The bootscripts should use the variables rc_done and rc_failed to
  6. #   report whether they failed or succeeded.  See /etc/init.d/skeleton for
  7. #   an example how the shell functions rc_status and rc_reset are used.
  8. #
  9. #   These functions make use of the variables rc_done and rc_failed;
  10. #   rc_done_up and rc_failed_up are the same as rc_done and rc_failed
  11. #   but contain a terminal code to move up one line before the output
  12. #   of the actual string. (This is particularly useful when the script
  13. #    starts a daemon which produces user output with a newline character)
  14. #
  15. #   The variable rc_reset is used by the master resource control script
  16. #   /etc/init.d/rc to turn off all attributes and switch to the standard
  17. #   character set.
  18. #
  19. #    \033          ascii ESCape
  20. #    \033[<NUM>G   move to column <NUM> (linux console, xterm, not vt100)
  21. #    \033[<NUM>C   move <NUM> columns forward but only upto last column
  22. #    \033[<NUM>D   move <NUM> columns backward but only upto first column
  23. #    \033[<NUM>A   move <NUM> rows up
  24. #    \033[<NUM>B   move <NUM> rows down
  25. #    \033[1m       switch on bold
  26. #    \033[31m      switch on red
  27. #    \033[32m      switch on green
  28. #    \033[33m      switch on yellow
  29. #    \033[m        switch off color/bold
  30. #    \017          exit alternate mode (xterm, vt100, linux console)
  31. #    \033[10m      exit alternate mode (linux console)
  32. #    \015          carriage return (without newline)
  33. #
  34.  
  35. # Do _not_ be fooled by non POSIX locale
  36. LC_ALL=POSIX
  37. export LC_ALL
  38.  
  39. # Seek for terminal size and, if needed, set default size
  40. rc_lc () {
  41.     if test -n "$REDIRECT" ; then
  42.     set -- $(stty size < "$REDIRECT"  2> /dev/null || echo 0 0)
  43.     else
  44.     set -- $(stty size 2> /dev/null || echo 0 0)
  45.     fi
  46.     LINES=$1
  47.     COLUMNS=$2
  48.     test $LINES   -eq 0 && LINES=24
  49.     test $COLUMNS -eq 0 && COLUMNS=80
  50.     export LINES COLUMNS
  51. }
  52. trap 'rc_lc' SIGWINCH
  53. rc_lc
  54.  
  55. # Make sure we have /sbin and /usr/sbin in PATH
  56. case ":$PATH:" in 
  57.     *:/sbin:*)
  58.     ;;
  59.     *)
  60.     PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH
  61.     export PATH
  62.     ;;
  63. esac
  64.  
  65. if test -t 1 -a "$TERM" != "raw" -a "$TERM" != "dumb" && stty size <&1 > /dev/null 2>&1 ; then
  66.      esc=`echo -en "\033"`
  67.         extd="${esc}[1m"
  68.         warn="${esc}[1;31m"
  69.         done="${esc}[1;32m"
  70.         attn="${esc}[1;33m"
  71.         norm=`echo -en "${esc}[m\017"`
  72.         stat=`echo -en "\015${esc}[${COLUMNS}C${esc}[10D"`
  73.  
  74.      rc_done="${stat}${done}done${norm}"
  75.   rc_running="${stat}${done}running${norm}"
  76.    rc_failed="${stat}${warn}failed${norm}"
  77.    rc_missed="${stat}${warn}missing${norm}"
  78.   rc_skipped="${stat}${attn}skipped${norm}"
  79.      rc_dead="${stat}${warn}dead${norm}"
  80.    rc_unused="${stat}${extd}unused${norm}"
  81.   rc_unknown="${stat}${attn}unknown${norm}"
  82.   rc_done_up="${esc}[1A${rc_done}"
  83. rc_failed_up="${esc}[1A${rc_failed}"
  84.     rc_reset="${norm}${esc}[?25h"
  85.      rc_save="${esc}7${esc}[?25l"
  86.   rc_restore="${esc}8${esc}[?25h"
  87.     rc_cuu () { test $1 -eq 0 && return; echo -en "\033[${1}A"; }
  88.     rc_cud () { test $1 -eq 0 && return; echo -en "\033[${1}B"; }
  89.     rc_timer_on () {
  90.     # Draw seconds of running timout to column.
  91.     # Two arguments: timeout in seconds and offset
  92.     local n=$1
  93.     local c=$2
  94.     (trap "exit 0" SIGTERM
  95.      while test $((n--)) -gt 0; do
  96.         sleep 1;
  97.         if test $n -gt 9 ; then
  98.         echo -en "\015${esc}[${c}C(${n}s) "
  99.         else
  100.         echo -en "\015${esc}[${c}C( ${n}s) "
  101.         fi
  102.     done) & _rc_timer_pid=$!
  103.     }
  104.     rc_timer_off () {
  105.     if test -n "$_rc_timer_pid" ; then
  106.         kill -TERM $_rc_timer_pid > /dev/null 2>&1
  107.     fi
  108.     unset _rc_timer_pid
  109.     }
  110. else
  111.      esc=""
  112.         extd=""
  113.         warn=""
  114.         done=""
  115.         attn=""
  116.         norm=""
  117.         stat=""
  118.  
  119.      rc_done="..done"
  120.   rc_running="..running"
  121.    rc_failed="..failed"
  122.    rc_missed="..missing"
  123.   rc_skipped="..skipped"
  124.      rc_dead="..dead"
  125.    rc_unused="..unused"
  126.   rc_unknown="..unknown"
  127.   rc_done_up="${rc_done}"
  128. rc_failed_up="${rc_failed}"
  129.     rc_reset=""
  130.      rc_save=""
  131.   rc_restore=""
  132.     rc_cuu () { return; }
  133.     rc_cud () { return; }
  134.     rc_timer_on  () { return; }
  135.     rc_timer_off () { return; }
  136. fi
  137.  
  138. _rc_service=${0##*/[SK][0-9][0-9]}
  139. _rc_status=0
  140. _rc_status_all=0
  141. _rc_todo=$1
  142.  
  143. rc_check ()
  144. {
  145.     _rc_status_ret=$?
  146.     test $_rc_status_ret -eq 0 || _rc_status=$_rc_status_ret
  147.     test $_rc_status     -eq 0 || _rc_status_all=$_rc_status
  148.     return $_rc_status_ret
  149. }
  150.  
  151. rc_reset ()
  152. {
  153.     _rc_status=0
  154.     _rc_status_all=0
  155.     rc_check
  156.     return 0
  157. }
  158.  
  159. if   test "$_rc_todo" = "status" ; then
  160. rc_status ()
  161. {
  162.     rc_check
  163.     _rc_status_ret=$_rc_status
  164.     local i
  165.     for i ; do
  166.     case "$i" in
  167.     -v|-v[1-9]|-v[1-9][0-9])
  168.         local vrt=""
  169.         local out=1
  170.         local opt="en"
  171.  
  172.         test -n "${i#-v}" && vrt=${esc:+"${esc}[${i#-v}A"} || opt="e"
  173.         case "$_rc_status" in
  174.         0)    vrt="$vrt$rc_running";        ;; # service running
  175.         1)    vrt="$vrt$rc_dead"   ; out=2    ;; # service dead (but has pid file)
  176.         2)    vrt="$vrt$rc_dead"   ; out=2    ;; # service dead (but has lock file)
  177.         3)    vrt="$vrt$rc_unused" ;        ;; # service not running
  178.         4)    vrt="$vrt$rc_unknown";        ;; # status is unknown
  179.         esac
  180.         echo -$opt "$rc_save$vrt$rc_restore" 1>&$out
  181.  
  182.         # reset _rc_status to 0 after verbose case
  183.         _rc_status=0 ;;
  184.     -r) rc_reset ;;
  185.     -s) echo -e "$rc_skipped" ; rc_failed 3 ;;
  186.     -u) echo -e "$rc_unused"  ; rc_failed 3 ;;
  187.     *)  echo "rc_status: Usage: [-v[<num>] [-r]|-s|-u]" 1>&2 ; return 0 ;;
  188.     esac
  189.     done
  190.     return $_rc_status_ret
  191. }
  192. elif test -n "$_rc_todo" ; then
  193. rc_status ()
  194. {
  195.     rc_check
  196.     test "$_rc_status" -gt 7 && rc_failed 1
  197.     _rc_status_ret=$_rc_status
  198.     case "$_rc_todo" in
  199.     stop)
  200.     # program is not running which
  201.     # is success if we stop service
  202.     test "$_rc_status" -eq 7 && rc_failed 0 ;;
  203.     esac
  204.     local i
  205.     for i ; do
  206.     case "$i" in
  207.     -v|-v[1-9]|-v[1-9][0-9])
  208.         local vrt=""
  209.         local out=1
  210.         local opt="en"
  211.  
  212.         test -n "${i#-v}" && vrt=${esc:+"${esc}[${i#-v}A"} || opt="e"
  213.         case "$_rc_status" in
  214.         0)    vrt="$vrt$rc_done"   ;        ;; # success
  215.         1)    vrt="$vrt$rc_failed" ; out=2    ;; # generic or unspecified error
  216.         2)    vrt="$vrt$rc_failed" ; out=2    ;; # invalid or excess args
  217.         3)    vrt="$vrt$rc_missed" ; out=2    ;; # unimplemented feature
  218.         4)    vrt="$vrt$rc_failed" ; out=2    ;; # insufficient privilege
  219.         5)    vrt="$vrt$rc_skipped"; out=2    ;; # program is not installed
  220.         6)    vrt="$vrt$rc_unused" ; out=2    ;; # program is not configured
  221.         7)    vrt="$vrt$rc_failed" ; out=2    ;; # program is not running
  222.         *)    vrt="$vrt$rc_failed" ; out=2    ;; # unknown (maybe used in future)
  223.         esac
  224.         echo -$opt "$rc_save$vrt$rc_restore" 1>&$out
  225.  
  226.         # reset _rc_status to 0 after verbose case
  227.         _rc_status=0 ;;
  228.     -r) rc_reset ;;
  229.     -s) echo -e "$rc_skipped" 1>&2 ; rc_failed 5 ;;
  230.     -u) echo -e "$rc_unused"  1>&2 ; rc_failed 6 ;;
  231.     *)  echo "rc_status: Usage: [-v[<num>] [-r]|-s|-u]" 1>&2 ; return 0 ;;
  232.     esac
  233.     done
  234.     return $_rc_status_ret
  235. }
  236. else
  237. rc_status ()
  238. {
  239.     rc_check
  240.     _rc_status_ret=$_rc_status
  241.     local i
  242.     for i ; do
  243.     case "$i" in
  244.     -v|-v[1-9]|-v[1-9][0-9])
  245.         local vrt=""
  246.         local out=1
  247.         local opt="en"
  248.  
  249.         test -n "${i#-v}" && vrt=${esc:+"${esc}[${i#-v}A"} || opt="e"
  250.         case "$_rc_status" in
  251.         0)    vrt="$vrt$rc_done"  ;        ;; # success
  252.         *)    vrt="$vrt$rc_failed"; out=2    ;; # failed
  253.         esac
  254.         echo -$opt "$rc_save$vrt$rc_restore" 1>&$out
  255.  
  256.         # reset _rc_status to 0 after verbose case
  257.         _rc_status=0 ;;
  258.     -r) rc_reset ;;
  259.     -s) echo -e "$rc_skipped"  ; return 0 ;;
  260.     -u) echo -e "$rc_unused"   ; return 0 ;;
  261.     *)  echo "rc_status: Usage: [-v[<num>] [-r]|-s|-u]" 1>&2 ; return 0 ;;
  262.     esac
  263.     done
  264.     return $_rc_status_ret
  265. }
  266. fi
  267.  
  268. rc_failed ()
  269. {
  270.     rc_reset
  271.     case "$1" in
  272.     [0-7]) _rc_status=$1 ;;
  273.     "")    _rc_status=1
  274.     esac
  275.     rc_check
  276.     return $_rc_status
  277. }
  278.  
  279. rc_exit ()
  280. {
  281.     exit $_rc_status_all
  282. }
  283.  
  284. rc_confirm()
  285. {
  286.     local timeout="30"
  287.     local answer=""
  288.     local ret=0
  289.  
  290.     case "$1" in
  291.     -t) timeout=$2; shift 2 ;;
  292.     esac
  293.     local message="$@, (Y)es/(N)o/(C)ontinue? [y] "
  294.     : ${REDIRECT:=/dev/tty}
  295.  
  296.     while true ; do
  297.     read -t ${timeout} -n 1 -p "${message}" answer < $REDIRECT > $REDIRECT 2>&1
  298.     case "$answer" in
  299.     [yY]|"") ret=0; break ;;
  300.     [nN])     ret=1; break ;;
  301.     [cC])     ret=2; break ;;
  302.     *)     echo; continue
  303.     esac
  304.     done
  305.     echo
  306.     return $ret
  307. }
  308.  
  309. rc_active ()
  310. {
  311.     local link
  312.     for link in /etc/init.d/*.d/S[0-9][0-9]${1} ; do
  313.     test -e $link || break
  314.     return 0
  315.     done
  316.     return 1
  317. }
  318.  
  319. rc_splash()
  320. {
  321.     return 0
  322. }
  323.  
  324. # Wait between last SIGTERM and the next SIGKILL
  325. # any argument specify a *path* of a process which
  326. # process identity should *not* be checked.
  327. rc_wait()
  328. {
  329.     local -i etime=$SECONDS
  330.  
  331.     if test -f /fastboot ; then
  332.     let etime+=2
  333.     else
  334.     let etime+=6
  335.     fi
  336.  
  337.     local -i pid
  338.     local -i ppid=$$
  339.     local comm state rest
  340.     local parent_processes="$ppid"
  341.  
  342.     while test $ppid -gt 1; do
  343.     read -t 1 pid comm state ppid rest < /proc/$ppid/stat
  344.     parent_processes="${parent_processes:+$parent_processes:}${ppid}"
  345.     done
  346.     for comm ; do
  347.     ppid="$(/sbin/pidofproc $comm)" || continue
  348.     parent_processes="${parent_processes:+$parent_processes:}${ppid}"
  349.     done
  350.     unset comm state ppid rest
  351.  
  352.     local -i busy
  353.     while test $SECONDS -lt $etime; do
  354.     let busy=0
  355.     for proc in /proc/[0-9]* ; do
  356.         test -e $proc/exe || continue
  357.         let pid=${proc##*/}
  358.         case ":${parent_processes}:" in
  359.         *:${pid}:*) continue
  360.         esac
  361.         let busy=pid
  362.         break
  363.     done
  364.     test $busy -ne 0 || return 0
  365.     usleep 500000
  366.     done
  367. }
  368.  
  369. rc_runlevel()
  370. {
  371.     test -z "$RUNLEVEL" || return
  372.     set -- $(/sbin/runlevel)
  373.     PREVLEVEL=$1
  374.     RUNLEVEL=$2
  375.     export PREVLEVEL RUNLEVEL
  376. }
  377.  
  378. cmdline=""
  379. rc_cmdline()
  380. {
  381.     local arg cmd key val
  382.     test -e /proc/cmdline || mount -nt proc proc /proc
  383.     test -n "$cmdline"    || read -t 2 cmdline < /proc/cmdline
  384.     for arg; do
  385.     for cmd in $cmdline ; do
  386.         key="${cmd%%=*}"
  387.         key="${key//-/_}"
  388.         case "${key}" in
  389.         $arg)
  390.         case "$cmd" in
  391.         *=*) val="${cmd#*=}" ;;
  392.         *)   val=yes
  393.         esac
  394.         echo $key=$val
  395.         return 0
  396.         esac
  397.     done
  398.     done
  399.     return 1
  400. }
  401.  
  402. rc_readonlyroot()
  403. {
  404.     if test -z "${READONLYROOT+x}" ; then
  405.     if rc_cmdline readonlyroot && ! rc_cmdline noreadonlyroot ; then
  406.         READONLYROOT=yes
  407.     fi > /dev/null
  408.     export READONLYROOT
  409.     fi
  410.     test -n "$READONLYROOT"
  411. }
  412.