diff options
author | Benedikt Boehm <hollow@gentoo.org> | 2005-09-26 14:29:47 +0000 |
---|---|---|
committer | Benedikt Boehm <hollow@gentoo.org> | 2005-09-26 14:29:47 +0000 |
commit | 04e3d8d8d4d5099d0be47fc3db870bd5f9ffc134 (patch) | |
tree | 0a263ff3b2d8bd27d58ed29fc8cf551f2b58c2d4 /sbin | |
parent | fix init timeout (diff) | |
download | baselayout-vserver-04e3d8d8d4d5099d0be47fc3db870bd5f9ffc134.tar.gz baselayout-vserver-04e3d8d8d4d5099d0be47fc3db870bd5f9ffc134.tar.bz2 baselayout-vserver-04e3d8d8d4d5099d0be47fc3db870bd5f9ffc134.zip |
sync with baselayout trunk, rev 1550
svn path=/baselayout-vserver/trunk/; revision=41
Diffstat (limited to 'sbin')
-rwxr-xr-x | sbin/depscan.sh | 4 | ||||
-rwxr-xr-x | sbin/env-update.sh | 11 | ||||
-rwxr-xr-x | sbin/functions.sh | 151 | ||||
-rwxr-xr-x | sbin/init-common-post.sh | 58 | ||||
-rwxr-xr-x | sbin/init-common-pre.sh | 7 | ||||
-rwxr-xr-x | sbin/init-functions.sh | 57 | ||||
-rwxr-xr-x | sbin/init.linux.sh | 43 | ||||
-rwxr-xr-x | sbin/rc | 189 | ||||
-rwxr-xr-x | sbin/rc-daemon.sh | 13 | ||||
-rwxr-xr-x | sbin/rc-help.sh | 116 | ||||
-rwxr-xr-x | sbin/rc-services.sh | 164 | ||||
-rwxr-xr-x | sbin/runscript.sh | 32 |
12 files changed, 470 insertions, 375 deletions
diff --git a/sbin/depscan.sh b/sbin/depscan.sh index 1fbf0ef..59bda9d 100755 --- a/sbin/depscan.sh +++ b/sbin/depscan.sh @@ -9,7 +9,7 @@ if [[ $1 == "--debug" ]] ; then set -x fi -if [[ ! -d ${svcdir} ]]; then +if [[ ! -d ${svcdir} ]] ; then if ! mkdir -p -m 0755 "${svcdir}" 2>/dev/null ; then eerror "Could not create needed directory '${svcdir}'!" fi @@ -18,7 +18,7 @@ fi for x in softscripts snapshot options daemons \ started starting inactive stopping failed \ exclusive exitcodes ; do - if [[ ! -d "${svcdir}/${x}" ]] ; then + if [[ ! -d ${svcdir}/${x} ]] ; then if ! mkdir -p -m 0755 "${svcdir}/${x}" 2>/dev/null ; then eerror "Could not create needed directory '${svcdir}/${x}'!" fi diff --git a/sbin/env-update.sh b/sbin/env-update.sh index 533e989..975203d 100755 --- a/sbin/env-update.sh +++ b/sbin/env-update.sh @@ -4,8 +4,7 @@ source /sbin/functions.sh || exit 1 -if [ "${EUID}" -ne 0 ] -then +if [[ ${EUID} -ne 0 ]] ; then eerror "$0: must be root." exit 1 fi @@ -20,17 +19,15 @@ note: exit 1 } -export SVCDIR="${svcdir}" +export SVCDIR=${svcdir} # Only update if files have actually changed -if [ "$1" == "-u" ] -then +if [[ $1 == "-u" ]] ; then is_older_than "${svcdir}/envcache" /etc/env.d && exit 0 shift fi -if [ "$#" -ne 0 ] -then +if [[ $# -ne 0 ]] ; then usage else /bin/gawk \ diff --git a/sbin/functions.sh b/sbin/functions.sh index e9bf234..5c5d870 100755 --- a/sbin/functions.sh +++ b/sbin/functions.sh @@ -3,12 +3,15 @@ RC_GOT_FUNCTIONS="yes" +# Override defaults with user settings ... +[ -f /etc/conf.d/rc ] && source /etc/conf.d/rc + # daemontools dir SVCDIR="/var/lib/supervise" # Check /etc/conf.d/rc for a description of these ... -svcdir="/var/lib/init.d" svclib="/lib/rcscripts" +svcdir=${svcdir:-/var/lib/init.d} # Different types of dependencies deptypes="need use" @@ -31,8 +34,8 @@ RC_ENDCOL="yes" # # Default values for rc system # -RC_TTY_NUMBER=0 -RC_PARALLEL_STARTUP="no" +RC_TTY_NUMBER=${RC_TTY_NUMBER:-0} +RC_PARALLEL_STARTUP=${RC_PARALLEL_STARTUP:-no} # # Default values for e-message indentation and dots @@ -42,9 +45,6 @@ RC_DEFAULT_INDENT=2 #RC_DOT_PATTERN=' .' RC_DOT_PATTERN='' -# Override defaults with user settings ... -[ -f /etc/conf.d/rc ] && source /etc/conf.d/rc - # void import_addon(char *addon) # # Import code from the specified addon if it exists @@ -98,11 +98,11 @@ get_bootconfig() { setup_defaultlevels() { get_bootconfig - if [ -z "${SOFTLEVEL}" ] ; then - if [ -f "${svcdir}/softlevel" ] ; then - export SOFTLEVEL="$(< ${svcdir}/softlevel)" + if [[ -z ${SOFTLEVEL} ]] ; then + if [[ -f ${svcdir}/softlevel ]] ; then + export SOFTLEVEL=$(< "${svcdir}/softlevel") else - export SOFTLEVEL="${BOOTLEVEL}" + export SOFTLEVEL=${BOOTLEVEL} fi fi @@ -114,10 +114,10 @@ setup_defaultlevels() { # prints the current libdir {lib,lib32,lib64} # get_libdir() { - if [ -n "${CONF_LIBDIR_OVERRIDE}" ] ; then - CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}" - elif [ -x "/usr/bin/portageq" ] ; then - CONF_LIBDIR="$(/usr/bin/portageq envvar CONF_LIBDIR)" + if [[ -n ${CONF_LIBDIR_OVERRIDE} ]] ; then + CONF_LIBDIR=${CONF_LIBDIR_OVERRIDE} + elif [[ -x /usr/bin/portageq ]] ; then + CONF_LIBDIR=$(/usr/bin/portageq envvar CONF_LIBDIR) fi echo ${CONF_LIBDIR:=lib} } @@ -130,10 +130,9 @@ esyslog() { local pri= local tag= - if [ -x /usr/bin/logger ] - then - pri="$1" - tag="$2" + if [[ -x /usr/bin/logger ]] ; then + pri=$1 + tag=$2 shift 2 [[ -z "$*" ]] && return 0 @@ -181,7 +180,7 @@ esetdent() { # einfo() { einfon "$*\n" - LAST_E_CMD=einfo + LAST_E_CMD="einfo" return 0 } @@ -190,10 +189,10 @@ einfo() { # show an informative message (without a newline) # einfon() { - [[ ${RC_QUIET_STDOUT} == yes ]] && return 0 - [[ ${RC_ENDCOL} != yes && ${LAST_E_CMD} == ebegin ]] && echo + [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0 + [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo echo -ne " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*" - LAST_E_CMD=einfon + LAST_E_CMD="einfon" return 0 } @@ -202,17 +201,17 @@ einfon() { # show a warning message + log it # ewarn() { - if [[ ${RC_QUIET_STDOUT} == yes ]]; then + if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then echo " $*" else - [[ ${RC_ENDCOL} != yes && ${LAST_E_CMD} == ebegin ]] && echo + [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo echo -e " ${WARN}*${NORMAL} ${RC_INDENTATION}$*" fi # Log warnings to system log esyslog "daemon.warning" "rc-scripts" "$*" - LAST_E_CMD=ewarn + LAST_E_CMD="ewarn" return 0 } @@ -221,17 +220,17 @@ ewarn() { # show an error message + log it # eerror() { - if [[ ${RC_QUIET_STDOUT} == yes ]]; then + if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then echo " $*" >/dev/stderr else - [[ ${RC_ENDCOL} != yes && ${LAST_E_CMD} == ebegin ]] && echo + [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo echo -e " ${BAD}*${NORMAL} ${RC_INDENTATION}$*" fi # Log errors to system log esyslog "daemon.err" "rc-scripts" "$*" - LAST_E_CMD=eerror + LAST_E_CMD="eerror" return 0 } @@ -241,9 +240,9 @@ eerror() { # ebegin() { local msg="$*" dots spaces=${RC_DOT_PATTERN//?/ } - [[ ${RC_QUIET_STDOUT} == yes ]] && return 0 + [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0 - if [[ -n ${RC_DOT_PATTERN} ]]; then + if [[ -n ${RC_DOT_PATTERN} ]] ; then dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '') dots=${dots//${spaces}/${RC_DOT_PATTERN}} msg="${msg}${dots}" @@ -251,10 +250,10 @@ ebegin() { msg="${msg} ..." fi einfon "${msg}" - [[ ${RC_ENDCOL} == yes ]] && echo + [[ ${RC_ENDCOL} == "yes" ]] && echo LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} )) - LAST_E_CMD=ebegin + LAST_E_CMD="ebegin" return 0 } @@ -270,17 +269,17 @@ _eend() { local retval=${1:-0} efunc=${2:-eerror} msg shift 2 - if [[ ${retval} == 0 ]]; then - [[ ${RC_QUIET_STDOUT} == yes ]] && return 0 + if [[ ${retval} == "0" ]]; then + [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}" else - if [[ -n "$*" ]]; then + if [[ -n $* ]] ; then ${efunc} "$*" fi msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}" fi - if [[ ${RC_ENDCOL} == yes ]]; then + if [[ ${RC_ENDCOL} == "yes" ]] ; then echo -e "${ENDCOL} ${msg}" else [[ ${LAST_E_CMD} == ebegin ]] || LAST_E_LEN=0 @@ -301,8 +300,8 @@ eend() { _eend ${retval} eerror "$*" - LAST_E_CMD=eend - return $retval + LAST_E_CMD="eend" + return ${retval} } # void ewend(int error, char* errstr) @@ -316,23 +315,23 @@ ewend() { _eend ${retval} ewarn "$*" - LAST_E_CMD=ewend - return $retval + LAST_E_CMD="ewend" + return ${retval} } # v-e-commands honor RC_VERBOSE which defaults to no. # The condition is negated so the return value will be zero. -veinfo() { [[ "${RC_VERBOSE}" != yes ]] || einfo "$@"; } -veinfon() { [[ "${RC_VERBOSE}" != yes ]] || einfon "$@"; } -vewarn() { [[ "${RC_VERBOSE}" != yes ]] || ewarn "$@"; } -veerror() { [[ "${RC_VERBOSE}" != yes ]] || eerror "$@"; } -vebegin() { [[ "${RC_VERBOSE}" != yes ]] || ebegin "$@"; } +veinfo() { [[ ${RC_VERBOSE} != "yes" ]] || einfo "$@"; } +veinfon() { [[ ${RC_VERBOSE} != "yes" ]] || einfon "$@"; } +vewarn() { [[ ${RC_VERBOSE} != "yes" ]] || ewarn "$@"; } +veerror() { [[ ${RC_VERBOSE} != "yes" ]] || eerror "$@"; } +vebegin() { [[ ${RC_VERBOSE} != "yes" ]] || ebegin "$@"; } veend() { - [[ "${RC_VERBOSE}" == yes ]] && { eend "$@"; return $?; } + [[ ${RC_VERBOSE} == "yes" ]] && { eend "$@"; return $?; } return ${1:-0} } veend() { - [[ "${RC_VERBOSE}" == yes ]] && { ewend "$@"; return $?; } + [[ ${RC_VERBOSE} == "yes" ]] && { ewend "$@"; return $?; } return ${1:-0} } @@ -344,7 +343,7 @@ KV_major() { [[ -z $1 ]] && return 1 local KV=$@ - echo ${KV%%.*} + echo "${KV%%.*}" } # char *KV_minor(string) @@ -356,7 +355,7 @@ KV_minor() { local KV=$@ KV=${KV#*.} - echo ${KV%%.*} + echo "${KV%%.*}" } # char *KV_micro(string) @@ -368,7 +367,7 @@ KV_micro() { local KV=$@ KV=${KV#*.*.} - echo ${KV%%[^[:digit:]]*} + echo "${KV%%[^[:digit:]]*}" } # int KV_to_int(string) @@ -404,7 +403,7 @@ KV_to_int() { get_KV() { local KV=$(uname -r) - echo $(KV_to_int "${KV}") + echo "$(KV_to_int "${KV}")" return $? } @@ -436,25 +435,21 @@ dolisting() { local mylist= local mypath="$*" - if [ "${mypath%/\*}" != "${mypath}" ] - then - mypath="${mypath%/\*}" + if [[ ${mypath%/\*} != "${mypath}" ]] ; then + mypath=${mypath%/\*} fi - for x in ${mypath} - do - [ ! -e "${x}" ] && continue + for x in ${mypath} ; do + [[ ! -e ${x} ]] && continue - if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] ) - then + if [[ ! -d ${x} ]] && [[ -L ${x} || -f ${x} ]] ; then mylist="${mylist} $(ls "${x}" 2> /dev/null)" else - [ "${x%/}" != "${x}" ] && x="${x%/}" + [[ ${x%/} != "${x}" ]] && x=${x%/} - cd "${x}"; tmpstr="$(ls)" + cd "${x}"; tmpstr=$(ls) - for y in ${tmpstr} - do + for y in ${tmpstr} ; do mylist="${mylist} ${x}/${y}" done fi @@ -468,11 +463,10 @@ dolisting() { # save the settings ("optstring") for "option" # save_options() { - local myopts="$1" + local myopts=$1 shift - if [ ! -d "${svcdir}/options/${myservice}" ] - then + if [[ ! -d ${svcdir}/options/${myservice} ]] ; then mkdir -p -m 0755 "${svcdir}/options/${myservice}" fi @@ -487,8 +481,7 @@ save_options() { # by calling the save_options function # get_options() { - if [ -f "${svcdir}/options/${myservice}/$1" ] - then + if [[ -f ${svcdir}/options/${myservice}/$1 ]] ; then echo "$(< ${svcdir}/options/${myservice}/$1)" fi @@ -511,7 +504,7 @@ add_suffix() { get_base_ver() { [[ ! -r /etc/gentoo-release ]] && return 0 local ver=$(</etc/gentoo-release) - echo ${ver##* } + echo "${ver##* }" } # Network filesystems list for common use in rc-scripts. @@ -580,7 +573,7 @@ get_mount_fstab() { # Returns the reversed order of list # reverse_list() { - for (( i = $# ; i > 0 ; --i )); do + for (( i = $# ; i > 0 ; --i )) ; do echo -n "${!i} " done } @@ -591,7 +584,7 @@ reverse_list() { # start_addon() { local addon=$1 - (import_addon ${addon}-start.sh) + (import_addon "${addon}-start.sh") return 0 } @@ -609,7 +602,7 @@ start_volumes() { # stop_addon() { local addon=$1 - (import_addon ${addon}-stop.sh) + (import_addon "${addon}-stop.sh") return 0 } @@ -675,7 +668,7 @@ requote() { # # ############################################################################## -if [ -z "${EBUILD}" ] ; then +if [[ -z ${EBUILD} ]] ; then # Setup a basic $PATH. Just add system default to existing. # This should solve both /sbin and /usr/sbin not present when # doing 'su -c foo', or for something like: PATH= rcscript start @@ -684,7 +677,7 @@ if [ -z "${EBUILD}" ] ; then # Cache the CONSOLETYPE - this is important as backgrounded shells don't # have a TTY. rc unsets it at the end of running so it shouldn't hang # around - if [[ -z ${CONSOLETYPE} ]]; then + if [[ -z ${CONSOLETYPE} ]] ; then export CONSOLETYPE=$( /sbin/consoletype 2>/dev/null ) fi if [[ ${CONSOLETYPE} == "serial" ]] ; then @@ -704,10 +697,10 @@ if [ -z "${EBUILD}" ] ; then setup_defaultlevels else # Should we use colors ? - if [[ $* != *depend* ]]; then + if [[ $* != *depend* ]] ; then # Check user pref in portage RC_NOCOLOR="$(portageq envvar NOCOLOR 2>/dev/null)" - [ "${RC_NOCOLOR}" = "true" ] && RC_NOCOLOR="yes" + [[ ${RC_NOCOLOR} == "true" ]] && RC_NOCOLOR="yes" else # We do not want colors during emerge depend RC_NOCOLOR="yes" @@ -716,7 +709,7 @@ else fi fi -if [[ -n ${EBUILD} && $* = *depend* ]]; then +if [[ -n ${EBUILD} && $* == *depend* ]]; then # We do not want stty to run during emerge depend COLS=80 else @@ -726,14 +719,14 @@ else (( COLS > 0 )) || (( COLS = 80 )) # width of [ ok ] == 7 fi -if [[ ${RC_ENDCOL} == yes ]]; then +if [[ ${RC_ENDCOL} == "yes" ]]; then ENDCOL=$'\e[A\e['$(( COLS - 8 ))'C' else ENDCOL='' fi # Setup the colors so our messages all look pretty -if [[ ${RC_NOCOLOR} == yes ]]; then +if [[ ${RC_NOCOLOR} == "yes" ]]; then unset GOOD WARN BAD NORMAL HILITE BRACKET else GOOD=$'\e[32;01m' diff --git a/sbin/init-common-post.sh b/sbin/init-common-post.sh new file mode 100755 index 0000000..5d4e102 --- /dev/null +++ b/sbin/init-common-post.sh @@ -0,0 +1,58 @@ +# Copyright 1999-2005 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +# We set the forced softlevel from the kernel command line +# It needs to be run right after proc is mounted for the +# boot runlevel +setup_defaultlevels + +# $BOOT can be used by rc-scripts to test if it is the first time +# the 'boot' runlevel is executed. Now also needed by some stuff in +# the 'sysinit' runlevel ... +export BOOT="yes" + +# We first try to find a locally defined list of critical services +# for a particular runlevel. If we cannot find it, we use the +# defaults. +get_critical_services + +export START_CRITICAL="yes" + +# We do not want to break compatibility, so we do not fully integrate +# these into /sbin/rc, but rather start them by hand ... +for x in ${CRITICAL_SERVICES} ; do + if ! start_critical_service "${x}" ; then + echo + eerror "One of more critical startup scripts failed to start!" + einfo "Rebooting" + /sbin/reboot -f + fi +done + +unset START_CRITICAL + +# Check that $svcdir exists ... +check_statedir "${svcdir}" + +# Clear $svcdir from stale entries, but leave the caches around, as it +# should help speed things up a bit +rm -rf $(ls -d1 "${svcdir}/"* 2>/dev/null | \ + grep -ve '\(depcache\|deptree\|envcache\)') + +echo "sysinit" > "${svcdir}/softlevel" + +# Update the dependency cache +/sbin/depscan.sh -u + +# Now that the dependency cache are up to date, make sure these +# are marked as started ... +( + # Needed for mark_service_started() + source "${svclib}"/sh/rc-services.sh + + for x in ${CRITICAL_SERVICES} ; do + mark_service_started "${x}" + done +) + +# vim:ts=4 diff --git a/sbin/init-common-pre.sh b/sbin/init-common-pre.sh new file mode 100755 index 0000000..5c4ef4c --- /dev/null +++ b/sbin/init-common-pre.sh @@ -0,0 +1,7 @@ +# Copyright 1999-2005 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +# Setup initial $PATH just in case +export PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" + +# vim:ts=4 diff --git a/sbin/init-functions.sh b/sbin/init-functions.sh new file mode 100755 index 0000000..04a1f13 --- /dev/null +++ b/sbin/init-functions.sh @@ -0,0 +1,57 @@ +# Copyright 1999-2005 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +# bool check_statedir(dir) +# +# Check that 'dir' exists, if not, drop to a shell. +# +check_statedir() { + [[ -z $1 ]] && return 0 + + if [[ ! -d $1 ]] && ! mkdir -p "$1" &>/dev/null ; then + splash "critical" & + echo + eerror "For Gentoo to function properly, \"$1\" needs to exist." + if [[ ${RC_FORCE_AUTO} == "yes" ]] ; then + eerror "Attempting to create \"$1\" for you ..." + mount -o remount,rw / + mkdir -p "$1" + fi + if [[ ! -d $1 ]] ; then + eerror "Please mount your root partition read/write, and execute:" + echo + eerror " # mkdir -p $1" + echo; echo + single_user + fi + fi + + return 0 +} + +# void start_critical_service() +# +# Start critical services needed for bootup +# +start_critical_service() { + ( + local retval= + local service=$1 + # Needed for some addons like dm-crypt that starts in critical services + local myservice=$1 + + source "/etc/init.d/${service}" || eerror "Failed to source /etc/init.d/${service}" + retval=$? + [[ ${retval} -ne 0 ]] && return "${retval}" + [[ -e /etc/conf.d/${service} ]] && source "/etc/conf.d/${service}" + source /etc/rc.conf + + start || eerror "Failed to start /etc/init.d/${service}" + retval=$? + + return "${retval}" + ) +} + + +# vim:ts=4 diff --git a/sbin/init.linux.sh b/sbin/init.linux.sh new file mode 100755 index 0000000..338f7cb --- /dev/null +++ b/sbin/init.linux.sh @@ -0,0 +1,43 @@ +# Copyright 1999-2005 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +# void single_user() +# +# Drop to a shell, remount / ro, and then reboot +# +single_user() { + einfo "Rebooting" + /sbin/reboot -f +} + +source "${svclib}"/sh/init-functions.sh +source "${svclib}"/sh/init-common-pre.sh + +echo +echo -e "${GOOD}Gentoo Linux${GENTOO_VERS}; ${BRACKET}http://www.gentoo.org/${NORMAL}" +echo -e " Copyright 1999-2005 Gentoo Foundation; Distributed under the GPLv2" +echo +echo -e "Press ${GOOD}I${NORMAL} to enter interactive boot mode" +echo + +check_statedir /proc +check_statedir /dev + +source "${svclib}"/sh/init-common-post.sh + +# Setup login records ... this has to be done here because when +# we exit this runlevel, init will write a boot record to utmp +# If /var/run is readonly, then print a warning, not errors +if touch /var/run/utmp 2>/dev/null ; then + > /var/run/utmp + touch /var/log/wtmp + chgrp utmp /var/run/utmp /var/log/wtmp + chmod 0664 /var/run/utmp /var/log/wtmp + # Remove /var/run/utmpx (bug from the past) + rm -f /var/run/utmpx +else + ewarn "Skipping /var/run/utmp initialization (ro root?)" +fi + + +# vim:ts=4 @@ -6,30 +6,16 @@ trap ":" INT QUIT TSTP source /sbin/functions.sh umask 022 -# Check that $1 exists ... -check_statedir() { - [ -z "$1" ] && return 0 - - if [ ! -d "$1" ] ; then - if ! mkdir -p "$1" &>/dev/null ; then - echo - eerror "For Gentoo to function properly, \"$1\" needs to exist." - einfo "Rebooting" - /sbin/reboot -f - fi - fi - - return 0 -} - +# void get_critical_services() +# +# Get critical services needed for bootup, and exports CRITICAL_SERVICES +# get_critical_services() { local x= CRITICAL_SERVICES= - if [ -f "/etc/runlevels/${BOOTLEVEL}/.critical" ] - then - for x in $(< /etc/runlevels/${BOOTLEVEL}/.critical) - do + if [[ -f /etc/runlevels/${BOOTLEVEL}/.critical ]] ; then + for x in $(< "/etc/runlevels/${BOOTLEVEL}/.critical") ; do CRITICAL_SERVICES="${CRITICAL_SERVICES} ${x##*/}" done else @@ -42,7 +28,7 @@ get_critical_services() { } # Save $1 -argv1="$1" +argv1=$1 # we need this to prevent a warning below [[ ! -e ${svcdir}/softlevel ]] && touch ${svcdir}/softlevel @@ -52,108 +38,11 @@ argv1="$1" # directly ... if [[ ${argv1} = "sysinit" || ( ${argv1} = "boot" && "$(<${svcdir}/softlevel)" != "sysinit" ) ]] then - # Setup initial $PATH just in case - PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" - - echo - echo -e "${GOOD}Gentoo Linux${GENTOO_VERS}; ${BRACKET}http://www.gentoo.org/${NORMAL}" - echo -e " Copyright 1999-2005 Gentoo Foundation; Distributed under the GPLv2" - echo - - check_statedir /proc - check_statedir /dev - - # We set the forced softlevel from the kernel command line - # It needs to be run right after proc is mounted for the - # boot runlevel - setup_defaultlevels - - # $BOOT can be used by rc-scripts to test if it is the first time - # the 'boot' runlevel is executed. Now also needed by some stuff in - # the 'sysinit' runlevel ... - export BOOT="yes" - - start_critical_service() { - ( - local retval= - local service=$1 - # Needed for some addons like dm-crypt that starts in critical services - local myservice=$1 - - source "/etc/init.d/${service}" || eerror "Failed to source /etc/init.d/${service}" - retval=$? - [ "${retval}" -ne 0 ] && return "${retval}" - [ -e "/etc/conf.d/${service}" ] && source "/etc/conf.d/${service}" - source /etc/rc.conf - - start || eerror "Failed to start /etc/init.d/${service}" - retval=$? - - return "${retval}" - ) + source "${svclib}"/sh/init.sh || { + echo "Could not source init.sh !?" + exit 1 } - - # We first try to find a locally defined list of critical services - # for a particular runlevel. If we cannot find it, we use the - # defaults. - get_critical_services - - export START_CRITICAL="yes" - # We do not want to break compatibility, so we do not fully integrate - # these into /sbin/rc, but rather start them by hand ... - for x in ${CRITICAL_SERVICES} - do - if ! start_critical_service "${x}" ; then - echo - eerror "One of more critical startup scripts failed to start!" - einfo "Rebooting" - /sbin/reboot -f - fi - done - - unset START_CRITICAL - - # Check that $svcdir exists ... - check_statedir "${svcdir}" - - # Clear $svcdir from stale entries, but leave the caches around, as it - # should help speed things up a bit - rm -rf $(ls -d1 "${svcdir}/"* 2>/dev/null | \ - grep -ve '\(depcache\|deptree\|envcache\)') - - echo "sysinit" > "${svcdir}/softlevel" - - # Update the dependency cache - /sbin/depscan.sh -u - - # Now that the dependency cache are up to date, make sure these - # are marked as started ... - ( - # Needed for mark_service_started() - source "${svclib}/sh/rc-services.sh" - - for x in ${CRITICAL_SERVICES} - do - mark_service_started "${x}" - done - ) - - # Setup login records ... this has to be done here because when - # we exit this runlevel, init will write a boot record to utmp - # If /var/run is readonly, then print a warning, not errors - if touch /var/run/utmp 2>/dev/null - then - > /var/run/utmp - touch /var/log/wtmp - chgrp utmp /var/run/utmp /var/log/wtmp - chmod 0664 /var/run/utmp /var/log/wtmp - # Remove /var/run/utmpx (bug from the past) - rm -f /var/run/utmpx - else - ewarn "Skipping /var/run/utmp initialization (ro root?)" - fi - [[ ${argv1} = "boot" ]] || exit 0 fi # Sysinit ends here @@ -167,67 +56,58 @@ then # We reset argv1 to the bootlevel given on the kernel command line # if there is one - argv1="${BOOTLEVEL}" + argv1=${BOOTLEVEL} fi source "${svclib}/sh/rc-services.sh" -if [ -f "${svcdir}/softlevel" ] -then +if [[ -f ${svcdir}/softlevel ]] ; then # Set OLDSOFTLEVEL if we had a valid SOFTLEVEL - export OLDSOFTLEVEL="$(< ${svcdir}/softlevel)" + export OLDSOFTLEVEL=$(< ${svcdir}/softlevel) else export OLDSOFTLEVEL= fi -if [ -z "${argv1}" ] -then - if [ -f "${svcdir}/softlevel" ] - then - export SOFTLEVEL="$(< ${svcdir}/softlevel)" +if [[ -z ${argv1} ]] ; then + if [[ -f ${svcdir}/softlevel ]] ; then + export SOFTLEVEL=$(< ${svcdir}/softlevel) else - export SOFTLEVEL="${BOOTLEVEL}" + export SOFTLEVEL=${BOOTLEVEL} fi else - export SOFTLEVEL="${argv1}" + export SOFTLEVEL=${argv1} fi -if [ ! -f "${svcdir}/softlevel" ] -then +if [[ ! -f ${svcdir}/softlevel ]] ; then echo "${SOFTLEVEL}" > "${svcdir}/softlevel" fi # For keeping a list of services that fails during boot/halt -if [ ! -d "${svcdir}/failed" ] -then +if [[ ! -d ${svcdir}/failed ]] ; then mkdir -p -m 0755 "${svcdir}/failed" else rm -rf "${svcdir}"/failed/* fi -if [ "${SOFTLEVEL}" = "reboot" -o "${SOFTLEVEL}" = "shutdown" ] -then +if [[ ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] ; then myscripts= -elif [ ! -d "/etc/runlevels/${SOFTLEVEL}" ] -then +elif [[ ! -d /etc/runlevels/${SOFTLEVEL} ]] ; then eerror "ERROR: runlevel ${SOFTLEVEL} does not exist; exiting ..." exit 1 else myscripts= - if [ "${SOFTLEVEL}" != "${BOOTLEVEL}" ] - then + if [[ ${SOFTLEVEL} != "${BOOTLEVEL}" ]] ; then # Normal runlevels *include* boot scripts - mylevels="$(dolisting "/etc/runlevels/${SOFTLEVEL}/")" + mylevels=$(dolisting "/etc/runlevels/${SOFTLEVEL}/") mylevels="${mylevels} $(dolisting /etc/runlevels/${BOOTLEVEL}/)" else # Non-normal runlevels don't include boot scripts as default - mylevels="$(dolisting "/etc/runlevels/${SOFTLEVEL}/")" + mylevels=$(dolisting "/etc/runlevels/${SOFTLEVEL}/") fi - for x in ${mylevels} - do - [ -L "${x}" ] && myscripts="${myscripts} ${x##*/}" + for x in ${mylevels} ; do + [[ -L ${x} ]] && myscripts="${myscripts} ${x##*/}" done fi @@ -253,7 +133,7 @@ get_stop_services() { local x list for x in $(dolisting "${svcdir}/inactive/") \ - $(dolisting "${svcdir}/started/") ; do + $(dolisting "${svcdir}/started/") ; do list="${list} ${x##*/}" done @@ -280,7 +160,7 @@ dep_stop() { for dep in $(needsme "${myservice}") ; do #if service_started "${dep}" && \ - if [[ -L "${svcdir}/softscripts.new/${dep}" ]] ; then + if [[ -L ${svcdir}/softscripts.new/${dep} ]] ; then # This dep is valid needsme=1 @@ -294,8 +174,7 @@ dep_stop() { # Stop services if [[ ${SOFTLEVEL} != "reboot" && \ - ${SOFTLEVEL} != "shutdown" ]] -then + ${SOFTLEVEL} != "shutdown" ]] ; then for i in $(get_stop_services) ; do dep_stop "${i}" done @@ -322,7 +201,7 @@ else done # Wait for any services that may still be stopping ... - [ "${RC_PARALLEL_STARTUP}" = "yes" ] && wait + [[ ${RC_PARALLEL_STARTUP} == "yes" ]] && wait export STOP_CRITICAL="yes" # Now stop the rest @@ -345,6 +224,9 @@ if [[ ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] ; then rm -rf $(ls -d1 "${svcdir}/"* 2>/dev/null | \ grep -ve '\(depcache\|deptree\|envcache\)') + # Need try(), etc + source "${svclib}"/sh/init-functions.sh + source /etc/init.d/halt.sh # we just die here since we have no init @@ -394,7 +276,7 @@ for i in $(get_start_services) ; do done # Wait for any services that may still be running ... -[ "${RC_PARALLEL_STARTUP}" = "yes" ] && wait +[[ ${RC_PARALLEL_STARTUP} == "yes" ]] && wait # Clean the old runlevel rm -rf "${svcdir}/softscripts.old" &>/dev/null @@ -414,4 +296,5 @@ fi # Remove the cached CONSOLETYPE unset CONSOLETYPE + # vim:ts=4 diff --git a/sbin/rc-daemon.sh b/sbin/rc-daemon.sh index fab3577..e5cc63e 100755 --- a/sbin/rc-daemon.sh +++ b/sbin/rc-daemon.sh @@ -358,8 +358,17 @@ start-stop-daemon() { # We started the daemon sucessfully # so we add it to our state local max="${#RC_DAEMONS[@]}" - RC_DAEMONS[max]="${cmd}" - RC_PIDFILES[max]="${pidfile}" + for (( i=0; i<${max}; i++ )); do + if [[ ${RC_DAEMONS[i]} == "{cmd}" \ + && ${RC_PIDFILES[i]}="${pidfile}" ]]; then + break + fi + done + + if [[ ${i} == "${max}" ]]; then + RC_DAEMONS[max]="${cmd}" + RC_PIDFILES[max]="${pidfile}" + fi fi fi diff --git a/sbin/rc-help.sh b/sbin/rc-help.sh index 2a28859..214c19d 100755 --- a/sbin/rc-help.sh +++ b/sbin/rc-help.sh @@ -1,11 +1,10 @@ #!/bin/bash -# Copyright 1999-2004 Gentoo Foundation +# Copyright 1999-2005 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 source /sbin/functions.sh -if [ "${RC_NOCOLOR}" = "yes" ] -then +if [[ ${RC_NOCOLOR} == "yes" ]] ; then unset BLUE GREEN OFF CYAN else BLUE="\033[34;01m" @@ -14,23 +13,43 @@ else CYAN="\033[36;01m" fi -myscript="${1}" -if [ -L "${1}" ] -then - myservice="$(readlink "${1}")" -else - myservice=${1} +myscript=$1 +if [[ -z ${myscript} ]] ; then + echo "Please execute an init.d script" + exit 1 fi +if [[ -L ${myscript} ]] ; then + myservice=$(readlink "${myscript}") +else + myservice=${myscript} +fi myservice=${myservice##*/} +if [[ $2 == "help" ]] ; then + BE_VERBOSE="yes" + NL=$'\n' +else + BE_VERBOSE="no" + NL= +fi + +default_opts="start stop restart pause zap" +extra_opts=$(source "${myscript}" 2>/dev/null ; echo ${opts}) + +if [[ ${BE_VERBOSE} == "yes" ]] ; then echo -e " -${GREEN}Gentoo Linux RC-Scripts; ${BLUE}http://www.gentoo.org/${OFF} - Copyright 1999-2004 Gentoo Foundation; Distributed under the GPL +${GREEN}Gentoo RC-Scripts; ${BLUE}http://www.gentoo.org/${OFF} + Copyright 1999-2005 Gentoo Foundation; Distributed under the GPL +" +fi + +echo -e "Usage: ${CYAN}${myservice}${OFF} [ ${GREEN}flags${OFF} ] < ${GREEN}options${OFF} > -Usage: ${CYAN}${myservice}${OFF} < ${GREEN}flags${OFF} > [ ${GREEN}options${OFF} ] +${CYAN}Normal Options:${OFF}" -${CYAN}Options:${OFF} +if [[ ${BE_VERBOSE} == "yes" ]] ; then +echo -e " ${GREEN}start${OFF} Start service, as well as the services it depends on (if not already started). @@ -75,18 +94,35 @@ ${CYAN}Options:${OFF} ${GREEN}broken${OFF} List the missing or broken dependencies of type 'need' this service depends on. +" -${CYAN}Flags:${OFF} - ${GREEN}--quiet${OFF} - Suppress output to stdout, except if: +else - 1) It is a warning, then output to stdout - 2) It is an error, then output to stderr +echo -e " ${GREEN}${default_opts}${OFF} + Default init.d options." + +fi + +if [[ -n ${extra_opts} ]] ; then +echo -e " +${CYAN}Additional Options:${OFF}${NL} + ${GREEN}${extra_opts}${OFF} + Extra options supported by this init.d script." +fi +echo -e " +${CYAN}Flags:${OFF}${NL} + ${GREEN}--quiet${OFF} + Suppress output to stdout, except if:${NL} + 1) It is a warning, then output to stdout + 2) It is an error, then output to stderr${NL} ${GREEN}--nocolor${OFF} - Suppress the use of colors. + Suppress the use of colors." +if [[ ${BE_VERBOSE} == "yes" ]] ; then +echo -e " ${CYAN}Dependencies:${OFF} + This is the heart of the Gentoo RC-Scripts, as it determines the order in which services gets started, and also to some extend what services get started in the first place. @@ -105,17 +141,17 @@ ${CYAN}Dependencies:${OFF} on one line only. ${GREEN}need${OFF} - These are all the services needed for this service to start. If any service - in the 'need' line is not started, it will be started even if it is not - in the current, or 'boot' runlevel, and then this service will be started. - If any services in the 'need' line fails to start or is missing, this - service will never be started. + These are all the services needed for this service to start. If any + service in the 'need' line is not started, it will be started even if it + is not in the current, or 'boot' runlevel, and then this service will be + started. If any services in the 'need' line fails to start or is + missing, this service will never be started. ${GREEN}use${OFF} This can be seen as representing optional services this service depends on that are not critical for it to start. For any service in the 'use' line, - it must be added to the 'boot' or current runlevel to be considered a valid - 'use' dependency. It can also be used to determine startup order. + it must be added to the 'boot' or current runlevel to be considered a + valid 'use' dependency. It can also be used to determine startup order. ${GREEN}before${OFF} This, together with the 'after' dependency type, can be used to control @@ -137,8 +173,8 @@ ${CYAN}Dependencies:${OFF} of a system logger depend on 'logger'. This should make things much more generic. - Note that the 'need', 'use', 'before' and 'after' dependeny types can have '*' - as argument. Having: + Note that the 'need', 'use', 'before', and 'after' dependency types accept + an '*' as an argument. Having: depend() { before * @@ -157,6 +193,7 @@ ${CYAN}Dependencies:${OFF} been warned! ${CYAN}'net' Dependency and 'net.*' Services:${OFF} + Example: depend() { @@ -171,16 +208,31 @@ ${CYAN}'net' Dependency and 'net.*' Services:${OFF} 1. It is part of the 'boot' runlevel 2. It is part of the current runlevel - A few examples are the /etc/init.d/net.eth0 and /etc/init.d/net.lo services. + A few examples are the /etc/init.d/net.eth0 and /etc/init.d/net.lo services." +fi + +echo -e " +${CYAN}Configuration files:${OFF}" -${CYAN}Configuration files:${OFF} +if [[ ${BE_VERBOSE} == "yes" ]] ; then +echo -e " There are two files which will be sourced for possible configuration by the rc-scripts. They are (sourced from top to bottom): +" +fi - /etc/conf.d/${myservice} - /etc/rc.conf +echo -e " /etc/conf.d/${myservice}${NL} /etc/rc.conf" +if [[ ${BE_VERBOSE} == "yes" ]] ; then +echo -e " ${CYAN}Management:${OFF} + Services are added and removed via the 'rc-update' tool. Running it without arguments should give sufficient help. " +else +echo -e " +For more info, please run '${myscript} help'." +fi + +exit 0 diff --git a/sbin/rc-services.sh b/sbin/rc-services.sh index d928a5f..4a177cd 100755 --- a/sbin/rc-services.sh +++ b/sbin/rc-services.sh @@ -7,7 +7,7 @@ RC_GOT_SERVICES="yes" [[ ${RC_GOT_FUNCTIONS} != "yes" ]] && source /sbin/functions.sh -if [[ ${RC_GOT_DEPTREE_INFO} != "yes" ]]; then +if [[ ${RC_GOT_DEPTREE_INFO} != "yes" ]] ; then # Only try and update if we are root if [[ ${EUID} == "0" ]] && ! /sbin/depscan.sh -u ; then echo @@ -17,7 +17,7 @@ if [[ ${RC_GOT_DEPTREE_INFO} != "yes" ]]; then fi source "${svcdir}/deptree" - if [[ ${RC_GOT_DEPTREE_INFO} != "yes" ]]; then + if [[ ${RC_GOT_DEPTREE_INFO} != "yes" ]] ; then echo eerror "Dependency info is missing! Please run" eerror " # /sbin/depscan.sh" @@ -53,23 +53,23 @@ rc_mtime= # Print the index of 'service'. 'index' is the current index. # get_service_index() { - if [[ -z $1 || -z $2 ]]; then + if [[ -z $1 || -z $2 ]] ; then echo "0" return 1 fi - local x myservice="$1" index="$2" + local x myservice=$1 index=$2 # Do we already have the index? if [[ -n ${index} && ${index} -gt 0 \ - && ${myservice} == ${RC_DEPEND_TREE[${index}]} ]]; then + && ${myservice} == "${RC_DEPEND_TREE[${index}]}" ]] ; then echo "${index}" return 0 fi for (( x=1; x<=${RC_DEPEND_TREE[0]}; x++ )); do index=$(( ${x} * ${rc_index_scale} )) - if [[ ${myservice} == ${RC_DEPEND_TREE[${index}]} ]]; then + if [[ ${myservice} == "${RC_DEPEND_TREE[${index}]}" ]] ; then echo "${index}" return 0 fi @@ -86,10 +86,10 @@ get_service_index() { get_dep_info() { [[ -z $1 ]] && return 1 - local myservice="$1" + local myservice=$1 # We already have the right stuff ... - [[ ${myservice} == ${rc_name} && -n ${rc_mtime} ]] && return 0 + [[ ${myservice} == "${rc_name}" && -n ${rc_mtime} ]] && return 0 rc_index="`get_service_index "${myservice}" "${rc_index}"`" rc_mtime="${RC_DEPEND_TREE[$((${rc_index} + ${rc_type_mtime}))]}" @@ -97,7 +97,7 @@ get_dep_info() { # Verify that we have the correct index (rc_index) ... # [[ ${rc_index} == "0" ]] && return 1 - rc_name="${RC_DEPEND_TREE[${rc_index}]}" + rc_name=${RC_DEPEND_TREE[${rc_index}]} rc_ineed="${RC_DEPEND_TREE[$((${rc_index} + ${rc_type_ineed}))]}" rc_needsme="${RC_DEPEND_TREE[$((${rc_index} + ${rc_type_needsme}))]}" rc_iuse="${RC_DEPEND_TREE[$((${rc_index} + ${rc_type_iuse}))]}" @@ -125,11 +125,11 @@ check_dependency() { local x myservice deps # Set the dependency variables to relate to 'service1' - if [[ $2 == "-t" ]]; then + if [[ $2 == "-t" ]] ; then [[ -z $3 || -z $4 ]] && return 1 - myservice="$3" + myservice=$3 else - myservice="$2" + myservice=$2 fi if ! get_dep_info "${myservice}" >/dev/null ; then @@ -146,8 +146,8 @@ check_dependency() { if [[ $2 == "-t" && -n $4 ]]; then # Check if 'service1' have 'deptype' dependency on 'service2' - for x in ${deps}; do - [[ ${x} == $4 ]] && return 0 + for x in ${deps} ; do + [[ ${x} == "$4" ]] && return 0 done return 1 else @@ -213,14 +213,14 @@ is_fake_service() { [[ -z $1 || -z $2 ]] && return 1 - [[ $2 != ${BOOTLEVEL} && -e "/etc/runlevels/${BOOTLEVEL}/.fake" ]] \ - && fake_services="$( < /etc/runlevels/${BOOTLEVEL}/.fake )" + [[ $2 != "${BOOTLEVEL}" && -e /etc/runlevels/${BOOTLEVEL}/.fake ]] && \ + fake_services=$( < /etc/runlevels/${BOOTLEVEL}/.fake ) - [[ -e "/etc/runlevels/$2/.fake" ]] \ - && fake_services="${fake_services} $( < /etc/runlevels/$2/.fake )" + [[ -e /etc/runlevels/$2/.fake ]] && \ + fake_services="${fake_services} $( < /etc/runlevels/$2/.fake )" - for x in ${fake_services}; do - [[ $1 == ${x##*/} ]] && return 0 + for x in ${fake_services} ; do + [[ $1 == "${x##*/}" ]] && return 0 done return 1 @@ -233,7 +233,7 @@ is_fake_service() { in_runlevel() { [[ -z $1 || -z $2 ]] && return 1 - [[ -L "/etc/runlevels/$2/$1" ]] && return 0 + [[ -L /etc/runlevels/$2/$1 ]] && return 0 return 1 } @@ -269,15 +269,15 @@ service_message() { [[ ${RC_PARALLEL_STARTUP} != "yes" ]] && return local cmd="einfo" - if [[ $1 == 1 || $1 == "error" || $1 == "eerror" ]]; then + if [[ $1 == "1" || $1 == "error" || $1 == "eerror" ]] ; then cmd="eerror" shift fi - local r="${RC_QUIET_STDOUT}" + local r=${RC_QUIET_STDOUT} RC_QUIET_STDOUT="no" ${cmd} "$@" - RC_QUIET_STDOUT="${r}" + RC_QUIET_STDOUT=${r} } # bool begin_service( service ) @@ -289,8 +289,7 @@ service_message() { # whatever is in here can only be executed by one process # end_service service # fi -begin_service() -{ +begin_service() { [[ {$START_CRITICAL} == "yes" ]] && return 0 mkfifo "${svcdir}/exclusive/${service}" 2> /dev/null @@ -302,9 +301,8 @@ begin_service() # stops executing a exclusive region and # wakes up anybody who is waiting for the exclusive region # -end_service() -{ - local service="$1" exitstatus="$2" +end_service() { + local service=$1 exitstatus=$2 # if we are doing critical services, there is no fifo [[ ${START_CRITICAL} == "yes" ]] && return @@ -315,7 +313,7 @@ end_service() # move the fifo to a unique name so no-one is waiting for it local fifo="${svcdir}/exclusive/${service}" - if [[ -e "${fifo}" ]]; then + if [[ -e ${fifo} ]] ; then local tempname="${fifo}.$$" mv -f "${fifo}" "${tempname}" @@ -332,9 +330,8 @@ end_service() # If a service has started, or a fifo does not exist return 0 # Otherwise, wait until we get an exit code via the fifo and return # that instead. -wait_service() -{ - local service="$1" +wait_service() { + local service=$1 local fifo="${svcdir}/exclusive/${service}" [[ ${START_CRITICAL} == "yes" || ${STOP_CRITICAL} == "yes" ]] && return 0 @@ -353,10 +350,10 @@ wait_service() # Start 'service' if it is not already running. # start_service() { - local service="$1" + local service=$1 [[ -z ${service} ]] && return 1 - if [[ ! -e "/etc/init.d/${service}" ]]; then + if [[ ! -e "/etc/init.d/${service}" ]] ; then mark_service_stopped "${service}" return 1 fi @@ -371,19 +368,19 @@ start_service() { fi begin_service "${service}" || return 0 - if [[ ${RC_PARALLEL_STARTUP} != "yes" \ - || ${START_CRITICAL} == "yes" ]] ; then + if [[ ${RC_PARALLEL_STARTUP} != "yes" || \ + ${START_CRITICAL} == "yes" ]] ; then # if we can not start the services in parallel # then just start it and return the exit status ( "/etc/init.d/${service}" start ) - retval="$?" + retval=$? end_service "${service}" "${retval}" return "${retval}" else # if parallel startup is allowed, start it in background ( "/etc/init.d/${service}" start - retval="$?" + retval=$? end_service "${service}" "${retval}" ) & return 0 @@ -395,10 +392,10 @@ start_service() { # Stop 'service' if it is not already running. # stop_service() { - local service="$1" + local service=$1 [[ -z ${service} ]] && return 1 - if [[ ! -e "/etc/init.d/${service}" ]]; then + if [[ ! -e /etc/init.d/${service} ]] ; then mark_service_stopped "${service}" return 0 fi @@ -407,7 +404,7 @@ stop_service() { service_stopped "${service}" && return 0 local level="${SOFTLEVEL}" - is_runlevel_stop && level="${OLDSOFTLEVEL}" + is_runlevel_stop && level=${OLDSOFTLEVEL} if is_fake_service "${service}" "${level}" ; then mark_service_stopped "${service}" @@ -416,19 +413,19 @@ stop_service() { begin_service "${service}" || return 0 - if [[ ${RC_PARALLEL_STARTUP} != "yes" \ - || ${STOP_CRITICAL} == "yes" ]] ; then + if [[ ${RC_PARALLEL_STARTUP} != "yes" || \ + ${STOP_CRITICAL} == "yes" ]] ; then # if we can not start the services in parallel # then just start it and return the exit status ( "/etc/init.d/${service}" stop ) - retval="$?" + retval=$? end_service "${service}" "${retval}" return "${retval}" else # if parallel startup is allowed, start it in background ( ( "/etc/init.d/${service}" stop ) - retval="$?" + retval=$? end_service "${service}" "${retval}" ) & return 0 @@ -445,9 +442,9 @@ mark_service_starting() { ln -snf "/etc/init.d/$1" "${svcdir}/starting/$1" local retval=$? - [[ -f "${svcdir}/started/$1" ]] && rm -f "${svcdir}/started/$1" - [[ -f "${svcdir}/inactive/$1" ]] && rm -f "${svcdir}/inactive/$1" - [[ -f "${svcdir}/stopping/$1" ]] && rm -f "${svcdir}/stopping/$1" + [[ -f ${svcdir}/started/$1 ]] && rm -f "${svcdir}/started/$1" + [[ -f ${svcdir}/inactive/$1 ]] && rm -f "${svcdir}/inactive/$1" + [[ -f ${svcdir}/stopping/$1 ]] && rm -f "${svcdir}/stopping/$1" return "${retval}" } @@ -460,11 +457,11 @@ mark_service_started() { [[ -z $1 ]] && return 1 ln -snf "/etc/init.d/$1" "${svcdir}/started/$1" - local retval="$?" + local retval=$? - [[ -f "${svcdir}/starting/$1" ]] && rm -f "${svcdir}/starting/$1" - [[ -f "${svcdir}/inactive/$1" ]] && rm -f "${svcdir}/inactive/$1" - [[ -f "${svcdir}/stopping/$1" ]] && rm -f "${svcdir}/stopping/$1" + [[ -f ${svcdir}/starting/$1 ]] && rm -f "${svcdir}/starting/$1" + [[ -f ${svcdir}/inactive/$1 ]] && rm -f "${svcdir}/inactive/$1" + [[ -f ${svcdir}/stopping/$1 ]] && rm -f "${svcdir}/stopping/$1" return "${retval}" } @@ -477,10 +474,10 @@ mark_service_inactive() { [[ -z $1 ]] && return 1 ln -snf "/etc/init.d/$1" "${svcdir}/inactive/$1" - local retval="$?" - [[ -f "${svcdir}/started/$1" ]] && rm -f "${svcdir}/started/$1" - [[ -f "${svcdir}/starting/$1" ]] && rm -f "${svcdir}/starting/$1" - [[ -f "${svcdir}/stopping/$1" ]] && rm -f "${svcdir}/stopping/$1" + local retval=$? + [[ -f ${svcdir}/started/$1 ]] && rm -f "${svcdir}/started/$1" + [[ -f ${svcdir}/starting/$1 ]] && rm -f "${svcdir}/starting/$1" + [[ -f ${svcdir}/stopping/$1 ]] && rm -f "${svcdir}/stopping/$1" return "${retval}" } @@ -493,11 +490,11 @@ mark_service_stopping() { [[ -z $1 ]] && return 1 ln -snf "/etc/init.d/$1" "${svcdir}/stopping/$1" - local retval="$?" + local retval=$? - [ -f "${svcdir}/starting/$1" ] && rm -f "${svcdir}/starting/$1" - [ -f "${svcdir}/started/$1" ] && rm -f "${svcdir}/started/$1" - [ -f "${svcdir}/inactive/$1" ] && rm -f "${svcdir}/inactive/$1" + [ -f ${svcdir}/starting/$1 ] && rm -f "${svcdir}/starting/$1" + [ -f ${svcdir}/started/$1 ] && rm -f "${svcdir}/started/$1" + [ -f ${svcdir}/inactive/$1 ] && rm -f "${svcdir}/inactive/$1" return "${retval}" } @@ -509,10 +506,11 @@ mark_service_stopping() { mark_service_stopped() { [[ -z $1 ]] && return 1 - [[ -f "${svcdir}/starting/$1" ]] && rm -f "${svcdir}/starting/$1" - [[ -f "${svcdir}/started/$1" ]] && rm -f "${svcdir}/started/$1" - [[ -f "${svcdir}/inactive/$1" ]] && rm -f "${svcdir}/inactive/$1" - [[ -f "${svcdir}/stopping/$1" ]] && rm -f "${svcdir}/stopping/$1" + [[ -f ${svcdir}/daemons/$1 ]] && rm -f "${svcdir}/daemons/$1" + [[ -f ${svcdir}/starting/$1 ]] && rm -f "${svcdir}/starting/$1" + [[ -f ${svcdir}/started/$1 ]] && rm -f "${svcdir}/started/$1" + [[ -f ${svcdir}/inactive/$1 ]] && rm -f "${svcdir}/inactive/$1" + [[ -f ${svcdir}/stopping/$1 ]] && rm -f "${svcdir}/stopping/$1" return $? } @@ -529,7 +527,7 @@ test_service_state() { # Service is in the state requested [[ -L ${f} ]] && return 0 - if [[ ! -e ${f} ]]; then + if [[ ! -e ${f} ]] ; then rm -f "${f}" return 1 fi @@ -590,7 +588,7 @@ service_stopped() { # this is only valid on runlevel change ... # mark_service_failed() { - [[ -z $1 || ! -d "${svcdir}/failed" ]] && return 1 + [[ -z $1 || ! -d ${svcdir}/failed ]] && return 1 ln -snf "/etc/init.d/$1" "${svcdir}/failed/$1" } @@ -600,7 +598,7 @@ mark_service_failed() { # Return true if 'service' have failed during this runlevel. # service_failed() { - [[ -n $1 && -L "${svcdir}/failed/$1" ]] + [[ -n $1 && -L ${svcdir}/failed/$1 ]] } # bool dependon(service1, service2) @@ -625,10 +623,9 @@ valid_i() { # Cannot be SOFTLEVEL, as we need to know current runlevel [[ -f ${svcdir}/softlevel ]] && mylevel=$( < "${svcdir}/softlevel" ) - for x in $( i$1 "$2" ) - do - [[ -e "/etc/runlevels/${BOOTLEVEL}/${x}" \ - || -e "/etc/runlevels/${mylevel}/${x}" ]] \ + for x in $( i$1 "$2" ) ; do + [[ -e /etc/runlevels/${BOOTLEVEL}/${x} || \ + -e "/etc/runlevels/${mylevel}/${x}" ]] \ && echo "${x}" done @@ -661,8 +658,8 @@ trace_dependencies() { local -a services=( "$@" ) deps local i j - if [[ $1 == -* ]]; then - deptype="${1/-}" + if [[ $1 == -* ]] ; then + deptype=${1/-} services=( "${myservice}" ) fi @@ -672,9 +669,9 @@ trace_dependencies() { } local last="" - while [[ ${services[@]} != "${last}" ]]; do + while [[ ${services[@]} != "${last}" ]] ; do last="${services[*]}" - for (( i=0; i<${#services[@]}; i++ )); do + for (( i=0; i<${#services[@]}; i++ )) ; do if [[ -n ${deptype} ]] ; then deps=( "${deps[@]}" $( "${deptype}" "${services[i]}" ) ) else @@ -698,34 +695,34 @@ trace_dependencies() { # revisit any dependencies. Finally we add ourselves to the sorted list. # This should never get into an infinite loop, thanks to our dead array. local -a dead=() deadname=() sorted=() - for (( i=0; i<${#services[@]}; i++ )); do - dead[i]=false; + for (( i=0; i<${#services[@]}; i++ )) ; do + dead[i]="false" deadname[i]="${services[i]}" done after_visit() { - local service="$1" i + local service=$1 i - for (( i=0; i<${#deadname[@]}; i++)); do + for (( i=0; i<${#deadname[@]}; i++)) ; do [[ ${service} == ${deadname[i]} ]] && break done ${dead[i]} && return - dead[i]=true + dead[i]="true" local x deps="$( ineed ${service} ) $( valid_iuse ${service} )" if is_runlevel_start || is_runlevel_stop ; then deps="${deps} $( valid_iafter ${service} )" fi - for x in ${deps}; do + for x in ${deps} ; do after_visit "${x}" done sorted=( "${sorted[@]}" "${service}" ) } - for (( i=0; i<${#services[*]}; i++ )); do + for (( i=0; i<${#services[*]}; i++ )) ; do after_visit "${services[i]}" done services=( "${sorted[@]}" ) @@ -758,4 +755,5 @@ query_before() { return 1 } + # vim:ts=4 diff --git a/sbin/runscript.sh b/sbin/runscript.sh index bb31bfc..79820f3 100755 --- a/sbin/runscript.sh +++ b/sbin/runscript.sh @@ -20,7 +20,7 @@ svcpause="no" svcrestart="no" myscript=$1 -if [[ -L $1 ]] && [[ ! -L /etc/init.d/${1##*/} ]] ; then +if [[ -L $1 && ! -L /etc/init.d/${1##*/} ]] ; then myservice=$(readlink "$1") else myservice=$1 @@ -97,8 +97,7 @@ svc_stop() { service_message "Stopping service ${myservice}" if in_runlevel "${myservice}" "${BOOTLEVEL}" && \ - [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] - then + [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] ; then ewarn "WARNING: you are stopping a boot service." fi @@ -115,8 +114,7 @@ svc_stop() { # If some service 'need' $mydep, stop it first; or if it is a runlevel change, # first stop all services that is started 'after' $mydep. if needsme "${mydep}" >/dev/null || \ - (is_runlevel_stop && ibefore "${mydep}" >/dev/null) - then + (is_runlevel_stop && ibefore "${mydep}" >/dev/null) ; then local -a sl=( $(needsme "${mydep}") ) # On runlevel change, stop all services "after $mydep" first ... @@ -133,8 +131,7 @@ svc_stop() { fi if ibefore -t "${mydep}" "${x}" >/dev/null && \ - [[ -L ${svcdir}/softscripts.new/${x} ]] - then + [[ -L ${svcdir}/softscripts.new/${x} ]] ; then # Service do not 'need' $mydep, and is still present in # new runlevel ... unset sl[x] @@ -154,8 +151,7 @@ svc_stop() { service_stopped "${x}" && continue if ibefore -t "${mydep}" "${x}" >/dev/null && \ - [[ -L "${svcdir}/softscripts.new/${x}" ]] - then + [[ -L ${svcdir}/softscripts.new/${x} ]] ; then # Service do not 'need' $mydep, and is still present in # new runlevel ... continue @@ -168,8 +164,8 @@ svc_stop() { # clean as possible, else do not stop our service if # a dependent service did not stop. if needsme -t "${mydep}" "${x}" >/dev/null && \ - [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] - then + [[ ${SOFTLEVEL} != "reboot" && \ + ${SOFTLEVEL} != "shutdown" ]] ; then retval=1 fi break @@ -270,7 +266,7 @@ svc_start() { # Start dependencies, if any for x in ${startupservices} ; do - if service_stopped "${x}"; then + if service_stopped "${x}" ; then start_service "${x}" fi done @@ -315,7 +311,7 @@ svc_start() { service_inactive "${myservice}" && return 1 fi - if [[ ${retval} != 0 ]]; then + if [[ ${retval} != 0 ]] ; then is_runlevel_start && mark_service_failed "${myservice}" # Remove link if service didn't start; but only if we're not booting @@ -400,7 +396,7 @@ svc_homegrown() { # Walk through the list of available options, looking for the # requested one. for x in ${opts} ; do - if [[ ${x} == ${arg} ]] ; then + if [[ ${x} == "${arg}" ]] ; then if typeset -F "${x}" &>/dev/null ; then # Run the homegrown function "${x}" @@ -468,9 +464,8 @@ for arg in $* ; do # Simple way to try and detect if the service use svc_{start,stop} # to restart if it have a custom restart() funtion. if [[ -n $(egrep '^[[:space:]]*restart[[:space:]]*()' "/etc/init.d/${myservice}") ]] ; then - if [[ -z $(egrep 'svc_stop' "/etc/init.d/${myservice}") ]] || \ - [[ -z $(egrep 'svc_start' "/etc/init.d/${myservice}") ]] - then + if [[ -z $(egrep 'svc_stop' "/etc/init.d/${myservice}") || \ + -z $(egrep 'svc_start' "/etc/init.d/${myservice}") ]] ; then echo ewarn "Please use 'svc_stop; svc_start' and not 'stop; start' to" ewarn "restart the service in its custom 'restart()' function." @@ -507,6 +502,9 @@ for arg in $* ; do ;; --quiet|--nocolor) ;; + help) + exec "${svclib}"/sh/rc-help.sh "${myscript}" help + ;; *) # Allow for homegrown functions svc_homegrown ${arg} |