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

Contents of /branches/baselayout-1_12/sbin/runscript.sh

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20