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