/[vps]/baselayout-vserver/branches/baselayout-1_12/sbin/functions.sh
Gentoo

Diff of /baselayout-vserver/branches/baselayout-1_12/sbin/functions.sh

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 275 Revision 276
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3 3
4RC_GOT_FUNCTIONS="yes" 4RC_GOT_FUNCTIONS="yes"
5 5
6# Override defaults with user settings ... 6# Override defaults with user settings ...
7[ -f /etc/conf.d/rc ] && source /etc/conf.d/rc 7[[ -f /etc/conf.d/rc ]] && source /etc/conf.d/rc
8 8
9# daemontools dir 9# daemontools dir
10SVCDIR="/var/lib/supervise" 10SVCDIR="/var/lib/supervise"
11 11
12# Check /etc/conf.d/rc for a description of these ... 12# Check /etc/conf.d/rc for a description of these ...
13svclib="/lib/rcscripts" 13declare -r svclib="/lib/rcscripts"
14svcdir=${svcdir:-/var/lib/init.d} 14declare -r svcdir="${svcdir:-/var/lib/init.d}"
15 15
16# Different types of dependencies 16# Different types of dependencies
17deptypes="need use" 17deptypes="need use"
18# Different types of order deps 18# Different types of order deps
19ordtypes="before after" 19ordtypes="before after"
21# 21#
22# Internal variables 22# Internal variables
23# 23#
24 24
25# Dont output to stdout? 25# Dont output to stdout?
26RC_QUIET_STDOUT=${RC_QUIET_STDOUT:-no} 26RC_QUIET_STDOUT="${RC_QUIET_STDOUT:-no}"
27RC_VERBOSE=${RC_VERBOSE:-no} 27RC_VERBOSE="${RC_VERBOSE:-no}"
28 28
29# Should we use color? 29# Should we use color?
30RC_NOCOLOR=${RC_NOCOLOR:-no} 30RC_NOCOLOR="${RC_NOCOLOR:-no}"
31# Can the terminal handle endcols? 31# Can the terminal handle endcols?
32RC_ENDCOL="yes" 32RC_ENDCOL="yes"
33 33
34# 34#
35# Default values for rc system 35# Default values for rc system
36# 36#
37RC_TTY_NUMBER=${RC_TTY_NUMBER:-0} 37RC_TTY_NUMBER="${RC_TTY_NUMBER:-0}"
38RC_PARALLEL_STARTUP=${RC_PARALLEL_STARTUP:-no} 38RC_PARALLEL_STARTUP="${RC_PARALLEL_STARTUP:-no}"
39RC_NET_STRICT_CHECKING=${RC_NET_STRICT_CHECKING:-none} 39RC_NET_STRICT_CHECKING="${RC_NET_STRICT_CHECKING:-none}"
40RC_DOWN_INTERFACE=${RC_DOWN_INTERFACE:-yes} 40RC_DOWN_INTERFACE="${RC_DOWN_INTERFACE:-yes}"
41 41
42# 42#
43# Default values for e-message indentation and dots 43# Default values for e-message indentation and dots
44# 44#
45RC_INDENTATION='' 45RC_INDENTATION=''
117# 117#
118get_libdir() { 118get_libdir() {
119 if [[ -n ${CONF_LIBDIR_OVERRIDE} ]] ; then 119 if [[ -n ${CONF_LIBDIR_OVERRIDE} ]] ; then
120 CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}" 120 CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}"
121 elif [[ -x /usr/bin/portageq ]] ; then 121 elif [[ -x /usr/bin/portageq ]] ; then
122 CONF_LIBDIR=$(/usr/bin/portageq envvar CONF_LIBDIR) 122 CONF_LIBDIR="$(/usr/bin/portageq envvar CONF_LIBDIR)"
123 fi 123 fi
124 echo ${CONF_LIBDIR:=lib} 124 echo "${CONF_LIBDIR:=lib}"
125} 125}
126 126
127# void esyslog(char* priority, char* tag, char* message) 127# void esyslog(char* priority, char* tag, char* message)
128# 128#
129# use the system logger to log a message 129# use the system logger to log a message
239# void ebegin(char* message) 239# void ebegin(char* message)
240# 240#
241# show a message indicating the start of a process 241# show a message indicating the start of a process
242# 242#
243ebegin() { 243ebegin() {
244 local msg="$*" dots spaces=${RC_DOT_PATTERN//?/ } 244 local msg="$*" dots spaces="${RC_DOT_PATTERN//?/ }"
245 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0 245 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
246 246
247 if [[ -n ${RC_DOT_PATTERN} ]] ; then 247 if [[ -n ${RC_DOT_PATTERN} ]] ; then
248 dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '') 248 dots="$(printf "%$((COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7))s" '')"
249 dots=${dots//${spaces}/${RC_DOT_PATTERN}} 249 dots="${dots//${spaces}/${RC_DOT_PATTERN}}"
250 msg="${msg}${dots}" 250 msg="${msg}${dots}"
251 else 251 else
252 msg="${msg} ..." 252 msg="${msg} ..."
253 fi 253 fi
254 einfon "${msg}" 254 einfon "${msg}"
255 [[ ${RC_ENDCOL} == "yes" ]] && echo 255 [[ ${RC_ENDCOL} == "yes" ]] && echo
256 256
257 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} )) 257 LAST_E_LEN="$(( 3 + ${#RC_INDENTATION} + ${#msg} ))"
258 LAST_E_CMD="ebegin" 258 LAST_E_CMD="ebegin"
259 return 0 259 return 0
260} 260}
261 261
262# void _eend(int error, char *efunc, char* errstr) 262# void _eend(int error, char *efunc, char* errstr)
266# 266#
267# This function is private to functions.sh. Do not call it from a 267# This function is private to functions.sh. Do not call it from a
268# script. 268# script.
269# 269#
270_eend() { 270_eend() {
271 local retval=${1:-0} efunc=${2:-eerror} msg 271 local retval="${1:-0}" efunc="${2:-eerror}" msg
272 shift 2 272 shift 2
273 273
274 if [[ ${retval} == "0" ]] ; then 274 if [[ ${retval} == "0" ]] ; then
275 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0 275 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
276 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}" 276 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
295# 295#
296# indicate the completion of process 296# indicate the completion of process
297# if error, show errstr via eerror 297# if error, show errstr via eerror
298# 298#
299eend() { 299eend() {
300 local retval=${1:-0} 300 local retval="${1:-0}"
301 shift 301 shift
302 302
303 _eend ${retval} eerror "$*" 303 _eend "${retval}" eerror "$*"
304 304
305 LAST_E_CMD="eend" 305 LAST_E_CMD="eend"
306 return ${retval} 306 return ${retval}
307} 307}
308 308
310# 310#
311# indicate the completion of process 311# indicate the completion of process
312# if error, show errstr via ewarn 312# if error, show errstr via ewarn
313# 313#
314ewend() { 314ewend() {
315 local retval=${1:-0} 315 local retval="${1:-0}"
316 shift 316 shift
317 317
318 _eend ${retval} ewarn "$*" 318 _eend "${retval}" ewarn "$*"
319 319
320 LAST_E_CMD="ewend" 320 LAST_E_CMD="ewend"
321 return ${retval} 321 return ${retval}
322} 322}
323 323
342# Return the Major (X of X.Y.Z) kernel version 342# Return the Major (X of X.Y.Z) kernel version
343# 343#
344KV_major() { 344KV_major() {
345 [[ -z $1 ]] && return 1 345 [[ -z $1 ]] && return 1
346 346
347 local KV=$@ 347 local KV="$@"
348 echo "${KV%%.*}" 348 echo "${KV%%.*}"
349} 349}
350 350
351# char *KV_minor(string) 351# char *KV_minor(string)
352# 352#
353# Return the Minor (Y of X.Y.Z) kernel version 353# Return the Minor (Y of X.Y.Z) kernel version
354# 354#
355KV_minor() { 355KV_minor() {
356 [[ -z $1 ]] && return 1 356 [[ -z $1 ]] && return 1
357 357
358 local KV=$@ 358 local KV="$@"
359 KV=${KV#*.} 359 KV="${KV#*.}"
360 echo "${KV%%.*}" 360 echo "${KV%%.*}"
361} 361}
362 362
363# char *KV_micro(string) 363# char *KV_micro(string)
364# 364#
365# Return the Micro (Z of X.Y.Z) kernel version. 365# Return the Micro (Z of X.Y.Z) kernel version.
366# 366#
367KV_micro() { 367KV_micro() {
368 [[ -z $1 ]] && return 1 368 [[ -z $1 ]] && return 1
369 369
370 local KV=$@ 370 local KV="$@"
371 KV=${KV#*.*.} 371 KV="${KV#*.*.}"
372 echo "${KV%%[^[:digit:]]*}" 372 echo "${KV%%[^[:digit:]]*}"
373} 373}
374 374
375# int KV_to_int(string) 375# int KV_to_int(string)
376# 376#
378# for easy compairing or versions ... 378# for easy compairing or versions ...
379# 379#
380KV_to_int() { 380KV_to_int() {
381 [[ -z $1 ]] && return 1 381 [[ -z $1 ]] && return 1
382 382
383 local KV_MAJOR=$(KV_major "$1") 383 local KV_MAJOR="$(KV_major "$1")"
384 local KV_MINOR=$(KV_minor "$1") 384 local KV_MINOR="$(KV_minor "$1")"
385 local KV_MICRO=$(KV_micro "$1") 385 local KV_MICRO="$(KV_micro "$1")"
386 local KV_int=$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO )) 386 local KV_int="$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))"
387 387
388 # We make version 2.2.0 the minimum version we will handle as 388 # We make version 2.2.0 the minimum version we will handle as
389 # a sanity check ... if its less, we fail ... 389 # a sanity check ... if its less, we fail ...
390 if [[ ${KV_int} -ge 131584 ]] ; then 390 if [[ ${KV_int} -ge 131584 ]] ; then
391 echo "${KV_int}" 391 echo "${KV_int}"
403# e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo 403# e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo
404# 404#
405_RC_GET_KV_CACHE="" 405_RC_GET_KV_CACHE=""
406get_KV() { 406get_KV() {
407 [[ -z ${_RC_GET_KV_CACHE} ]] \ 407 [[ -z ${_RC_GET_KV_CACHE} ]] \
408 && _RC_GET_KV_CACHE=$(uname -r) 408 && _RC_GET_KV_CACHE="$(uname -r)"
409 409
410 echo $(KV_to_int "${_RC_GET_KV_CACHE}") 410 echo "$(KV_to_int "${_RC_GET_KV_CACHE}")"
411 411
412 return $? 412 return $?
413} 413}
414 414
415# bool get_bootparam(param) 415# bool get_bootparam(param)
468# 468#
469save_options() { 469save_options() {
470 local myopts="$1" 470 local myopts="$1"
471 471
472 shift 472 shift
473 if [[ ! -d "${svcdir}/options/${myservice}" ]] ; then 473 if [[ ! -d "${svcdir}/options/${SVCNAME}" ]] ; then
474 mkdir -p -m 0755 "${svcdir}/options/${myservice}" 474 mkdir -p -m 0755 "${svcdir}/options/${SVCNAME}"
475 fi 475 fi
476 476
477 echo "$*" > "${svcdir}/options/${myservice}/${myopts}" 477 echo "$*" > "${svcdir}/options/${SVCNAME}/${myopts}"
478 478
479 return 0 479 return 0
480} 480}
481 481
482# char *get_options(char *option) 482# char *get_options(char *option)
483# 483#
484# get the "optstring" for "option" that was saved 484# get the "optstring" for "option" that was saved
485# by calling the save_options function 485# by calling the save_options function
486# 486#
487get_options() { 487get_options() {
488 if [[ -f "${svcdir}/options/${myservice}/$1" ]] ; then 488 if [[ -f "${svcdir}/options/${SVCNAME}/$1" ]] ; then
489 echo "$(< ${svcdir}/options/${myservice}/$1)" 489 echo "$(< ${svcdir}/options/${SVCNAME}/$1)"
490 fi 490 fi
491 491
492 return 0 492 return 0
493} 493}
494 494
505# 505#
506# get the version of baselayout that this system is running 506# get the version of baselayout that this system is running
507# 507#
508get_base_ver() { 508get_base_ver() {
509 [[ ! -r /etc/gentoo-release ]] && return 0 509 [[ ! -r /etc/gentoo-release ]] && return 0
510 local ver=$(</etc/gentoo-release) 510 local ver="$(</etc/gentoo-release)"
511 echo "${ver##* }" 511 echo "${ver##* }"
512} 512}
513 513
514# Network filesystems list for common use in rc-scripts. 514# Network filesystems list for common use in rc-scripts.
515# This variable is used in is_net_fs and other places such as 515# This variable is used in is_net_fs and other places such as
680 680
681 # Cache the CONSOLETYPE - this is important as backgrounded shells don't 681 # Cache the CONSOLETYPE - this is important as backgrounded shells don't
682 # have a TTY. rc unsets it at the end of running so it shouldn't hang 682 # have a TTY. rc unsets it at the end of running so it shouldn't hang
683 # around 683 # around
684 if [[ -z ${CONSOLETYPE} ]] ; then 684 if [[ -z ${CONSOLETYPE} ]] ; then
685 export CONSOLETYPE=$( /sbin/consoletype 2>/dev/null ) 685 export CONSOLETYPE="$( /sbin/consoletype 2>/dev/null )"
686 fi 686 fi
687 if [[ ${CONSOLETYPE} == "serial" ]] ; then 687 if [[ ${CONSOLETYPE} == "serial" ]] ; then
688 RC_NOCOLOR="yes" 688 RC_NOCOLOR="yes"
689 RC_ENDCOL="no" 689 RC_ENDCOL="no"
690 fi 690 fi
697 ;; 697 ;;
698 esac 698 esac
699 done 699 done
700 700
701 setup_defaultlevels 701 setup_defaultlevels
702
703 # If we are not /sbin/rc then ensure that we cannot change level variables
704 if [[ -n ${BASH_SOURCE} \
705 && ${BASH_SOURCE[${#BASH_SOURCE[@]}-1]} != "/sbin/rc" ]] ; then
706 declare -r BOOTLEVEL DEFAULTLEVEL SOFTLEVEL
707 fi
702else 708else
703 setup_defaultlevels 709 setup_defaultlevels
704 # Should we use colors ? 710 # Should we use colors ?
705 if [[ $* != *depend* ]] ; then 711 if [[ $* != *depend* ]] ; then
706 # Check user pref in portage 712 # Check user pref in portage
717if [[ -n ${EBUILD} && $* == *depend* ]] ; then 723if [[ -n ${EBUILD} && $* == *depend* ]] ; then
718 # We do not want stty to run during emerge depend 724 # We do not want stty to run during emerge depend
719 COLS=80 725 COLS=80
720else 726else
721 # Setup COLS and ENDCOL so eend can line up the [ ok ] 727 # Setup COLS and ENDCOL so eend can line up the [ ok ]
722 COLS=${COLUMNS:-0} # bash's internal COLUMNS variable 728 COLS="${COLUMNS:-0}" # bash's internal COLUMNS variable
723 (( COLS == 0 )) && COLS=$(stty size 2>/dev/null | cut -d' ' -f2) 729 (( COLS == 0 )) && COLS="$(set -- `stty size 2>/dev/null` ; echo "$2")"
724 (( COLS > 0 )) || (( COLS = 80 )) # width of [ ok ] == 7 730 (( COLS > 0 )) || (( COLS = 80 )) # width of [ ok ] == 7
725fi 731fi
726 732
727if [[ ${RC_ENDCOL} == "yes" ]] ; then 733if [[ ${RC_ENDCOL} == "yes" ]] ; then
728 ENDCOL=$'\e[A\e['$(( COLS - 8 ))'C' 734 ENDCOL=$'\e[A\e['$(( COLS - 8 ))'C'

Legend:
Removed from v.275  
changed lines
  Added in v.276

  ViewVC Help
Powered by ViewVC 1.1.20