/[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 200 Revision 201
82 # Dummy function 82 # Dummy function
83 return 0 83 return 0
84} 84}
85 85
86svc_stop() { 86svc_stop() {
87 local x= 87 local x= mydep= mydeps= retval=0 was_inactive=false
88 local mydep= 88 local -a servicelist=()
89 local mydeps= 89
90 local retval=0
91 local ordservice=
92 local was_inactive=false
93
94 if service_stopping "${myservice}" ; then
95 eerror "ERROR: \"${myservice}\" is already stopping."
96 return 0
97 elif service_stopped "${myservice}" ; then
98 eerror "ERROR: \"${myservice}\" has not yet been started."
99 return 0
100 fi
101
102 # Do not try to stop if it had already failed to do so on runlevel change 90 # Do not try to stop if it had already failed to do so on runlevel change
103 if is_runlevel_stop && service_failed "${myservice}" ; then 91 if is_runlevel_stop && service_failed "${myservice}" ; then
104 return 1 92 return 1
105 fi 93 fi
94
95 if service_stopped "${myservice}" ; then
96 ewarn "WARNING: \"${myservice}\" has not yet been started."
97 return 0
98 fi
106 99
107 service_inactive "${myservice}" && was_inactive=true 100 service_inactive "${myservice}" && was_inactive=true
108 101 if ! mark_service_stopping "${myservice}" ; then
109 # Remove symlink to prevent recursion 102 ewarn "WARNING: \"${myservice}\" is already stopping."
103 return 0
104 fi
105 # Lock service starting too ...
110 mark_service_stopping "${myservice}" 106 mark_service_starting "${myservice}"
111 107 begin_service "${myservice}"
108 local begun=$?
109
112 service_message "Stopping service ${myservice}" 110 service_message "Stopping service ${myservice}"
113 111
114 if in_runlevel "${myservice}" "${BOOTLEVEL}" && \ 112 if in_runlevel "${myservice}" "${BOOTLEVEL}" && \
115 [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] ; then 113 [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] ; then
116 ewarn "WARNING: you are stopping a boot service." 114 ewarn "WARNING: you are stopping a boot service."
135 fi 133 fi
136 134
137 # Save the IN_BACKGROUND var as we need to clear it for stopping depends 135 # Save the IN_BACKGROUND var as we need to clear it for stopping depends
138 local ib_save="${IN_BACKGROUND}" 136 local ib_save="${IN_BACKGROUND}"
139 unset IN_BACKGROUND 137 unset IN_BACKGROUND
140 local -a servicelist=() index=0
141 138
142 for mydep in ${mydeps} ; do 139 for mydep in ${mydeps} ; do
143 # If some service 'need' $mydep, stop it first; or if it is a runlevel change, 140 for x in $(needsme "${mydep}") ; do
144 # first stop all services that is started 'after' $mydep.
145 if needsme "${mydep}" >/dev/null || \
146 (is_runlevel_stop && ibefore "${mydep}" >/dev/null) ; then
147 local -a sl=( $(needsme "${mydep}") )
148
149 # On runlevel change, stop all services "after $mydep" first ...
150 if is_runlevel_stop ; then
151 sl=( "${sl[@]}" $(ibefore "${mydep}") )
152 fi
153
154 local z="${#sl[@]}"
155 for (( x=0; x<z; x++ )); do
156 # Service not currently running, continue 141 # Service not currently running, continue
157 if ! service_started "${sl[x]}" ; then 142 if service_started "${x}" ; then
158 unset sl[x]
159 continue
160 fi
161
162 if ibefore -t "${mydep}" "${x}" >/dev/null && \
163 [[ -L ${svcdir}/softscripts.new/${x} ]] ; then
164 # Service do not 'need' $mydep, and is still present in
165 # new runlevel ...
166 unset sl[x]
167 continue
168 fi
169
170 stop_service "${sl[x]}" 143 stop_service "${x}"
144 service_list=( "${service_list[@]}" "${x}" )
145 fi
171 done 146 done
172 fi
173 servicelist[index]="${sl[index]}"
174 (( index++ ))
175 done 147 done
176 148
177 index=0
178 for mydep in ${mydeps} ; do
179 for x in ${servicelist[index]} ; do 149 for x in "${service_list[@]}" ; do
180 service_stopped "${x}" && continue 150 service_stopped "${x}" && continue
181
182 if ibefore -t "${mydep}" "${x}" >/dev/null && \
183 [[ -L ${svcdir}/softscripts.new/${x} ]] ; then
184 # Service do not 'need' $mydep, and is still present in
185 # new runlevel ...
186 continue
187 fi
188
189 wait_service "${x}" 151 wait_service "${x}"
190
191 if ! service_stopped "${x}" ; then 152 if ! service_stopped "${x}" ; then
192 # If we are halting the system, try and get it down as
193 # clean as possible, else do not stop our service if
194 # a dependent service did not stop.
195 if needsme -t "${mydep}" "${x}" >/dev/null && \
196 [[ ${SOFTLEVEL} != "reboot" && \
197 ${SOFTLEVEL} != "shutdown" ]] ; then
198 retval=1 153 retval=1
199 fi
200 break 154 break
201 fi 155 fi
202 done
203 (( index++ ))
204 done 156 done
205 157
206 IN_BACKGROUND="${ib_save}" 158 IN_BACKGROUND="${ib_save}"
207 159
208 if [[ ${retval} -ne 0 ]] ; then 160 if [[ ${retval} != 0 ]] ; then
209 eerror "ERROR: problems stopping dependent services." 161 eerror "ERROR: problems stopping dependent services."
210 eerror " \"${myservice}\" is still up." 162 eerror " \"${myservice}\" is still up."
211 else 163 else
212 # Stop einfo/ebegin/eend from working as parallel messes us up 164 # Stop einfo/ebegin/eend from working as parallel messes us up
213 [[ ${RC_PARALLEL_STARTUP} == "yes" ]] && RC_QUIET_STDOUT="yes" 165 [[ ${RC_PARALLEL_STARTUP} == "yes" ]] && RC_QUIET_STDOUT="yes"
166
214 # Now that deps are stopped, stop our service 167 # Now that deps are stopped, stop our service
215 ( stop ) 168 ( stop )
216 retval=$? 169 retval=$?
217 170
218 # If a service has been marked inactive, exit now as something 171 # If a service has been marked inactive, exit now as something
219 # may attempt to start it again later 172 # may attempt to start it again later
220 service_inactive "${myservice}" && return 0 173 if service_inactive "${myservice}" ; then
174 [[ ${begun} == 0 ]] && end_service "${myservice}" 0
175 return 0
221 fi 176 fi
177 fi
222 178
223 if [[ ${retval} -ne 0 ]] ; then 179 if [[ ${retval} != 0 ]] ; then
224 # Did we fail to stop? create symlink to stop multible attempts at 180 # Did we fail to stop? create symlink to stop multible attempts at
225 # runlevel change. Note this is only used at runlevel change ... 181 # runlevel change. Note this is only used at runlevel change ...
226 if is_runlevel_stop ; then
227 mark_service_failed "${myservice}" 182 is_runlevel_stop && mark_service_failed "${myservice}"
228 fi
229 183
230 # If we are halting the system, do it as cleanly as possible 184 # If we are halting the system, do it as cleanly as possible
231 if [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] ; then 185 if [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] ; then
232 if ${was_inactive} ; then 186 if ${was_inactive} ; then
233 mark_service_inactive "${myservice}" 187 mark_service_inactive "${myservice}"
246 mark_service_stopped "${myservice}" 200 mark_service_stopped "${myservice}"
247 fi 201 fi
248 service_message "Stopped service ${myservice}" 202 service_message "Stopped service ${myservice}"
249 fi 203 fi
250 204
205 [[ ${begun} == 0 ]] && end_service "${myservice}" "${retval}"
251 return "${retval}" 206 return "${retval}"
252} 207}
253 208
254svc_start() { 209svc_start() {
255 local retval=0 210 local x= y= retval=0 was_inactive=false startfail="no"
256 local startfail="no"
257 local x=
258 local y=
259 local myserv=
260 local ordservice=
261 211
212 # Do not try to start if i have done so already on runlevel change
213 if is_runlevel_start && service_failed "${myservice}" ; then
214 return 1
215 fi
216
262 if service_starting "${myservice}" ; then 217 if service_started "${myservice}" ; then
263 ewarn "WARNING: \"${myservice}\" is already starting." 218 ewarn "WARNING: \"${myservice}\" has already been started."
264 return 0 219 return 0
265 elif service_stopping "${myservice}" ; then 220 elif service_stopping "${myservice}" ; then
266 ewarn "WARNING: please wait for \"${myservice}\" to stop first." 221 eerror "ERROR: please wait for \"${myservice}\" to stop first."
267 return 0 222 return 1
268 elif service_inactive "${myservice}" ; then 223 elif service_inactive "${myservice}" ; then
269 if [[ ${IN_BACKGROUND} != "true" ]] ; then 224 if [[ ${IN_BACKGROUND} != "true" ]] ; then
270 ewarn "WARNING: \"${myservice}\" has already been started." 225 ewarn "WARNING: \"${myservice}\" has already been started."
271 return 0 226 return 0
272 fi 227 fi
228 fi
229
230 service_inactive "${myservice}" && was_inactive=true
273 elif service_started "${myservice}" ; then 231 if ! mark_service_starting "${myservice}" ; then
274 ewarn "WARNING: \"${myservice}\" has already been started." 232 ewarn "WARNING: \"${myservice}\" is already starting."
275 return 0 233 return 0
276 fi 234 fi
235 begin_service "${myservice}"
236 local begun=$?
277 237
278 # Do not try to start if i have done so already on runlevel change
279 if is_runlevel_start && service_failed "${myservice}" ; then
280 return 1
281 fi
282
283 mark_service_starting "${myservice}"
284 service_message "Starting service ${myservice}" 238 service_message "Starting service ${myservice}"
285 239
286 # On rc change, start all services "before $myservice" first
287 if is_runlevel_start ; then
288 startupservices="$(ineed "${myservice}") \ 240 local startupservices="$(trace_dependencies $(ineed "${myservice}") \
289 $(valid_iuse "${myservice}") \
290 $(valid_iafter "${myservice}")"
291 else
292 startupservices="$(ineed "${myservice}") \
293 $(valid_iuse "${myservice}")" 241 $(valid_iuse ${myservice}))"
294 fi
295 242
296 # Start dependencies, if any 243 # Start dependencies, if any
297 for x in ${startupservices} ; do 244 for x in ${startupservices} ; do
298 if [[ ${x} == "net" ]] && [[ ${NETSERVICE} != "yes" ]] && ! is_net_up ; then 245 if [[ ${x} == "net" && ${NETSERVICE} != "yes" ]] && ! is_net_up ; then
299 local netservices="$(dolisting "/etc/runlevels/${BOOTLEVEL}/net.*") \ 246 local netservices="$(dolisting "/etc/runlevels/${BOOTLEVEL}/net.*") \
300 $(dolisting "/etc/runlevels/${mylevel}/net.*")" 247 $(dolisting "/etc/runlevels/${mylevel}/net.*")"
301 248
302 for y in ${netservices} ; do 249 for y in ${netservices} ; do
303 mynetservice="${y##*/}" 250 mynetservice="${y##*/}"
313 done 260 done
314 261
315 # wait for dependencies to finish 262 # wait for dependencies to finish
316 for x in ${startupservices} ; do 263 for x in ${startupservices} ; do
317 if [ "${x}" = "net" -a "${NETSERVICE}" != "yes" ] ; then 264 if [ "${x}" = "net" -a "${NETSERVICE}" != "yes" ] ; then
318 local netservices="$(dolisting "/etc/runlevels/${BOOTLEVEL}/net.*") \
319 $(dolisting "/etc/runlevels/${mylevel}/net.*")"
320
321 for y in ${netservices} ; do 265 for y in ${netservices} ; do
322 mynetservice="${y##*/}" 266 mynetservice="${y##*/}"
323 267
324 wait_service "${mynetservice}" 268 wait_service "${mynetservice}"
325 269
370 ) 314 )
371 retval=$? 315 retval=$?
372 316
373 # If a service has been marked inactive, exit now as something 317 # If a service has been marked inactive, exit now as something
374 # may attempt to start it again later 318 # may attempt to start it again later
375 service_inactive "${myservice}" && return 1 319 if service_inactive "${myservice}" ; then
320 [[ ${begun} == 0 ]] && end_service "${myservice}" 1
321 return 1
322 fi
376 fi 323 fi
377 324
378 if [[ ${retval} != 0 ]] ; then 325 if [[ ${retval} != 0 ]] ; then
379 is_runlevel_start && mark_service_failed "${myservice}" 326 is_runlevel_start && mark_service_failed "${myservice}"
380 327
381 # Remove link if service didn't start; but only if we're not booting 328 # Remove link if service didn't start; but only if we're not booting
382 # If we're booting, we need to continue and do our best to get the 329 # If we're booting, we need to continue and do our best to get the
383 # system up. 330 # system up.
384 if [[ ${SOFTLEVEL} != "${BOOTLEVEL}" ]]; then 331 if [[ ${SOFTLEVEL} != "${BOOTLEVEL}" ]]; then
332 if ${was_inactive} ; then
333 mark_service_inactive "${myservice}"
334 else
385 mark_service_stopped "${myservice}" 335 mark_service_stopped "${myservice}"
336 fi
386 fi 337 fi
387 338
388 service_message "eerror" "FAILED to start service ${myservice}!" 339 service_message "eerror" "FAILED to start service ${myservice}!"
389 else 340 else
390 mark_service_started "${myservice}" 341 mark_service_started "${myservice}"
391 342
392 service_message "Service ${myservice} started OK" 343 service_message "Service ${myservice} started OK"
393 fi 344 fi
394 345
346 [[ ${begun} == 0 ]] && end_service "${myservice}" "${retval}"
395 return "${retval}" 347 return "${retval}"
396} 348}
397 349
398svc_restart() { 350svc_restart() {
399 if ! service_stopped "${myservice}" ; then 351 if ! service_stopped "${myservice}" ; then
412 364
413 # If we are effectively root, check to see if required daemons are running 365 # If we are effectively root, check to see if required daemons are running
414 # and update our status accordingly 366 # and update our status accordingly
415 [[ ${EUID} == 0 ]] && update_service_status "${myservice}" 367 [[ ${EUID} == 0 ]] && update_service_status "${myservice}"
416 368
369 if service_stopping "${myservice}" ; then
370 efunc="eerror"
371 state="stopping"
417 if service_starting "${myservice}" ; then 372 elif service_starting "${myservice}" ; then
418 efunc="einfo" 373 efunc="einfo"
419 state="starting" 374 state="starting"
420 elif service_inactive "${myservice}" ; then 375 elif service_inactive "${myservice}" ; then
421 efunc="ewarn" 376 efunc="ewarn"
422 state="inactive" 377 state="inactive"
423 elif service_started "${myservice}" ; then 378 elif service_started "${myservice}" ; then
424 efunc="einfo" 379 efunc="einfo"
425 state="started" 380 state="started"
426 elif service_stopping "${myservice}" ; then
427 efunc="eerror"
428 state="stopping"
429 else 381 else
430 efunc="eerror" 382 efunc="eerror"
431 state="stopped" 383 state="stopped"
432 fi 384 fi
433 [[ ${RC_QUIET_STDOUT} != "yes" ]] \ 385 [[ ${RC_QUIET_STDOUT} != "yes" ]] \
500 esac 452 esac
501done 453done
502for arg in $* ; do 454for arg in $* ; do
503 case "${arg}" in 455 case "${arg}" in
504 stop) 456 stop)
457 if [[ -e "${svcdir}/restart/${myservice}" ]]; then
458 rm -f "${svcdir}/restart/${myservice}"
459 fi
460
461 # Stoped from the background - treat this as a restart so that
462 # stopped services come back up again when started.
463 if [[ ${IN_BACKGROUND} == "true" ]]; then
464 rm -rf "${svcdir}/snapshot/$$"
465 mkdir -p "${svcdir}/snapshot/$$"
466 cp -a "${svcdir}"/started/* "${svcdir}/snapshot/$$/"
467 fi
468
505 svc_stop 469 svc_stop
470
471 if [[ ${IN_BACKGROUND} == "true" ]]; then
472 res=
473 for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
474 if service_stopped "${x##*/}" ; then
475 res="${res}${x##*/} "
476 fi
477 done
478 echo "${res}" > "${svcdir}/restart/${myservice}"
479 fi
506 ;; 480 ;;
507 start) 481 start)
508 svc_start 482 svc_start
483 retval=$?
484 if [[ -e "${svcdir}/restart/${myservice}" ]]; then
485 for x in $(trace_dependencies $(< "${svcdir}/restart/${myservice}")) ; do
486 service_stopped "${x}" && start_service "${x}"
487 done
488 rm -f "${svcdir}/restart/${myservice}"
489 fi
490 exit ${retval}
509 ;; 491 ;;
510 needsme|ineed|usesme|iuse|broken) 492 needsme|ineed|usesme|iuse|broken)
511 trace_dependencies "-${arg}" 493 trace_dependencies "-${arg}"
512 ;; 494 ;;
513 status) 495 status)
514 svc_status 496 svc_status
515 ;; 497 ;;
516 zap) 498 zap)
499 if [[ -e "${svcdir}/restart/${myservice}" ]]; then
500 rm -f "${svcdir}/restart/${myservice}"
501 fi
517 if ! service_stopped "${myservice}" ; then 502 if ! service_stopped "${myservice}" ; then
518 einfo "Manually resetting ${myservice} to stopped state." 503 einfo "Manually resetting ${myservice} to stopped state."
519 mark_service_stopped "${myservice}" 504 mark_service_stopped "${myservice}"
505 end_service "${myservice}"
520 fi 506 fi
521 ;; 507 ;;
522 restart) 508 restart)
523 svcrestart="yes" 509 svcrestart="yes"
524 510
537 if [[ -z $(egrep 'svc_stop' "/etc/init.d/${myservice}") || \ 523 if [[ -z $(egrep 'svc_stop' "/etc/init.d/${myservice}") || \
538 -z $(egrep 'svc_start' "/etc/init.d/${myservice}") ]] ; then 524 -z $(egrep 'svc_start' "/etc/init.d/${myservice}") ]] ; then
539 echo 525 echo
540 ewarn "Please use 'svc_stop; svc_start' and not 'stop; start' to" 526 ewarn "Please use 'svc_stop; svc_start' and not 'stop; start' to"
541 ewarn "restart the service in its custom 'restart()' function." 527 ewarn "restart the service in its custom 'restart()' function."
542 ewarn "Run ${myservice} without arguments for more info." 528/var/lib/init.d/exclusive/net.lan ewarn "Run ${myservice} without arguments for more info."
543 echo 529 echo
544 svc_restart 530 svc_restart
545 else 531 else
546 restart 532 restart
547 fi 533 fi
555 $(dolisting "${svcdir}/snapshot/$$/") ) ; do 541 $(dolisting "${svcdir}/snapshot/$$/") ) ; do
556 if service_stopped "${x##*/}" ; then 542 if service_stopped "${x##*/}" ; then
557 start_service "${x##*/}" 543 start_service "${x##*/}"
558 fi 544 fi
559 done 545 done
546 elif service_inactive "${myservice}" ; then
547 res=
548 for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
549 if service_stopped "${x##*/}" ; then
550 res="${res}${x##*/} "
551 fi
552 done
553 echo "${res}" > "${svcdir}/restart/${myservice}"
560 fi 554 fi
561 555
562 # Wait for any services that may still be running ... 556 # Wait for any services that may still be running ...
563 [[ ${RC_PARALLEL_STARTUP} == "yes" ]] && wait 557 [[ ${RC_PARALLEL_STARTUP} == "yes" ]] && wait
564 558
580 svc_homegrown ${arg} 574 svc_homegrown ${arg}
581 ;; 575 ;;
582 esac 576 esac
583done 577done
584 578
585
586# vim:ts=4 579# vim:ts=4

Legend:
Removed from v.200  
changed lines
  Added in v.201

  ViewVC Help
Powered by ViewVC 1.1.20