/[baselayout]/trunk/init.d/runscript.sh
Gentoo

Contents of /trunk/init.d/runscript.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 127 - (show annotations) (download) (as text)
Sun Feb 17 02:24:47 2002 UTC (18 years, 8 months ago) by azarah
File MIME type: text/x-sh
File size: 11900 byte(s)
misc fixes

1 #!/bin/bash
2 # Copyright 1999-2002 Gentoo Technologies, Inc.
3 # Distributed under the terms of the GNU General Public License, v2 or later
4 # $Header$
5
6
7 source /etc/init.d/functions.sh
8
9 svcpause="no"
10
11 myscript=${1}
12 if [ -L $1 ]
13 then
14 myservice=`readlink ${1}`
15 else
16 myservice=${1}
17 fi
18
19 myservice=${myservice##*/}
20 mylevel=`cat ${svcdir}/softlevel`
21
22
23 #set $IFACE to the name of the network interface if it is a 'net.*' script
24 IFACE=""
25 NETSERVICE=""
26 if [ "${myservice%%.*}" = "net" ] && [ "${myservice##*.}" != "$myservice" ]
27 then
28 IFACE="${myservice##*.}"
29 NETSERVICE="yes"
30 fi
31
32 # Source configuration files.
33 # (1) Source /etc/conf.d/basic to get common configuration.
34 # (2) Source /etc/conf.d/${myservice} to get initscript-specific
35 # configuration (if it exists).
36 # (3) Source /etc/conf.d/net if it is a net.* service
37 # (4) Source /etc/rc.conf to pick up potentially overriding
38 # configuration, if the system administrator chose to put it
39 # there (if it exists).
40 [ -e /etc/conf.d/basic ] && source /etc/conf.d/basic
41 [ -e /etc/conf.d/${myservice} ] && source /etc/conf.d/${myservice}
42 [ -e /etc/conf.d/net ] && [ "${myservice%%.*}" = "net" ] && \
43 [ "${myservice##*.}" != "$myservice" ] && source /etc/conf.d/net
44 [ -e /etc/rc.conf ] && source /etc/rc.conf
45
46 usage() {
47 local IFS="|"
48 myline="Usage: ${myservice} {$*"
49 echo
50 eerror "${myline}}"
51 eerror " ${myservice} without arguments for full help"
52 }
53
54 stop() {
55 #return success so the symlink gets removed
56 return 0
57 }
58
59 start() {
60 einfo "${myservice} does not have a start function."
61 #return failure so the symlink doesn't get created
62 return 1
63 }
64
65 restart() {
66 svc_restart || return $?
67 }
68
69 svc_stop() {
70 local x
71 local stopfail="no"
72 local mydeps
73 local mydep
74 local retval
75 local depservice
76 if [ ! -L ${svcdir}/started/${myservice} ]
77 then
78 einfo "${myservice} has not yet been started."
79 return 1
80 fi
81
82 #remove symlink to prevent recursion
83 rm -f ${svcdir}/started/${myservice}
84
85 #stop all services that should be before on runlevel change
86 for x in ${svcdir}/before/*/${myservice}
87 do
88 if [ ! -L ${x} ]
89 then
90 continue
91 fi
92 depservice=${x%/*}
93 if [ -d ${svcdir}/softscripts.new ]
94 then
95 if [ ! -L ${svcdir}/softscripts.new/${depservice##*/} ] && \
96 [ -L ${svcdir}/started/${depservice##*/} ] && \
97 [ ! -L ${svcdir}/need/${depservice##*/}/${myservice} ] && \
98 [ ! -L ${svcdir}/use/${depservice##*/}/${myservice} ] && \
99 [ ! -L ${svcdir}/before/${myservice}/${depservice##*/} ]
100 then
101 /etc/init.d/${depservice##*/} stop
102 fi
103 fi
104 done
105
106 if [ -L /etc/init.d/boot/${myservice} ]
107 then
108 einfo "Warning: you are stopping a boot service."
109 fi
110 if [ "$NETSERVICE" = "yes" ]
111 then
112 #net.* service
113 if [ -L /etc/runlevels/boot/${myservice} ] || [ -L /etc/runlevels/${mylevel}/${myservice} ]
114 then
115 mydeps="net $myservice"
116 else
117 mydeps=$myservice
118 fi
119 else
120 mydeps=$myservice
121 fi
122 if [ "$svcpause" = "yes" ]
123 then
124 mydeps=""
125 fi
126 for mydep in $mydeps
127 do
128 for mytype in ${deptypes/before/}
129 do
130 if [ -d ${svcdir}/${mytype}/${mydep} ]
131 then
132 for x in ${svcdir}/${mytype}/${mydep}/*
133 do
134 if [ ! -L ${x} ]
135 then
136 continue
137 fi
138 if [ ! -L ${svcdir}/started/${x##*/} ]
139 then
140 #service not currently running, continue
141 continue
142 fi
143 ${x} stop
144 if [ $? -ne 0 ]
145 then
146 stopfail="yes"
147 break
148 fi
149 done
150 if [ "$stopfail" = "yes" ] && [ -L ${svcdir}/need/${mydep}/${x} ]
151 then
152 eerror "Problems stopping dependent services. \"${myservice}\" still up."
153 ln -sf /etc/init.d/${myservice} ${svcdir}/started/${myservice}
154 exit 1
155 fi
156 fi
157 done
158 done
159
160 #now that deps are stopped, stop our service
161 stop
162 retval=$?
163
164 # #stop all services that should be after on runlevel change
165 # for x in ${svcdir}/after/*/${myservice}
166 # do
167 # if [ ! -L ${x} ]
168 # then
169 # continue
170 # fi
171 # depservice=${x%/*}
172 # if [ -d ${svcdir}/softscripts.new ]
173 # then
174 # if [ ! -L ${svcdir}/softscripts.new/${depservice##*/} ] && \
175 # [ -L ${svcdir}/started/${depservice##*/} ] && \
176 # [ ! -L ${svcdir}/need/${myservice}/${depservice##*/} ] && \
177 # [ ! -L ${svcdir}/use/${myservice}/${depservice##*/} ] && \
178 # [ ! -L ${svcdir}/after/${myservice}/${depservice##*/} ]
179 # then
180 # /etc/init.d/${depservice##*/} stop
181 # fi
182 # fi
183 # done
184
185 if [ $retval -ne 0 ]
186 then
187 ln -sf /etc/init.d/${myservice} ${svcdir}/started/${myservice}
188 fi
189 return $retval
190 }
191
192 svc_start() {
193 local retval=0
194 local startfail="no"
195 local x
196 local y
197 local myserv
198 local depservice
199 if [ ! -L ${svcdir}/started/${myservice} ]
200 then
201 #link first to prevent possible recursion
202 ln -sf /etc/init.d/${myservice} ${svcdir}/started/${myservice}
203
204 #start anything that should be started before on runlevel change
205 for x in ${svcdir}/after/*/${myservice}
206 do
207 if [ ! -L ${x} ]
208 then
209 continue
210 fi
211 depservice=${x%/*}
212 if [ -d ${svcdir}/softscripts.old ]
213 then
214 if [ ! -L ${svcdir}/softscripts.old/${depservice##*/} ] && \
215 [ ! -L ${svcdir}/started/${depservice##*/} ] && \
216 [ -L ${svcdir}/softscripts/${depservice##*/} ] && \
217 [ ! -L ${svcdir}/need/${myservice}/${depservice##*/} ] && \
218 [ ! -L ${svcdir}/use/${myservice}/${depservice##*/} ] && \
219 [ ! -L ${svcdir}/after/${myservice}/${depservice##*/} ]
220 then
221 /etc/init.d/${depservice##*/} start
222 fi
223 fi
224 done
225
226 #start dependencies, if any
227 for x in `ineed ${myservice}` `valid_iuse ${myservice}`
228 do
229 if [ "$x" = "net" ]
230 then
231 for y in /etc/runlevels/boot/net.* /etc/runlevels/${mylevel}/net.*
232 do
233 myserv=${y##*/}
234 if [ ! -L ${svcdir}/started/${myserv} ]
235 then
236 /etc/init.d/${myserv} start
237
238 #a 'need' dependacy is critical for the service to start
239 if [ $? -ne 0 ] && [ -L ${svcdir}/need/${x}/${myservice} ]
240 then
241 startfail="yes"
242 fi
243 fi
244 done
245 else
246 if [ ! -L ${svcdir}/started/${x} ]
247 then
248 /etc/init.d/${x} start
249
250 #a 'need' dependacy is critical for the service to start
251 if [ $? -ne 0 ] && [ -L ${svcdir}/need/${x}/${myservice} ]
252 then
253 startfail="yes"
254 fi
255 fi
256 fi
257 done
258
259 if [ "$startfail" = "yes" ]
260 then
261 eerror "Problem starting needed services. \"${myservice}\" was not started."
262 retval=1
263 fi
264
265 #start service
266 if [ -d ${svcdir}/broken/${myservice} ] && [ $retval -eq 0 ]
267 then
268 eerror "Some services needed are missing. Run './${myservice} broken'"
269 eerror "for a list of those services. \"${myservice}\" was not started."
270 retval=1
271 elif [ ! -d ${svcdir}/broken/${myservice} ] && [ $retval -eq 0 ]
272 then
273 start
274 retval=$?
275 fi
276
277 #start anything that should be started after on runlevel change
278 for x in ${svcdir}/before/*/${myservice}
279 do
280 if [ ! -L ${x} ]
281 then
282 continue
283 fi
284 depservice=${x%/*}
285 if [ -d ${svcdir}/softscripts.old ]
286 then
287 if [ ! -L ${svcdir}/softscripts.old/${depservice##*/} ] && \
288 [ ! -L ${svcdir}/started/${depservice##*/} ] && \
289 [ -L ${svcdir}/softscripts/${depservice##*/} ] && \
290 [ ! -L ${svcdir}/need/${depservice##*/}/${myservice} ] && \
291 [ ! -L ${svcdir}/use/${depservice##*/}/${myservice} ] && \
292 [ ! -L ${svcdir}/before/${myservice}/${depservice##*/} ]
293 then
294 /etc/init.d/${depservice##*/} start
295 fi
296 fi
297 done
298
299 #remove link if service didn't start; but only if we're not booting
300 #if we're booting, we need to continue and do our best to get the
301 #system up.
302 if [ $retval -ne 0 ] && [ "$SOFTLEVEL" != "boot" ]
303 then
304 rm ${svcdir}/started/${myservice}
305 fi
306 return $retval
307 else
308 einfo "${myservice} has already been started."
309 return 1
310 fi
311 }
312
313 svc_restart() {
314 svc_stop || return $?
315 sleep 1
316 svc_start || return $?
317 }
318
319 source ${myscript}
320 if [ "$opts" = "" ]
321 then
322 opts="start stop restart"
323 fi
324
325 needsme() {
326 local x
327 if [ -d ${svcdir}/need/${1} ]
328 then
329 for x in ${svcdir}/need/${1}/*
330 do
331 if [ ! -L $x ]
332 then
333 continue
334 fi
335 echo ${x##*/}
336 done
337 fi
338 }
339
340 usesme() {
341 local x
342 if [ -d ${svcdir}/use/${1} ]
343 then
344 for x in ${svcdir}/use/${1}/*
345 do
346 if [ ! -L $x ]
347 then
348 continue
349 fi
350 echo ${x##*/}
351 done
352 fi
353 }
354
355 ineed() {
356 local x
357 local z
358 for x in ${svcdir}/need/*/${1}
359 do
360 if [ ! -L ${x} ]
361 then
362 continue
363 fi
364 z=${x%/*}
365 echo ${z##*/}
366 done
367 }
368
369 #this will give all the use's of the service, even if not in current or boot
370 #runlevels
371 iuse() {
372 local x
373 local z
374 for x in ${svcdir}/use/*/${1}
375 do
376 if [ ! -L ${x} ]
377 then
378 continue
379 fi
380 z=${x%/*}
381 echo ${z##*/}
382 done
383 }
384
385 #this will only give the valid use's for the service (they must be in the boot
386 #or current runlevel)
387 valid_iuse() {
388 local x
389 local y
390 for x in `iuse ${1}`
391 do
392 if [ -e /etc/runlevels/boot/${x} ] || [ -e /etc/runlevels/${mylevel}/${x} ]
393 then
394 z=${x%/*}
395 echo ${z##*/}
396 fi
397 done
398 }
399
400 #list broken dependancies of type 'need'
401 broken() {
402 local x
403 if [ -d ${svcdir}/broken/${1} ]
404 then
405 for x in ${svcdir}/broken/${1}/*
406 do
407 if [ ! -f $x ]
408 then
409 continue
410 fi
411 echo ${x##*/}
412 done
413 fi
414 }
415
416 #call this with "needsme", "ineed", "usesme", "iuse" or "broken" as first arg
417 query() {
418 local deps
419 local x
420 install -d -m0755 ${svcdir}/depcheck/$$
421 if [ "$1" = "ineed" ] && [ ! -L ${svcdir}/started/${myservice} ]
422 then
423 einfo "Warning: ${myservice} not running. need info may not be accurate."
424 fi
425 if [ "$1" = "iuse" ] && [ ! -L ${svcdir}/started/${myservice} ]
426 then
427 einfo "Warning: ${myservice} not running. use info may not be accurate."
428 fi
429
430 deps="${myservice}"
431 while [ "$deps" != "" ]
432 do
433 deps=`${1} ${deps}`
434 for x in $deps
435 do
436 if [ ! -e ${svcdir}/depcheck/$$/${x} ]
437 then
438 touch ${svcdir}/depcheck/$$/${x}
439 fi
440 done
441 done
442 for x in ${svcdir}/depcheck/$$/*
443 do
444 if [ ! -e $x ]
445 then
446 continue
447 fi
448 echo ${x##*/}
449 done
450 rm -rf ${svcdir}/depcheck/
451 }
452
453 svc_homegrown() {
454 local arg="$1" x
455 # Walk through the list of available options, looking for the
456 # requested one.
457 for x in $opts; do
458 if [ $x = "$arg" ]; then
459 if typeset -F $x &>/dev/null; then
460 # Run the homegrown function
461 $x
462 return $?
463 else
464 # This is a weak error message
465 echo "Function $x doesn't exist."
466 usage $opts
467 exit 1
468 fi
469 fi
470 done
471 # If we're here, then the function wasn't in $opts. This is
472 # the same error message that used to be in the case statement
473 # before homegrown functions were supported.
474 echo "wrong args. ( $arg / $* )"
475 usage $opts
476 exit 1
477 }
478
479 shift
480 if [ $# -lt 1 ]
481 then
482 echo "not enough args."
483 usage $opts
484 exit 1
485 fi
486 for arg in ${*}
487 do
488 case $arg in
489 stop)
490 svc_stop
491 ;;
492 start)
493 svc_start
494 ;;
495 needsme|ineed|usesme|iuse|broken)
496 query $arg
497 ;;
498 zap)
499 if [ -e ${svcdir}/started/${myservice} ]
500 then
501 einfo "Manually resetting ${myservice} to stopped state."
502 rm ${svcdir}/started/${myservice}
503 fi
504 ;;
505 restart)
506 #create a snapshot of started services
507 rm -rf ${svcdir}/snapshot/*
508 cp ${svcdir}/started/* ${svcdir}/snapshot/
509 #simple way to try and detect if the service use svc_{start,stop} to restart
510 #if it have a custom restart() funtion.
511 if [ "`grep 'restart()' /etc/init.d/${myservice}`" ]
512 then
513 if [ -z "`grep svc_stop /etc/init.d/${myservice}`" ] || [ -z "`grep svc_start /etc/init.d/${myservice}`" ]
514 then
515 echo
516 einfo "Please use 'svc_stop; svc_start' and not 'start; stop' to restart the service"
517 einfo "in the custom 'restart()' function. Run ${myservice} without arguments for"
518 einfo "more info."
519 echo
520 svc_restart
521 else
522 restart
523 fi
524 else
525 restart
526 fi
527
528 #restart dependancies as well
529 if [ -L ${svcdir}/started/${myservice} ]
530 then
531 for x in ${svcdir}/snapshot/*
532 do
533 if [ ! -L ${svcdir}/started/${x##*/} ]
534 then
535 ${x} start
536 fi
537 done
538 fi
539 ;;
540 pause)
541 svcpause="yes"
542 svc_stop
543 svcpause="no"
544 ;;
545 *)
546 # Allow for homegrown functions
547 svc_homegrown $arg
548 ;;
549 esac
550 done
551
552
553 # 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