xref: /dragonfly/sbin/rcrun/rcrun.sh (revision 8accc937)
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
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}
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	    ;;
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/rcone"
91		    ;;
92		Xfailed*)
93		    echo "$i has failed to start"
94		    ;;
95
96		esac
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    ;;
129rcone)
130    arg=onestart
131    ;;
132rcenable)
133    arg=enable
134    ;;
135rcdisable)
136    arg=disable
137    ;;
138*)
139    arg=$1
140    shift
141    ;;
142esac
143
144case $arg in
145start)
146	dostart start $@
147	;;
148forcestart)
149	dostart forcestart $@
150	;;
151faststart)
152	dostart faststart $@
153	;;
154onestart)
155	dostart onestart $@
156	;;
157stop)
158	for i in $@; do
159	    buildrclist $i
160	    j=`echo "$rclist" | tail -1`
161	    if [ X$j = X ]; then
162		echo "Unable to find keyword $i"
163	    else
164		(sh $j stop)
165	    fi
166	done
167	;;
168restart)
169	for i in $@; do
170	    buildrclist $i
171	    j=`echo "$rclist" | tail -1`
172	    if [ X$j = X ]; then
173		echo "Unable to find keyword $i"
174	    else
175		(sh $j restart)
176	    fi
177	done
178	;;
179disable|enable)
180	if [ "$arg" = "enable" ]; then
181	    mode=YES
182	else
183	    mode=NO
184	fi
185	for i in $@; do
186	    buildrclist $i
187	    j=`echo "$rclist" | tail -1`
188	    if [ X$j = X ]; then
189		echo "Unable to find provider id $i"
190	    elif [ `varsym -s -q rcng_$i` = "$mode" ]; then
191		echo "$i is already $mode"
192	    else
193		vars=`(sh $j rcvar) 2>/dev/null | grep = | sed -e 's/\\$//g' | sed -e 's/=.*//g'`
194		cp /etc/rc.conf /etc/rc.conf.bak
195		if [ $arg = disable ]; then
196		    rcstop $i
197		fi
198		for k in $vars; do
199		    rm -f /etc/rc.conf.$$
200		    ( egrep -v "# rcrun enable ${k}$" /etc/rc.conf; printf "${k}=${mode}\t# rcrun enable ${k}\n" ) > /etc/rc.conf.$$
201		    mv -f /etc/rc.conf.$$ /etc/rc.conf
202		    echo "added/modified: ${k}=${mode}"
203		done
204		if [ $arg = enable ]; then
205		    rcstart $i
206		fi
207	    fi
208	done
209	;;
210rcvar)
211	for i in $@; do
212	    buildrclist $i
213	    j=`echo "$rclist" | tail -1`
214	    if [ X$j = X ]; then
215		echo "Unable to find provider id $i"
216	    else
217		(sh $j rcvar)
218	    fi
219	done
220	;;
221list)
222	if [ "X$*" = X ]; then
223	    for i in `varsym -a -s | egrep '^rcng_'`; do
224		echo $i
225	    done
226	else
227	    for i in $@; do
228		varsym -s rcng_$i 2>/dev/null || varsym -s rcng_$i
229	    done
230	fi
231	;;
232*)
233	echo "usage: rcrun action rcscript1 ..."
234	echo "  where 'action' is one of:"
235	echo "    start|stop|restart|rcvar|list|forcestart|faststart|onestart"
236	echo "    disable|enable"
237	;;
238esac
239