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

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

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

Revision 202 Revision 203
2# Copyright 1999-2006 Gentoo Foundation 2# Copyright 1999-2006 Gentoo Foundation
3# Distributed under the terms of the GNU General Public License v2 3# Distributed under the terms of the GNU General Public License v2
4 4
5# Common functions 5# Common functions
6[[ ${RC_GOT_FUNCTIONS} != "yes" ]] && source /sbin/functions.sh 6[[ ${RC_GOT_FUNCTIONS} != "yes" ]] && source /sbin/functions.sh
7# Functions to handle dependencies and services
8[[ ${RC_GOT_SERVICES} != "yes" ]] && source "${svclib}/sh/rc-services.sh"
9# Functions to control daemons
10[[ ${RC_GOT_DAEMON} != "yes" ]] && source "${svclib}/sh/rc-daemon.sh"
11 7
12# User must be root to run most script stuff (except status) 8# User must be root to run most script stuff (except status)
13if [[ ${EUID} != 0 ]] && ! [[ $2 == "status" && $# -eq 2 ]] ; then 9if [[ ${EUID} != 0 ]] && ! [[ $2 == "status" && $# -eq 2 ]] ; then
14 eerror "$0: must be root to run init scripts" 10 eerror "$0: must be root to run init scripts"
15 exit 1 11 exit 1
18# State variables 14# State variables
19svcpause="no" 15svcpause="no"
20svcrestart="no" 16svcrestart="no"
21 17
22myscript="$1" 18myscript="$1"
23if [[ -L $1 && ! -L /etc/init.d/${1##*/} ]] ; then 19if [[ -L $1 && ! -L "/etc/init.d/${1##*/}" ]] ; then
24 myservice="$(readlink $1)" 20 myservice="$(readlink $1)"
25else 21else
26 myservice="$1" 22 myservice="$1"
27fi 23fi
28 24
29myservice="${myservice##*/}" 25myservice="${myservice##*/}"
30export SVCNAME="${myservice}" 26export SVCNAME="${myservice}"
31mylevel="$(<${svcdir}/softlevel)" 27mylevel="$(<${svcdir}/softlevel)"
28
29svc_trap() {
30 trap 'eerror "ERROR: \"${myservice}\" caught an interrupt"; exit 1' \
31 INT QUIT TSTP
32}
33
34# Setup a default trap
35svc_trap
36
37# Functions to handle dependencies and services
38[[ ${RC_GOT_SERVICES} != "yes" ]] && source "${svclib}/sh/rc-services.sh"
39# Functions to control daemons
40[[ ${RC_GOT_DAEMON} != "yes" ]] && source "${svclib}/sh/rc-daemon.sh"
32 41
33# Set $IFACE to the name of the network interface if it is a 'net.*' script 42# Set $IFACE to the name of the network interface if it is a 'net.*' script
34if [[ ${myservice%%.*} == "net" && ${myservice##*.} != "${myservice}" ]] ; then 43if [[ ${myservice%%.*} == "net" && ${myservice##*.} != "${myservice}" ]] ; then
35 IFACE="${myservice##*.}" 44 IFACE="${myservice##*.}"
36 NETSERVICE="yes" 45 NETSERVICE="yes"
44# (2) Source /etc/conf.d/${myservice} to get initscript-specific 53# (2) Source /etc/conf.d/${myservice} to get initscript-specific
45# configuration (if it exists). 54# configuration (if it exists).
46# (3) Source /etc/rc.conf to pick up potentially overriding 55# (3) Source /etc/rc.conf to pick up potentially overriding
47# configuration, if the system administrator chose to put it 56# configuration, if the system administrator chose to put it
48# there (if it exists). 57# there (if it exists).
49if [[ ${NETSERVICE} == "yes" ]]; then 58if [[ ${NETSERVICE} == "yes" ]] ; then
50 conf="$(add_suffix /etc/conf.d/net)" 59 conf="$(add_suffix /etc/conf.d/net)"
51 [[ -e ${conf} ]] && source "${conf}" 60 [[ -e ${conf} ]] && source "${conf}"
52fi 61fi
53conf="$(add_suffix /etc/conf.d/${myservice})" 62conf="$(add_suffix /etc/conf.d/${myservice})"
54[[ -e ${conf} ]] && source "${conf}" 63[[ -e ${conf} ]] && source "${conf}"
55conf="$(add_suffix /etc/rc.conf)" 64conf="$(add_suffix /etc/rc.conf)"
56[[ -e ${conf} ]] && source "${conf}" 65[[ -e ${conf} ]] && source "${conf}"
57 66
67# Call svc_quit if we abort AND we have obtained a lock
68svcbegun=1
69service_started "${myservice}"
70svcstarted=$?
71svc_quit() {
72 eerror "ERROR: \"${myservice}\" caught an interrupt"
73 if [[ ${svcbegun} == 0 ]] ; then
74 end_service "${myservice}"
75 svcbegun=1
76 fi
77 if [[ ${svcstarted} == 0 ]] ; then
78 mark_service_started "${myservice}"
79 else
80 mark_service_stopped "${myservice}"
81 fi
82 exit 1
83}
84
58usage() { 85usage() {
59 local IFS="|" 86 local IFS="|"
60 myline="Usage: ${myservice} { $* " 87 myline="Usage: ${myservice} { $* "
61 echo 88 echo
62 eerror "${myline}}" 89 eerror "${myline}}"
80 107
81status() { 108status() {
82 # Dummy function 109 # Dummy function
83 return 0 110 return 0
84} 111}
85 112
86svc_stop() { 113svc_stop() {
87 local x= mydep= mydeps= retval=0 was_inactive=false 114 local x= mydep= mydeps= retval=0 was_inactive=false
88 local -a servicelist=() 115 local -a servicelist=()
89 116
90 # Do not try to stop if it had already failed to do so on runlevel change 117 # Do not try to stop if it had already failed to do so on runlevel change
102 ewarn "WARNING: \"${myservice}\" is already stopping." 129 ewarn "WARNING: \"${myservice}\" is already stopping."
103 return 0 130 return 0
104 fi 131 fi
105 # Lock service starting too ... 132 # Lock service starting too ...
106 mark_service_starting "${myservice}" 133 mark_service_starting "${myservice}"
134
135 # Ensure that we clean up if we abort for any reason
136 trap "svc_quit" INT QUIT TSTP
137
107 begin_service "${myservice}" 138 begin_service "${myservice}"
108 local begun=$? 139 svcbegun=$?
109 140
110 service_message "Stopping service ${myservice}" 141 service_message "Stopping service ${myservice}"
111 142
112 if in_runlevel "${myservice}" "${BOOTLEVEL}" && \ 143 if in_runlevel "${myservice}" "${BOOTLEVEL}" && \
113 [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] ; then 144 [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] ; then
145 fi 176 fi
146 done 177 done
147 done 178 done
148 179
149 for x in "${service_list[@]}" ; do 180 for x in "${service_list[@]}" ; do
150 service_stopped "${x}" && continue
151 wait_service "${x}" 181 wait_service "${x}"
152 if ! service_stopped "${x}" ; then 182 if ! service_stopped "${x}" ; then
153 retval=1 183 retval=1
154 break 184 break
155 fi 185 fi
169 retval=$? 199 retval=$?
170 200
171 # If a service has been marked inactive, exit now as something 201 # If a service has been marked inactive, exit now as something
172 # may attempt to start it again later 202 # may attempt to start it again later
173 if service_inactive "${myservice}" ; then 203 if service_inactive "${myservice}" ; then
174 [[ ${begun} == 0 ]] && end_service "${myservice}" 0 204 [[ ${svcbegun} == 0 ]] && end_service "${myservice}" 0
175 return 0 205 return 0
176 fi 206 fi
177 fi 207 fi
178 208
179 if [[ ${retval} != 0 ]] ; then 209 if [[ ${retval} != 0 ]] ; then
192 222
193 service_message "eerror" "FAILED to stop service ${myservice}!" 223 service_message "eerror" "FAILED to stop service ${myservice}!"
194 else 224 else
195 # If we're stopped from a daemon that sets ${IN_BACKGROUND} such as 225 # If we're stopped from a daemon that sets ${IN_BACKGROUND} such as
196 # wpa_monitor when we mark as inactive instead of taking the down 226 # wpa_monitor when we mark as inactive instead of taking the down
227 svcstarted=1
197 if ${IN_BACKGROUND:-false} ; then 228 if ${IN_BACKGROUND:-false} ; then
198 mark_service_inactive "${myservice}" 229 mark_service_inactive "${myservice}"
199 else 230 else
200 mark_service_stopped "${myservice}" 231 mark_service_stopped "${myservice}"
201 fi 232 fi
202 service_message "Stopped service ${myservice}" 233 service_message "Stopped service ${myservice}"
203 fi 234 fi
204 235
236 if [[ ${svcbegun} == 0 ]] ; then
205 [[ ${begun} == 0 ]] && end_service "${myservice}" "${retval}" 237 end_service "${myservice}" "${retval}"
238 svcbegun=1
239 fi
240
241 # Reset the trap
242 svc_trap
243
206 return "${retval}" 244 return "${retval}"
207} 245}
208 246
209svc_start() { 247svc_start() {
210 local x= y= retval=0 was_inactive=false startfail="no" 248 local x= y= retval=0 was_inactive=false startfail="no"
213 if is_runlevel_start && service_failed "${myservice}" ; then 251 if is_runlevel_start && service_failed "${myservice}" ; then
214 return 1 252 return 1
215 fi 253 fi
216 254
217 if service_started "${myservice}" ; then 255 if service_started "${myservice}" ; then
218 ewarn "WARNING: \"${myservice}\" has already been started." 256 ewarn "WARNING: \"${myservice}\" has already been started."
219 return 0 257 return 0
220 elif service_stopping "${myservice}" ; then 258 elif service_stopping "${myservice}" ; then
221 eerror "ERROR: please wait for \"${myservice}\" to stop first." 259 eerror "ERROR: please wait for \"${myservice}\" to stop first."
222 return 1 260 return 1
223 elif service_inactive "${myservice}" ; then 261 elif service_inactive "${myservice}" ; then
224 if [[ ${IN_BACKGROUND} != "true" ]] ; then 262 if [[ ${IN_BACKGROUND} != "true" ]] ; then
225 ewarn "WARNING: \"${myservice}\" has already been started." 263 ewarn "WARNING: \"${myservice}\" has already been started."
226 return 0 264 return 0
227 fi 265 fi
228 fi 266 fi
229 267
230 service_inactive "${myservice}" && was_inactive=true 268 service_inactive "${myservice}" && was_inactive=true
231 if ! mark_service_starting "${myservice}" ; then 269 if ! mark_service_starting "${myservice}" ; then
232 ewarn "WARNING: \"${myservice}\" is already starting." 270 ewarn "WARNING: \"${myservice}\" is already starting."
233 return 0 271 return 0
234 fi 272 fi
273
274 # Ensure that we clean up if we abort for any reason
275 trap "svc_quit" INT QUIT TSTP
276
235 begin_service "${myservice}" 277 begin_service "${myservice}"
236 local begun=$? 278 svcbegun=$?
237 279
238 service_message "Starting service ${myservice}" 280 service_message "Starting service ${myservice}"
239 281
240 local startupservices="$(trace_dependencies $(ineed "${myservice}") \ 282 local startupservices="$(trace_dependencies $(ineed "${myservice}") \
241 $(valid_iuse ${myservice}))" 283 $(valid_iuse ${myservice}))"
284 local netservices="$(dolisting "/etc/runlevels/${BOOTLEVEL}/net.*") \
285 $(dolisting "/etc/runlevels/${mylevel}/net.*")"
286 local mynetservice=
242 287
243 # Start dependencies, if any 288 # Start dependencies, if any.
289 # We don't handle "after" deps here as it's the job of rc to start them.
244 for x in ${startupservices} ; do 290 for x in ${startupservices} ; do
245 if [[ ${x} == "net" && ${NETSERVICE} != "yes" ]] && ! is_net_up ; then 291 if [[ ${x} == "net" && ${NETSERVICE} != "yes" ]] && ! is_net_up ; then
246 local netservices="$(dolisting "/etc/runlevels/${BOOTLEVEL}/net.*") \
247 $(dolisting "/etc/runlevels/${mylevel}/net.*")"
248
249 for y in ${netservices} ; do 292 for y in ${netservices} ; do
250 mynetservice="${y##*/}" 293 mynetservice="${y##*/}"
251 if service_stopped "${mynetservice}" ; then 294 if service_stopped "${mynetservice}" ; then
252 start_service "${mynetservice}" 295 start_service "${mynetservice}"
253 fi 296 fi
257 start_service "${x}" 300 start_service "${x}"
258 fi 301 fi
259 fi 302 fi
260 done 303 done
261 304
305 # We also wait for any services we're after to finish incase they
306 # have a "before" dep but we don't dep on them.
307 if is_runlevel_start ; then
308 startupservices="${startupservices} $(valid_iafter ${myservice})"
309 fi
310
262 # wait for dependencies to finish 311 # Wait for dependencies to finish.
263 for x in ${startupservices} ; do 312 for x in ${startupservices} ; do
264 if [ "${x}" = "net" -a "${NETSERVICE}" != "yes" ] ; then 313 if [[ ${x} == "net" && ${NETSERVICE} != "yes" ]] ; then
265 for y in ${netservices} ; do 314 for y in ${netservices} ; do
266 mynetservice="${y##*/}" 315 mynetservice="${y##*/}"
267 316
268 wait_service "${mynetservice}" 317 wait_service "${mynetservice}"
269 318
277 startfail="yes" 326 startfail="yes"
278 fi 327 fi
279 fi 328 fi
280 fi 329 fi
281 done 330 done
282 elif [ "${x}" != "net" ] ; then 331 elif [[ ${x} != "net" ]] ; then
283 wait_service "${x}" 332 wait_service "${x}"
284 if ! service_started "${x}" ; then 333 if ! service_started "${x}" ; then
285 # A 'need' dependacy is critical for startup 334 # A 'need' dependacy is critical for startup
286 if ineed -t "${myservice}" "${x}" >/dev/null ; then 335 if ineed -t "${myservice}" "${x}" >/dev/null ; then
287 startfail="yes" 336 startfail="yes"
315 retval=$? 364 retval=$?
316 365
317 # If a service has been marked inactive, exit now as something 366 # If a service has been marked inactive, exit now as something
318 # may attempt to start it again later 367 # may attempt to start it again later
319 if service_inactive "${myservice}" ; then 368 if service_inactive "${myservice}" ; then
320 [[ ${begun} == 0 ]] && end_service "${myservice}" 1 369 [[ ${svcbegun} == 0 ]] && end_service "${myservice}" 1
321 return 1 370 return 1
322 fi 371 fi
323 fi 372 fi
324 373
325 if [[ ${retval} != 0 ]] ; then 374 if [[ ${retval} != 0 ]] ; then
326 is_runlevel_start && mark_service_failed "${myservice}" 375 is_runlevel_start && mark_service_failed "${myservice}"
327 376
328 # Remove link if service didn't start; but only if we're not booting 377 # Remove link if service didn't start; but only if we're not booting
329 # If we're booting, we need to continue and do our best to get the 378 # If we're booting, we need to continue and do our best to get the
330 # system up. 379 # system up.
331 if [[ ${SOFTLEVEL} != "${BOOTLEVEL}" ]]; then 380 if [[ ${SOFTLEVEL} != "${BOOTLEVEL}" ]] ; then
332 if ${was_inactive} ; then 381 if ${was_inactive} ; then
333 mark_service_inactive "${myservice}" 382 mark_service_inactive "${myservice}"
334 else 383 else
335 mark_service_stopped "${myservice}" 384 mark_service_stopped "${myservice}"
336 fi 385 fi
337 fi 386 fi
338 387
339 service_message "eerror" "FAILED to start service ${myservice}!" 388 service_message "eerror" "FAILED to start service ${myservice}!"
340 else 389 else
390 svcstarted=0
341 mark_service_started "${myservice}" 391 mark_service_started "${myservice}"
342 392
343 service_message "Service ${myservice} started OK" 393 service_message "Service ${myservice} started OK"
344 fi 394 fi
345 395
396 if [[ ${svcbegun} == 0 ]] ; then
346 [[ ${begun} == 0 ]] && end_service "${myservice}" "${retval}" 397 end_service "${myservice}" "${retval}"
398 svcbegun=1
399 fi
400
401 # Reset the trap
402 svc_trap
403
347 return "${retval}" 404 return "${retval}"
348} 405}
349 406
350svc_restart() { 407svc_restart() {
351 if ! service_stopped "${myservice}" ; then 408 if ! service_stopped "${myservice}" ; then
387 444
388 status 445 status
389 [[ ${efunc} != "eerror" ]] 446 [[ ${efunc} != "eerror" ]]
390} 447}
391 448
392rcscript_errors=$(bash -n "${myscript}" 2>&1) || { 449rcscript_errors="$(bash -n ${myscript} 2>&1)" || {
393 [[ -n ${rcscript_errors} ]] && echo "${rcscript_errors}" >&2 450 [[ -n ${rcscript_errors} ]] && echo "${rcscript_errors}" >&2
394 eerror "ERROR: \"${myscript}\" has syntax errors in it; aborting ..." 451 eerror "ERROR: \"${myscript}\" has syntax errors in it; aborting ..."
395 exit 1 452 exit 1
396} 453}
397 454
404if [[ -z ${opts} ]] ; then 461if [[ -z ${opts} ]] ; then
405 opts="start stop restart" 462 opts="start stop restart"
406fi 463fi
407 464
408svc_homegrown() { 465svc_homegrown() {
409 local x arg=$1 466 local x arg="$1"
410 shift 467 shift
411 468
412 # Walk through the list of available options, looking for the 469 # Walk through the list of available options, looking for the
413 # requested one. 470 # requested one.
414 for x in ${opts} ; do 471 for x in ${opts} ; do
452 esac 509 esac
453done 510done
454for arg in $* ; do 511for arg in $* ; do
455 case "${arg}" in 512 case "${arg}" in
456 stop) 513 stop)
457 if [[ -e "${svcdir}/restart/${myservice}" ]]; then 514 if [[ -e "${svcdir}/restart/${myservice}" ]] ; then
458 rm -f "${svcdir}/restart/${myservice}" 515 rm -f "${svcdir}/restart/${myservice}"
459 fi 516 fi
460 517
461 # Stoped from the background - treat this as a restart so that 518 # Stoped from the background - treat this as a restart so that
462 # stopped services come back up again when started. 519 # stopped services come back up again when started.
463 if [[ ${IN_BACKGROUND} == "true" ]]; then 520 if [[ ${IN_BACKGROUND} == "true" ]] ; then
464 rm -rf "${svcdir}/snapshot/$$" 521 rm -rf "${svcdir}/snapshot/$$"
465 mkdir -p "${svcdir}/snapshot/$$" 522 mkdir -p "${svcdir}/snapshot/$$"
466 cp -a "${svcdir}"/started/* "${svcdir}/snapshot/$$/" 523 cp -a "${svcdir}"/started/* "${svcdir}/snapshot/$$/"
467 fi 524 fi
468 525
469 svc_stop 526 svc_stop
470 527
471 if [[ ${IN_BACKGROUND} == "true" ]]; then 528 if [[ ${IN_BACKGROUND} == "true" ]] ; then
472 res= 529 res=
473 for x in $(dolisting "${svcdir}/snapshot/$$/") ; do 530 for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
474 if service_stopped "${x##*/}" ; then 531 if service_stopped "${x##*/}" ; then
475 res="${res}${x##*/} " 532 res="${res}${x##*/} "
476 fi 533 fi
477 done 534 done
478 echo "${res}" > "${svcdir}/restart/${myservice}" 535 [[ -n ${res} ]] && echo "${res}" > "${svcdir}/restart/${myservice}"
479 fi 536 fi
480 ;; 537 ;;
481 start) 538 start)
482 svc_start 539 svc_start
483 retval=$? 540 retval=$?
484 if [[ -e "${svcdir}/restart/${myservice}" ]]; then 541 if ! is_runlevel_start && [[ -s "${svcdir}/restart/${myservice}" ]] ; then
485 for x in $(trace_dependencies $(< "${svcdir}/restart/${myservice}")) ; do 542 for x in $(trace_dependencies $(< "${svcdir}/restart/${myservice}")) ; do
486 service_stopped "${x}" && start_service "${x}" 543 service_stopped "${x}" && start_service "${x}"
487 done 544 done
545 fi
546 if [[ -e "${svcdir}/restart/${myservice}" ]] ; then
488 rm -f "${svcdir}/restart/${myservice}" 547 rm -f "${svcdir}/restart/${myservice}"
489 fi 548 fi
490 exit ${retval} 549 exit ${retval}
491 ;; 550 ;;
492 needsme|ineed|usesme|iuse|broken) 551 needsme|ineed|usesme|iuse|broken)
494 ;; 553 ;;
495 status) 554 status)
496 svc_status 555 svc_status
497 ;; 556 ;;
498 zap) 557 zap)
499 if [[ -e "${svcdir}/restart/${myservice}" ]]; then 558 if [[ -e "${svcdir}/restart/${myservice}" ]] ; then
500 rm -f "${svcdir}/restart/${myservice}" 559 rm -f "${svcdir}/restart/${myservice}"
501 fi 560 fi
502 if ! service_stopped "${myservice}" ; then 561 if ! service_stopped "${myservice}" ; then
503 einfo "Manually resetting ${myservice} to stopped state." 562 einfo "Manually resetting ${myservice} to stopped state."
504 mark_service_stopped "${myservice}" 563 mark_service_stopped "${myservice}"
564 fi
505 end_service "${myservice}" 565 end_service "${myservice}"
506 fi
507 ;; 566 ;;
508 restart) 567 restart)
509 svcrestart="yes" 568 svcrestart="yes"
510 569
511 # We don't kill child processes if we're restarting 570 # We don't kill child processes if we're restarting
523 if [[ -z $(egrep 'svc_stop' "/etc/init.d/${myservice}") || \ 582 if [[ -z $(egrep 'svc_stop' "/etc/init.d/${myservice}") || \
524 -z $(egrep 'svc_start' "/etc/init.d/${myservice}") ]] ; then 583 -z $(egrep 'svc_start' "/etc/init.d/${myservice}") ]] ; then
525 echo 584 echo
526 ewarn "Please use 'svc_stop; svc_start' and not 'stop; start' to" 585 ewarn "Please use 'svc_stop; svc_start' and not 'stop; start' to"
527 ewarn "restart the service in its custom 'restart()' function." 586 ewarn "restart the service in its custom 'restart()' function."
528/var/lib/init.d/exclusive/net.lan ewarn "Run ${myservice} without arguments for more info." 587 ewarn "Run ${myservice} without arguments for more info."
529 echo 588 echo
530 svc_restart 589 svc_restart
531 else 590 else
532 restart 591 restart
533 fi 592 fi
548 for x in $(dolisting "${svcdir}/snapshot/$$/") ; do 607 for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
549 if service_stopped "${x##*/}" ; then 608 if service_stopped "${x##*/}" ; then
550 res="${res}${x##*/} " 609 res="${res}${x##*/} "
551 fi 610 fi
552 done 611 done
553 echo "${res}" > "${svcdir}/restart/${myservice}" 612 [[ -n ${res} ]] && echo "${res}" > "${svcdir}/restart/${myservice}"
554 fi 613 fi
555 614
556 # Wait for any services that may still be running ... 615 # Wait for any services that may still be running ...
557 [[ ${RC_PARALLEL_STARTUP} == "yes" ]] && wait 616 [[ ${RC_PARALLEL_STARTUP} == "yes" ]] && wait
558 617

Legend:
Removed from v.202  
changed lines
  Added in v.203

  ViewVC Help
Powered by ViewVC 1.1.20