rcrun(8): Teach rcstart, rcstop and friends to handle other rc dirs.
[dragonfly.git] / sbin / rcrun / rcrun.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2003
4 #       The DragonFly Project.  All rights reserved.
5 #
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
36 buildrclist()
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 }
46
47 dostart()
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             ;;
57         Xconfigured*)
58             echo "$i has already been configured"
59             ;;
60         *)
61             _return=0
62             buildrclist $i
63             for j in $rclist; do
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
71                         Xrunning*|Xconfigured*|Xirrelevant*|Xdisabled*)
72                             ;;
73                         *)
74                             echo "$i depends on $k, current state: $state"
75                             _return=1
76                             ;;
77                         esac
78                     fi
79                 done
80             done
81             # $j contains the last dependency, which we run
82             #
83             if [ X$j = X ]; then
84                 echo "Unable to find keyword $i"
85             elif [ $_return = 0 ]; then
86                 echo "Running $j $arg"
87                 (sh $j $arg)
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                     ;;
92                 Xfailed*)
93                     echo "$i has failed to start"
94                     ;;
95                         
96                 esac
97             fi
98             ;;
99         esac
100     done
101 }
102
103 arg=$0
104 case ${0##*/} in
105 rcstart)
106     arg=start
107     ;;
108 rcstop)
109     arg=stop
110     ;;
111 rcrestart)
112     arg=restart
113     ;;
114 rcvar)
115     arg=rcvar
116     ;;
117 rcvars)
118     arg=rcvar
119     ;;
120 rclist)
121     arg=list
122     ;;
123 rcforce)
124     arg=forcestart
125     ;;
126 rcfast)
127     arg=faststart
128     ;;
129 rcenable)
130     arg=enable
131     ;;
132 rcdisable)
133     arg=disable
134     ;;
135 *)
136     arg=$1
137     shift
138     ;;
139 esac
140
141 case $arg in
142 start)
143         dostart start $@
144         ;;
145 forcestart)
146         dostart forcestart $@
147         ;;
148 faststart)
149         dostart faststart $@
150         ;;
151 stop)
152         for i in $@; do
153             buildrclist $i
154             j=`echo "$rclist" | tail -1`
155             if [ X$j = X ]; then
156                 echo "Unable to find keyword $i"
157             else
158                 (sh $j stop)
159             fi
160         done
161         ;;
162 restart)
163         for i in $@; do
164             buildrclist $i
165             j=`echo "$rclist" | tail -1`
166             if [ X$j = X ]; then
167                 echo "Unable to find keyword $i"
168             else
169                 (sh $j restart)
170             fi
171         done
172         ;;
173 disable|enable)
174         if [ "$arg" = "enable" ]; then
175             mode=YES
176         else
177             mode=NO
178         fi
179         for i in $@; do
180             buildrclist $i
181             j=`echo "$rclist" | tail -1`
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
187                 vars=`(sh $j rcvar) 2>/dev/null | grep = | sed -e 's/\\$//g' | sed -e 's/=.*//g'`
188                 cp /etc/rc.conf /etc/rc.conf.bak
189                 if [ $arg = disable ]; then
190                     rcstop $i
191                 fi
192                 for k in $vars; do
193                     rm -f /etc/rc.conf.$$
194                     ( egrep -v "# rcrun enable ${k}$" /etc/rc.conf; printf "${k}=${mode}\t# rcrun enable ${k}\n" ) > /etc/rc.conf.$$
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
200                 fi
201             fi
202         done
203         ;;
204 rcvar)
205         for i in $@; do
206             buildrclist $i
207             j=`echo "$rclist" | tail -1`
208             if [ X$j = X ]; then
209                 echo "Unable to find provider id $i"
210             else
211                 (sh $j rcvar)
212             fi
213         done
214         ;;
215 list)
216         if [ "X$*" = X ]; then
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 *)
227         echo "usage: rcrun start|stop|restart|rcvar|list|forcestart|faststart|disable|enable"
228         echo "             script ..."
229         ;;
230 esac