| 1 |
#!/bin/bash
|
| 2 |
# Copyright 1999-2007 Gentoo Foundation
|
| 3 |
# Distributed under the terms of the GNU General Public License v2
|
| 4 |
|
| 5 |
[[ " $* " == *" --debug "* ]] && set -x
|
| 6 |
|
| 7 |
if [[ $1 == "/"* ]] ; then
|
| 8 |
myscript="$1"
|
| 9 |
else
|
| 10 |
myscript="$(pwd)/$1"
|
| 11 |
fi
|
| 12 |
cd /
|
| 13 |
|
| 14 |
# Common functions
|
| 15 |
[[ ${RC_GOT_FUNCTIONS} != "yes" ]] && . /sbin/functions.sh
|
| 16 |
|
| 17 |
# User must be root to run most script stuff (except status)
|
| 18 |
if [[ ${EUID} != "0" ]] && ! [[ $2 == "status" && $# -eq 2 ]] ; then
|
| 19 |
eerror "$0:" $"must be root to run init scripts"
|
| 20 |
exit 1
|
| 21 |
fi
|
| 22 |
|
| 23 |
if [[ -L $1 && ! -L "/etc/init.d/${1##*/}" ]] ; then
|
| 24 |
SVCNAME=$(readlink "$1")
|
| 25 |
else
|
| 26 |
SVCNAME="$1"
|
| 27 |
fi
|
| 28 |
declare -r SVCNAME="${SVCNAME##*/}"
|
| 29 |
export SVCNAME
|
| 30 |
# Support deprecated myservice variable
|
| 31 |
myservice="${SVCNAME}"
|
| 32 |
|
| 33 |
svc_trap() {
|
| 34 |
trap 'eerror $"ERROR:" " ${SVCNAME}" $"caught an interrupt"; eflush; rm -rf "${svcdir}/snapshot/$$"; exit 1' \
|
| 35 |
INT QUIT TSTP
|
| 36 |
}
|
| 37 |
|
| 38 |
# Setup a default trap
|
| 39 |
svc_trap
|
| 40 |
|
| 41 |
# Now check script for syntax errors
|
| 42 |
rcscript_errors=$(bash -n "${myscript}" 2>&1) || {
|
| 43 |
[[ -n ${rcscript_errors} ]] && echo "${rcscript_errors}" >&2
|
| 44 |
eerror $"ERROR:" " $1" $"has syntax errors in it; aborting ..."
|
| 45 |
exit 1
|
| 46 |
}
|
| 47 |
|
| 48 |
# coldplug events can trigger init scripts, but we don't want to run them
|
| 49 |
# until after rc sysinit has completed so we punt them to the boot runlevel
|
| 50 |
if [[ -e /dev/.rcsysinit ]] ; then
|
| 51 |
eerror $"ERROR: cannot run" "${SVCNAME}" $"until sysinit completes"
|
| 52 |
[[ ${RC_COLDPLUG:-yes} != "yes" ]] && exit 1
|
| 53 |
set -f
|
| 54 |
for x in ${RC_PLUG_SERVICES} ; do
|
| 55 |
[[ ${SVCNAME} == ${x} ]] && break
|
| 56 |
[[ "!${SVCNAME}" == ${x} ]] && exit 1
|
| 57 |
done
|
| 58 |
eerror "${SVCNAME}" $"will be started in the" "${BOOTLEVEL}" $"runlevel"
|
| 59 |
if [[ ! -L /dev/.rcboot/"${SVCNAME}" ]] ; then
|
| 60 |
[[ ! -d /dev/.rcboot ]] && mkdir /dev/.rcboot
|
| 61 |
ln -snf "$1" /dev/.rcboot/"${SVCNAME}"
|
| 62 |
fi
|
| 63 |
exit 1
|
| 64 |
fi
|
| 65 |
|
| 66 |
# Only hotplug if we're allowed to
|
| 67 |
if [[ ${IN_HOTPLUG} == "1" ]] ; then
|
| 68 |
if [[ ${RC_HOTPLUG:-yes} != "yes" ]] ; then
|
| 69 |
eerror "${SVCNAME}" $"is not allowed to be hotplugged"
|
| 70 |
exit 1
|
| 71 |
fi
|
| 72 |
|
| 73 |
set -f
|
| 74 |
for x in ${RC_PLUG_SERVICES} ; do
|
| 75 |
[[ ${SVCNAME} == ${x} ]] && break
|
| 76 |
if [[ "!${SVCNAME}" == ${x} ]] ; then
|
| 77 |
eerror "${SVCNAME}" $"is not allowed to be hotplugged"
|
| 78 |
exit 1
|
| 79 |
fi
|
| 80 |
done
|
| 81 |
set +f
|
| 82 |
fi
|
| 83 |
|
| 84 |
# State variables
|
| 85 |
svcpause="no"
|
| 86 |
|
| 87 |
# Functions to handle dependencies and services
|
| 88 |
[[ ${RC_GOT_SERVICES} != "yes" ]] && . "${svclib}/sh/rc-services.sh"
|
| 89 |
# Functions to control daemons
|
| 90 |
[[ ${RC_GOT_DAEMON} != "yes" ]] && . "${svclib}/sh/rc-daemon.sh"
|
| 91 |
|
| 92 |
# Check if the textdomain is non-default
|
| 93 |
search_lang="${LC_ALL:-${LC_MESSAGES:-${LANG}}}"
|
| 94 |
[[ -f ${TEXTDOMAINDIR}/${search_lang%.*}/LC_MESSAGES/${myservice}.mo ]] \
|
| 95 |
&& TEXTDOMAIN="${myservice}"
|
| 96 |
|
| 97 |
# Source configuration files.
|
| 98 |
# (1) Source /etc/conf.d/${PREFIX} where ${PREFIX} is the first part of
|
| 99 |
# ${SVCNAME} dot seperated. For example if net.eth0 then load net.
|
| 100 |
# (2) Source /etc/conf.d/${SVCNAME} to get initscript-specific
|
| 101 |
# configuration (if it exists).
|
| 102 |
# (3) Source /etc/rc.conf to pick up potentially overriding
|
| 103 |
# configuration, if the system administrator chose to put it
|
| 104 |
# there (if it exists).
|
| 105 |
conf="${SVCNAME%%.*}"
|
| 106 |
if [[ -n ${conf} && ${conf} != "${SVCNAME}" ]] ; then
|
| 107 |
conf=$(add_suffix "/etc/conf.d/${conf}")
|
| 108 |
[[ -e ${conf} ]] && . "${conf}"
|
| 109 |
fi
|
| 110 |
conf=$(add_suffix "/etc/conf.d/${SVCNAME}")
|
| 111 |
[[ -e ${conf} ]] && . "${conf}"
|
| 112 |
conf=$(add_suffix /etc/rc.conf)
|
| 113 |
[[ -e ${conf} ]] && . "${conf}"
|
| 114 |
|
| 115 |
mylevel="${SOFTLEVEL}"
|
| 116 |
[[ ${SOFTLEVEL} == "${BOOTLEVEL}" \
|
| 117 |
|| ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] \
|
| 118 |
&& mylevel="${DEFAULTLEVEL}"
|
| 119 |
|
| 120 |
# Call svc_quit if we abort AND we have obtained a lock
|
| 121 |
service_started "${SVCNAME}"
|
| 122 |
svcstarted="$?"
|
| 123 |
service_inactive "${SVCNAME}"
|
| 124 |
svcinactive="$?"
|
| 125 |
|
| 126 |
svc_quit() {
|
| 127 |
eerror $"ERROR:" " ${SVCNAME}" $"caught an interrupt"
|
| 128 |
eflush
|
| 129 |
rm -rf "${svcdir}/snapshot/$$" "${svcdir}/exclusive/${SVCNAME}.$$"
|
| 130 |
if service_inactive "${SVCNAME}" || [[ ${svcinactive} == "0" ]] ; then
|
| 131 |
mark_service_inactive "${SVCNAME}"
|
| 132 |
elif [[ ${svcstarted} == "0" ]] ; then
|
| 133 |
mark_service_started "${SVCNAME}"
|
| 134 |
else
|
| 135 |
mark_service_stopped "${SVCNAME}"
|
| 136 |
fi
|
| 137 |
end_service "${SVCNAME}"
|
| 138 |
exit 1
|
| 139 |
}
|
| 140 |
|
| 141 |
usage() {
|
| 142 |
local IFS="|"
|
| 143 |
myline="${SVCNAME} { $* "
|
| 144 |
unset IFS
|
| 145 |
echo
|
| 146 |
eerror $"Usage:" "${myline}}"
|
| 147 |
eerror " ${SVCNAME}" $"without arguments for full help"
|
| 148 |
}
|
| 149 |
|
| 150 |
stop() {
|
| 151 |
# Return success so the symlink gets removed
|
| 152 |
return 0
|
| 153 |
}
|
| 154 |
|
| 155 |
start() {
|
| 156 |
eerror $"ERROR:" " ${SVCNAME}" $"does not have a start function."
|
| 157 |
# Return failure so the symlink doesn't get created
|
| 158 |
return 1
|
| 159 |
}
|
| 160 |
|
| 161 |
restart() {
|
| 162 |
if ! service_stopped "${SVCNAME}" ; then
|
| 163 |
svc_stop || return "$?"
|
| 164 |
fi
|
| 165 |
svc_start
|
| 166 |
}
|
| 167 |
|
| 168 |
status() {
|
| 169 |
# Dummy function
|
| 170 |
return 0
|
| 171 |
}
|
| 172 |
|
| 173 |
svc_schedule_start() {
|
| 174 |
local service="$1" start="$2" x=
|
| 175 |
|
| 176 |
[[ ! -d "${svcdir}/scheduled/${service}" ]] \
|
| 177 |
&& mkdir -p "${svcdir}/scheduled/${service}"
|
| 178 |
ln -snf "/etc/init.d/${service}" \
|
| 179 |
"${svcdir}/scheduled/${service}/${start}"
|
| 180 |
|
| 181 |
for x in $(rc-depend --notrace -iprovide "${service}" ) ; do
|
| 182 |
[[ ! -d "${svcdir}/scheduled/${x}" ]] \
|
| 183 |
&& mkdir -p "${svcdir}/scheduled/${x}"
|
| 184 |
ln -snf "/etc/init.d/${service}" "${svcdir}/scheduled/${x}/${start}"
|
| 185 |
done
|
| 186 |
|
| 187 |
mark_service_stopped "${start}"
|
| 188 |
end_service "${start}"
|
| 189 |
}
|
| 190 |
|
| 191 |
svc_start_scheduled() {
|
| 192 |
# If we're being started in the background, then don't
|
| 193 |
# tie up the daemon that called us starting our scheduled services
|
| 194 |
if [[ ${IN_BACKGROUND} == "true" || ${IN_BACKGROUND} == "1" ]] ; then
|
| 195 |
unset IN_BACKGROUND
|
| 196 |
svc_start_scheduled &
|
| 197 |
export IN_BACKGROUND=true
|
| 198 |
return
|
| 199 |
fi
|
| 200 |
|
| 201 |
local x= y= services= provides=$(rc-depend --notrace -iprovide "${SVCNAME}")
|
| 202 |
for x in "${SVCNAME}" ${provides} ; do
|
| 203 |
for y in $(dolisting "${svcdir}/scheduled/${x}/") ; do
|
| 204 |
services="${services} ${y##*/}"
|
| 205 |
done
|
| 206 |
done
|
| 207 |
|
| 208 |
if [[ -n ${services} ]] ; then
|
| 209 |
for x in $(rc-depend -ineed -iuse ${services}) ; do
|
| 210 |
service_stopped "${x}" && start_service "${x}"
|
| 211 |
rm -f "${svcdir}/scheduled/${SVCNAME}/${x}"
|
| 212 |
for y in ${provides} ; do
|
| 213 |
rm -f "${svcdir}/scheduled/${y}/${x}"
|
| 214 |
done
|
| 215 |
done
|
| 216 |
fi
|
| 217 |
|
| 218 |
for x in "${SVCNAME}" ${provides} ; do
|
| 219 |
rmdir "${svcdir}/scheduled/${x}" 2>/dev/null
|
| 220 |
done
|
| 221 |
}
|
| 222 |
|
| 223 |
# Tests to see if we are still in control or not as we
|
| 224 |
# could have re-entered instantly blocking our inactive check
|
| 225 |
svc_in_control() {
|
| 226 |
local x
|
| 227 |
for x in starting started stopping ; do
|
| 228 |
[[ "${svcdir}/${x}/${SVCNAME}" -nt "${svcdir}/exclusive/${SVCNAME}.$$" ]] \
|
| 229 |
&& return 1
|
| 230 |
done
|
| 231 |
return 0
|
| 232 |
}
|
| 233 |
|
| 234 |
svc_stop() {
|
| 235 |
local x= retval=0
|
| 236 |
local -a servicelist=()
|
| 237 |
|
| 238 |
# Do not try to stop if it had already failed to do so
|
| 239 |
if is_runlevel_stop && service_failed "${SVCNAME}" ; then
|
| 240 |
return 1
|
| 241 |
elif service_stopped "${SVCNAME}" ; then
|
| 242 |
ewarn $"WARNING:" " ${SVCNAME}" $"has not yet been started."
|
| 243 |
return 0
|
| 244 |
fi
|
| 245 |
if ! mark_service_stopping "${SVCNAME}" ; then
|
| 246 |
eerror $"ERROR:" " ${SVCNAME}" $"is already stopping."
|
| 247 |
return 1
|
| 248 |
fi
|
| 249 |
|
| 250 |
# Ensure that we clean up if we abort for any reason
|
| 251 |
trap "svc_quit" INT QUIT TSTP
|
| 252 |
|
| 253 |
mark_service_starting "${SVCNAME}"
|
| 254 |
begin_service "${SVCNAME}"
|
| 255 |
|
| 256 |
# This is our mtime file to work out if we're still in control or not
|
| 257 |
touch "${svcdir}/exclusive/${SVCNAME}.$$"
|
| 258 |
|
| 259 |
# Store our e* messages in a buffer so we pretty print when parallel
|
| 260 |
[[ ${RC_PARALLEL_STARTUP} == "yes" && ${RC_QUIET} != "yes" ]] \
|
| 261 |
&& ebuffer "${svcdir}/ebuffer/${SVCNAME}"
|
| 262 |
|
| 263 |
veinfo $"Service" "${SVCNAME}" $"stopping"
|
| 264 |
|
| 265 |
if in_runlevel "${SVCNAME}" "${BOOTLEVEL}" && \
|
| 266 |
[[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" && \
|
| 267 |
${SOFTLEVEL} != "single" ]] ; then
|
| 268 |
ewarn $"WARNING: you are stopping a boot service."
|
| 269 |
fi
|
| 270 |
|
| 271 |
# Save the IN_BACKGROUND var as we need to clear it for stopping depends
|
| 272 |
local ib_save="${IN_BACKGROUND}"
|
| 273 |
unset IN_BACKGROUND
|
| 274 |
|
| 275 |
if [[ ${svcpause} != "yes" && ${RC_NO_DEPS} != "yes" ]] \
|
| 276 |
&& ! service_wasinactive "${SVCNAME}" ; then
|
| 277 |
for x in $(reverse_list $(rc-depend -needsme "${SVCNAME}")) ; do
|
| 278 |
if service_started "${x}" || service_inactive "${x}" ; then
|
| 279 |
stop_service "${x}"
|
| 280 |
fi
|
| 281 |
service_list=( "${service_list[@]}" "${x}" )
|
| 282 |
done
|
| 283 |
fi
|
| 284 |
|
| 285 |
for x in "${service_list[@]}" ; do
|
| 286 |
local retry=3
|
| 287 |
while [[ ${retry} -gt 0 ]] ; do
|
| 288 |
service_stopped "${x}" && break
|
| 289 |
wait_service "${x}"
|
| 290 |
((retry--))
|
| 291 |
done
|
| 292 |
if ! service_stopped "${x}" ; then
|
| 293 |
eerror $"ERROR:" $"cannot stop" "${SVCNAME}" $"as" "${x}" $"is still up."
|
| 294 |
retval=1
|
| 295 |
break
|
| 296 |
fi
|
| 297 |
done
|
| 298 |
|
| 299 |
# Work with uses, before and after deps too, but as they are not needed
|
| 300 |
# we cannot explicitly stop them.
|
| 301 |
# We use -needsme with -usesme so we get the full dep list.
|
| 302 |
# We use --notrace with -ibefore to stop circular deps.
|
| 303 |
for x in $(rc-depend -needsme -usesme "${SVCNAME}") \
|
| 304 |
$(rc-depend --notrace -ibefore "${SVCNAME}"); do
|
| 305 |
service_stopping "${x}" && wait_service "${x}"
|
| 306 |
done
|
| 307 |
|
| 308 |
IN_BACKGROUND="${ib_save}"
|
| 309 |
|
| 310 |
if [[ ${retval} == "0" ]] ; then
|
| 311 |
# Now that deps are stopped, stop our service
|
| 312 |
veindent
|
| 313 |
(
|
| 314 |
[[ ${RC_QUIET} == "yes" ]] && RC_QUIET_STDOUT="yes"
|
| 315 |
exit() {
|
| 316 |
eerror $"DO NOT USE EXIT IN INIT.D SCRIPTS"
|
| 317 |
eerror $"This IS a bug, please fix your broken init.d"
|
| 318 |
unset -f exit
|
| 319 |
exit "$@"
|
| 320 |
}
|
| 321 |
stop
|
| 322 |
)
|
| 323 |
retval="$?"
|
| 324 |
|
| 325 |
# Don't trust init scripts to reset indentation properly
|
| 326 |
# Needed for ebuffer
|
| 327 |
eoutdent 99999
|
| 328 |
|
| 329 |
# If a service has been marked inactive, exit now as something
|
| 330 |
# may attempt to start it again later
|
| 331 |
if [[ ${retval} == "0" ]] ; then
|
| 332 |
if service_inactive "${SVCNAME}" || ! svc_in_control ; then
|
| 333 |
rm -f "${svcdir}/exclusive/${SVCNAME}.$$"
|
| 334 |
svcinactive=0
|
| 335 |
return 0
|
| 336 |
fi
|
| 337 |
fi
|
| 338 |
fi
|
| 339 |
|
| 340 |
if [[ ${retval} != 0 ]] ; then
|
| 341 |
# Did we fail to stop? create symlink to stop multible attempts at
|
| 342 |
# runlevel change. Note this is only used at runlevel change ...
|
| 343 |
is_runlevel_stop && mark_service_failed "${SVCNAME}"
|
| 344 |
|
| 345 |
# If we are halting the system, do it as cleanly as possible
|
| 346 |
if [[ ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] ; then
|
| 347 |
mark_service_stopped "${SVCNAME}"
|
| 348 |
else
|
| 349 |
if [[ ${svcinactive} == "0" ]] ; then
|
| 350 |
mark_service_inactive "${SVCNAME}"
|
| 351 |
else
|
| 352 |
mark_service_started "${SVCNAME}"
|
| 353 |
fi
|
| 354 |
fi
|
| 355 |
|
| 356 |
eerror $"ERROR:" " ${SVCNAME}" $"failed to stop"
|
| 357 |
else
|
| 358 |
svcstarted=1
|
| 359 |
if service_inactive "${SVCNAME}" ; then
|
| 360 |
svcinactive=0
|
| 361 |
else
|
| 362 |
mark_service_stopped "${SVCNAME}"
|
| 363 |
fi
|
| 364 |
|
| 365 |
veinfo $"Service" "${SVCNAME}" $"stopped"
|
| 366 |
fi
|
| 367 |
|
| 368 |
# Flush the ebuffer
|
| 369 |
if [[ ${RC_PARALLEL_STARTUP} == "yes" && ${RC_QUIET} != "yes" ]] ; then
|
| 370 |
eflush
|
| 371 |
ebuffer ""
|
| 372 |
fi
|
| 373 |
|
| 374 |
rm -f "${svcdir}/exclusive/${SVCNAME}.$$"
|
| 375 |
end_service "${SVCNAME}"
|
| 376 |
|
| 377 |
# Reset the trap
|
| 378 |
svc_trap
|
| 379 |
|
| 380 |
return "${retval}"
|
| 381 |
}
|
| 382 |
|
| 383 |
svc_start() {
|
| 384 |
local x= y= retval=0 startinactive=
|
| 385 |
|
| 386 |
# Do not try to start if i have done so already on runlevel change
|
| 387 |
if is_runlevel_start && service_failed "${SVCNAME}" ; then
|
| 388 |
return 1
|
| 389 |
elif service_started "${SVCNAME}" ; then
|
| 390 |
ewarn $"WARNING:" " ${SVCNAME}" $"has already been started."
|
| 391 |
return 0
|
| 392 |
elif service_inactive "${SVCNAME}" ; then
|
| 393 |
if [[ ${IN_BACKGROUND} != "true" \
|
| 394 |
&& ${IN_BACKGROUND} != "1" ]] ; then
|
| 395 |
ewarn $"WARNING:" " ${SVCNAME}" $"has already been started."
|
| 396 |
return 0
|
| 397 |
fi
|
| 398 |
elif [[ ${SOFTLEVEL} == "shutdown" || ${SOFTLEVEL} == "reboot" ]] ; then
|
| 399 |
ewarn $"WARNING: system shutting down, will not start" "${SVCNAME}"
|
| 400 |
return 1
|
| 401 |
elif [[ ${SOFTLEVEL} == "single" ]] ; then
|
| 402 |
eerror $"ERROR: system is in single user mode, will not start" "${SVCNAME}"
|
| 403 |
return 1
|
| 404 |
fi
|
| 405 |
|
| 406 |
if ! mark_service_starting "${SVCNAME}" ; then
|
| 407 |
if service_stopping "${SVCNAME}" ; then
|
| 408 |
eerror $"ERROR:" " ${SVCNAME}" $"is already stopping."
|
| 409 |
else
|
| 410 |
eerror $"ERROR: "" ${SVCNAME}" $"is already starting."
|
| 411 |
fi
|
| 412 |
return 1
|
| 413 |
fi
|
| 414 |
|
| 415 |
# Ensure that we clean up if we abort for any reason
|
| 416 |
trap "svc_quit" INT QUIT TSTP
|
| 417 |
begin_service "${SVCNAME}"
|
| 418 |
|
| 419 |
# This is our mtime file to work out if we're still in control or not
|
| 420 |
touch "${svcdir}/exclusive/${SVCNAME}.$$"
|
| 421 |
|
| 422 |
# Store our e* messages in a buffer so we pretty print when parallel
|
| 423 |
[[ ${RC_PARALLEL_STARTUP} == "yes" && ${RC_QUIET} != "yes" ]] \
|
| 424 |
&& ebuffer "${svcdir}/ebuffer/${SVCNAME}"
|
| 425 |
|
| 426 |
veinfo $"Service" "${SVCNAME}" $"starting"
|
| 427 |
|
| 428 |
if [[ -n $(rc-depend --notrace -broken "${SVCNAME}") ]] ; then
|
| 429 |
eerror $"ERROR: Some services needed are missing. Run"
|
| 430 |
eerror " ""'./${SVCNAME}" $"broken' for a list of those"
|
| 431 |
eerror " " $"services." "${SVCNAME}" $"was not started."
|
| 432 |
retval=1
|
| 433 |
fi
|
| 434 |
|
| 435 |
# Save the IN_BACKGROUND var as we need to clear it for starting depends
|
| 436 |
local ib_save="${IN_BACKGROUND}"
|
| 437 |
unset IN_BACKGROUND
|
| 438 |
|
| 439 |
if [[ ${retval} == "0" && ${RC_NO_DEPS} != "yes" ]] ; then
|
| 440 |
# Start dependencies, if any.
|
| 441 |
local startsvc=$(rc-depend -ineed -iuse "${SVCNAME}")
|
| 442 |
if ! is_runlevel_start ; then
|
| 443 |
for x in ${startsvc} ; do
|
| 444 |
service_stopped "${x}" && start_service "${x}"
|
| 445 |
done
|
| 446 |
fi
|
| 447 |
|
| 448 |
# Wait for dependencies to finish.
|
| 449 |
local ineed=$(rc-depend --notrace -ineed "${SVCNAME}")
|
| 450 |
for x in ${startsvc} $(rc-depend -iafter "${SVCNAME}") ; do
|
| 451 |
local timeout=3
|
| 452 |
while [[ ${timeout} -gt 0 ]] ; do
|
| 453 |
service_started "${x}" && continue 2
|
| 454 |
wait_service "${x}"
|
| 455 |
service_started "${x}" && continue 2
|
| 456 |
if service_inactive "${x}" || service_scheduled "${x}" ; then
|
| 457 |
if [[ " ${startsvc} " == *" ${x} "* ||
|
| 458 |
" ${ineed} " == *" $(rc-depend --notrace -iprovide "${x}") " ]] ; then
|
| 459 |
svc_schedule_start "${x}" "${SVCNAME}"
|
| 460 |
[[ -n ${startinactive} ]] && startinactive="${startinactive}, "
|
| 461 |
startinactive="${startinactive}${x}"
|
| 462 |
fi
|
| 463 |
|
| 464 |
continue 2
|
| 465 |
fi
|
| 466 |
service_stopped "${x}" && break
|
| 467 |
|
| 468 |
((timeout--))
|
| 469 |
done
|
| 470 |
|
| 471 |
[[ " ${ineed} " != *" ${x} "* ]] && continue
|
| 472 |
|
| 473 |
eerror "ERROR:" $"cannot start" "${SVCNAME}" $"as" "${x}" $"could not start"
|
| 474 |
retval=1
|
| 475 |
break
|
| 476 |
done
|
| 477 |
|
| 478 |
if [[ -n ${startinactive} && ${retval} == "0" ]] ; then
|
| 479 |
# Change the last , to or for correct grammar.
|
| 480 |
x="${startinactive##*, }"
|
| 481 |
startinactive="${startinactive/%, ${x}/ or ${x}}"
|
| 482 |
ewarn "WARNING:" " ${SVCNAME}" $"is scheduled to start when" "${startinactive}" $"has started."
|
| 483 |
retval=1
|
| 484 |
fi
|
| 485 |
fi
|
| 486 |
|
| 487 |
if [[ ${retval} == "0" ]] ; then
|
| 488 |
IN_BACKGROUND="${ib_save}"
|
| 489 |
veindent
|
| 490 |
(
|
| 491 |
[[ ${RC_QUIET} == "yes" ]] && RC_QUIET_STDOUT="yes"
|
| 492 |
exit() {
|
| 493 |
eerror $"DO NOT USE EXIT IN INIT.D SCRIPTS"
|
| 494 |
eerror $"This IS a bug, please fix your broken init.d"
|
| 495 |
unset -f exit
|
| 496 |
exit "$@"
|
| 497 |
}
|
| 498 |
|
| 499 |
# Apply any ulimits if defined
|
| 500 |
[[ -n ${RC_ULIMIT} ]] && ulimit ${RC_ULIMIT}
|
| 501 |
|
| 502 |
start
|
| 503 |
)
|
| 504 |
retval="$?"
|
| 505 |
|
| 506 |
# Don't trust init scripts to reset indentation properly
|
| 507 |
# Needed for ebuffer
|
| 508 |
eoutdent 99999
|
| 509 |
|
| 510 |
# If a service has been marked inactive, exit now as something
|
| 511 |
# may attempt to start it again later
|
| 512 |
if [[ ${retval} == "0" ]] ; then
|
| 513 |
if service_inactive "${SVCNAME}" || ! svc_in_control ; then
|
| 514 |
rm -f "${svcdir}/exclusive/${SVCNAME}.$$"
|
| 515 |
svcinactive=0
|
| 516 |
ewarn $"WARNING:" " ${SVCNAME}" $"has started but is inactive"
|
| 517 |
if [[ ${RC_PARALLEL_STARTUP} == "yes" && ${RC_QUIET} != "yes" ]] ; then
|
| 518 |
eflush
|
| 519 |
ebuffer ""
|
| 520 |
fi
|
| 521 |
return 1
|
| 522 |
fi
|
| 523 |
fi
|
| 524 |
fi
|
| 525 |
|
| 526 |
if [[ ${retval} != "0" ]] ; then
|
| 527 |
if [[ ${svcinactive} == "0" ]] ; then
|
| 528 |
mark_service_inactive "${SVCNAME}"
|
| 529 |
elif [[ -z ${startinactive} ]] ; then
|
| 530 |
mark_service_stopped "${SVCNAME}"
|
| 531 |
is_runlevel_start && mark_service_failed "${SVCNAME}"
|
| 532 |
eerror $"ERROR:" " ${SVCNAME}" $"failed to start"
|
| 533 |
fi
|
| 534 |
else
|
| 535 |
svcstarted=0
|
| 536 |
mark_service_started "${SVCNAME}"
|
| 537 |
veinfo $"Service" "${SVCNAME}" $"started"
|
| 538 |
fi
|
| 539 |
|
| 540 |
# Flush the ebuffer
|
| 541 |
if [[ ${RC_PARALLEL_STARTUP} == "yes" && ${RC_QUIET} != "yes" ]] ; then
|
| 542 |
eflush
|
| 543 |
ebuffer ""
|
| 544 |
fi
|
| 545 |
|
| 546 |
rm -f "${svcdir}/exclusive/${SVCNAME}.$$"
|
| 547 |
end_service "${SVCNAME}"
|
| 548 |
|
| 549 |
# Reset the trap
|
| 550 |
svc_trap
|
| 551 |
|
| 552 |
return "${retval}"
|
| 553 |
}
|
| 554 |
|
| 555 |
svc_restart() {
|
| 556 |
if [[ ${SOFTLEVEL} == "shutdown" || ${SOFTLEVEL} == "reboot" ]] ; then
|
| 557 |
ewarn $"WARNING: system shutting down, will not restart" "${SVCNAME}"
|
| 558 |
return 1
|
| 559 |
elif [[ ${SOFTLEVEL} == "single" ]] ; then
|
| 560 |
eerror $"ERROR: system is in single user mode, will not restart" "${SVCNAME}"
|
| 561 |
return 1
|
| 562 |
fi
|
| 563 |
|
| 564 |
# We don't kill child processes if we're restarting
|
| 565 |
# This is especically important for sshd ....
|
| 566 |
RC_KILL_CHILDREN="no"
|
| 567 |
|
| 568 |
# Create a snapshot of started services
|
| 569 |
rm -rf "${svcdir}/snapshot/$$"
|
| 570 |
mkdir -p "${svcdir}/snapshot/$$"
|
| 571 |
cp -pP "${svcdir}"/started/* "${svcdir}"/inactive/* \
|
| 572 |
"${svcdir}/snapshot/$$/" 2>/dev/null
|
| 573 |
rm -f "${svcdir}/snapshot/$$/${SVCNAME}"
|
| 574 |
|
| 575 |
# Simple way to try and detect if the service use svc_{start,stop}
|
| 576 |
# to restart if it have a custom restart() funtion.
|
| 577 |
svcres=$(sed -ne '/^[[:space:]]*restart[[:space:]]*()/,/}/ p' "${myscript}")
|
| 578 |
if [[ -n ${svcres} ]] ; then
|
| 579 |
if [[ ! ${svcres} =~ svc_stop \
|
| 580 |
|| ! ${svcres} =~ svc_start ]] ; then
|
| 581 |
echo
|
| 582 |
ewarn $"Please use 'svc_stop; svc_start' and not 'stop; start' to"
|
| 583 |
ewarn $"restart the service in its custom 'restart()' function."
|
| 584 |
ewarn $"Run" "${SVCNAME}" $"without arguments for more info."
|
| 585 |
echo
|
| 586 |
if ! service_stopped "${SVCNAME}" ; then
|
| 587 |
svc_stop || return "$?"
|
| 588 |
fi
|
| 589 |
svc_start
|
| 590 |
else
|
| 591 |
restart
|
| 592 |
fi
|
| 593 |
else
|
| 594 |
restart
|
| 595 |
fi
|
| 596 |
retval="$?"
|
| 597 |
|
| 598 |
[[ -e "${svcdir}/scheduled/${SVCNAME}" ]] \
|
| 599 |
&& rm -Rf "${svcdir}/scheduled/${SVCNAME}"
|
| 600 |
|
| 601 |
# Restart dependencies as well
|
| 602 |
local x=
|
| 603 |
for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
|
| 604 |
if [[ -x ${x} ]] && service_stopped "${x##*/}" ; then
|
| 605 |
if service_inactive "${SVCNAME}" \
|
| 606 |
|| service_wasinactive "${SVCNAME}" ; then
|
| 607 |
svc_schedule_start "${SVCNAME}" "${x##*/}"
|
| 608 |
ewarn $"WARNING:" " ${x##*/}" $"is scheduled to start when" "${SVCNAME}" $"has started."
|
| 609 |
elif service_started "${SVCNAME}" ; then
|
| 610 |
start_service "${x##*/}"
|
| 611 |
fi
|
| 612 |
fi
|
| 613 |
done
|
| 614 |
rm -rf "${svcdir}/snapshot/$$"
|
| 615 |
|
| 616 |
service_started "${SVCNAME}" && svc_start_scheduled
|
| 617 |
|
| 618 |
# Wait for services to come up
|
| 619 |
if [[ ${IN_BACKGROUND} != "true" \
|
| 620 |
&& ${IN_BACKGROUND} != "1" ]] ; then
|
| 621 |
[[ ${RC_PARALLEL_STARTUP} == "yes" ]] && wait
|
| 622 |
fi
|
| 623 |
|
| 624 |
return 0
|
| 625 |
}
|
| 626 |
|
| 627 |
svc_status() {
|
| 628 |
# The basic idea here is to have some sort of consistent
|
| 629 |
# output in the status() function which scripts can use
|
| 630 |
# as an generic means to detect status. Any other output
|
| 631 |
# should thus be formatted in the custom status() function
|
| 632 |
# to work with the printed " * status: foo".
|
| 633 |
local efunc="" state=""
|
| 634 |
|
| 635 |
# If we are effectively root, check to see if required daemons are running
|
| 636 |
# and update our status accordingly
|
| 637 |
[[ ${EUID} == 0 ]] && update_service_status "${SVCNAME}"
|
| 638 |
|
| 639 |
if service_stopping "${SVCNAME}" ; then
|
| 640 |
efunc="eerror"
|
| 641 |
state=$"stopping"
|
| 642 |
elif service_starting "${SVCNAME}" ; then
|
| 643 |
efunc="einfo"
|
| 644 |
state=$"starting"
|
| 645 |
elif service_inactive "${SVCNAME}" ; then
|
| 646 |
efunc="ewarn"
|
| 647 |
state=$"inactive"
|
| 648 |
elif service_started "${SVCNAME}" ; then
|
| 649 |
efunc="einfo"
|
| 650 |
state=$"started"
|
| 651 |
else
|
| 652 |
efunc="eerror"
|
| 653 |
state=$"stopped"
|
| 654 |
fi
|
| 655 |
[[ ${RC_QUIET_STDOUT} != "yes" ]] \
|
| 656 |
&& ${efunc} $"status:" "${state}"
|
| 657 |
|
| 658 |
status
|
| 659 |
# Return 0 if started, otherwise 1
|
| 660 |
[[ ${state} == "started" ]]
|
| 661 |
}
|
| 662 |
|
| 663 |
# set *after* wrap_rcscript, else we get duplicates.
|
| 664 |
opts="start stop restart"
|
| 665 |
|
| 666 |
. "${myscript}"
|
| 667 |
|
| 668 |
# make sure whe have valid $opts
|
| 669 |
if [[ -z ${opts} ]] ; then
|
| 670 |
opts="start stop restart"
|
| 671 |
fi
|
| 672 |
|
| 673 |
svc_homegrown() {
|
| 674 |
local x arg="$1"
|
| 675 |
shift
|
| 676 |
|
| 677 |
# Walk through the list of available options, looking for the
|
| 678 |
# requested one.
|
| 679 |
for x in ${opts} ; do
|
| 680 |
if [[ ${x} == "${arg}" ]] ; then
|
| 681 |
if typeset -F "${x}" &>/dev/null ; then
|
| 682 |
# Run the homegrown function
|
| 683 |
"${x}"
|
| 684 |
|
| 685 |
return $?
|
| 686 |
fi
|
| 687 |
fi
|
| 688 |
done
|
| 689 |
x=""
|
| 690 |
|
| 691 |
# If we're here, then the function wasn't in $opts.
|
| 692 |
[[ -n $* ]] && x="/ $* "
|
| 693 |
eerror $"ERROR:" $"wrong args" "( "${arg}" ${x})"
|
| 694 |
# Do not quote this either ...
|
| 695 |
usage ${opts}
|
| 696 |
exit 1
|
| 697 |
}
|
| 698 |
|
| 699 |
shift
|
| 700 |
if [[ $# -lt 1 ]] ; then
|
| 701 |
eerror $"ERROR:" $"not enough args."
|
| 702 |
usage ${opts}
|
| 703 |
exit 1
|
| 704 |
fi
|
| 705 |
for arg in "$@" ; do
|
| 706 |
case "${arg}" in
|
| 707 |
--quiet)
|
| 708 |
RC_QUIET="yes"
|
| 709 |
RC_QUIET_STDOUT="yes"
|
| 710 |
;;
|
| 711 |
# We check this in functions.sh ...
|
| 712 |
# --nocolor)
|
| 713 |
# RC_NOCOLOR="yes"
|
| 714 |
# ;;
|
| 715 |
--nodeps)
|
| 716 |
RC_NO_DEPS="yes"
|
| 717 |
;;
|
| 718 |
--verbose)
|
| 719 |
RC_VERBOSE="yes"
|
| 720 |
;;
|
| 721 |
esac
|
| 722 |
done
|
| 723 |
|
| 724 |
retval=0
|
| 725 |
for arg in "$@" ; do
|
| 726 |
case "${arg}" in
|
| 727 |
stop)
|
| 728 |
if [[ -e "${svcdir}/scheduled/${SVCNAME}" ]] ; then
|
| 729 |
rm -Rf "${svcdir}/scheduled/${SVCNAME}"
|
| 730 |
fi
|
| 731 |
|
| 732 |
# Stoped from the background - treat this as a restart so that
|
| 733 |
# stopped services come back up again when started.
|
| 734 |
if [[ ${IN_BACKGROUND} == "true" || ${IN_BACKGROUND} == "1" ]] ; then
|
| 735 |
rm -rf "${svcdir}/snapshot/$$"
|
| 736 |
mkdir -p "${svcdir}/snapshot/$$"
|
| 737 |
cp -pP "${svcdir}"/started/* "${svcdir}"/inactive/* \
|
| 738 |
"${svcdir}/snapshot/$$/" 2>/dev/null
|
| 739 |
rm -f "${svcdir}/snapshot/$$/${SVCNAME}"
|
| 740 |
fi
|
| 741 |
|
| 742 |
svc_stop
|
| 743 |
retval="$?"
|
| 744 |
|
| 745 |
if [[ ${IN_BACKGROUND} == "true" || ${IN_BACKGROUND} == "1" ]] ; then
|
| 746 |
for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
|
| 747 |
if [[ -x ${x} ]] && service_stopped "${x##*/}" ; then
|
| 748 |
svc_schedule_start "${SVCNAME}" "${x##*/}"
|
| 749 |
fi
|
| 750 |
done
|
| 751 |
rm -rf "${svcdir}/snapshot/$$"
|
| 752 |
elif service_stopped "${SVCNAME}" ; then
|
| 753 |
rm -f "${svcdir}"/scheduled/*/"${SVCNAME}"
|
| 754 |
if [[ ${SOFTLEVEL} != "single" ]] ; then
|
| 755 |
rm -f "${svcdir}/coldplugged/${SVCNAME}"
|
| 756 |
fi
|
| 757 |
fi
|
| 758 |
|
| 759 |
;;
|
| 760 |
start)
|
| 761 |
svc_start
|
| 762 |
retval="$?"
|
| 763 |
service_started "${SVCNAME}" && svc_start_scheduled
|
| 764 |
;;
|
| 765 |
needsme|ineed|usesme|iuse|broken|iafter|iprovide)
|
| 766 |
rc-depend "-${arg}" "${SVCNAME}"
|
| 767 |
;;
|
| 768 |
status)
|
| 769 |
svc_status
|
| 770 |
retval="$?"
|
| 771 |
;;
|
| 772 |
zap)
|
| 773 |
einfo $"Manually resetting" "${SVCNAME}" $"to stopped state."
|
| 774 |
mark_service_stopped "${SVCNAME}"
|
| 775 |
;;
|
| 776 |
restart)
|
| 777 |
svc_restart
|
| 778 |
retval="$?"
|
| 779 |
;;
|
| 780 |
condrestart|conditionalrestart)
|
| 781 |
if service_started "${SVCNAME}" ; then
|
| 782 |
svc_restart
|
| 783 |
fi
|
| 784 |
retval="$?"
|
| 785 |
;;
|
| 786 |
pause)
|
| 787 |
svcpause="yes"
|
| 788 |
svc_stop
|
| 789 |
retval="$?"
|
| 790 |
svcpause="no"
|
| 791 |
;;
|
| 792 |
--quiet|--nocolor|--nocolour|--nodeps|--verbose|--debug)
|
| 793 |
;;
|
| 794 |
-V|--version)
|
| 795 |
exec cat "${ROOT}"/etc/gentoo-release
|
| 796 |
exit 1
|
| 797 |
;;
|
| 798 |
help|-h|--help)
|
| 799 |
exec "${svclib}"/sh/rc-help.sh "${myscript}" help
|
| 800 |
;;
|
| 801 |
*)
|
| 802 |
# Allow for homegrown functions
|
| 803 |
svc_homegrown ${arg}
|
| 804 |
retval="$?"
|
| 805 |
;;
|
| 806 |
esac
|
| 807 |
done
|
| 808 |
|
| 809 |
exit "${retval}"
|
| 810 |
|
| 811 |
# vim: set ts=4 :
|