| 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 |
svcdir=/dev/shm/.init.d
|
| 9 |
|
| 10 |
myscript=${1}
|
| 11 |
if [ -L $1 ]
|
| 12 |
then
|
| 13 |
myservice=`readlink ${1}`
|
| 14 |
else
|
| 15 |
myservice=${1}
|
| 16 |
fi
|
| 17 |
|
| 18 |
myservice=${myservice##*/}
|
| 19 |
mylevel=`cat ${svcdir}/softlevel`
|
| 20 |
|
| 21 |
# Source configuration files.
|
| 22 |
# (1) Source /etc/conf.d/basic to get common configuration.
|
| 23 |
# (2) Source /etc/conf.d/${myservice} to get initscript-specific
|
| 24 |
# configuration (if it exists).
|
| 25 |
# (3) Source /etc/rc.conf to pick up potentially overriding
|
| 26 |
# configuration, if the system administrator chose to put it
|
| 27 |
# there (if it exists).
|
| 28 |
[ -e /etc/conf.d/basic ] && source /etc/conf.d/basic
|
| 29 |
[ -e /etc/conf.d/${myservice} ] && source /etc/conf.d/${myservice}
|
| 30 |
[ -e /etc/rc.conf ] && source /etc/rc.conf
|
| 31 |
|
| 32 |
usage() {
|
| 33 |
export IFS="|"
|
| 34 |
myline="Usage: ${myservice} {$*"
|
| 35 |
eerror "${myline}}"
|
| 36 |
}
|
| 37 |
|
| 38 |
stop() {
|
| 39 |
#return success so the symlink gets removed
|
| 40 |
return 0
|
| 41 |
}
|
| 42 |
|
| 43 |
start() {
|
| 44 |
einfo "${myservice} does not have a start function."
|
| 45 |
#return failure so the symlink doesn't get created
|
| 46 |
return 1
|
| 47 |
}
|
| 48 |
|
| 49 |
svc_stop() {
|
| 50 |
local x
|
| 51 |
local stopfail="no"
|
| 52 |
local mydeps
|
| 53 |
local mydep
|
| 54 |
if [ ! -L ${svcdir}/started/${myservice} ]
|
| 55 |
then
|
| 56 |
einfo "${myservice} has not yet been started."
|
| 57 |
return 1
|
| 58 |
fi
|
| 59 |
if [ -L /etc/init.d/boot/${myservice} ]
|
| 60 |
then
|
| 61 |
einfo "Warning: you are stopping a boot service."
|
| 62 |
fi
|
| 63 |
if [ "${myservice##*.}" != "$myservice" ]
|
| 64 |
then
|
| 65 |
#net.* service
|
| 66 |
if [ -L /etc/runlevels/boot/${myservice} ] || [ -L /etc/runlevels/${mylevel}/${myservice} ]
|
| 67 |
then
|
| 68 |
mydeps="net $myservice"
|
| 69 |
else
|
| 70 |
mydeps=$myservice
|
| 71 |
fi
|
| 72 |
else
|
| 73 |
mydeps=$myservice
|
| 74 |
fi
|
| 75 |
for mydep in $mydeps
|
| 76 |
do
|
| 77 |
for mytype in need use
|
| 78 |
do
|
| 79 |
if [ -d ${svcdir}/${mytype}/${mydep} ]
|
| 80 |
then
|
| 81 |
for x in ${svcdir}/${mytype}/${mydep}/*
|
| 82 |
do
|
| 83 |
if [ ! -L ${x} ]
|
| 84 |
then
|
| 85 |
continue
|
| 86 |
fi
|
| 87 |
if [ ! -L ${svcdir}/started/${x##*/} ]
|
| 88 |
then
|
| 89 |
#service not currently running, continue
|
| 90 |
# This breaks the need's, and since they do not get
|
| 91 |
# regenerated, things will break when the services
|
| 92 |
# gets started again. We should keep the need's and
|
| 93 |
# use's (when actually official) intact at all time
|
| 94 |
# anyhow.
|
| 95 |
# rm ${x}
|
| 96 |
continue
|
| 97 |
fi
|
| 98 |
${x} stop
|
| 99 |
if [ $? -ne 0 ]
|
| 100 |
then
|
| 101 |
stopfail="yes"
|
| 102 |
break
|
| 103 |
# See above. The need's and use's needs to stay intact.
|
| 104 |
#else
|
| 105 |
# rm ${x}
|
| 106 |
fi
|
| 107 |
done
|
| 108 |
if [ "$stopfail" = "yes" ]
|
| 109 |
then
|
| 110 |
einfo "Problems stopping dependent services. ${myservice} still up."
|
| 111 |
exit 1
|
| 112 |
fi
|
| 113 |
fi
|
| 114 |
done
|
| 115 |
done
|
| 116 |
#now that deps are stopped, stop our service
|
| 117 |
stop
|
| 118 |
if [ $? -eq 0 ]
|
| 119 |
then
|
| 120 |
rm ${svcdir}/started/${myservice}
|
| 121 |
else
|
| 122 |
return $?
|
| 123 |
fi
|
| 124 |
}
|
| 125 |
|
| 126 |
svc_start() {
|
| 127 |
local retval
|
| 128 |
local x
|
| 129 |
local y
|
| 130 |
local myserv
|
| 131 |
if [ ! -L ${svcdir}/started/${myservice} ]
|
| 132 |
then
|
| 133 |
#link first to prevent possible recursion
|
| 134 |
ln -s /etc/init.d/${myservice} ${svcdir}/started/${myservice}
|
| 135 |
|
| 136 |
#start dependencies, if any
|
| 137 |
for x in `ineed ${myservice}` `valid_iuse ${myservice}`
|
| 138 |
do
|
| 139 |
if [ "$x" = "net" ]
|
| 140 |
then
|
| 141 |
for y in /etc/runlevels/boot/net.* /etc/runlevels/${mylevel}/net.*
|
| 142 |
do
|
| 143 |
myserv=${y##*/}
|
| 144 |
if [ ! -L ${svcdir}/started/${myserv} ]
|
| 145 |
then
|
| 146 |
/etc/init.d/${myserv} start
|
| 147 |
fi
|
| 148 |
done
|
| 149 |
else
|
| 150 |
if [ ! -L ${svcdir}/started/${x} ]
|
| 151 |
then
|
| 152 |
/etc/init.d/${x} start
|
| 153 |
fi
|
| 154 |
fi
|
| 155 |
done
|
| 156 |
#start service
|
| 157 |
start
|
| 158 |
retval=$?
|
| 159 |
|
| 160 |
#remove link if service didn't start; but only if we're not booting
|
| 161 |
#if we're booting, we need to continue and do our best to get the
|
| 162 |
#system up.
|
| 163 |
if [ "$SOFTLEVEL" = "boot" ]
|
| 164 |
then
|
| 165 |
return $retval
|
| 166 |
elif [ $retval -ne 0 ]
|
| 167 |
then
|
| 168 |
rm ${svcdir}/started/${myservice}
|
| 169 |
return $retval
|
| 170 |
fi
|
| 171 |
else
|
| 172 |
einfo "${myservice} has already been started."
|
| 173 |
return 1
|
| 174 |
fi
|
| 175 |
}
|
| 176 |
|
| 177 |
source ${myscript}
|
| 178 |
if [ "$opts" = "" ]
|
| 179 |
then
|
| 180 |
opts="start stop restart"
|
| 181 |
fi
|
| 182 |
|
| 183 |
needsme() {
|
| 184 |
local x
|
| 185 |
if [ -d ${svcdir}/need/${1} ]
|
| 186 |
then
|
| 187 |
for x in ${svcdir}/need/${1}/*
|
| 188 |
do
|
| 189 |
if [ ! -L $x ]
|
| 190 |
then
|
| 191 |
continue
|
| 192 |
fi
|
| 193 |
echo ${x##*/}
|
| 194 |
done
|
| 195 |
fi
|
| 196 |
}
|
| 197 |
|
| 198 |
usesme() {
|
| 199 |
local x
|
| 200 |
if [ -d ${svcdir}/use/${1} ]
|
| 201 |
then
|
| 202 |
for x in ${svcdir}/use/${1}/*
|
| 203 |
do
|
| 204 |
if [ ! -L $x ]
|
| 205 |
then
|
| 206 |
continue
|
| 207 |
fi
|
| 208 |
echo ${x##*/}
|
| 209 |
done
|
| 210 |
fi
|
| 211 |
}
|
| 212 |
|
| 213 |
ineed() {
|
| 214 |
local x
|
| 215 |
local z
|
| 216 |
for x in ${svcdir}/need/*/${1}
|
| 217 |
do
|
| 218 |
if [ ! -L ${x} ]
|
| 219 |
then
|
| 220 |
continue
|
| 221 |
fi
|
| 222 |
z=${x%/*}
|
| 223 |
echo ${z##*/}
|
| 224 |
done
|
| 225 |
}
|
| 226 |
|
| 227 |
#this will give all the use's of the service, even if not in current or boot
|
| 228 |
#runlevels
|
| 229 |
iuse() {
|
| 230 |
local x
|
| 231 |
local z
|
| 232 |
for x in ${svcdir}/use/*/${1}
|
| 233 |
do
|
| 234 |
if [ ! -L ${x} ]
|
| 235 |
then
|
| 236 |
continue
|
| 237 |
fi
|
| 238 |
z=${x%/*}
|
| 239 |
echo ${z##*/}
|
| 240 |
done
|
| 241 |
}
|
| 242 |
|
| 243 |
#this will only give the valid use's for the service (they must be in the boot
|
| 244 |
#or current runlevel)
|
| 245 |
valid_iuse() {
|
| 246 |
local x
|
| 247 |
local y
|
| 248 |
for x in `iuse ${1}`
|
| 249 |
do
|
| 250 |
if [ -e /etc/runlevels/boot/${x} ] || [ -e /etc/runlevels/${mylevel}/${x} ]
|
| 251 |
then
|
| 252 |
z=${x%/*}
|
| 253 |
echo ${z##*/}
|
| 254 |
fi
|
| 255 |
done
|
| 256 |
}
|
| 257 |
|
| 258 |
#call this with "needsme", "ineed", "usesme" or "iuse" as first arg
|
| 259 |
query() {
|
| 260 |
local deps
|
| 261 |
local x
|
| 262 |
install -d -m0755 ${svcdir}/depcheck/$$
|
| 263 |
if [ "$1" = "ineed" ] && [ ! -L ${svcdir}/started/${myservice} ]
|
| 264 |
then
|
| 265 |
einfo "Warning: ${myservice} not running. need info may not be accurate."
|
| 266 |
fi
|
| 267 |
if [ "$1" = "iuse" ] && [ ! -L ${svcdir}/started/${myservice} ]
|
| 268 |
then
|
| 269 |
einfo "Warning: ${myservice} not running. use info may not be accurate."
|
| 270 |
fi
|
| 271 |
|
| 272 |
deps="${myservice}"
|
| 273 |
while [ "$deps" != "" ]
|
| 274 |
do
|
| 275 |
deps=`${1} ${deps}`
|
| 276 |
for x in $deps
|
| 277 |
do
|
| 278 |
if [ ! -e ${svcdir}/depcheck/$$/${x} ]
|
| 279 |
then
|
| 280 |
touch ${svcdir}/depcheck/$$/${x}
|
| 281 |
fi
|
| 282 |
done
|
| 283 |
done
|
| 284 |
for x in ${svcdir}/depcheck/$$/*
|
| 285 |
do
|
| 286 |
if [ ! -e $x ]
|
| 287 |
then
|
| 288 |
continue
|
| 289 |
fi
|
| 290 |
echo ${x##*/}
|
| 291 |
done
|
| 292 |
rm -rf ${svcdir}/depcheck/
|
| 293 |
}
|
| 294 |
|
| 295 |
svc_homegrown() {
|
| 296 |
local arg="$1" x
|
| 297 |
# Walk through the list of available options, looking for the
|
| 298 |
# requested one.
|
| 299 |
for x in $opts; do
|
| 300 |
if [ $x = "$arg" ]; then
|
| 301 |
if typeset -F $x &>/dev/null; then
|
| 302 |
# Run the homegrown function
|
| 303 |
$x
|
| 304 |
return $?
|
| 305 |
else
|
| 306 |
# This is a weak error message
|
| 307 |
echo "Function $x doesn't exist."
|
| 308 |
usage $opts
|
| 309 |
exit 1
|
| 310 |
fi
|
| 311 |
fi
|
| 312 |
done
|
| 313 |
# If we're here, then the function wasn't in $opts. This is
|
| 314 |
# the same error message that used to be in the case statement
|
| 315 |
# before homegrown functions were supported.
|
| 316 |
echo "wrong args. ( $arg / $* )"
|
| 317 |
usage $opts
|
| 318 |
exit 1
|
| 319 |
}
|
| 320 |
|
| 321 |
shift
|
| 322 |
if [ $# -lt 1 ]
|
| 323 |
then
|
| 324 |
echo "not enough args."
|
| 325 |
usage $opts
|
| 326 |
exit 1
|
| 327 |
fi
|
| 328 |
for arg in ${*}
|
| 329 |
do
|
| 330 |
case $arg in
|
| 331 |
stop)
|
| 332 |
svc_stop
|
| 333 |
;;
|
| 334 |
start)
|
| 335 |
svc_start
|
| 336 |
;;
|
| 337 |
needsme|ineed|usesme|iuse)
|
| 338 |
query $arg
|
| 339 |
;;
|
| 340 |
zap)
|
| 341 |
if [ -e ${svcdir}/started/${myservice} ]
|
| 342 |
then
|
| 343 |
einfo "Manually resetting ${myservice} to stopped state."
|
| 344 |
rm ${svcdir}/started/${myservice}
|
| 345 |
fi
|
| 346 |
;;
|
| 347 |
restart)
|
| 348 |
#add snapshot support here so any dependent services that
|
| 349 |
#are stopped are restarted after the svc_start
|
| 350 |
if [ -e ${svcdir}/started/${myservice} ]
|
| 351 |
then
|
| 352 |
svc_stop
|
| 353 |
sleep 1
|
| 354 |
fi
|
| 355 |
svc_start
|
| 356 |
;;
|
| 357 |
*)
|
| 358 |
# Allow for homegrown functions
|
| 359 |
svc_homegrown $arg
|
| 360 |
;;
|
| 361 |
esac
|
| 362 |
done
|
| 363 |
|
| 364 |
# vim:ts=4
|