home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 November - Disc 1 / PCNET_CD_2006_11_1.iso / linux / gparted-livecd-0.3.1-1.iso / gparted / etc / rc.d / init.d / functions < prev    next >
Encoding:
Text File  |  2006-07-30  |  14.2 KB  |  672 lines

  1. #!/bin/sh
  2. ########################################################################
  3. # Begin $rc_base/init.d/functions
  4. #
  5. # Description : Run Level Control Functions
  6. #
  7. # Authors     : Gerard Beekmans - gerard@linuxfromscratch.org
  8. #
  9. # Version     : 00.00
  10. #
  11. # Notes       : With code based on Matthias Benkmann's simpleinit-msb
  12. #        http://winterdrache.de/linux/newboot/index.html
  13. #
  14. ########################################################################
  15.  
  16. ## Environmental setup
  17. # Setup default values for environment
  18. umask 022
  19. export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
  20.  
  21. # Signal sent to running processes to refresh their configuration
  22. RELOADSIG="HUP"
  23.  
  24. # Number of seconds between STOPSIG and FALLBACK when stopping processes
  25. KILLDELAY="3"
  26.  
  27. ## Screen Dimensions
  28. # Find current screen size
  29. if [ -z "${COLUMNS}" ]; then
  30.     COLUMNS=$(stty size)
  31.     COLUMNS=${COLUMNS##* }
  32. fi
  33.  
  34. # When using remote connections, such as a serial port, stty size returns 0
  35. if [ "${COLUMNS}" = "0" ]; then 
  36.     COLUMNS=80
  37. fi
  38.  
  39. ## Measurements for positioning result messages
  40. COL=$((${COLUMNS} - 8))
  41. WCOL=$((${COL} - 2))
  42.  
  43. ## Set Cursor Position Commands, used via echo -e
  44. SET_COL="\\033[${COL}G"      # at the $COL char
  45. SET_WCOL="\\033[${WCOL}G"    # at the $WCOL char
  46. CURS_UP="\\033[1A\\033[0G"   # Up one line, at the 0'th char
  47.  
  48. ## Set color commands, used via echo -e
  49. # Please consult `man console_codes for more information
  50. # under the "ECMA-48 Set Graphics Rendition" section
  51. #
  52. # Warning: when switching from a 8bit to a 9bit font,
  53. # the linux console will reinterpret the bold (1;) to
  54. # the top 256 glyphs of the 9bit font.  This does
  55. # not affect framebuffer consoles
  56. NORMAL="\\033[0;39m"         # Standard console grey
  57. SUCCESS="\\033[1;32m"        # Success is green
  58. WARNING="\\033[1;33m"        # Warnings are yellow
  59. FAILURE="\\033[1;31m"        # Failures are red
  60. INFO="\\033[1;36m"           # Information is light cyan
  61. BRACKET="\\033[1;34m"        # Brackets are blue
  62.  
  63. STRING_LENGTH="0"   # the length of the current message
  64.  
  65. #*******************************************************************************
  66. # Function - boot_mesg()
  67. #
  68. # Purpose:      Sending information from bootup scripts to the console
  69. #
  70. # Inputs:       $1 is the message
  71. #               $2 is the colorcode for the console
  72. #
  73. # Outputs:      Standard Output
  74. #
  75. # Dependencies: - sed for parsing strings.
  76. #            - grep for counting string length.
  77. #               
  78. # Todo:         
  79. #*******************************************************************************
  80. boot_mesg()
  81. {
  82.     local ECHOPARM=""
  83.  
  84.     while true
  85.     do
  86.         case "${1}" in
  87.             -n)
  88.                 ECHOPARM=" -n "
  89.                 shift 1
  90.                 ;;
  91.             -*)
  92.                 echo "Unknown Option: ${1}"
  93.                 return 1
  94.                 ;;
  95.             *)
  96.                 break
  97.                 ;;
  98.         esac
  99.     done
  100.  
  101.     ## Figure out the length of what is to be printed to be used
  102.         ## for warning messges. 
  103.     STRING_LENGTH="`echo "${1}" | sed \
  104.         -e 's,.,.,g' -e 'l 1' | grep -c \$`"
  105.  
  106.     # Print the message to the screen
  107.     echo ${ECHOPARM} -e "${2}${1}"
  108.     
  109. }
  110.  
  111. boot_mesg_flush()
  112. {
  113.     # Reset STRING_LENGTH for next message
  114.     STRING_LENGTH="0"
  115. }
  116.  
  117. boot_log()
  118. {
  119.     # Left in for backwards compatibility
  120.     echo -n ""
  121. }
  122.  
  123. echo_ok()
  124. {
  125.     echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS}  OK  ${BRACKET}]"
  126.     echo -e "${NORMAL}"
  127.     boot_mesg_flush "[  OK  ]"
  128. }
  129.  
  130. echo_failure()
  131. {
  132.     echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
  133.     echo -e "${NORMAL}"
  134.     boot_mesg_flush "[ FAIL ]"
  135. }
  136.  
  137. echo_warning()
  138. {
  139.     echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
  140.     echo -e "${NORMAL}"
  141.     boot_mesg_flush "[ WARN ]"
  142. }
  143.  
  144. print_error_msg()
  145. {
  146. echo
  147. }
  148.  
  149. check_script_status()
  150. {
  151.     # $i is inherited by the rc script
  152.     if [ ! -f ${i} ]; then
  153.         boot_mesg "${i} is not a valid symlink." ${WARNING}
  154.         echo_warning
  155.         continue
  156.     fi
  157.  
  158.     if [ ! -x ${i} ]; then
  159.         boot_mesg "${i} is not executable, skipping." ${WARNING}
  160.         echo_warning
  161.         continue
  162.     fi
  163. }
  164.  
  165. evaluate_retval()
  166. {
  167.     error_value="${?}"
  168.  
  169.     if [ ${error_value} = 0 ]; then
  170.         echo_ok
  171.     else
  172.         echo_failure
  173.     fi
  174.  
  175.     # This prevents the 'An Unexpected Error Has Occurred' from trivial
  176.     # errors.
  177.     return 0
  178. }
  179.  
  180. print_status()
  181. {
  182.     if [ "${#}" = "0" ]; then
  183.         echo "Usage: ${0} {success|warning|failure}"
  184.         return 1
  185.     fi
  186.  
  187. #    boot_mesg_flush
  188. #    echo_warning
  189.  
  190.     case "${1}" in
  191.  
  192.         success)
  193.             echo_ok
  194.             ;;
  195.  
  196.         warning)
  197.             # Leave this extra case in because old scripts
  198.             # may call it this way.
  199.             case "${2}" in
  200.                 running)
  201.                     echo -e -n "${CURS_UP}"
  202.                     echo -e -n "\\033[${STRING_LENGTH}G   "
  203.                     boot_mesg "Already running." ${WARNING}
  204.                     echo_warning
  205.                     ;;
  206.                 not_running)
  207.                     echo -e -n "${CURS_UP}"
  208.                     echo -e -n "\\033[${STRING_LENGTH}G   "
  209.                     boot_mesg "Not running." ${WARNING}
  210.                     echo_warning
  211.                     ;;
  212.                 not_available)
  213.                     echo -e -n "${CURS_UP}"
  214.                     echo -e -n "\\033[${STRING_LENGTH}G   "
  215.                     boot_mesg "Not available." ${WARNING}
  216.                     echo_warning
  217.                     ;;
  218.                 *)
  219.                     # This is how it is supposed to
  220.                     # be called
  221.                     echo_warning
  222.                     ;;
  223.             esac
  224.         ;;
  225.  
  226.         failure)
  227.             echo_failure
  228.         ;;
  229.  
  230.     esac
  231.  
  232. }
  233.  
  234. reloadproc()
  235. {
  236.     if [ "${#}" = "0" ]; then
  237.         echo "Usage: reloadproc [{program}]"
  238.         exit 1
  239.     fi
  240.  
  241.     getpids "${1}"
  242.  
  243.     if [ -n "${pidlist}" ];    then
  244.         failure="0"
  245.         for pid in ${pidlist}
  246.         do
  247.             kill -"${RELOADSIG}" "${pid}" || failure="1"
  248.         done
  249.  
  250.         (exit ${failure})
  251.         evaluate_retval
  252.  
  253.     else
  254.         boot_mesg "Process ${1} not running." ${WARNING}
  255.         echo_warning
  256.     fi
  257. }
  258.  
  259. statusproc()
  260. {
  261.     if [ "${#}" = "0" ]
  262.     then
  263.         echo "Usage: statusproc {program}"
  264.         exit 1
  265.     fi
  266.  
  267.     getpids "${1}"
  268.  
  269.     if [ -n "${pidlist}" ];    then
  270.         echo -e "${INFO}${base} is running with Process"\
  271.             "ID(s) ${pidlist}.${NORMAL}"
  272.     else
  273.         if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
  274.             echo -e "${WARNING}${1} is not running but"\
  275.                 "/var/run/${base}.pid exists.${NORMAL}"
  276.         else
  277.             if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
  278.                 echo -e "${WARNING}${1} is not running"\
  279.                     "but ${PIDFILE} exists.${NORMAL}"
  280.             else
  281.                 echo -e "${INFO}${1} is not running.${NORMAL}"
  282.             fi
  283.         fi
  284.     fi
  285. }
  286.  
  287. # The below functions are documented in the LSB-generic 2.1.0
  288.  
  289. #*******************************************************************************
  290. # Function - pidofproc [-s] [-p pidfile] pathname
  291. #
  292. # Purpose: This function returns one or more pid(s) for a particular daemon
  293. #
  294. # Inputs: -p pidfile, use the specified pidfile instead of pidof
  295. #         pathname, path to the specified program
  296. #
  297. # Outputs: return 0 - Success, pid's in stdout
  298. #          return 1 - Program is dead, pidfile exists
  299. #          return 2 - Invalid or excessive number of arguments, 
  300. #                     warning in stdout
  301. #          return 3 - Program is not running
  302. #
  303. # Dependencies: pidof, echo, head
  304. #
  305. # Todo: Remove dependency on head
  306. #       This depreciates getpids
  307. #       Test changes to pidof
  308. #
  309. #*******************************************************************************
  310. pidofproc()
  311. {
  312.     local pidfile=""
  313.     local lpids=""
  314.     local silent=""
  315.     pidlist=""
  316.     while true
  317.     do
  318.         case "${1}" in
  319.             -p)
  320.                 pidfile="${2}"
  321.                 shift 2
  322.                 ;;
  323.  
  324.             -s)
  325.                 # Added for legacy opperation of getpids
  326.                 # eliminates several '> /dev/null'
  327.                 silent="1"
  328.                 shift 1
  329.                 ;;
  330.             -*)
  331.                 log_failure_msg "Unknown Option: ${1}"
  332.                 return 2
  333.                 ;;
  334.             *)
  335.                 break
  336.                 ;;
  337.         esac
  338.     done
  339.  
  340.     if [ "${#}" != "1" ]; then
  341.         shift 1
  342.         log_failure_msg "Usage: pidofproc [-s] [-p pidfile] pathname"
  343.         return 2
  344.     fi
  345.  
  346.     if [ -n "${pidfile}" ]; then
  347.         if [ ! -r "${pidfile}" ]; then
  348.             return 3 # Program is not running
  349.         fi
  350.  
  351.         lpids=`head -n 1 ${pidfile}`
  352.         for pid in ${lpids}
  353.         do
  354.             if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
  355.                 kill -0 "${pid}" > /dev/null &&
  356.                 pidlist="${pidlist} ${pid}"
  357.             fi
  358.             
  359.             if [ "${silent}" -ne "1" ]; then
  360.                 echo "${pidlist}"
  361.             fi
  362.  
  363.             test -z "${pidlist}" && 
  364.             # Program is dead, pidfile exists
  365.             return 1
  366.             # else
  367.             return 0
  368.         done
  369.  
  370.     else
  371.         pidlist=`pidof -o $$ -o $PPID -x "$1"`
  372.         if [ "x${silent}" != "x1" ]; then
  373.             echo "${pidlist}"
  374.         fi
  375.  
  376.         # Get provide correct running status
  377.         if [ -n "${pidlist}" ]; then
  378.             return 0
  379.         else
  380.             return 3
  381.         fi
  382.  
  383.     fi
  384.  
  385.     if [ "$?" != "0" ]; then
  386.         return 3 # Program is not running
  387.     fi
  388. }
  389.  
  390. # This will ensure compatibility with previous LFS Bootscripts
  391. getpids()
  392. {
  393.     if [ -z "${PIDFILE}" ]; then
  394.         pidofproc -s -p "${PIDFILE}" $@
  395.     else
  396.         pidofproc -s $@
  397.     fi
  398.     base="${1##*/}"
  399. }
  400.  
  401. #*******************************************************************************
  402. # Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
  403. #
  404. # Purpose: This runs the specified program as a daemon
  405. #
  406. # Inputs: -f, run the program even if it is already running
  407. #         -n nicelevel, specifies a nice level. See nice(1).
  408. #         -p pidfile, uses the specified pidfile
  409. #         pathname, pathname to the specified program
  410. #         args, arguments to pass to specified program
  411. #
  412. # Outputs: return 0 - Success
  413. #          return 2 - Invalid of excessive number of arguments, 
  414. #                     warning in stdout
  415. #          return 4 - Program or service status is unknown
  416. #
  417. # Dependencies: nice
  418. #
  419. # Todo: LSB says this should be called start_daemon
  420. #       LSB does not say that it should call evaluate_retval
  421. #       It checks for PIDFILE, which is deprecated.
  422. #         Will be removed after BLFS 6.0
  423. #       loadproc returns 0 if program is already running, not LSB compliant
  424. #
  425. #*******************************************************************************
  426. loadproc()
  427. {
  428.     local pidfile=""
  429.     local forcestart=""
  430.     local nicelevel="10"
  431.  
  432. # This will ensure compatibility with previous LFS Bootscripts
  433.     if [ -n "${PIDFILE}" ];    then
  434.         pidfile="${PIDFILE}"
  435.     fi
  436.  
  437.   while true
  438.     do
  439.         case "${1}" in
  440.             -f)
  441.                 forcestart="1"
  442.                 shift 1
  443.                 ;;
  444.             -n)
  445.                 nicelevel="${2}"
  446.                 shift 2
  447.                 ;;
  448.             -p)
  449.                 pidfile="${2}"
  450.                 shift 2
  451.                 ;;
  452.             -*)
  453.                 log_failure_msg "Unknown Option: ${1}"
  454.                 return 2 #invalid or excess argument(s)
  455.                 ;;
  456.             *)
  457.                 break
  458.                 ;;
  459.         esac
  460.     done
  461.  
  462.     if [ "${#}" = "0" ]; then
  463.         log_failure_msg "Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
  464.         return 2 #invalid or excess argument(s)
  465.     fi
  466.  
  467.     if [ -z "${forcestart}" ]; then
  468.         if [ -z "${pidfile}" ];    then
  469.             pidofproc -s "${1}"
  470.         else
  471.             pidofproc -s -p "${pidfile}" "${1}"
  472.         fi
  473.  
  474.         case "${?}" in
  475.             0)
  476.                 log_warning_msg "Unable to continue: ${1} is running"
  477.                 return 0 # 4
  478.                 ;;
  479.             1)
  480.                 log_warning_msg "Unable to continue: ${pidfile} exists"
  481.                 return 0 # 4
  482.                 ;;
  483.             3)
  484.                 ;;
  485.             *)
  486.                 log_failure_msg "Unknown error code from pidofproc: ${?}"
  487.                 return 4
  488.                 ;;
  489.         esac
  490.     fi
  491.  
  492.     nice -n "${nicelevel}" "${@}"
  493.     evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
  494.     return 0
  495. }
  496.  
  497. #*******************************************************************************
  498. # Function - killproc  [-p pidfile] pathname [signal]
  499. #
  500. # Purpose:
  501. #
  502. # Inputs: -p pidfile, uses the specified pidfile
  503. #         pathname, pathname to the specified program
  504. #         signal, send this signal to pathname
  505. #
  506. # Outputs: return 0 - Success
  507. #          return 2 - Invalid of excessive number of arguments, 
  508. #                     warning in stdout
  509. #          return 4 - Unknown Status
  510. #
  511. # Dependencies: kill
  512. #
  513. # Todo: LSB does not say that it should call evaluate_retval
  514. #       It checks for PIDFILE, which is deprecated.
  515. #         Will be removed after BLFS 6.0
  516. #
  517. #*******************************************************************************
  518. killproc()
  519. {
  520.     local pidfile=""
  521.     local killsig=""
  522.     pidlist=""
  523.  
  524. # This will ensure compatibility with previous LFS Bootscripts
  525.     if [ -n "${PIDFILE}" ];    then
  526.         pidfile="${PIDFILE}"
  527.     fi
  528.  
  529.     while true
  530.     do
  531.         case "${1}" in
  532.             -p)
  533.                 pidfile="${2}"
  534.                 shift 2
  535.                 ;;
  536.             -*)
  537.                 log_failure_msg "Unknown Option: ${1}"
  538.                 return 2
  539.             ;;
  540.             *)
  541.                  break
  542.                 ;;
  543.         esac
  544.     done
  545.  
  546.     if [ "${#}" = "2" ]; then
  547.         killsig="${2}"
  548.     elif [ "${#}" != "1" ];    then
  549.         shift 2
  550.         log_failure_msg "Usage: killproc  [-p pidfile] pathname [signal]"
  551.         return 2
  552.     fi
  553.  
  554.     if [ -z "${pidfile}" ];    then
  555.         pidofproc -s "${1}"
  556.     else
  557.         pidofproc -s -p "${pidfile}" "${1}"
  558.     fi
  559.  
  560.     # Change....
  561.     if [ -n "${pidlist}" ]; then
  562.     for pid in ${pidlist}
  563.     do
  564.         kill -${killsig:-TERM} ${pid} 2>/dev/null
  565.         if [ -z "${killsig}" ]; then
  566.             # Wait up to 3 seconds, for ${pid} to terminate
  567.             local dtime=${KILLDELAY}
  568.             while [ "${dtime}" != "0" ]
  569.             do
  570.                 kill -0 ${pid} 2>/dev/null || break
  571.                 sleep 1
  572.                 dtime=$(( ${dtime} - 1))
  573.             done
  574.             # If ${pid} is still running, kill it
  575.             kill -0 ${pid} 2>/dev/null && kill -KILL ${pid} 2>/dev/null
  576.         fi
  577.     done
  578.  
  579.     if [ -z "${killsig}" ];    then
  580.         pidofproc -s "${1}"
  581.  
  582.         # Program was terminated
  583.         if [ "$?" != "0" ]; then
  584.             # Pidfile Exists
  585.             if [ -f "${pidfile}" ];    then
  586.                 rm -f "${pidfile}"
  587.             fi
  588.             echo_ok
  589.             return 0
  590.         else # Program is still running
  591.             echo_failure
  592.             return 4 # Unknown Status
  593.         fi
  594.     else
  595.         if [ -z "${pidfile}" ];    then
  596.             pidofproc -s "${1}"
  597.         else
  598.             pidofproc -s -p "${pidfile}" "${1}"
  599.         fi
  600.     fi
  601.  
  602.     evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
  603.  
  604.     else
  605.     print_status warning not_running
  606.     fi
  607. }
  608.  
  609.  
  610. #*******************************************************************************
  611. # Function - log_success_msg "message"
  612. #
  613. # Purpose: Print a success message
  614. #
  615. # Inputs: $@ - Message
  616. #
  617. # Outputs: Text output to screen
  618. #
  619. # Dependencies: echo
  620. #
  621. # Todo: logging
  622. #
  623. #*******************************************************************************
  624. log_success_msg()
  625. {
  626.     echo -n -e "${BOOTMESG_PREFIX}${@}"
  627.     echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}""  OK  ""${BRACKET}""]""${NORMAL}"
  628.     return 0
  629. }
  630.  
  631. #*******************************************************************************
  632. # Function - log_failure_msg "message"
  633. #
  634. # Purpose: Print a failure message
  635. #
  636. # Inputs: $@ - Message
  637. #
  638. # Outputs: Text output to screen
  639. #
  640. # Dependencies: echo
  641. #
  642. # Todo: logging
  643. #
  644. #*******************************************************************************
  645. log_failure_msg() {
  646.     echo -n -e "${BOOTMESG_PREFIX}${@}"
  647.     echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
  648.     return 0
  649. }
  650.  
  651. #*******************************************************************************
  652. # Function - log_warning_msg "message"
  653. #
  654. # Purpose: print a warning message
  655. #
  656. # Inputs: $@ - Message
  657. #
  658. # Outputs: Text output to screen
  659. #
  660. # Dependencies: echo
  661. #
  662. # Todo: logging
  663. #
  664. #*******************************************************************************
  665. log_warning_msg() {
  666.     echo -n -e "${BOOTMESG_PREFIX}${@}"
  667.     echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
  668.     return 0
  669. }
  670.  
  671. # End $rc_base/init.d/functions
  672.