| 1 |
# Copyright 1999-2006 Gentoo Foundation
|
| 2 |
# Distributed under the terms of the GNU General Public License v2
|
| 3 |
|
| 4 |
RC_GOT_FUNCTIONS="yes"
|
| 5 |
|
| 6 |
# Override defaults with user settings ...
|
| 7 |
[[ -f /etc/conf.d/rc ]] && source /etc/conf.d/rc
|
| 8 |
|
| 9 |
# Check /etc/conf.d/rc for a description of these ...
|
| 10 |
declare -r svclib="/lib/rcscripts"
|
| 11 |
declare -r svcdir="${svcdir:-/var/lib/init.d}"
|
| 12 |
svcmount="${svcmount:-no}"
|
| 13 |
svcfstype="${svcfstype:-tmpfs}"
|
| 14 |
svcsize="${svcsize:-1024}"
|
| 15 |
|
| 16 |
# Different types of dependencies
|
| 17 |
deptypes="need use"
|
| 18 |
# Different types of order deps
|
| 19 |
ordtypes="before after"
|
| 20 |
|
| 21 |
#
|
| 22 |
# Internal variables
|
| 23 |
#
|
| 24 |
|
| 25 |
# Dont output to stdout?
|
| 26 |
RC_QUIET_STDOUT="${RC_QUIET_STDOUT:-no}"
|
| 27 |
RC_VERBOSE="${RC_VERBOSE:-no}"
|
| 28 |
|
| 29 |
# Should we use color?
|
| 30 |
RC_NOCOLOR="${RC_NOCOLOR:-no}"
|
| 31 |
# Can the terminal handle endcols?
|
| 32 |
RC_ENDCOL="yes"
|
| 33 |
|
| 34 |
#
|
| 35 |
# Default values for rc system
|
| 36 |
#
|
| 37 |
RC_TTY_NUMBER="${RC_TTY_NUMBER:-11}"
|
| 38 |
RC_PARALLEL_STARTUP="${RC_PARALLEL_STARTUP:-no}"
|
| 39 |
RC_NET_STRICT_CHECKING="${RC_NET_STRICT_CHECKING:-no}"
|
| 40 |
RC_USE_FSTAB="${RC_USE_FSTAB:-no}"
|
| 41 |
RC_USE_CONFIG_PROFILE="${RC_USE_CONFIG_PROFILE:-yes}"
|
| 42 |
RC_FORCE_AUTO="${RC_FORCE_AUTO:-no}"
|
| 43 |
RC_DEVICES="${RC_DEVICES:-auto}"
|
| 44 |
RC_DOWN_INTERFACE="${RC_DOWN_INTERFACE:-yes}"
|
| 45 |
|
| 46 |
#
|
| 47 |
# Default values for e-message indentation and dots
|
| 48 |
#
|
| 49 |
RC_INDENTATION=''
|
| 50 |
RC_DEFAULT_INDENT=2
|
| 51 |
#RC_DOT_PATTERN=' .'
|
| 52 |
RC_DOT_PATTERN=''
|
| 53 |
|
| 54 |
# void import_addon(char *addon)
|
| 55 |
#
|
| 56 |
# Import code from the specified addon if it exists
|
| 57 |
#
|
| 58 |
import_addon() {
|
| 59 |
local addon="${svclib}/addons/$1"
|
| 60 |
if [[ -r ${addon} ]] ; then
|
| 61 |
source "${addon}"
|
| 62 |
return 0
|
| 63 |
fi
|
| 64 |
return 1
|
| 65 |
}
|
| 66 |
|
| 67 |
# void splash(...)
|
| 68 |
#
|
| 69 |
# Notify bootsplash/splashutils/gensplash/whatever about
|
| 70 |
# important events.
|
| 71 |
#
|
| 72 |
splash() {
|
| 73 |
return 0
|
| 74 |
}
|
| 75 |
# This will override the splash() function...
|
| 76 |
if ! import_addon splash-functions.sh ; then
|
| 77 |
[[ -f /sbin/splash-functions.sh ]] && source /sbin/splash-functions.sh
|
| 78 |
fi
|
| 79 |
|
| 80 |
# void profiling(...)
|
| 81 |
#
|
| 82 |
# Notify bootsplash/whatever about important events.
|
| 83 |
#
|
| 84 |
profiling() {
|
| 85 |
return 0
|
| 86 |
}
|
| 87 |
import_addon profiling-functions.sh
|
| 88 |
|
| 89 |
# void bootlog(...)
|
| 90 |
#
|
| 91 |
# Notify bootlogger about important events.
|
| 92 |
bootlog() {
|
| 93 |
return 0
|
| 94 |
}
|
| 95 |
[[ ${RC_BOOTLOG} == "yes" ]] && import_addon bootlogger.sh
|
| 96 |
|
| 97 |
# void get_bootconfig()
|
| 98 |
#
|
| 99 |
# Get the BOOTLEVEL and SOFTLEVEL by setting
|
| 100 |
# 'bootlevel' and 'softlevel' via kernel
|
| 101 |
# parameters.
|
| 102 |
#
|
| 103 |
get_bootconfig() {
|
| 104 |
local copt=
|
| 105 |
local newbootlevel=
|
| 106 |
local newsoftlevel=
|
| 107 |
|
| 108 |
if [[ -r /proc/cmdline ]] ; then
|
| 109 |
for copt in $(</proc/cmdline) ; do
|
| 110 |
case "${copt%=*}" in
|
| 111 |
bootlevel)
|
| 112 |
newbootlevel="${copt##*=}"
|
| 113 |
;;
|
| 114 |
softlevel)
|
| 115 |
newsoftlevel="${copt##*=}"
|
| 116 |
;;
|
| 117 |
esac
|
| 118 |
done
|
| 119 |
fi
|
| 120 |
|
| 121 |
if [[ -n ${newbootlevel} ]] ; then
|
| 122 |
export BOOTLEVEL="${newbootlevel}"
|
| 123 |
else
|
| 124 |
export BOOTLEVEL="boot"
|
| 125 |
fi
|
| 126 |
|
| 127 |
if [[ -n ${newsoftlevel} ]] ; then
|
| 128 |
export DEFAULTLEVEL="${newsoftlevel}"
|
| 129 |
else
|
| 130 |
export DEFAULTLEVEL="default"
|
| 131 |
fi
|
| 132 |
|
| 133 |
return 0
|
| 134 |
}
|
| 135 |
|
| 136 |
setup_defaultlevels() {
|
| 137 |
get_bootconfig
|
| 138 |
|
| 139 |
if get_bootparam "noconfigprofile" ; then
|
| 140 |
export RC_USE_CONFIG_PROFILE="no"
|
| 141 |
|
| 142 |
elif get_bootparam "configprofile" ; then
|
| 143 |
export RC_USE_CONFIG_PROFILE="yes"
|
| 144 |
fi
|
| 145 |
|
| 146 |
if [[ ${RC_USE_CONFIG_PROFILE} == "yes" && -n ${DEFAULTLEVEL} ]] && \
|
| 147 |
[[ -d "/etc/runlevels/${BOOTLEVEL}.${DEFAULTLEVEL}" || \
|
| 148 |
-L "/etc/runlevels/${BOOTLEVEL}.${DEFAULTLEVEL}" ]] ; then
|
| 149 |
export BOOTLEVEL="${BOOTLEVEL}.${DEFAULTLEVEL}"
|
| 150 |
fi
|
| 151 |
|
| 152 |
if [[ -z ${SOFTLEVEL} ]] ; then
|
| 153 |
if [[ -f "${svcdir}/softlevel" ]] ; then
|
| 154 |
export SOFTLEVEL=$(< "${svcdir}/softlevel")
|
| 155 |
else
|
| 156 |
export SOFTLEVEL="${BOOTLEVEL}"
|
| 157 |
fi
|
| 158 |
fi
|
| 159 |
|
| 160 |
return 0
|
| 161 |
}
|
| 162 |
|
| 163 |
# void get_libdir(void)
|
| 164 |
#
|
| 165 |
# prints the current libdir {lib,lib32,lib64}
|
| 166 |
#
|
| 167 |
get_libdir() {
|
| 168 |
if [[ -n ${CONF_LIBDIR_OVERRIDE} ]] ; then
|
| 169 |
CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}"
|
| 170 |
elif [[ -x /usr/bin/portageq ]] ; then
|
| 171 |
CONF_LIBDIR="$(/usr/bin/portageq envvar CONF_LIBDIR)"
|
| 172 |
fi
|
| 173 |
echo "${CONF_LIBDIR:=lib}"
|
| 174 |
}
|
| 175 |
|
| 176 |
# void esyslog(char* priority, char* tag, char* message)
|
| 177 |
#
|
| 178 |
# use the system logger to log a message
|
| 179 |
#
|
| 180 |
esyslog() {
|
| 181 |
local pri=
|
| 182 |
local tag=
|
| 183 |
|
| 184 |
if [[ -x /usr/bin/logger ]] ; then
|
| 185 |
pri="$1"
|
| 186 |
tag="$2"
|
| 187 |
|
| 188 |
shift 2
|
| 189 |
[[ -z "$*" ]] && return 0
|
| 190 |
|
| 191 |
/usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
|
| 192 |
fi
|
| 193 |
|
| 194 |
return 0
|
| 195 |
}
|
| 196 |
|
| 197 |
# void eindent(int num)
|
| 198 |
#
|
| 199 |
# increase the indent used for e-commands.
|
| 200 |
#
|
| 201 |
eindent() {
|
| 202 |
local i="$1"
|
| 203 |
(( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
|
| 204 |
esetdent $(( ${#RC_INDENTATION} + i ))
|
| 205 |
}
|
| 206 |
|
| 207 |
# void eoutdent(int num)
|
| 208 |
#
|
| 209 |
# decrease the indent used for e-commands.
|
| 210 |
#
|
| 211 |
eoutdent() {
|
| 212 |
local i="$1"
|
| 213 |
(( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
|
| 214 |
esetdent $(( ${#RC_INDENTATION} - i ))
|
| 215 |
}
|
| 216 |
|
| 217 |
# void esetdent(int num)
|
| 218 |
#
|
| 219 |
# hard set the indent used for e-commands.
|
| 220 |
# num defaults to 0
|
| 221 |
#
|
| 222 |
esetdent() {
|
| 223 |
local i="$1"
|
| 224 |
(( i < 0 )) && (( i = 0 ))
|
| 225 |
RC_INDENTATION=$(printf "%${i}s" '')
|
| 226 |
}
|
| 227 |
|
| 228 |
# void einfo(char* message)
|
| 229 |
#
|
| 230 |
# show an informative message (with a newline)
|
| 231 |
#
|
| 232 |
einfo() {
|
| 233 |
einfon "$*\n"
|
| 234 |
LAST_E_CMD="einfo"
|
| 235 |
return 0
|
| 236 |
}
|
| 237 |
|
| 238 |
# void einfon(char* message)
|
| 239 |
#
|
| 240 |
# show an informative message (without a newline)
|
| 241 |
#
|
| 242 |
einfon() {
|
| 243 |
[[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
|
| 244 |
[[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
|
| 245 |
echo -ne " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
|
| 246 |
LAST_E_CMD="einfon"
|
| 247 |
return 0
|
| 248 |
}
|
| 249 |
|
| 250 |
# void ewarn(char* message)
|
| 251 |
#
|
| 252 |
# show a warning message + log it
|
| 253 |
#
|
| 254 |
ewarn() {
|
| 255 |
if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then
|
| 256 |
echo " $*"
|
| 257 |
else
|
| 258 |
[[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
|
| 259 |
echo -e " ${WARN}*${NORMAL} ${RC_INDENTATION}$*"
|
| 260 |
fi
|
| 261 |
|
| 262 |
local name="rc-scripts"
|
| 263 |
[[ $0 != "/sbin/runscript.sh" ]] && name="${0##*/}"
|
| 264 |
# Log warnings to system log
|
| 265 |
esyslog "daemon.warning" "${name}" "$*"
|
| 266 |
|
| 267 |
LAST_E_CMD="ewarn"
|
| 268 |
return 0
|
| 269 |
}
|
| 270 |
|
| 271 |
# void eerror(char* message)
|
| 272 |
#
|
| 273 |
# show an error message + log it
|
| 274 |
#
|
| 275 |
eerror() {
|
| 276 |
if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then
|
| 277 |
echo " $*" >/dev/stderr
|
| 278 |
else
|
| 279 |
[[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
|
| 280 |
echo -e " ${BAD}*${NORMAL} ${RC_INDENTATION}$*"
|
| 281 |
fi
|
| 282 |
|
| 283 |
local name="rc-scripts"
|
| 284 |
[[ $0 != "/sbin/runscript.sh" ]] && name="${0##*/}"
|
| 285 |
# Log errors to system log
|
| 286 |
esyslog "daemon.err" "rc-scripts" "$*"
|
| 287 |
|
| 288 |
LAST_E_CMD="eerror"
|
| 289 |
return 0
|
| 290 |
}
|
| 291 |
|
| 292 |
# void ebegin(char* message)
|
| 293 |
#
|
| 294 |
# show a message indicating the start of a process
|
| 295 |
#
|
| 296 |
ebegin() {
|
| 297 |
local msg="$*" dots spaces="${RC_DOT_PATTERN//?/ }"
|
| 298 |
[[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
|
| 299 |
|
| 300 |
if [[ -n ${RC_DOT_PATTERN} ]] ; then
|
| 301 |
dots="$(printf "%$((COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7))s" '')"
|
| 302 |
dots="${dots//${spaces}/${RC_DOT_PATTERN}}"
|
| 303 |
msg="${msg}${dots}"
|
| 304 |
else
|
| 305 |
msg="${msg} ..."
|
| 306 |
fi
|
| 307 |
einfon "${msg}"
|
| 308 |
[[ ${RC_ENDCOL} == "yes" ]] && echo
|
| 309 |
|
| 310 |
LAST_E_LEN="$(( 3 + ${#RC_INDENTATION} + ${#msg} ))"
|
| 311 |
LAST_E_CMD="ebegin"
|
| 312 |
return 0
|
| 313 |
}
|
| 314 |
|
| 315 |
# void _eend(int error, char *efunc, char* errstr)
|
| 316 |
#
|
| 317 |
# indicate the completion of process, called from eend/ewend
|
| 318 |
# if error, show errstr via efunc
|
| 319 |
#
|
| 320 |
# This function is private to functions.sh. Do not call it from a
|
| 321 |
# script.
|
| 322 |
#
|
| 323 |
_eend() {
|
| 324 |
local retval="${1:-0}" efunc="${2:-eerror}" msg
|
| 325 |
shift 2
|
| 326 |
|
| 327 |
if [[ ${retval} == "0" ]] ; then
|
| 328 |
[[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
|
| 329 |
msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
|
| 330 |
else
|
| 331 |
if [[ -c /dev/null ]] ; then
|
| 332 |
rc_splash "stop" &>/dev/null &
|
| 333 |
else
|
| 334 |
rc_splash "stop" &
|
| 335 |
fi
|
| 336 |
if [[ -n $* ]] ; then
|
| 337 |
${efunc} "$*"
|
| 338 |
fi
|
| 339 |
msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
|
| 340 |
fi
|
| 341 |
|
| 342 |
if [[ ${RC_ENDCOL} == "yes" ]] ; then
|
| 343 |
echo -e "${ENDCOL} ${msg}"
|
| 344 |
else
|
| 345 |
[[ ${LAST_E_CMD} == ebegin ]] || LAST_E_LEN=0
|
| 346 |
printf "%$(( COLS - LAST_E_LEN - 6 ))s%b\n" '' "${msg}"
|
| 347 |
fi
|
| 348 |
|
| 349 |
return ${retval}
|
| 350 |
}
|
| 351 |
|
| 352 |
# void eend(int error, char* errstr)
|
| 353 |
#
|
| 354 |
# indicate the completion of process
|
| 355 |
# if error, show errstr via eerror
|
| 356 |
#
|
| 357 |
eend() {
|
| 358 |
local retval="${1:-0}"
|
| 359 |
shift
|
| 360 |
|
| 361 |
_eend "${retval}" eerror "$*"
|
| 362 |
|
| 363 |
LAST_E_CMD="eend"
|
| 364 |
return ${retval}
|
| 365 |
}
|
| 366 |
|
| 367 |
# void ewend(int error, char* errstr)
|
| 368 |
#
|
| 369 |
# indicate the completion of process
|
| 370 |
# if error, show errstr via ewarn
|
| 371 |
#
|
| 372 |
ewend() {
|
| 373 |
local retval="${1:-0}"
|
| 374 |
shift
|
| 375 |
|
| 376 |
_eend "${retval}" ewarn "$*"
|
| 377 |
|
| 378 |
LAST_E_CMD="ewend"
|
| 379 |
return ${retval}
|
| 380 |
}
|
| 381 |
|
| 382 |
# v-e-commands honor RC_VERBOSE which defaults to no.
|
| 383 |
# The condition is negated so the return value will be zero.
|
| 384 |
veinfo() { [[ ${RC_VERBOSE} != "yes" ]] || einfo "$@"; }
|
| 385 |
veinfon() { [[ ${RC_VERBOSE} != "yes" ]] || einfon "$@"; }
|
| 386 |
vewarn() { [[ ${RC_VERBOSE} != "yes" ]] || ewarn "$@"; }
|
| 387 |
veerror() { [[ ${RC_VERBOSE} != "yes" ]] || eerror "$@"; }
|
| 388 |
vebegin() { [[ ${RC_VERBOSE} != "yes" ]] || ebegin "$@"; }
|
| 389 |
veend() {
|
| 390 |
[[ ${RC_VERBOSE} == "yes" ]] && { eend "$@"; return $?; }
|
| 391 |
return ${1:-0}
|
| 392 |
}
|
| 393 |
vewend() {
|
| 394 |
[[ ${RC_VERBOSE} == "yes" ]] && { ewend "$@"; return $?; }
|
| 395 |
return ${1:-0}
|
| 396 |
}
|
| 397 |
|
| 398 |
# char *KV_major(string)
|
| 399 |
#
|
| 400 |
# Return the Major (X of X.Y.Z) kernel version
|
| 401 |
#
|
| 402 |
KV_major() {
|
| 403 |
[[ -z $1 ]] && return 1
|
| 404 |
|
| 405 |
local KV="$@"
|
| 406 |
echo "${KV%%.*}"
|
| 407 |
}
|
| 408 |
|
| 409 |
# char *KV_minor(string)
|
| 410 |
#
|
| 411 |
# Return the Minor (Y of X.Y.Z) kernel version
|
| 412 |
#
|
| 413 |
KV_minor() {
|
| 414 |
[[ -z $1 ]] && return 1
|
| 415 |
|
| 416 |
local KV="$@"
|
| 417 |
KV="${KV#*.}"
|
| 418 |
echo "${KV%%.*}"
|
| 419 |
}
|
| 420 |
|
| 421 |
# char *KV_micro(string)
|
| 422 |
#
|
| 423 |
# Return the Micro (Z of X.Y.Z) kernel version.
|
| 424 |
#
|
| 425 |
KV_micro() {
|
| 426 |
[[ -z $1 ]] && return 1
|
| 427 |
|
| 428 |
local KV="$@"
|
| 429 |
KV="${KV#*.*.}"
|
| 430 |
echo "${KV%%[^[:digit:]]*}"
|
| 431 |
}
|
| 432 |
|
| 433 |
# int KV_to_int(string)
|
| 434 |
#
|
| 435 |
# Convert a string type kernel version (2.4.0) to an int (132096)
|
| 436 |
# for easy compairing or versions ...
|
| 437 |
#
|
| 438 |
KV_to_int() {
|
| 439 |
[[ -z $1 ]] && return 1
|
| 440 |
|
| 441 |
local KV_MAJOR="$(KV_major "$1")"
|
| 442 |
local KV_MINOR="$(KV_minor "$1")"
|
| 443 |
local KV_MICRO="$(KV_micro "$1")"
|
| 444 |
local KV_int="$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))"
|
| 445 |
|
| 446 |
# We make version 2.2.0 the minimum version we will handle as
|
| 447 |
# a sanity check ... if its less, we fail ...
|
| 448 |
if [[ ${KV_int} -ge 131584 ]] ; then
|
| 449 |
echo "${KV_int}"
|
| 450 |
return 0
|
| 451 |
fi
|
| 452 |
|
| 453 |
return 1
|
| 454 |
}
|
| 455 |
|
| 456 |
# int get_KV()
|
| 457 |
#
|
| 458 |
# Return the kernel version (major, minor and micro concated) as an integer.
|
| 459 |
# Assumes X and Y of X.Y.Z are numbers. Also assumes that some leading
|
| 460 |
# portion of Z is a number.
|
| 461 |
# e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo
|
| 462 |
#
|
| 463 |
_RC_GET_KV_CACHE=""
|
| 464 |
get_KV() {
|
| 465 |
[[ -z ${_RC_GET_KV_CACHE} ]] \
|
| 466 |
&& _RC_GET_KV_CACHE="$(uname -r)"
|
| 467 |
|
| 468 |
echo "$(KV_to_int "${_RC_GET_KV_CACHE}")"
|
| 469 |
|
| 470 |
return $?
|
| 471 |
}
|
| 472 |
|
| 473 |
# bool get_bootparam(param)
|
| 474 |
#
|
| 475 |
# return 0 if gentoo=param was passed to the kernel
|
| 476 |
#
|
| 477 |
# EXAMPLE: if get_bootparam "nodevfs" ; then ....
|
| 478 |
#
|
| 479 |
get_bootparam() {
|
| 480 |
local x copt params retval=1
|
| 481 |
|
| 482 |
[[ ! -r /proc/cmdline ]] && return 1
|
| 483 |
|
| 484 |
for copt in $(< /proc/cmdline) ; do
|
| 485 |
if [[ ${copt%=*} == "gentoo" ]] ; then
|
| 486 |
params=$(gawk -v PARAMS="${copt##*=}" '
|
| 487 |
BEGIN {
|
| 488 |
split(PARAMS, nodes, ",")
|
| 489 |
for (x in nodes)
|
| 490 |
print nodes[x]
|
| 491 |
}')
|
| 492 |
|
| 493 |
# Parse gentoo option
|
| 494 |
for x in ${params} ; do
|
| 495 |
if [[ ${x} == "$1" ]] ; then
|
| 496 |
# echo "YES"
|
| 497 |
retval=0
|
| 498 |
fi
|
| 499 |
done
|
| 500 |
fi
|
| 501 |
done
|
| 502 |
|
| 503 |
return ${retval}
|
| 504 |
}
|
| 505 |
|
| 506 |
# Safer way to list the contents of a directory,
|
| 507 |
# as it do not have the "empty dir bug".
|
| 508 |
#
|
| 509 |
# char *dolisting(param)
|
| 510 |
#
|
| 511 |
# print a list of the directory contents
|
| 512 |
#
|
| 513 |
# NOTE: quote the params if they contain globs.
|
| 514 |
# also, error checking is not that extensive ...
|
| 515 |
#
|
| 516 |
dolisting() {
|
| 517 |
local x=
|
| 518 |
local y=
|
| 519 |
local tmpstr=
|
| 520 |
local mylist=
|
| 521 |
local mypath="$*"
|
| 522 |
|
| 523 |
if [[ ${mypath%/\*} != "${mypath}" ]] ; then
|
| 524 |
mypath=${mypath%/\*}
|
| 525 |
fi
|
| 526 |
|
| 527 |
for x in ${mypath} ; do
|
| 528 |
[[ ! -e ${x} ]] && continue
|
| 529 |
|
| 530 |
if [[ ! -d ${x} ]] && [[ -L ${x} || -f ${x} ]] ; then
|
| 531 |
mylist="${mylist} $(ls "${x}" 2> /dev/null)"
|
| 532 |
else
|
| 533 |
[[ ${x%/} != "${x}" ]] && x=${x%/}
|
| 534 |
|
| 535 |
cd "${x}"; tmpstr=$(ls)
|
| 536 |
|
| 537 |
for y in ${tmpstr} ; do
|
| 538 |
mylist="${mylist} ${x}/${y}"
|
| 539 |
done
|
| 540 |
fi
|
| 541 |
done
|
| 542 |
|
| 543 |
echo "${mylist}"
|
| 544 |
}
|
| 545 |
|
| 546 |
|
| 547 |
# char *add_suffix(char * configfile)
|
| 548 |
#
|
| 549 |
# Returns a config file name with the softlevel suffix
|
| 550 |
# appended to it. For use with multi-config services.
|
| 551 |
add_suffix() {
|
| 552 |
if [[ ${RC_USE_CONFIG_PROFILE} != "yes" ]] ; then
|
| 553 |
echo "$1"
|
| 554 |
return 0
|
| 555 |
fi
|
| 556 |
|
| 557 |
local suffix="${SOFTLEVEL}"
|
| 558 |
[[ ${SOFTLEVEL} == "${BOOTLEVEL}" \
|
| 559 |
|| ${SOFTLEVEL} == "reboot" \
|
| 560 |
|| ${SOFTLEVEL} == "shutdown" \
|
| 561 |
|| ${SOFTLEVEL} == "single" ]] \
|
| 562 |
&& suffix="${DEFAULTLEVEL}"
|
| 563 |
if [[ -e "$1.${suffix}" ]] ; then
|
| 564 |
echo "$1.${suffix}"
|
| 565 |
else
|
| 566 |
echo "$1"
|
| 567 |
fi
|
| 568 |
|
| 569 |
return 0
|
| 570 |
}
|
| 571 |
|
| 572 |
# char *get_base_ver()
|
| 573 |
#
|
| 574 |
# get the version of baselayout that this system is running
|
| 575 |
#
|
| 576 |
get_base_ver() {
|
| 577 |
[[ ! -r /etc/gentoo-release ]] && return 0
|
| 578 |
local ver="$(</etc/gentoo-release)"
|
| 579 |
echo "${ver##* }"
|
| 580 |
}
|
| 581 |
|
| 582 |
# Network filesystems list for common use in rc-scripts.
|
| 583 |
# This variable is used in is_net_fs and other places such as
|
| 584 |
# localmount.
|
| 585 |
NET_FS_LIST="afs cifs coda davfs fuse gfs ncpfs nfs nfs4 ocfs2 shfs smbfs"
|
| 586 |
|
| 587 |
# bool is_net_fs(path)
|
| 588 |
#
|
| 589 |
# return 0 if path is the mountpoint of a networked filesystem
|
| 590 |
#
|
| 591 |
# EXAMPLE: if is_net_fs / ; then ...
|
| 592 |
#
|
| 593 |
is_net_fs() {
|
| 594 |
local fstype
|
| 595 |
# /proc/mounts is always accurate but may not always be available
|
| 596 |
if [[ -e /proc/mounts ]] ; then
|
| 597 |
fstype="$( sed -n -e '/^rootfs/!s:.* '"$1"' \([^ ]*\).*:\1:p' /proc/mounts )"
|
| 598 |
else
|
| 599 |
fstype="$( mount | sed -n -e 's:.* on '"$1"' type \([^ ]*\).*:\1:p' )"
|
| 600 |
fi
|
| 601 |
[[ " ${NET_FS_LIST} " == *" ${fstype} "* ]]
|
| 602 |
return $?
|
| 603 |
}
|
| 604 |
|
| 605 |
# bool is_net_fs(path)
|
| 606 |
#
|
| 607 |
# return 0 if path is under unionfs control
|
| 608 |
#
|
| 609 |
# EXAMPLE: if is_union_fs / ; then ...
|
| 610 |
#
|
| 611 |
is_union_fs() {
|
| 612 |
[[ ! -x /sbin/unionctl ]] && return 1
|
| 613 |
unionctl "$1" --list &>/dev/null
|
| 614 |
}
|
| 615 |
|
| 616 |
# bool is_uml_sys()
|
| 617 |
#
|
| 618 |
# return 0 if the currently running system is User Mode Linux
|
| 619 |
#
|
| 620 |
# EXAMPLE: if is_uml_sys ; then ...
|
| 621 |
#
|
| 622 |
is_uml_sys() {
|
| 623 |
grep -qs 'UML' /proc/cpuinfo
|
| 624 |
}
|
| 625 |
|
| 626 |
# bool is_vserver_sys()
|
| 627 |
#
|
| 628 |
# return 0 if the currently running system is a Linux VServer
|
| 629 |
#
|
| 630 |
# EXAMPLE: if is_vserver_sys ; then ...
|
| 631 |
#
|
| 632 |
is_vserver_sys() {
|
| 633 |
grep -qs '^s_context:[[:space:]]*[1-9]' /proc/self/status
|
| 634 |
}
|
| 635 |
|
| 636 |
# bool is_xenU_sys()
|
| 637 |
#
|
| 638 |
# return 0 if the currently running system is an unprivileged Xen domain
|
| 639 |
#
|
| 640 |
# EXAMPLE: if is_xenU_sys ; then ...
|
| 641 |
#
|
| 642 |
is_xenU_sys() {
|
| 643 |
[[ ! -d /proc/xen ]] && return 1
|
| 644 |
[[ ! -e /proc/xen/capabilities ]] && return 0
|
| 645 |
grep -vq "control_d" /proc/xen/capabilities
|
| 646 |
}
|
| 647 |
|
| 648 |
# bool get_mount_fstab(path)
|
| 649 |
#
|
| 650 |
# return the parameters to pass to the mount command generated from fstab
|
| 651 |
#
|
| 652 |
# EXAMPLE: cmd=$( get_mount_fstab /proc )
|
| 653 |
# cmd=${cmd:--t proc none /proc}
|
| 654 |
# mount -n ${cmd}
|
| 655 |
#
|
| 656 |
get_mount_fstab() {
|
| 657 |
gawk '$1 ~ "^#" { next }
|
| 658 |
$2 == "'$*'" { stab="-t "$3" -o "$4" "$1" "$2; }
|
| 659 |
END { print stab; }
|
| 660 |
' /etc/fstab
|
| 661 |
}
|
| 662 |
|
| 663 |
# char *reverse_list(list)
|
| 664 |
#
|
| 665 |
# Returns the reversed order of list
|
| 666 |
#
|
| 667 |
reverse_list() {
|
| 668 |
for (( i = $# ; i > 0 ; --i )) ; do
|
| 669 |
echo -n "${!i} "
|
| 670 |
done
|
| 671 |
}
|
| 672 |
|
| 673 |
# void start_addon(addon)
|
| 674 |
#
|
| 675 |
# Starts addon.
|
| 676 |
#
|
| 677 |
start_addon() {
|
| 678 |
local addon="$1"
|
| 679 |
(import_addon "${addon}-start.sh")
|
| 680 |
return 0
|
| 681 |
}
|
| 682 |
|
| 683 |
# void start_volumes()
|
| 684 |
#
|
| 685 |
# Starts all volumes in RC_VOLUME_ORDER.
|
| 686 |
#
|
| 687 |
start_volumes() {
|
| 688 |
local x=
|
| 689 |
|
| 690 |
for x in ${RC_VOLUME_ORDER} ; do
|
| 691 |
start_addon "${x}"
|
| 692 |
done
|
| 693 |
|
| 694 |
return 0
|
| 695 |
}
|
| 696 |
|
| 697 |
# void stop_addon(addon)
|
| 698 |
#
|
| 699 |
# Stops addon.
|
| 700 |
#
|
| 701 |
stop_addon() {
|
| 702 |
local addon=$1
|
| 703 |
(import_addon "${addon}-stop.sh")
|
| 704 |
return 0
|
| 705 |
}
|
| 706 |
|
| 707 |
# void stop_volumes()
|
| 708 |
#
|
| 709 |
# Stops all volumes in RC_VOLUME_ORDER (reverse order).
|
| 710 |
#
|
| 711 |
stop_volumes() {
|
| 712 |
local x=
|
| 713 |
|
| 714 |
for x in $(reverse_list ${RC_VOLUME_ORDER}) ; do
|
| 715 |
stop_addon "${x}"
|
| 716 |
done
|
| 717 |
|
| 718 |
return 0
|
| 719 |
}
|
| 720 |
|
| 721 |
# bool is_older_than(reference, files/dirs to check)
|
| 722 |
#
|
| 723 |
# return 0 if any of the files/dirs are newer than
|
| 724 |
# the reference file
|
| 725 |
#
|
| 726 |
# EXAMPLE: if is_older_than a.out *.o ; then ...
|
| 727 |
is_older_than() {
|
| 728 |
local x=
|
| 729 |
local ref="$1"
|
| 730 |
shift
|
| 731 |
|
| 732 |
for x in "$@" ; do
|
| 733 |
if [[ -d ${x} ]] ; then
|
| 734 |
is_older_than "${ref}" "${x}"/* && return 0
|
| 735 |
elif [[ ${x} -nt ${ref} ]] ; then
|
| 736 |
return 0
|
| 737 |
fi
|
| 738 |
done
|
| 739 |
|
| 740 |
return 1
|
| 741 |
}
|
| 742 |
|
| 743 |
# char* bash_variable(char *variable)
|
| 744 |
#
|
| 745 |
# Turns the given variable into something that bash can use
|
| 746 |
# Basically replaces anything not a-z,A-Z into a _
|
| 747 |
#
|
| 748 |
bash_variable() {
|
| 749 |
local args="$@"
|
| 750 |
LC_ALL=C echo "${args//[![:word:]]/_}"
|
| 751 |
}
|
| 752 |
|
| 753 |
# void requote()
|
| 754 |
#
|
| 755 |
# Requotes params so they're suitable to be eval'd, just like this would:
|
| 756 |
# set -- 1 2 "3 4"
|
| 757 |
# /usr/bin/getopt -- '' "$@" | sed 's/^ -- //'
|
| 758 |
#
|
| 759 |
requote() {
|
| 760 |
local q=\'
|
| 761 |
set -- "${@//\'/$q\'$q}" # quote inner instances of '
|
| 762 |
set -- "${@/#/$q}" # add ' to start of each param
|
| 763 |
set -- "${@/%/$q}" # add ' to end of each param
|
| 764 |
echo "$*"
|
| 765 |
}
|
| 766 |
|
| 767 |
# char* uniqify(char *arg, ...)
|
| 768 |
#
|
| 769 |
# Ensure that params are unique
|
| 770 |
#
|
| 771 |
uniqify() {
|
| 772 |
local result= x=
|
| 773 |
while [[ -n "$1" ]] ; do
|
| 774 |
[[ " ${result} " != *" $1 "* ]] && result="${result} $1"
|
| 775 |
shift
|
| 776 |
done
|
| 777 |
echo "${result# *}"
|
| 778 |
}
|
| 779 |
|
| 780 |
##############################################################################
|
| 781 |
# #
|
| 782 |
# This should be the last code in here, please add all functions above!! #
|
| 783 |
# #
|
| 784 |
# *** START LAST CODE *** #
|
| 785 |
# #
|
| 786 |
##############################################################################
|
| 787 |
|
| 788 |
if [[ -z ${EBUILD} ]] ; then
|
| 789 |
# Setup a basic $PATH. Just add system default to existing.
|
| 790 |
# This should solve both /sbin and /usr/sbin not present when
|
| 791 |
# doing 'su -c foo', or for something like: PATH= rcscript start
|
| 792 |
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
|
| 793 |
|
| 794 |
# Cache the CONSOLETYPE - this is important as backgrounded shells don't
|
| 795 |
# have a TTY. rc unsets it at the end of running so it shouldn't hang
|
| 796 |
# around
|
| 797 |
if [[ -z ${CONSOLETYPE} ]] ; then
|
| 798 |
export CONSOLETYPE="$( /sbin/consoletype 2>/dev/null )"
|
| 799 |
fi
|
| 800 |
if [[ ${CONSOLETYPE} == "serial" ]] ; then
|
| 801 |
RC_NOCOLOR="yes"
|
| 802 |
RC_ENDCOL="no"
|
| 803 |
fi
|
| 804 |
|
| 805 |
for arg in "$@" ; do
|
| 806 |
case "${arg}" in
|
| 807 |
# Lastly check if the user disabled it with --nocolor argument
|
| 808 |
--nocolor|-nc)
|
| 809 |
RC_NOCOLOR="yes"
|
| 810 |
;;
|
| 811 |
esac
|
| 812 |
done
|
| 813 |
|
| 814 |
setup_defaultlevels
|
| 815 |
|
| 816 |
# If we are not /sbin/rc then ensure that we cannot change level variables
|
| 817 |
if [[ -n ${BASH_SOURCE} \
|
| 818 |
&& ${BASH_SOURCE[${#BASH_SOURCE[@]}-1]} != "/sbin/rc" ]] ; then
|
| 819 |
declare -r BOOTLEVEL DEFAULTLEVEL SOFTLEVEL
|
| 820 |
fi
|
| 821 |
else
|
| 822 |
# Should we use colors ?
|
| 823 |
if [[ $* != *depend* ]] ; then
|
| 824 |
# Check user pref in portage
|
| 825 |
RC_NOCOLOR="$(portageq envvar NOCOLOR 2>/dev/null)"
|
| 826 |
[[ ${RC_NOCOLOR} == "true" ]] && RC_NOCOLOR="yes"
|
| 827 |
else
|
| 828 |
# We do not want colors during emerge depend
|
| 829 |
RC_NOCOLOR="yes"
|
| 830 |
# No output is seen during emerge depend, so this is not needed.
|
| 831 |
RC_ENDCOL="no"
|
| 832 |
fi
|
| 833 |
fi
|
| 834 |
|
| 835 |
if [[ -n ${EBUILD} && $* == *depend* ]] ; then
|
| 836 |
# We do not want stty to run during emerge depend
|
| 837 |
COLS=80
|
| 838 |
else
|
| 839 |
# Setup COLS and ENDCOL so eend can line up the [ ok ]
|
| 840 |
COLS="${COLUMNS:-0}" # bash's internal COLUMNS variable
|
| 841 |
(( COLS == 0 )) && COLS="$(set -- `stty size 2>/dev/null` ; echo "$2")"
|
| 842 |
(( COLS > 0 )) || (( COLS = 80 )) # width of [ ok ] == 7
|
| 843 |
fi
|
| 844 |
|
| 845 |
if [[ ${RC_ENDCOL} == "yes" ]] ; then
|
| 846 |
ENDCOL=$'\e[A\e['$(( COLS - 8 ))'C'
|
| 847 |
else
|
| 848 |
ENDCOL=''
|
| 849 |
fi
|
| 850 |
|
| 851 |
# Setup the colors so our messages all look pretty
|
| 852 |
if [[ ${RC_NOCOLOR} == "yes" ]] ; then
|
| 853 |
unset GOOD WARN BAD NORMAL HILITE BRACKET
|
| 854 |
else
|
| 855 |
GOOD=$'\e[32;01m'
|
| 856 |
WARN=$'\e[33;01m'
|
| 857 |
BAD=$'\e[31;01m'
|
| 858 |
HILITE=$'\e[36;01m'
|
| 859 |
BRACKET=$'\e[34;01m'
|
| 860 |
NORMAL=$'\e[0m'
|
| 861 |
fi
|
| 862 |
|
| 863 |
##############################################################################
|
| 864 |
# #
|
| 865 |
# *** END LAST CODE *** #
|
| 866 |
# #
|
| 867 |
# This should be the last code in here, please add all functions above!! #
|
| 868 |
# #
|
| 869 |
##############################################################################
|
| 870 |
|
| 871 |
|
| 872 |
# vim:ts=4
|