Advertisement
Guest User

Untitled

a guest
Mar 19th, 2011
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.66 KB | None | 0 0
  1. #!/bin/bash
  2. # initscripts functions
  3. #
  4.  
  5. # width:
  6.  
  7. STAT_COL=80
  8. if [[ ! -t 1 ]]; then
  9.     USECOLOR=""
  10. elif [[ -t 0 ]]; then
  11.     # stty will fail when stdin isn't a terminal
  12.     STAT_COL="$(/bin/stty size)"
  13.     # stty gives "rows cols"; strip the rows number, we just want columns
  14.     STAT_COL="${STAT_COL##* }"
  15. elif /bin/tput cols &>/dev/null; then
  16.     # is /usr/share/terminfo already mounted, and TERM recognized?
  17.     STAT_COL=$(/bin/tput cols)
  18. fi
  19. if ((STAT_COL==0)); then
  20.     # if output was 0 (serial console), set default width to 80
  21.     STAT_COL=80
  22.     USECOLOR=""
  23. fi
  24.  
  25. # we use 13 characters for our own stuff
  26. STAT_COL=$(($STAT_COL - 13))
  27.  
  28. # disable colors on broken terminals
  29. TERM_COLORS="$(/bin/tput colors 2>/dev/null)"
  30. if (($? != 3)); then
  31.     case $TERM_COLORS in
  32.     *[!0-9]*) USECOLOR="";;
  33.     [0-7])    USECOLOR="";;
  34.     '')       USECOLOR="";;
  35.     esac
  36. fi
  37. unset TERM_COLORS
  38.  
  39. # clear the TZ envvar, so daemons always respect /etc/localtime
  40. unset TZ
  41.  
  42. # sanitize the locale settins
  43. unset LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY \
  44.       LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE \
  45.       LC_MEASUREMENT LC_IDENTIFICATION LC_ALL
  46. if [[ $DAEMON_LOCALE =~ yes|YES && -n $LOCALE ]]; then
  47.     export LANG="${LOCALE}"
  48. else
  49.     export LANG=C
  50. fi
  51.  
  52. # colors:
  53. if [[ $USECOLOR = YES || $USECOLOR = yes ]]; then
  54.     C_MAIN="\033[1;37;40m"      # main text
  55.  
  56.     C_OTHER="\033[1;34;40m"     # prefix & brackets
  57.     C_SEPARATOR="\033[1;30;40m" # separator
  58.  
  59.     C_BUSY="\033[0;36;40m"      # busy
  60.     C_FAIL="\033[1;31;40m"      # failed
  61.     C_DONE="\033[1;37;40m"      # completed
  62.     C_BKGD="\033[1;35;40m"      # backgrounded
  63.  
  64.     C_H1="\033[1;37;40m"        # highlight text 1
  65.     C_H2="\033[1;36;40m"        # highlight text 2
  66.  
  67.     C_CLEAR="\033[1;0m"
  68. fi
  69.  
  70. if [[ -t 1 ]]; then
  71.     SAVE_POSITION="\033[s"
  72.     RESTORE_POSITION="\033[u"
  73.     DEL_TEXT="\033[$(($STAT_COL+4))G"
  74. else
  75.     SAVE_POSITION=""
  76.     RESTORE_POSITION=""
  77.     DEL_TEXT=""
  78. fi
  79.  
  80. # prefixes:
  81.  
  82. PREFIX_REG="::"
  83. PREFIX_HL=" >"
  84.  
  85. # functions:
  86.  
  87. deltext() {
  88.     printf "${DEL_TEXT}"
  89. }
  90.  
  91. printhl() {
  92.     printf "${C_OTHER}${PREFIX_HL} ${C_H1}${1}${C_CLEAR} \n"
  93. }
  94.  
  95. printsep() {
  96.     printf "\n${C_SEPARATOR}   ------------------------------\n"
  97. }
  98.  
  99. stat_bkgd() {
  100.     printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
  101.     deltext
  102.     printf "   ${C_OTHER}[${C_BKGD}BKGD${C_OTHER}]${C_CLEAR} "
  103. }
  104.  
  105. stat_busy() {
  106.     printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
  107.     printf "${SAVE_POSITION}"
  108.     deltext
  109.     printf "   ${C_OTHER}[${C_BUSY}BUSY${C_OTHER}]${C_CLEAR} "
  110. }
  111.  
  112. stat_append() {
  113.     printf "${RESTORE_POSITION}"
  114.     printf -- "${C_MAIN}${1}${C_CLEAR}"
  115.     printf "${SAVE_POSITION}"
  116. }
  117.  
  118. stat_done() {
  119.     deltext
  120.     printf "   ${C_OTHER}[${C_DONE}DONE${C_OTHER}]${C_CLEAR} \n"
  121. }
  122.  
  123. stat_fail() {
  124.     deltext
  125.     printf "   ${C_OTHER}[${C_FAIL}FAIL${C_OTHER}]${C_CLEAR} \n"
  126. }
  127.  
  128. stat_die() {
  129.     stat_fail
  130.     exit ${1:-1}
  131. }
  132.  
  133. status() {
  134.     stat_busy "$1"
  135.     shift
  136.     if "$@" >/dev/null 2>&1; then
  137.         stat_done
  138.         return 0
  139.     fi
  140.     stat_fail
  141.     return 1
  142. }
  143.  
  144. #  usage : in_array( $needle, $haystack )
  145. # return : 0 - found
  146. #          1 - not found
  147. # Copied from makepkg
  148. in_array() {
  149.     [[ $2 ]] || return 1
  150.     local needle=$1; shift
  151.     local item
  152.     for item in "$@"; do
  153.     [[ ${item#@} = $needle ]] && return 0
  154.     done
  155.     return 1 # Not Found
  156. }
  157.  
  158. # daemons:
  159.  
  160. add_daemon() {
  161.     [[ -d /var/run/daemons ]] || /bin/mkdir -p /var/run/daemons
  162.     > /var/run/daemons/"$1"
  163. }
  164.  
  165. rm_daemon() {
  166.     /bin/rm -f /var/run/daemons/"$1"
  167. }
  168.  
  169. ck_daemon() {
  170.     [[ ! -f /var/run/daemons/$1 ]]
  171. }
  172.  
  173. have_daemon() {
  174.     [[ -x /etc/rc.d/$1 ]]
  175. }
  176.  
  177. start_daemon() {
  178.     have_daemon "$1" && /etc/rc.d/"$1" start
  179. }
  180.  
  181. ck_depends() {
  182.     for daemon in "$@"; do
  183.     ck_daemon "$daemon" && start_daemon "$daemon"
  184.     done
  185. }
  186.  
  187. start_daemon_bkgd() {
  188.     stat_bkgd "Starting $1"
  189.     have_daemon "$1" && (start_daemon "$1") &>/dev/null &
  190. }
  191.  
  192. stop_daemon() {
  193.     have_daemon "$1" && /etc/rc.d/"$1" stop
  194. }
  195.  
  196. # Status functions
  197. status_started() {
  198.   deltext
  199.   echo -ne "$C_OTHER[${C_STRT}STARTED$C_OTHER]$C_CLEAR "
  200. }
  201.  
  202. status_stopped() {
  203.   deltext
  204.   echo -ne "$C_OTHER[${C_STRT}STOPPED$C_OTHER]$C_CLEAR "
  205. }
  206.  
  207. ck_status() {
  208.   if ! ck_daemon "$1"; then
  209.     status_started
  210.   else
  211.     status_stopped
  212.   fi
  213. }
  214.  
  215. kill_everything() {
  216.     # $1 = where we are being called from.
  217.     # This is used to determine which hooks to run.
  218.     # Find daemons NOT in the DAEMONS array. Shut these down first
  219.     for daemon in /var/run/daemons/*; do
  220.         [[ -f $daemon ]] || continue
  221.         daemon=${daemon##*/}
  222.     in_array "$daemon" "${DAEMONS[@]}" || stop_daemon "$daemon"
  223.     done
  224.  
  225.     # Shutdown daemons in reverse order
  226.     for ((i=${#DAEMONS[@]}-1; i>=0; i--)); do
  227.     [[ ${DAEMONS[$i]:0:1} = '!' ]] && continue
  228.     ck_daemon ${DAEMONS[$i]#@} || stop_daemon ${DAEMONS[$i]#@}
  229.     done
  230.  
  231.     # Terminate all processes
  232.     stat_busy "Sending SIGTERM To Processes"
  233.     run_hook "$1_prekillall"
  234.     /sbin/killall5 -15 &> /dev/null
  235.     /bin/sleep 5
  236.     stat_done
  237.  
  238.     stat_busy "Sending SIGKILL To Processes"
  239.     /sbin/killall5 -9 &> /dev/null
  240.     /bin/sleep 1
  241.     stat_done
  242.  
  243.     run_hook "$1_postkillall"
  244. }
  245.  
  246. activate_vgs() {
  247.     [[ $USELVM =~ yes|YES && -x /sbin/lvm && -d /sys/block ]] || return
  248.     # Kernel 2.6.x, LVM2 groups
  249.     /sbin/modprobe -q dm-mod 2>/dev/null
  250.     stat_busy "Activating LVM2 groups"
  251.     if /sbin/vgchange --sysinit -a y >/dev/null; then
  252.     stat_done
  253.     else
  254.     stat_fail
  255.      fi
  256. }
  257.  
  258. # Arch cryptsetup packages traditionally contained the binaries
  259. #  /usr/sbin/cryptsetup
  260. #  /sbin/cryptsetup.static
  261. # By default, initscripts used the /sbin/cryptsetup.static.
  262. # Newer packages will only have /sbin/cryptsetup and no static binary
  263. # This ensures maximal compatibility with the old and new layout
  264. for CS in /sbin/cryptsetup /usr/sbin/cryptsetup \
  265.     /sbin/cryptsetup.static ''; do
  266.     [[ -x $CS ]] && break
  267. done
  268.  
  269. read_crypttab() {
  270.     # $1 = function to call with the split out line from the crypttab
  271.     local line nspo failed=0
  272.     while read line; do
  273.         [[ $line && ${line:0:1} != '#' ]] || continue
  274.         eval nspo=("${line%#*}")
  275.         if $1 "${nspo[0]}" "${nspo[1]}" "${nspo[2]}" "${nspo[*]:3}"; then
  276.           crypto_unlocked=1
  277.         else
  278.           failed=1
  279.         fi
  280.     done < /etc/crypttab
  281.     return $failed
  282. }
  283.  
  284. ###############################
  285. # Custom hooks in initscripts #
  286. ###############################
  287. # Hooks can be used to include custom code in various places in the rc.* scripts
  288. #
  289. # Define a hook function in a functions.d file using:
  290. #  function_name() {
  291. #    ...
  292. #  }
  293. #  add_hook hook_name function_name
  294. # It is allowed to register several hook functions for the same hook
  295. # Is is also allowed to register the same hook function for several hooks
  296. #
  297. # Currently, the following hooks exist:
  298. # sysinit_start: at the beginning of rc.sysinit
  299. # multi_start: at the beginning of rc.multi
  300. # single_start: at the beginning of rc.single
  301. # shutdown_start: at the beginning of rc.shutdown
  302. # sysinit_end: at the end of rc.sysinit
  303. # multi_end: at the end of rc.multi
  304. # single_end: at the end of rc.single
  305. # sysinit_udevlaunched: after udev has been launched in rc.sysinit
  306. # single_udevlaunched: after udev has been launched in rc.single
  307. # sysinit_udevsettled: after uevents have settled in rc.sysinit
  308. # single_udevsettled: after uevents have settled in rc.single
  309. # sysinit_premount: before local filesystems are mounted, but after root is mounted read-write in rc.sysinit
  310. # shutdown_prekillall: before all processes are being killed in rc.shutdown
  311. # single_prekillall: before all processes are being killed in rc.single
  312. # shutdown_postkillall: after all processes have been killed in rc.shutdown
  313. # single_postkillall: after all processes have been killed in rc.single
  314. # shutdown_poweroff: directly before powering off in rc.shutdown
  315. #
  316. # Declare add_hook and run_hook as read-only to prevent overwriting them.
  317. # Too bad we cannot do the same thing with hook_funcs
  318.  
  319. if [[ $RC_FUNCTIONS_HOOK_FUNCS_DEFINED -ne 1 ]]; then
  320.     declare -A hook_funcs
  321.  
  322.     add_hook() {
  323.         [[ $1 && $2 ]] || return 1
  324.         hook_funcs["$1"]+=" $2"
  325.     }
  326.  
  327.     run_hook() {
  328.         [[ $1 ]] || return 1
  329.         local func
  330.         for func in ${hook_funcs["$1"]}; do
  331.             "${func}"
  332.         done
  333.     }
  334.  
  335.     declare -fr add_hook run_hook
  336.     declare -r RC_FUNCTIONS_HOOK_FUNCS_DEFINED=1
  337. fi
  338.  
  339. # Function for setting console font if required
  340. set_consolefont() {
  341.     [[ $CONSOLEFONT ]] || return 0
  342.     stat_busy "Loading Console Font: $CONSOLEFONT"
  343.     #CONSOLEMAP in UTF-8 shouldn't be used
  344.     [[ $CONSOLEMAP && ${LOCALE,,} =~ utf ]] && CONSOLEMAP=""
  345.     for i in /dev/tty[0-9]*; do
  346.     /usr/bin/setfont ${CONSOLEMAP:+-m ${CONSOLEMAP}} \
  347.         $CONSOLEFONT -C ${i} >/dev/null 2>&1
  348.     done
  349.     if (($? != 0)); then
  350.     stat_fail
  351.     elif [[ $CONSOLEMAP ]]; then
  352.     cat <<"EOF" >>/etc/profile.d/locale.sh
  353. if [ "$CONSOLE" = "" -a "$TERM" = "linux" -a -t 1 ]; then printf "\033(K"; fi
  354.  
  355. EOF
  356.     stat_done
  357.     fi
  358. }
  359.  
  360. # Source additional functions at the end to allow overrides
  361. for f in /etc/rc.d/functions.d/*; do
  362.   [[ -e $f ]] && . "$f"
  363. done
  364.  
  365. # End of file
  366. # vim: set ft=sh sw=2 ts=2 et:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement