rcrun(8): Teach rcstart, rcstop and friends to handle other rc dirs.
[dragonfly.git] / sbin / rcrun / rcrun.sh
CommitLineData
75326d0e
MD
1#!/bin/sh
2#
faee0ee7
SW
3# Copyright (c) 2003
4# The DragonFly Project. All rights reserved.
75326d0e 5#
faee0ee7
SW
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
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.
19#
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.
32#
33
34. /etc/defaults/rc.conf
35
36buildrclist()
37{
38 rcfiles=`find /etc/rc.d -type f`
39 for d in $local_startup; do
40 if [ -d $d ]; then
41 rcfiles="$rcfiles `find $d -type f`"
42 fi
43 done
44 rclist=`rcorder -o $1 $rcfiles`
45}
75326d0e
MD
46
47dostart()
48{
49 arg=$1
50 shift
51
52 for i in $@; do
53 case X`varsym -s -q rcng_$i` in
54 Xrunning*)
55 echo "$i has already been started"
56 ;;
6d94234b
MD
57 Xconfigured*)
58 echo "$i has already been configured"
59 ;;
75326d0e
MD
60 *)
61 _return=0
faee0ee7
SW
62 buildrclist $i
63 for j in $rclist; do
75326d0e
MD
64 need=1
65 for k in `rcorder -p $j`; do
66 if [ $k = $i ]; then
67 need=0
68 else
69 state=`varsym -s -q rcng_$k`
70 case X$state in
6d94234b 71 Xrunning*|Xconfigured*|Xirrelevant*|Xdisabled*)
453375e0 72 ;;
75326d0e
MD
73 *)
74 echo "$i depends on $k, current state: $state"
75 _return=1
76 ;;
77 esac
78 fi
79 done
80 done
36e1ae1a 81 # $j contains the last dependency, which we run
75326d0e
MD
82 #
83 if [ X$j = X ]; then
84 echo "Unable to find keyword $i"
85 elif [ $_return = 0 ]; then
86 echo "Running $j $arg"
faee0ee7 87 (sh $j $arg)
453375e0
MD
88 case X`varsym -s -q rcng_$i` in
89 Xdisabled*)
90 echo "$i is disabled, enable in rc.conf first or use rcforce"
91 ;;
6d94234b
MD
92 Xfailed*)
93 echo "$i has failed to start"
94 ;;
95
453375e0 96 esac
75326d0e
MD
97 fi
98 ;;
99 esac
100 done
101}
102
103arg=$0
104case ${0##*/} in
105rcstart)
106 arg=start
107 ;;
108rcstop)
109 arg=stop
110 ;;
111rcrestart)
112 arg=restart
113 ;;
114rcvar)
115 arg=rcvar
116 ;;
117rcvars)
118 arg=rcvar
119 ;;
120rclist)
121 arg=list
122 ;;
123rcforce)
124 arg=forcestart
125 ;;
126rcfast)
127 arg=faststart
128 ;;
6d94234b
MD
129rcenable)
130 arg=enable
131 ;;
132rcdisable)
133 arg=disable
134 ;;
75326d0e
MD
135*)
136 arg=$1
137 shift
138 ;;
139esac
140
141case $arg in
142start)
143 dostart start $@
144 ;;
145forcestart)
146 dostart forcestart $@
147 ;;
148faststart)
149 dostart faststart $@
150 ;;
151stop)
152 for i in $@; do
faee0ee7
SW
153 buildrclist $i
154 j=`echo "$rclist" | tail -1`
75326d0e
MD
155 if [ X$j = X ]; then
156 echo "Unable to find keyword $i"
157 else
faee0ee7 158 (sh $j stop)
75326d0e
MD
159 fi
160 done
161 ;;
162restart)
163 for i in $@; do
faee0ee7
SW
164 buildrclist $i
165 j=`echo "$rclist" | tail -1`
75326d0e
MD
166 if [ X$j = X ]; then
167 echo "Unable to find keyword $i"
168 else
faee0ee7 169 (sh $j restart)
75326d0e
MD
170 fi
171 done
172 ;;
6d94234b
MD
173disable|enable)
174 if [ "$arg" = "enable" ]; then
175 mode=YES
176 else
177 mode=NO
178 fi
179 for i in $@; do
faee0ee7
SW
180 buildrclist $i
181 j=`echo "$rclist" | tail -1`
6d94234b
MD
182 if [ X$j = X ]; then
183 echo "Unable to find provider id $i"
184 elif [ `varsym -s -q rcng_$i` = "$mode" ]; then
185 echo "$i is already $mode"
186 else
faee0ee7 187 vars=`(sh $j rcvar) 2>/dev/null | grep = | sed -e 's/\\$//g' | sed -e 's/=.*//g'`
6d94234b 188 cp /etc/rc.conf /etc/rc.conf.bak
6eb624aa
SW
189 if [ $arg = disable ]; then
190 rcstop $i
191 fi
6d94234b
MD
192 for k in $vars; do
193 rm -f /etc/rc.conf.$$
6eb624aa 194 ( egrep -v "# rcrun enable ${k}$" /etc/rc.conf; printf "${k}=${mode}\t# rcrun enable ${k}\n" ) > /etc/rc.conf.$$
6d94234b
MD
195 mv -f /etc/rc.conf.$$ /etc/rc.conf
196 echo "added/modified: ${k}=${mode}"
197 done
198 if [ $arg = enable ]; then
199 rcstart $i
6d94234b
MD
200 fi
201 fi
202 done
203 ;;
75326d0e
MD
204rcvar)
205 for i in $@; do
faee0ee7
SW
206 buildrclist $i
207 j=`echo "$rclist" | tail -1`
75326d0e 208 if [ X$j = X ]; then
6d94234b 209 echo "Unable to find provider id $i"
75326d0e 210 else
faee0ee7 211 (sh $j rcvar)
75326d0e
MD
212 fi
213 done
214 ;;
215list)
23da15d3 216 if [ "X$*" = X ]; then
75326d0e
MD
217 for i in `varsym -a -s | egrep '^rcng_'`; do
218 echo $i
219 done
220 else
221 for i in $@; do
222 varsym -s rcng_$i 2>/dev/null || varsym -s rcng_$i
223 done
224 fi
225 ;;
226*)
c56fbe27
SW
227 echo "usage: rcrun start|stop|restart|rcvar|list|forcestart|faststart|disable|enable"
228 echo " script ..."
75326d0e
MD
229 ;;
230esac