2011-05-26 04:45:00 +00:00
|
|
|
#!/bin/bash
|
2009-10-08 03:40:19 +00:00
|
|
|
# initscripts functions
|
|
|
|
#
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# width:
|
|
|
|
|
2009-10-08 03:40:19 +00:00
|
|
|
STAT_COL=80
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ ! -t 1 ]]; then
|
|
|
|
USECOLOR=""
|
|
|
|
elif [[ -t 0 ]]; then
|
|
|
|
# stty will fail when stdin isn't a terminal
|
|
|
|
STAT_COL="$(/bin/stty size)"
|
|
|
|
# stty gives "rows cols"; strip the rows number, we just want columns
|
|
|
|
STAT_COL="${STAT_COL##* }"
|
|
|
|
elif /bin/tput cols &>/dev/null; then
|
|
|
|
# is /usr/share/terminfo already mounted, and TERM recognized?
|
|
|
|
STAT_COL=$(/bin/tput cols)
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
if ((STAT_COL==0)); then
|
|
|
|
# if output was 0 (serial console), set default width to 80
|
|
|
|
STAT_COL=80
|
|
|
|
USECOLOR=""
|
2010-04-05 01:16:29 +00:00
|
|
|
fi
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# we use 13 characters for our own stuff
|
2009-10-08 03:40:19 +00:00
|
|
|
STAT_COL=$(($STAT_COL - 13))
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# disable colors on broken terminals
|
2009-10-08 03:40:19 +00:00
|
|
|
TERM_COLORS="$(/bin/tput colors 2>/dev/null)"
|
2011-05-26 04:45:00 +00:00
|
|
|
if (($? != 3)); then
|
|
|
|
case $TERM_COLORS in
|
|
|
|
*[!0-9]*) USECOLOR="";;
|
|
|
|
[0-7]) USECOLOR="";;
|
|
|
|
'') USECOLOR="";;
|
2009-10-08 03:40:19 +00:00
|
|
|
esac
|
|
|
|
fi
|
|
|
|
unset TERM_COLORS
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# clear the TZ envvar, so daemons always respect /etc/localtime
|
2009-10-08 03:40:19 +00:00
|
|
|
unset TZ
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
# sanitize the locale settins
|
|
|
|
unset LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY \
|
|
|
|
LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE \
|
|
|
|
LC_MEASUREMENT LC_IDENTIFICATION LC_ALL
|
|
|
|
if [[ $DAEMON_LOCALE =~ yes|YES && -n $LOCALE ]]; then
|
|
|
|
export LANG="${LOCALE}"
|
|
|
|
else
|
|
|
|
export LANG=C
|
|
|
|
fi
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# colors:
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $USECOLOR = YES || $USECOLOR = yes ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
C_MAIN="\033[1;37;40m" # main text
|
2011-05-26 04:45:00 +00:00
|
|
|
|
2009-10-08 03:40:19 +00:00
|
|
|
C_OTHER="\033[1;34;40m" # prefix & brackets
|
|
|
|
C_SEPARATOR="\033[1;30;40m" # separator
|
|
|
|
|
|
|
|
C_BUSY="\033[0;36;40m" # busy
|
|
|
|
C_FAIL="\033[1;31;40m" # failed
|
|
|
|
C_DONE="\033[1;37;40m" # completed
|
|
|
|
C_BKGD="\033[1;35;40m" # backgrounded
|
|
|
|
|
|
|
|
C_H1="\033[1;37;40m" # highlight text 1
|
|
|
|
C_H2="\033[1;36;40m" # highlight text 2
|
|
|
|
|
|
|
|
C_CLEAR="\033[1;0m"
|
|
|
|
fi
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ -t 1 ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
SAVE_POSITION="\033[s"
|
|
|
|
RESTORE_POSITION="\033[u"
|
|
|
|
DEL_TEXT="\033[$(($STAT_COL+4))G"
|
|
|
|
else
|
|
|
|
SAVE_POSITION=""
|
|
|
|
RESTORE_POSITION=""
|
|
|
|
DEL_TEXT=""
|
|
|
|
fi
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# prefixes:
|
|
|
|
|
2009-10-08 03:40:19 +00:00
|
|
|
PREFIX_REG="::"
|
|
|
|
PREFIX_HL=" >"
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# functions:
|
|
|
|
|
2009-10-08 03:40:19 +00:00
|
|
|
deltext() {
|
|
|
|
printf "${DEL_TEXT}"
|
|
|
|
}
|
|
|
|
|
|
|
|
printhl() {
|
|
|
|
printf "${C_OTHER}${PREFIX_HL} ${C_H1}${1}${C_CLEAR} \n"
|
|
|
|
}
|
|
|
|
|
|
|
|
printsep() {
|
|
|
|
printf "\n${C_SEPARATOR} ------------------------------\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
stat_bkgd() {
|
|
|
|
printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
|
|
|
|
deltext
|
|
|
|
printf " ${C_OTHER}[${C_BKGD}BKGD${C_OTHER}]${C_CLEAR} "
|
|
|
|
}
|
|
|
|
|
|
|
|
stat_busy() {
|
|
|
|
printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
|
|
|
|
printf "${SAVE_POSITION}"
|
|
|
|
deltext
|
|
|
|
printf " ${C_OTHER}[${C_BUSY}BUSY${C_OTHER}]${C_CLEAR} "
|
|
|
|
}
|
|
|
|
|
|
|
|
stat_append() {
|
|
|
|
printf "${RESTORE_POSITION}"
|
|
|
|
printf -- "${C_MAIN}${1}${C_CLEAR}"
|
|
|
|
printf "${SAVE_POSITION}"
|
|
|
|
}
|
|
|
|
|
|
|
|
stat_done() {
|
|
|
|
deltext
|
|
|
|
printf " ${C_OTHER}[${C_DONE}DONE${C_OTHER}]${C_CLEAR} \n"
|
|
|
|
}
|
|
|
|
|
|
|
|
stat_fail() {
|
|
|
|
deltext
|
|
|
|
printf " ${C_OTHER}[${C_FAIL}FAIL${C_OTHER}]${C_CLEAR} \n"
|
|
|
|
}
|
|
|
|
|
|
|
|
stat_die() {
|
|
|
|
stat_fail
|
2011-05-26 04:45:00 +00:00
|
|
|
exit ${1:-1}
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status() {
|
|
|
|
stat_busy "$1"
|
|
|
|
shift
|
2011-05-26 04:45:00 +00:00
|
|
|
if "$@" >/dev/null 2>&1; then
|
2009-10-08 03:40:19 +00:00
|
|
|
stat_done
|
|
|
|
return 0
|
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
stat_fail
|
|
|
|
return 1
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# usage : in_array( $needle, $haystack )
|
|
|
|
# return : 0 - found
|
|
|
|
# 1 - not found
|
|
|
|
# Copied from makepkg
|
2009-10-08 03:40:19 +00:00
|
|
|
in_array() {
|
2011-05-26 04:45:00 +00:00
|
|
|
[[ $2 ]] || return 1
|
|
|
|
local needle=$1; shift
|
|
|
|
local item
|
|
|
|
for item in "$@"; do
|
|
|
|
[[ ${item#@} = $needle ]] && return 0
|
|
|
|
done
|
|
|
|
return 1 # Not Found
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# daemons:
|
|
|
|
|
2009-10-08 03:40:19 +00:00
|
|
|
add_daemon() {
|
2011-05-26 04:45:00 +00:00
|
|
|
[[ -d /run/daemons ]] || /bin/mkdir -p /run/daemons
|
|
|
|
> /run/daemons/"$1"
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rm_daemon() {
|
2011-05-26 04:45:00 +00:00
|
|
|
/bin/rm -f /run/daemons/"$1"
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ck_daemon() {
|
2011-05-26 04:45:00 +00:00
|
|
|
[[ ! -f /run/daemons/$1 ]]
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
# Check if $1 is a valid daemon name
|
|
|
|
have_daemon() {
|
|
|
|
[[ -f /etc/rc.d/$1 && -x /etc/rc.d/$1 ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if $1 is started at boot
|
|
|
|
ck_autostart() {
|
|
|
|
local d
|
|
|
|
for d in "${DAEMONS[@]}"; do
|
|
|
|
[[ "$1" = ${d#@} ]] && return 1
|
2009-10-08 03:40:19 +00:00
|
|
|
done
|
2011-05-26 04:45:00 +00:00
|
|
|
return 0
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start_daemon() {
|
2011-05-26 04:45:00 +00:00
|
|
|
have_daemon "$1" && /etc/rc.d/"$1" start
|
|
|
|
}
|
|
|
|
|
|
|
|
ck_depends() {
|
|
|
|
for daemon in "$@"; do
|
|
|
|
ck_daemon "$daemon" && start_daemon "$daemon"
|
|
|
|
done
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start_daemon_bkgd() {
|
|
|
|
stat_bkgd "Starting $1"
|
2011-05-26 04:45:00 +00:00
|
|
|
have_daemon "$1" && (start_daemon "$1") &>/dev/null &
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stop_daemon() {
|
2011-05-26 04:45:00 +00:00
|
|
|
have_daemon "$1" && /etc/rc.d/"$1" stop
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# Status functions
|
2009-10-08 03:40:19 +00:00
|
|
|
status_started() {
|
2011-05-26 04:45:00 +00:00
|
|
|
deltext
|
|
|
|
echo -ne "$C_OTHER[${C_STRT}STARTED$C_OTHER]$C_CLEAR "
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_stopped() {
|
2011-05-26 04:45:00 +00:00
|
|
|
deltext
|
|
|
|
echo -ne "$C_OTHER[${C_STRT}STOPPED$C_OTHER]$C_CLEAR "
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ck_status() {
|
2011-05-26 04:45:00 +00:00
|
|
|
if ! ck_daemon "$1"; then
|
|
|
|
status_started
|
|
|
|
else
|
|
|
|
status_stopped
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
kill_everything() {
|
|
|
|
# $1 = where we are being called from.
|
|
|
|
# This is used to determine which hooks to run.
|
|
|
|
# Find daemons NOT in the DAEMONS array. Shut these down first
|
|
|
|
for daemon in /run/daemons/*; do
|
|
|
|
[[ -f $daemon ]] || continue
|
|
|
|
daemon=${daemon##*/}
|
|
|
|
in_array "$daemon" "${DAEMONS[@]}" || stop_daemon "$daemon"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Shutdown daemons in reverse order
|
|
|
|
for ((i=${#DAEMONS[@]}-1; i>=0; i--)); do
|
|
|
|
[[ ${DAEMONS[$i]:0:1} = '!' ]] && continue
|
|
|
|
ck_daemon ${DAEMONS[$i]#@} || stop_daemon ${DAEMONS[$i]#@}
|
|
|
|
done
|
|
|
|
|
|
|
|
# Terminate all processes
|
|
|
|
stat_busy "Sending SIGTERM To Processes"
|
|
|
|
run_hook "$1_prekillall"
|
|
|
|
/sbin/killall5 -15 &> /dev/null
|
|
|
|
/bin/sleep 5
|
|
|
|
stat_done
|
|
|
|
|
|
|
|
stat_busy "Sending SIGKILL To Processes"
|
|
|
|
/sbin/killall5 -9 &> /dev/null
|
|
|
|
/bin/sleep 1
|
|
|
|
stat_done
|
|
|
|
|
|
|
|
run_hook "$1_postkillall"
|
|
|
|
}
|
|
|
|
|
|
|
|
activate_vgs() {
|
|
|
|
[[ $USELVM =~ yes|YES && -x /sbin/lvm && -d /sys/block ]] || return
|
|
|
|
# Kernel 2.6.x, LVM2 groups
|
|
|
|
/sbin/modprobe -q dm-mod 2>/dev/null
|
|
|
|
stat_busy "Activating LVM2 groups"
|
|
|
|
if /sbin/vgchange --sysinit -a y >/dev/null; then
|
|
|
|
stat_done
|
|
|
|
else
|
|
|
|
stat_fail
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Arch cryptsetup packages traditionally contained the binaries
|
|
|
|
# /usr/sbin/cryptsetup
|
|
|
|
# /sbin/cryptsetup.static
|
|
|
|
# By default, initscripts used the /sbin/cryptsetup.static.
|
|
|
|
# Newer packages will only have /sbin/cryptsetup and no static binary
|
|
|
|
# This ensures maximal compatibility with the old and new layout
|
|
|
|
for CS in /sbin/cryptsetup /usr/sbin/cryptsetup \
|
|
|
|
/sbin/cryptsetup.static ''; do
|
|
|
|
[[ -x $CS ]] && break
|
|
|
|
done
|
|
|
|
|
|
|
|
read_crypttab() {
|
|
|
|
# $1 = function to call with the split out line from the crypttab
|
|
|
|
local line nspo failed=0
|
|
|
|
while read line; do
|
|
|
|
[[ $line && ${line:0:1} != '#' ]] || continue
|
|
|
|
eval nspo=("${line%#*}")
|
|
|
|
if $1 "${nspo[0]}" "${nspo[1]}" "${nspo[2]}" "${nspo[*]:3}"; then
|
|
|
|
crypto_unlocked=1
|
|
|
|
else
|
|
|
|
failed=1
|
|
|
|
fi
|
|
|
|
done < /etc/crypttab
|
|
|
|
return $failed
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
###############################
|
|
|
|
# Custom hooks in initscripts #
|
|
|
|
###############################
|
|
|
|
# Hooks can be used to include custom code in various places in the rc.* scripts
|
|
|
|
#
|
|
|
|
# Define a hook function in a functions.d file using:
|
|
|
|
# function_name() {
|
|
|
|
# ...
|
|
|
|
# }
|
|
|
|
# add_hook hook_name function_name
|
|
|
|
# It is allowed to register several hook functions for the same hook
|
|
|
|
# Is is also allowed to register the same hook function for several hooks
|
|
|
|
#
|
|
|
|
# Currently, the following hooks exist:
|
|
|
|
# sysinit_start: at the beginning of rc.sysinit
|
|
|
|
# multi_start: at the beginning of rc.multi
|
|
|
|
# single_start: at the beginning of rc.single
|
|
|
|
# shutdown_start: at the beginning of rc.shutdown
|
|
|
|
# sysinit_end: at the end of rc.sysinit
|
|
|
|
# multi_end: at the end of rc.multi
|
|
|
|
# single_end: at the end of rc.single
|
|
|
|
# sysinit_udevlaunched: after udev has been launched in rc.sysinit
|
|
|
|
# single_udevlaunched: after udev has been launched in rc.single
|
|
|
|
# sysinit_udevsettled: after uevents have settled in rc.sysinit
|
|
|
|
# single_udevsettled: after uevents have settled in rc.single
|
|
|
|
# sysinit_premount: before local filesystems are mounted, but after root is mounted read-write in rc.sysinit
|
|
|
|
# shutdown_prekillall: before all processes are being killed in rc.shutdown
|
|
|
|
# single_prekillall: before all processes are being killed in rc.single
|
|
|
|
# shutdown_postkillall: after all processes have been killed in rc.shutdown
|
|
|
|
# single_postkillall: after all processes have been killed in rc.single
|
|
|
|
# shutdown_poweroff: directly before powering off in rc.shutdown
|
|
|
|
#
|
2011-05-26 04:45:00 +00:00
|
|
|
# Declare add_hook and run_hook as read-only to prevent overwriting them.
|
|
|
|
# Too bad we cannot do the same thing with hook_funcs
|
|
|
|
|
|
|
|
if [[ $RC_FUNCTIONS_HOOK_FUNCS_DEFINED -ne 1 ]]; then
|
|
|
|
declare -A hook_funcs
|
|
|
|
|
|
|
|
add_hook() {
|
|
|
|
[[ $1 && $2 ]] || return 1
|
|
|
|
hook_funcs["$1"]+=" $2"
|
|
|
|
}
|
|
|
|
|
|
|
|
run_hook() {
|
|
|
|
[[ $1 ]] || return 1
|
|
|
|
local func
|
|
|
|
for func in ${hook_funcs["$1"]}; do
|
|
|
|
"${func}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
declare -fr add_hook run_hook
|
|
|
|
declare -r RC_FUNCTIONS_HOOK_FUNCS_DEFINED=1
|
|
|
|
fi
|
2009-10-08 03:40:19 +00:00
|
|
|
|
2010-07-28 19:27:38 +00:00
|
|
|
# Function for setting console font if required
|
2011-01-02 05:56:57 +00:00
|
|
|
set_consolefont() {
|
2011-05-26 04:45:00 +00:00
|
|
|
[[ $CONSOLEFONT ]] || return 0
|
2011-01-02 05:56:57 +00:00
|
|
|
stat_busy "Loading Console Font: $CONSOLEFONT"
|
|
|
|
#CONSOLEMAP in UTF-8 shouldn't be used
|
2011-05-26 04:45:00 +00:00
|
|
|
[[ $CONSOLEMAP && ${LOCALE,,} =~ utf ]] && CONSOLEMAP=""
|
2011-01-02 05:56:57 +00:00
|
|
|
for i in /dev/tty[0-9]*; do
|
2011-05-26 04:45:00 +00:00
|
|
|
/usr/bin/setfont ${CONSOLEMAP:+-m ${CONSOLEMAP}} \
|
|
|
|
$CONSOLEFONT -C ${i} >/dev/null 2>&1
|
2011-01-02 05:56:57 +00:00
|
|
|
done
|
2011-05-26 04:45:00 +00:00
|
|
|
if (($? != 0)); then
|
2011-01-02 05:56:57 +00:00
|
|
|
stat_fail
|
2011-05-26 04:45:00 +00:00
|
|
|
elif [[ $CONSOLEMAP ]]; then
|
|
|
|
cat <<"EOF" >>/etc/profile.d/locale.sh
|
|
|
|
if [ "$CONSOLE" = "" -a "$TERM" = "linux" -a -t 1 ]; then printf "\033(K"; fi
|
|
|
|
EOF
|
|
|
|
stat_done
|
2011-01-02 05:56:57 +00:00
|
|
|
else
|
|
|
|
stat_done
|
|
|
|
fi
|
2010-07-28 19:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Source additional functions at the end to allow overrides
|
2009-10-08 03:40:19 +00:00
|
|
|
for f in /etc/rc.d/functions.d/*; do
|
2011-05-26 04:45:00 +00:00
|
|
|
[[ -e $f ]] && . "$f"
|
2009-10-08 03:40:19 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# End of file
|
2011-05-26 04:45:00 +00:00
|
|
|
# vim: set ts=2 sw=2 noet:
|