/[baselayout]/trunk/sbin/runscript.sh
Gentoo

Contents of /trunk/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, 6 months ago) by uberlord
File MIME type: text/x-sh
File size: 21557 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 2449 # 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 uberlord 2221 if [[ $1 == "/"* ]] ; then
8     myscript="$1"
9     else
10     myscript="$(pwd)/$1"
11     fi
12 uberlord 2244 cd /
13 uberlord 2221
14 uberlord 2231 # Common functions
15 uberlord 2328 [[ ${RC_GOT_FUNCTIONS} != "yes" ]] && . /sbin/functions.sh
16 uberlord 2231
17 vapier 1136 # User must be root to run most script stuff (except status)
18 uberlord 1935 if [[ ${EUID} != "0" ]] && ! [[ $2 == "status" && $# -eq 2 ]] ; then
19 uberlord 2246 eerror "$0:" $"must be root to run init scripts"
20 agriffis 492 exit 1
21     fi
22    
23 uberlord 1800 if [[ -L $1 && ! -L "/etc/init.d/${1##*/}" ]] ; then
24 vapier 2097 SVCNAME=$(readlink "$1")
25 azarah 161 else
26 uberlord 1935 SVCNAME="$1"
27 azarah 161 fi
28 uberlord 1935 declare -r SVCNAME="${SVCNAME##*/}"
29     export SVCNAME
30     # Support deprecated myservice variable
31     myservice="${SVCNAME}"
32 azarah 161
33 uberlord 2025 svc_trap() {
34 uberlord 2465 trap 'eerror $"ERROR:" " ${SVCNAME}" $"caught an interrupt"; eflush; rm -rf "${svcdir}/snapshot/$$"; exit 1' \
35 uberlord 2025 INT QUIT TSTP
36     }
37    
38     # Setup a default trap
39     svc_trap
40    
41 uberlord 2221 # Now check script for syntax errors
42     rcscript_errors=$(bash -n "${myscript}" 2>&1) || {
43     [[ -n ${rcscript_errors} ]] && echo "${rcscript_errors}" >&2
44 uberlord 2246 eerror $"ERROR:" " $1" $"has syntax errors in it; aborting ..."
45 uberlord 2221 exit 1
46     }
47    
48 uberlord 1960 # 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 uberlord 1835 if [[ -e /dev/.rcsysinit ]] ; then
51 uberlord 2246 eerror $"ERROR: cannot run" "${SVCNAME}" $"until sysinit completes"
52 uberlord 2024 [[ ${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 uberlord 2246 eerror "${SVCNAME}" $"will be started in the" "${BOOTLEVEL}" $"runlevel"
59 uberlord 1978 if [[ ! -L /dev/.rcboot/"${SVCNAME}" ]] ; then
60     [[ ! -d /dev/.rcboot ]] && mkdir /dev/.rcboot
61     ln -snf "$1" /dev/.rcboot/"${SVCNAME}"
62     fi
63 uberlord 1835 exit 1
64     fi
65    
66 uberlord 2024 # Only hotplug if we're allowed to
67     if [[ ${IN_HOTPLUG} == "1" ]] ; then
68     if [[ ${RC_HOTPLUG:-yes} != "yes" ]] ; then
69 uberlord 2246 eerror "${SVCNAME}" $"is not allowed to be hotplugged"
70 uberlord 2024 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 uberlord 2246 eerror "${SVCNAME}" $"is not allowed to be hotplugged"
78 uberlord 2024 exit 1
79     fi
80     done
81     set +f
82     fi
83    
84 uberlord 1829 # State variables
85     svcpause="no"
86    
87 uberlord 1800 # Functions to handle dependencies and services
88 uberlord 2328 [[ ${RC_GOT_SERVICES} != "yes" ]] && . "${svclib}/sh/rc-services.sh"
89 uberlord 1800 # Functions to control daemons
90 uberlord 2328 [[ ${RC_GOT_DAEMON} != "yes" ]] && . "${svclib}/sh/rc-daemon.sh"
91 uberlord 1800
92 azarah 1644 # Check if the textdomain is non-default
93 uberlord 1795 search_lang="${LC_ALL:-${LC_MESSAGES:-${LANG}}}"
94 vapier 1646 [[ -f ${TEXTDOMAINDIR}/${search_lang%.*}/LC_MESSAGES/${myservice}.mo ]] \
95 uberlord 1795 && TEXTDOMAIN="${myservice}"
96 azarah 1644
97 azarah 161 # Source configuration files.
98 uberlord 2323 # (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 uberlord 1935 # (2) Source /etc/conf.d/${SVCNAME} to get initscript-specific
101 azarah 161 # configuration (if it exists).
102 vapier 695 # (3) Source /etc/rc.conf to pick up potentially overriding
103 azarah 161 # configuration, if the system administrator chose to put it
104     # there (if it exists).
105 uberlord 2323 conf="${SVCNAME%%.*}"
106     if [[ -n ${conf} && ${conf} != "${SVCNAME}" ]] ; then
107     conf=$(add_suffix "/etc/conf.d/${conf}")
108 uberlord 2328 [[ -e ${conf} ]] && . "${conf}"
109 uberlord 1795 fi
110 vapier 2097 conf=$(add_suffix "/etc/conf.d/${SVCNAME}")
111 uberlord 2328 [[ -e ${conf} ]] && . "${conf}"
112 vapier 2097 conf=$(add_suffix /etc/rc.conf)
113 uberlord 2328 [[ -e ${conf} ]] && . "${conf}"
114 azarah 161
115 uberlord 1835 mylevel="${SOFTLEVEL}"
116     [[ ${SOFTLEVEL} == "${BOOTLEVEL}" \
117     || ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] \
118     && mylevel="${DEFAULTLEVEL}"
119    
120 uberlord 1800 # Call svc_quit if we abort AND we have obtained a lock
121 uberlord 1935 service_started "${SVCNAME}"
122 uberlord 1821 svcstarted="$?"
123 uberlord 1935 service_inactive "${SVCNAME}"
124 uberlord 1821 svcinactive="$?"
125 uberlord 2465
126 uberlord 1800 svc_quit() {
127 uberlord 2246 eerror $"ERROR:" " ${SVCNAME}" $"caught an interrupt"
128 uberlord 2222 eflush
129 uberlord 2465 rm -rf "${svcdir}/snapshot/$$" "${svcdir}/exclusive/${SVCNAME}.$$"
130 uberlord 1954 if service_inactive "${SVCNAME}" || [[ ${svcinactive} == "0" ]] ; then
131 uberlord 1935 mark_service_inactive "${SVCNAME}"
132 uberlord 1954 elif [[ ${svcstarted} == "0" ]] ; then
133 uberlord 1935 mark_service_started "${SVCNAME}"
134 uberlord 1800 else
135 uberlord 1935 mark_service_stopped "${SVCNAME}"
136 uberlord 1800 fi
137 uberlord 2465 end_service "${SVCNAME}"
138 uberlord 1800 exit 1
139     }
140    
141 azarah 161 usage() {
142     local IFS="|"
143 uberlord 2252 myline="${SVCNAME} { $* "
144     unset IFS
145 azarah 161 echo
146 uberlord 2252 eerror $"Usage:" "${myline}}"
147 uberlord 2246 eerror " ${SVCNAME}" $"without arguments for full help"
148 azarah 161 }
149    
150     stop() {
151 azarah 295 # Return success so the symlink gets removed
152 azarah 161 return 0
153     }
154    
155     start() {
156 uberlord 2246 eerror $"ERROR:" " ${SVCNAME}" $"does not have a start function."
157 azarah 295 # Return failure so the symlink doesn't get created
158 azarah 161 return 1
159     }
160    
161     restart() {
162 uberlord 2466 if ! service_stopped "${SVCNAME}" ; then
163     svc_stop || return "$?"
164     fi
165     svc_start
166 azarah 161 }
167 azarah 242
168     status() {
169 azarah 295 # Dummy function
170 azarah 242 return 0
171     }
172 uberlord 1800
173 uberlord 1825 svc_schedule_start() {
174 uberlord 2291 local service="$1" start="$2" x=
175    
176 uberlord 1825 [[ ! -d "${svcdir}/scheduled/${service}" ]] \
177     && mkdir -p "${svcdir}/scheduled/${service}"
178 uberlord 2142 ln -snf "/etc/init.d/${service}" \
179     "${svcdir}/scheduled/${service}/${start}"
180 uberlord 2291
181 uberlord 2323 for x in $(rc-depend --notrace -iprovide "${service}" ) ; do
182 uberlord 2291 [[ ! -d "${svcdir}/scheduled/${x}" ]] \
183     && mkdir -p "${svcdir}/scheduled/${x}"
184 uberlord 2368 ln -snf "/etc/init.d/${service}" "${svcdir}/scheduled/${x}/${start}"
185 uberlord 2291 done
186 uberlord 2323
187     mark_service_stopped "${start}"
188 uberlord 2368 end_service "${start}"
189 uberlord 1819 }
190    
191 uberlord 1825 svc_start_scheduled() {
192 uberlord 2284 # 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 uberlord 2286 export IN_BACKGROUND=true
198 uberlord 2285 return
199 uberlord 2284 fi
200    
201 uberlord 2323 local x= y= services= provides=$(rc-depend --notrace -iprovide "${SVCNAME}")
202 uberlord 2291 for x in "${SVCNAME}" ${provides} ; do
203     for y in $(dolisting "${svcdir}/scheduled/${x}/") ; do
204     services="${services} ${y##*/}"
205     done
206 uberlord 1822 done
207 uberlord 2323
208     if [[ -n ${services} ]] ; then
209 uberlord 2334 for x in $(rc-depend -ineed -iuse ${services}) ; do
210 uberlord 2323 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 uberlord 2291 done
216 uberlord 2323 fi
217 uberlord 1822
218 uberlord 2291 for x in "${SVCNAME}" ${provides} ; do
219     rmdir "${svcdir}/scheduled/${x}" 2>/dev/null
220     done
221 uberlord 1822 }
222    
223 uberlord 2465 # 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 azarah 161 svc_stop() {
235 uberlord 2323 local x= retval=0
236 uberlord 1799 local -a servicelist=()
237 uberlord 1829
238     # Do not try to stop if it had already failed to do so
239 uberlord 1935 if is_runlevel_stop && service_failed "${SVCNAME}" ; then
240 uberlord 1334 return 1
241 uberlord 1935 elif service_stopped "${SVCNAME}" ; then
242 uberlord 2246 ewarn $"WARNING:" " ${SVCNAME}" $"has not yet been started."
243 uberlord 1799 return 0
244     fi
245 uberlord 1935 if ! mark_service_stopping "${SVCNAME}" ; then
246 uberlord 2246 eerror $"ERROR:" " ${SVCNAME}" $"is already stopping."
247 uberlord 1829 return 1
248 uberlord 1799 fi
249 uberlord 1829
250 uberlord 1800 # Ensure that we clean up if we abort for any reason
251     trap "svc_quit" INT QUIT TSTP
252 uberlord 1829
253 uberlord 1935 mark_service_starting "${SVCNAME}"
254 uberlord 2465 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 uberlord 2217 # 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 uberlord 1179
263 uberlord 2246 veinfo $"Service" "${SVCNAME}" $"stopping"
264 uberlord 2217
265 uberlord 1935 if in_runlevel "${SVCNAME}" "${BOOTLEVEL}" && \
266 vapier 879 [[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" && \
267 azarah 1519 ${SOFTLEVEL} != "single" ]] ; then
268 uberlord 2246 ewarn $"WARNING: you are stopping a boot service."
269 azarah 161 fi
270 uberlord 2091
271 uberlord 1339 # 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 uberlord 2323 if [[ ${svcpause} != "yes" && ${RC_NO_DEPS} != "yes" ]] \
276     && ! service_wasinactive "${SVCNAME}" ; then
277     for x in $(reverse_list $(rc-depend -needsme "${SVCNAME}")) ; do
278 uberlord 2141 if service_started "${x}" || service_inactive "${x}" ; then
279     stop_service "${x}"
280     fi
281 uberlord 2091 service_list=( "${service_list[@]}" "${x}" )
282 uberlord 1799 done
283 uberlord 2323 fi
284 azarah 359
285 uberlord 1799 for x in "${service_list[@]}" ; do
286 uberlord 2323 local retry=3
287     while [[ ${retry} -gt 0 ]] ; do
288     service_stopped "${x}" && break
289     wait_service "${x}"
290     ((retry--))
291     done
292 uberlord 1799 if ! service_stopped "${x}" ; then
293 uberlord 2246 eerror $"ERROR:" $"cannot stop" "${SVCNAME}" $"as" "${x}" $"is still up."
294 uberlord 1799 retval=1
295     break
296 uberlord 1418 fi
297     done
298 vapier 879
299 uberlord 2465 # Work with uses, before and after deps too, but as they are not needed
300 uberlord 2334 # we cannot explicitly stop them.
301 uberlord 2465 # We use -needsme with -usesme so we get the full dep list.
302 uberlord 2334 # We use --notrace with -ibefore to stop circular deps.
303 uberlord 2465 for x in $(rc-depend -needsme -usesme "${SVCNAME}") \
304 uberlord 2334 $(rc-depend --notrace -ibefore "${SVCNAME}"); do
305     service_stopping "${x}" && wait_service "${x}"
306     done
307    
308 uberlord 1339 IN_BACKGROUND="${ib_save}"
309    
310 uberlord 2170 if [[ ${retval} == "0" ]] ; then
311 uberlord 1805 # Now that deps are stopped, stop our service
312 uberlord 2217 veindent
313 uberlord 2231 (
314 uberlord 2217 [[ ${RC_QUIET} == "yes" ]] && RC_QUIET_STDOUT="yes"
315 uberlord 1805 exit() {
316 uberlord 2246 eerror $"DO NOT USE EXIT IN INIT.D SCRIPTS"
317     eerror $"This IS a bug, please fix your broken init.d"
318 uberlord 1805 unset -f exit
319 uberlord 1821 exit "$@"
320 uberlord 1805 }
321     stop
322     )
323 uberlord 1821 retval="$?"
324 uberlord 1185
325 uberlord 2217 # Don't trust init scripts to reset indentation properly
326     # Needed for ebuffer
327     eoutdent 99999
328    
329 uberlord 1185 # If a service has been marked inactive, exit now as something
330     # may attempt to start it again later
331 uberlord 2465 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 uberlord 1799 fi
338 azarah 161 fi
339 vapier 879
340 uberlord 1799 if [[ ${retval} != 0 ]] ; then
341 azarah 295 # Did we fail to stop? create symlink to stop multible attempts at
342     # runlevel change. Note this is only used at runlevel change ...
343 uberlord 1935 is_runlevel_stop && mark_service_failed "${SVCNAME}"
344 azarah 359
345 azarah 295 # If we are halting the system, do it as cleanly as possible
346 uberlord 1834 if [[ ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] ; then
347 uberlord 1935 mark_service_stopped "${SVCNAME}"
348 uberlord 1834 else
349 uberlord 1954 if [[ ${svcinactive} == "0" ]] ; then
350 uberlord 1935 mark_service_inactive "${SVCNAME}"
351 uberlord 1267 else
352 uberlord 1935 mark_service_started "${SVCNAME}"
353 uberlord 1267 fi
354 azarah 161 fi
355 uberlord 1380
356 uberlord 2246 eerror $"ERROR:" " ${SVCNAME}" $"failed to stop"
357 uberlord 760 else
358 uberlord 1800 svcstarted=1
359 uberlord 1935 if service_inactive "${SVCNAME}" ; then
360 uberlord 1805 svcinactive=0
361 uberlord 1086 else
362 uberlord 1935 mark_service_stopped "${SVCNAME}"
363 uberlord 1086 fi
364 uberlord 2217
365 uberlord 2246 veinfo $"Service" "${SVCNAME}" $"stopped"
366 uberlord 1179 fi
367    
368 uberlord 2217 # Flush the ebuffer
369     if [[ ${RC_PARALLEL_STARTUP} == "yes" && ${RC_QUIET} != "yes" ]] ; then
370     eflush
371     ebuffer ""
372     fi
373    
374 uberlord 2465 rm -f "${svcdir}/exclusive/${SVCNAME}.$$"
375     end_service "${SVCNAME}"
376 uberlord 2312
377 uberlord 1800 # Reset the trap
378     svc_trap
379    
380 azarah 295 return "${retval}"
381 azarah 161 }
382    
383     svc_start() {
384 uberlord 2170 local x= y= retval=0 startinactive=
385 azarah 295
386 uberlord 1799 # Do not try to start if i have done so already on runlevel change
387 uberlord 1935 if is_runlevel_start && service_failed "${SVCNAME}" ; then
388 uberlord 1799 return 1
389 uberlord 1935 elif service_started "${SVCNAME}" ; then
390 uberlord 2246 ewarn $"WARNING:" " ${SVCNAME}" $"has already been started."
391 uberlord 1380 return 0
392 uberlord 1935 elif service_inactive "${SVCNAME}" ; then
393 uberlord 2231 if [[ ${IN_BACKGROUND} != "true" \
394     && ${IN_BACKGROUND} != "1" ]] ; then
395 uberlord 2246 ewarn $"WARNING:" " ${SVCNAME}" $"has already been started."
396 uberlord 1313 return 0
397     fi
398 uberlord 2374 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 uberlord 759 fi
405 azarah 161
406 uberlord 1935 if ! mark_service_starting "${SVCNAME}" ; then
407     if service_stopping "${SVCNAME}" ; then
408 uberlord 2246 eerror $"ERROR:" " ${SVCNAME}" $"is already stopping."
409 uberlord 1829 else
410 uberlord 2246 eerror $"ERROR: "" ${SVCNAME}" $"is already starting."
411 uberlord 1829 fi
412     return 1
413 uberlord 759 fi
414 uberlord 1800
415     # Ensure that we clean up if we abort for any reason
416     trap "svc_quit" INT QUIT TSTP
417 uberlord 2465 begin_service "${SVCNAME}"
418 uberlord 1829
419 uberlord 2465 # This is our mtime file to work out if we're still in control or not
420     touch "${svcdir}/exclusive/${SVCNAME}.$$"
421    
422 uberlord 2217 # 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 uberlord 2218
426 uberlord 2246 veinfo $"Service" "${SVCNAME}" $"starting"
427 uberlord 1805
428 uberlord 2323 if [[ -n $(rc-depend --notrace -broken "${SVCNAME}") ]] ; then
429 uberlord 2246 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 uberlord 1954 retval=1
433     fi
434    
435 uberlord 1805 # 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 uberlord 1179
439 uberlord 1954 if [[ ${retval} == "0" && ${RC_NO_DEPS} != "yes" ]] ; then
440     # Start dependencies, if any.
441 uberlord 2334 local startsvc=$(rc-depend -ineed -iuse "${SVCNAME}")
442 uberlord 1954 if ! is_runlevel_start ; then
443 uberlord 2331 for x in ${startsvc} ; do
444 uberlord 2291 service_stopped "${x}" && start_service "${x}"
445 uberlord 1954 done
446     fi
447 uberlord 1800
448 uberlord 1954 # Wait for dependencies to finish.
449 uberlord 2323 local ineed=$(rc-depend --notrace -ineed "${SVCNAME}")
450 uberlord 2331 for x in ${startsvc} $(rc-depend -iafter "${SVCNAME}") ; do
451 uberlord 2323 local timeout=3
452     while [[ ${timeout} -gt 0 ]] ; do
453 uberlord 2331 service_started "${x}" && continue 2
454 uberlord 2323 wait_service "${x}"
455     service_started "${x}" && continue 2
456 uberlord 2374 if service_inactive "${x}" || service_scheduled "${x}" ; then
457 uberlord 2336 if [[ " ${startsvc} " == *" ${x} "* ||
458 uberlord 2323 " ${ineed} " == *" $(rc-depend --notrace -iprovide "${x}") " ]] ; then
459 uberlord 2142 svc_schedule_start "${x}" "${SVCNAME}"
460 uberlord 2323 [[ -n ${startinactive} ]] && startinactive="${startinactive}, "
461 uberlord 2142 startinactive="${startinactive}${x}"
462 uberlord 1954 fi
463 uberlord 2323
464     continue 2
465 uberlord 759 fi
466 uberlord 2374 service_stopped "${x}" && break
467    
468 uberlord 2323 ((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 uberlord 1954 done
477    
478 uberlord 2170 if [[ -n ${startinactive} && ${retval} == "0" ]] ; then
479 uberlord 1954 # Change the last , to or for correct grammar.
480     x="${startinactive##*, }"
481     startinactive="${startinactive/%, ${x}/ or ${x}}"
482 uberlord 2246 ewarn "WARNING:" " ${SVCNAME}" $"is scheduled to start when" "${startinactive}" $"has started."
483 uberlord 1954 retval=1
484 azarah 161 fi
485 uberlord 1954 fi
486    
487     if [[ ${retval} == "0" ]] ; then
488 uberlord 1805 IN_BACKGROUND="${ib_save}"
489 uberlord 2217 veindent
490 uberlord 1380 (
491 uberlord 2217 [[ ${RC_QUIET} == "yes" ]] && RC_QUIET_STDOUT="yes"
492 uberlord 1380 exit() {
493 uberlord 2246 eerror $"DO NOT USE EXIT IN INIT.D SCRIPTS"
494     eerror $"This IS a bug, please fix your broken init.d"
495 uberlord 1380 unset -f exit
496 uberlord 1821 exit "$@"
497 uberlord 1380 }
498 uberlord 1840
499     # Apply any ulimits if defined
500     [[ -n ${RC_ULIMIT} ]] && ulimit ${RC_ULIMIT}
501 uberlord 2231
502 uberlord 1380 start
503     )
504 uberlord 1821 retval="$?"
505 uberlord 2142
506 uberlord 2217 # Don't trust init scripts to reset indentation properly
507     # Needed for ebuffer
508     eoutdent 99999
509    
510 uberlord 1380 # If a service has been marked inactive, exit now as something
511     # may attempt to start it again later
512 uberlord 2465 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 uberlord 2217 fi
523 uberlord 1799 fi
524 uberlord 759 fi
525 vapier 879
526 uberlord 1954 if [[ ${retval} != "0" ]] ; then
527     if [[ ${svcinactive} == "0" ]] ; then
528 uberlord 1935 mark_service_inactive "${SVCNAME}"
529 uberlord 2323 elif [[ -z ${startinactive} ]] ; then
530 uberlord 1935 mark_service_stopped "${SVCNAME}"
531     is_runlevel_start && mark_service_failed "${SVCNAME}"
532 uberlord 2246 eerror $"ERROR:" " ${SVCNAME}" $"failed to start"
533 uberlord 1819 fi
534 uberlord 1380 else
535 uberlord 1800 svcstarted=0
536 uberlord 1935 mark_service_started "${SVCNAME}"
537 uberlord 2246 veinfo $"Service" "${SVCNAME}" $"started"
538 uberlord 1179 fi
539    
540 uberlord 2217 # Flush the ebuffer
541     if [[ ${RC_PARALLEL_STARTUP} == "yes" && ${RC_QUIET} != "yes" ]] ; then
542     eflush
543     ebuffer ""
544     fi
545    
546 uberlord 2465 rm -f "${svcdir}/exclusive/${SVCNAME}.$$"
547     end_service "${SVCNAME}"
548 uberlord 2312
549 uberlord 1800 # Reset the trap
550     svc_trap
551    
552 uberlord 759 return "${retval}"
553 azarah 161 }
554    
555     svc_restart() {
556 uberlord 2466 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 azarah 295 fi
563 uberlord 2466
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 azarah 161 }
626    
627 azarah 242 svc_status() {
628 agriffis 537 # The basic idea here is to have some sort of consistent
629 azarah 242 # 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 vapier 1080 local efunc="" state=""
634 azarah 242
635 uberlord 1441 # If we are effectively root, check to see if required daemons are running
636     # and update our status accordingly
637 uberlord 1935 [[ ${EUID} == 0 ]] && update_service_status "${SVCNAME}"
638 uberlord 1441
639 uberlord 1935 if service_stopping "${SVCNAME}" ; then
640 uberlord 1799 efunc="eerror"
641 uberlord 2246 state=$"stopping"
642 uberlord 1935 elif service_starting "${SVCNAME}" ; then
643 vapier 1080 efunc="einfo"
644 uberlord 2246 state=$"starting"
645 uberlord 1935 elif service_inactive "${SVCNAME}" ; then
646 vapier 1080 efunc="ewarn"
647 uberlord 2246 state=$"inactive"
648 uberlord 1935 elif service_started "${SVCNAME}" ; then
649 vapier 1080 efunc="einfo"
650 uberlord 2246 state=$"started"
651 azarah 242 else
652 vapier 1080 efunc="eerror"
653 uberlord 2246 state=$"stopped"
654 azarah 242 fi
655 vapier 1080 [[ ${RC_QUIET_STDOUT} != "yes" ]] \
656 uberlord 2246 && ${efunc} $"status:" "${state}"
657 azarah 242
658     status
659 uberlord 1808 # Return 0 if started, otherwise 1
660     [[ ${state} == "started" ]]
661 azarah 242 }
662    
663 azarah 215 # set *after* wrap_rcscript, else we get duplicates.
664     opts="start stop restart"
665    
666 uberlord 2328 . "${myscript}"
667 azarah 161
668 azarah 215 # make sure whe have valid $opts
669 vapier 879 if [[ -z ${opts} ]] ; then
670 azarah 161 opts="start stop restart"
671     fi
672    
673     svc_homegrown() {
674 uberlord 1800 local x arg="$1"
675 vapier 1585 shift
676 vapier 879
677 azarah 161 # Walk through the list of available options, looking for the
678     # requested one.
679 vapier 879 for x in ${opts} ; do
680 azarah 1519 if [[ ${x} == "${arg}" ]] ; then
681 vapier 879 if typeset -F "${x}" &>/dev/null ; then
682 azarah 161 # Run the homegrown function
683 azarah 427 "${x}"
684 vapier 879
685 azarah 161 return $?
686     fi
687     fi
688     done
689 vapier 1585 x=""
690 vapier 879
691 azarah 359 # If we're here, then the function wasn't in $opts.
692 vapier 1585 [[ -n $* ]] && x="/ $* "
693 uberlord 2246 eerror $"ERROR:" $"wrong args" "( "${arg}" ${x})"
694 azarah 295 # Do not quote this either ...
695 azarah 161 usage ${opts}
696     exit 1
697     }
698    
699     shift
700 vapier 879 if [[ $# -lt 1 ]] ; then
701 uberlord 2246 eerror $"ERROR:" $"not enough args."
702 azarah 161 usage ${opts}
703     exit 1
704     fi
705 vapier 2448 for arg in "$@" ; do
706 azarah 295 case "${arg}" in
707 azarah 161 --quiet)
708 uberlord 1954 RC_QUIET="yes"
709 azarah 345 RC_QUIET_STDOUT="yes"
710 azarah 161 ;;
711 azarah 345 # We check this in functions.sh ...
712     # --nocolor)
713     # RC_NOCOLOR="yes"
714     # ;;
715 uberlord 1954 --nodeps)
716     RC_NO_DEPS="yes"
717     ;;
718 agriffis 625 --verbose)
719     RC_VERBOSE="yes"
720     ;;
721 azarah 161 esac
722     done
723 uberlord 1829
724     retval=0
725 vapier 2448 for arg in "$@" ; do
726 azarah 295 case "${arg}" in
727 azarah 161 stop)
728 uberlord 1935 if [[ -e "${svcdir}/scheduled/${SVCNAME}" ]] ; then
729     rm -Rf "${svcdir}/scheduled/${SVCNAME}"
730 uberlord 1799 fi
731    
732     # Stoped from the background - treat this as a restart so that
733     # stopped services come back up again when started.
734 uberlord 2252 if [[ ${IN_BACKGROUND} == "true" || ${IN_BACKGROUND} == "1" ]] ; then
735 uberlord 1799 rm -rf "${svcdir}/snapshot/$$"
736     mkdir -p "${svcdir}/snapshot/$$"
737 uberlord 2290 cp -pP "${svcdir}"/started/* "${svcdir}"/inactive/* \
738 uberlord 2291 "${svcdir}/snapshot/$$/" 2>/dev/null
739 uberlord 1935 rm -f "${svcdir}/snapshot/$$/${SVCNAME}"
740 uberlord 1799 fi
741    
742 azarah 161 svc_stop
743 uberlord 1814 retval="$?"
744 uberlord 1819
745 uberlord 2252 if [[ ${IN_BACKGROUND} == "true" || ${IN_BACKGROUND} == "1" ]] ; then
746 uberlord 1799 for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
747 uberlord 2096 if [[ -x ${x} ]] && service_stopped "${x##*/}" ; then
748 uberlord 1935 svc_schedule_start "${SVCNAME}" "${x##*/}"
749 uberlord 1821 fi
750 uberlord 1799 done
751 uberlord 2164 rm -rf "${svcdir}/snapshot/$$"
752 uberlord 2382 elif service_stopped "${SVCNAME}" ; then
753 uberlord 1935 rm -f "${svcdir}"/scheduled/*/"${SVCNAME}"
754 uberlord 2382 if [[ ${SOFTLEVEL} != "single" ]] ; then
755     rm -f "${svcdir}/coldplugged/${SVCNAME}"
756     fi
757 uberlord 1799 fi
758 uberlord 1829
759 azarah 161 ;;
760     start)
761     svc_start
762 uberlord 1829 retval="$?"
763 uberlord 1935 service_started "${SVCNAME}" && svc_start_scheduled
764 azarah 161 ;;
765 uberlord 2291 needsme|ineed|usesme|iuse|broken|iafter|iprovide)
766 uberlord 2323 rc-depend "-${arg}" "${SVCNAME}"
767 azarah 161 ;;
768 azarah 167 status)
769 azarah 242 svc_status
770 uberlord 1829 retval="$?"
771 azarah 167 ;;
772 azarah 161 zap)
773 uberlord 2246 einfo $"Manually resetting" "${SVCNAME}" $"to stopped state."
774 uberlord 1935 mark_service_stopped "${SVCNAME}"
775 azarah 161 ;;
776     restart)
777 uberlord 2466 svc_restart
778     retval="$?"
779     ;;
780     condrestart|conditionalrestart)
781     if service_started "${SVCNAME}" ; then
782     svc_restart
783 uberlord 2374 fi
784 uberlord 1829 retval="$?"
785 azarah 161 ;;
786     pause)
787     svcpause="yes"
788     svc_stop
789 uberlord 1829 retval="$?"
790 azarah 161 svcpause="no"
791     ;;
792 vapier 2448 --quiet|--nocolor|--nocolour|--nodeps|--verbose|--debug)
793 azarah 161 ;;
794 vapier 2448 -V|--version)
795     exec cat "${ROOT}"/etc/gentoo-release
796     exit 1
797     ;;
798     help|-h|--help)
799 vapier 1473 exec "${svclib}"/sh/rc-help.sh "${myscript}" help
800     ;;
801 azarah 161 *)
802     # Allow for homegrown functions
803     svc_homegrown ${arg}
804 uberlord 1829 retval="$?"
805 azarah 161 ;;
806     esac
807     done
808    
809 uberlord 1829 exit "${retval}"
810    
811 uberlord 2246 # vim: set 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