xref: /dragonfly/sbin/rcrun/rcrun.sh (revision cfd1aba3)
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
34if [ -r /etc/defaults/rc.conf ]; then
35	. /etc/defaults/rc.conf
36fi
37
38if [ -r /etc/rc.conf ]; then
39	. /etc/rc.conf
40fi
41
42buildrclist()
43{
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`"
48	fi
49    done
50    # The last element of this list is the script that provides the target
51    # we want to run.
52    #
53    rclist=`rcorder -o $1 $rcfiles`
54}
55
56dostart()
57{
58    arg=$1
59    shift
60
61    for tgt in $@; do
62	case X`varsym -s -q rcng_$tgt` in
63	Xrunning*)
64	    echo "$tgt has already been started"
65	    ;;
66	Xconfigured*)
67	    echo "$tgt has already been configured"
68	    ;;
69	*)
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.
78		#
79		for dep_prvd in $dep_prvd_list; do
80		    if [ $dep_prvd = $tgt ]; then
81			need=0
82		    fi
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*)
89			    ;;
90			*)
91			    echo "$tgt depends on $dep_prvd, current state: $state"
92			    _return=1
93			    ;;
94			esac
95		    done
96		fi
97	    done
98	    # $dep contains the last dependency, which we run
99	    #
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"
108		    ;;
109		Xfailed*)
110		    echo "$tgt has failed to start"
111		    ;;
112
113		esac
114	    fi
115	    ;;
116	esac
117    done
118}
119
120arg=$0
121case ${0##*/} in
122rcstart)
123    arg=start
124    ;;
125rcstop)
126    arg=stop
127    ;;
128rcrestart)
129    arg=restart
130    ;;
131rcvar)
132    arg=rcvar
133    ;;
134rcvars)
135    arg=rcvar
136    ;;
137rclist)
138    arg=list
139    ;;
140rcforce)
141    arg=forcestart
142    ;;
143rcfast)
144    arg=faststart
145    ;;
146rcone)
147    arg=onestart
148    ;;
149rcenable)
150    arg=enable
151    ;;
152rcdisable)
153    arg=disable
154    ;;
155*)
156    arg=$1
157    shift
158    ;;
159esac
160
161case $arg in
162start)
163	dostart start $@
164	;;
165forcestart)
166	dostart forcestart $@
167	;;
168faststart)
169	dostart faststart $@
170	;;
171onestart)
172	dostart onestart $@
173	;;
174stop)
175	for tgt in $@; do
176	    buildrclist $tgt
177	    dep=`echo "$rclist" | tail -1`
178	    if [ X$dep = X ]; then
179		echo "Unable to find keyword $tgt"
180	    else
181		(sh $dep stop)
182	    fi
183	done
184	;;
185restart)
186	for tgt in $@; do
187	    buildrclist $tgt
188	    dep=`echo "$rclist" | tail -1`
189	    if [ X$dep = X ]; then
190		echo "Unable to find keyword $tgt"
191	    else
192		(sh $dep restart)
193	    fi
194	done
195	;;
196disable|enable)
197	if [ "$arg" = "enable" ]; then
198	    mode=YES
199	else
200	    mode=NO
201	fi
202	for tgt in $@; do
203	    buildrclist $tgt
204	    dep=`echo "$rclist" | tail -1`
205	    if [ X$dep = X ]; then
206		echo "Unable to find provider id $tgt"
207	    elif [ `varsym -s -q rcng_$tgt` = "$mode" ]; then
208		echo "$tgt is already $mode"
209	    else
210		vars=`(sh $dep rcvar) 2>/dev/null | grep = | sed -e 's/\\$//g' | sed -e 's/=.*//g'`
211		cp /etc/rc.conf /etc/rc.conf.bak
212		if [ $arg = disable ]; then
213		    rcstop $tgt
214		fi
215		for k in $vars; do
216		    rm -f /etc/rc.conf.$$
217		    ( egrep -v "# rcrun enable ${k}$" /etc/rc.conf; printf "${k}=${mode}\t# rcrun enable ${k}\n" ) > /etc/rc.conf.$$
218		    mv -f /etc/rc.conf.$$ /etc/rc.conf
219		    echo "added/modified: ${k}=${mode}"
220		done
221		if [ $arg = enable ]; then
222		    rcstart $tgt
223		fi
224	    fi
225	done
226	;;
227rcvar)
228	for tgt in $@; do
229	    buildrclist $tgt
230	    dep=`echo "$rclist" | tail -1`
231	    if [ X$dep = X ]; then
232		echo "Unable to find provider id $tgt"
233	    else
234		(sh $dep rcvar)
235	    fi
236	done
237	;;
238list)
239	if [ "X$*" = X ]; then
240	    for tgt in `varsym -a -s | egrep '^rcng_'`; do
241		echo $tgt
242	    done
243	else
244	    for tgt in $@; do
245		varsym -s rcng_$tgt 2>/dev/null || varsym -s rcng_$tgt
246	    done
247	fi
248	;;
249*)
250	echo "usage: rcrun action rcscript1 ..."
251	echo "  where 'action' is one of:"
252	echo "    start|stop|restart|rcvar|list|forcestart|faststart|onestart"
253	echo "    disable|enable"
254	;;
255esac
256