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