sys/subr_rman: fix some issues
[dragonfly.git] / sbin / rcrun / rcrun.sh
blob903ece116a4a685783a809f1012abccc5db2e21c
1 #!/bin/sh
3 # Copyright (c) 2003
4 # The DragonFly Project. All rights reserved.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 # 3. Neither the name of The DragonFly Project nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific, prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
34 if [ -r /etc/defaults/rc.conf ]; then
35 . /etc/defaults/rc.conf
38 if [ -r /etc/rc.conf ]; then
39 . /etc/rc.conf
42 buildrclist()
44 rcfiles=`find /etc/rc.d -type f`
45 for d in $local_startup; do
46 if [ -d $d ]; then
47 rcfiles="$rcfiles `find $d -type f`"
49 done
50 # The last element of this list is the script that provides the target
51 # we want to run.
53 rclist=`rcorder -o $1 $rcfiles`
56 dostart()
58 arg=$1
59 shift
61 for tgt in $@; do
62 case X`varsym -s -q rcng_$tgt` in
63 Xrunning*)
64 echo "$tgt has already been started"
66 Xconfigured*)
67 echo "$tgt has already been configured"
70 _return=0
71 buildrclist $tgt
72 for dep in $rclist; do
73 need=1
74 dep_prvd_list=`rcorder -p $dep`
75 # Because the dependency could actually provide more than one
76 # keyword, iterate it twice, first looking for a match in any
77 # of its PROVIDEs.
79 for dep_prvd in $dep_prvd_list; do
80 if [ $dep_prvd = $tgt ]; then
81 need=0
83 done
84 if [ $need = 1 ]; then
85 for dep_prvd in $dep_prvd_list; do
86 state=`varsym -s -q rcng_$dep_prvd`
87 case X$state in
88 Xrunning*|Xconfigured*|Xirrelevant*|Xdisabled*)
91 echo "$tgt depends on $dep_prvd, current state: $state"
92 _return=1
94 esac
95 done
97 done
98 # $dep contains the last dependency, which we run
100 if [ X$dep = X ]; then
101 echo "Unable to find keyword $tgt"
102 elif [ $_return = 0 ]; then
103 echo "Running $dep $arg"
104 (sh $dep $arg)
105 case X`varsym -s -q rcng_$tgt` in
106 Xdisabled*)
107 echo "$tgt is disabled, enable in rc.conf first or use rcforce/rcone"
109 Xfailed*)
110 echo "$tgt has failed to start"
113 esac
116 esac
117 done
120 arg=$0
121 case ${0##*/} in
122 rcstart)
123 arg=start
125 rcstop)
126 arg=stop
128 rcrestart)
129 arg=restart
131 rcreload)
132 arg=reload
134 rcvar)
135 arg=rcvar
137 rcvars)
138 arg=rcvar
140 rclist)
141 arg=list
143 rcforce)
144 arg=forcestart
146 rcfast)
147 arg=faststart
149 rcone)
150 arg=onestart
152 rcenable)
153 arg=enable
155 rcdisable)
156 arg=disable
159 arg=$1
160 shift
162 esac
164 case $arg in
165 start)
166 dostart start $@
168 forcestart)
169 dostart forcestart $@
171 faststart)
172 dostart faststart $@
174 onestart)
175 dostart onestart $@
177 stop)
178 for tgt in $@; do
179 buildrclist $tgt
180 dep=`echo "$rclist" | tail -1`
181 if [ X$dep = X ]; then
182 echo "Unable to find keyword $tgt"
183 else
184 (sh $dep stop)
186 done
188 restart)
189 for tgt in $@; do
190 buildrclist $tgt
191 dep=`echo "$rclist" | tail -1`
192 if [ X$dep = X ]; then
193 echo "Unable to find keyword $tgt"
194 else
195 (sh $dep restart)
197 done
199 reload)
200 for tgt in $@; do
201 buildrclist $tgt
202 dep=`echo "$rclist" | tail -1`
203 if [ X$dep = X ]; then
204 echo "Unable to find keyword $tgt"
205 else
206 (sh $dep reload)
208 done
210 disable|enable)
211 if [ "$arg" = "enable" ]; then
212 mode=YES
213 else
214 mode=NO
216 for tgt in $@; do
217 buildrclist $tgt
218 dep=`echo "$rclist" | tail -1`
219 if [ X$dep = X ]; then
220 echo "Unable to find provider id $tgt"
221 elif [ `varsym -s -q rcng_$tgt` = "$mode" ]; then
222 echo "$tgt is already $mode"
223 else
224 vars=`(sh $dep rcvar) 2>/dev/null | grep = | sed -e 's/\\$//g' | sed -e 's/=.*//g'`
225 cp /etc/rc.conf /etc/rc.conf.bak
226 if [ $arg = disable ]; then
227 rcstop $tgt
229 for k in $vars; do
230 rm -f /etc/rc.conf.$$
231 ( egrep -v "# rcrun enable ${k}$" /etc/rc.conf; printf "${k}=${mode}\t# rcrun enable ${k}\n" ) > /etc/rc.conf.$$
232 mv -f /etc/rc.conf.$$ /etc/rc.conf
233 echo "added/modified: ${k}=${mode}"
234 done
235 if [ $arg = enable ]; then
236 rcstart $tgt
239 done
241 rcvar)
242 for tgt in $@; do
243 buildrclist $tgt
244 dep=`echo "$rclist" | tail -1`
245 if [ X$dep = X ]; then
246 echo "Unable to find provider id $tgt"
247 else
248 (sh $dep rcvar)
250 done
252 list)
253 if [ "X$*" = X ]; then
254 for tgt in `varsym -a -s | egrep '^rcng_'`; do
255 echo $tgt
256 done
257 else
258 for tgt in $@; do
259 varsym -s rcng_$tgt 2>/dev/null || varsym -s rcng_$tgt
260 done
264 echo "usage: rcrun action rcscript1 ..."
265 echo " where 'action' is one of:"
266 echo " start|stop|restart|reload|rcvar|list|forcestart|faststart"
267 echo " onestart|disable|enable"
269 esac