No empty .Rs/.Re
[netbsd-mini2440.git] / sbin / chkconfig / chkconfig.sh
blobf4552347ce16f83755dcb13e5907ffab1b5a9af7
1 #!/bin/sh
3 # $NetBSD: chkconfig.sh,v 1.2 2003/01/04 23:43:04 wiz Exp $
5 # Copyright (c) 2001 Zembu Labs, Inc.
6 # All rights reserved.
8 # Author: Dan Mercer <dmercer@zembu.com>
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
18 # 3. All advertising materials mentioning features or use of this software
19 # must display the following acknowledgement:
20 # This product includes software developed by Zembu Labs, Inc.
21 # 4. Neither the name of Zembu Labs nor the names of its employees may
22 # be used to endorse or promote products derived from this software
23 # without specific prior written permission.
25 # THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
26 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
27 # RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
28 # CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 # chkconfig - configuration state checker
39 # This script is written to work with the NetBSD (1.5 and later) rc system.
40 # It is meant to provide the same functionality as found in IRIX chkconfig.
41 # This script has nothing to do with the abortion produced by RedHat that
42 # has the same name.
44 # chkconfig makes use of the '-k' flag to rcorder. It will not work
45 # with versions of rcorder that do not support '-k'.
47 # Dan Mercer <dmercer@zembu.com>
49 . /etc/rc.subr
51 display()
53 # output $1 with 'on' or 'off' based on the return of checkyesno()
54 # Returns 0 for yes, 1 for no.
56 _name=$1
57 load_rc_config ${_name}
58 if checkyesno ${_name}; then
59 printf "\t%-15s\t\ton\n" ${_name}
60 return 0
61 else
62 printf "\t%-15s\t\toff\n" ${_name}
63 return 1
67 exists()
69 # Returns true if an executable named $1 exists
70 # in /etc/rc.d/
72 _name=$1
73 fqp="/etc/rc.d/${_name}"
74 if [ -x "${fqp}" ]; then
75 return 0
76 else
77 usage "${fqp} does not exist"
78 return 1
82 is_valid()
84 # Returns true if $1 appears to be a valid NetBSD
85 # rc script.
87 _name=$1
88 fqp="/etc/rc.d/${_name}"
89 if ! grep -s '. /etc/rc.subr' ${fqp} > /dev/null 2>&1; then
90 usage "${fqp} does not appear to be a NetBSD rc script"
91 return 1
92 elif ! grep -s '# KEYWORD:' ${fqp} > /dev/null 2>&1; then
93 if [ ${force} -ne 1 ]; then
94 usage "${fqp} doesn't contain a KEYWORD directive. Use -f"
95 else
96 return 1
98 else
99 is_chkconfig=`grep -s '# KEYWORD:' ${fqp}|grep -s ${KEYWORD}`
100 if [ "${is_chkconfig}" ]; then
101 return 0
102 else
103 if [ ${force} -ne 1 ]; then
104 usage "${fqp} not under chkconfig control. Use -f"
105 else
106 return 1
110 return 1
113 add_keyword()
115 # Adds the 'chkconfig' keyword to $1 if it is not
116 # there already, returning a 0. Otherwise exits
117 # with an appropriate usage error.
119 _name=$1
120 fqp="/etc/rc.d/${_name}"
121 if is_valid ${_name}; then
122 usage "${fqp} is already managed by chkconfig."
123 else
124 echo '# KEYWORD: chkconfig' >> ${fqp}
125 return 0
129 usage()
131 # Print a (hopefully) useful usage message and exit nonzero.
132 # We don't make use of err() from rc.subr because we
133 # don't want error messages going to syslog.
135 _err=$1
136 echo "Error: ${_err}"
137 echo "usage: $0 flag"
138 echo " $0 flag [ on | off ] "
139 echo " $0 [-f] flag [ on | off ]"
140 exit 1
143 on()
145 _name=$1
146 if [ ${force} -eq 1 ]; then
147 add_keyword ${_name}
150 if is_valid ${_name}; then
151 output="/etc/rc.conf.d/${_name}"
152 echo "${_name}=YES" > "${output}"
154 return 0
157 off()
159 _name=$1
160 if [ ${force} -eq 1 ]; then
161 add_keyword ${_name}
164 if is_valid ${_name}; then
165 output="/etc/rc.conf.d/${_name}"
166 echo "${_name}=NO" > "${output}"
168 return 0
171 KEYWORD='chkconfig'
172 action='show'
173 force=0
175 for i
177 case $1 in
179 force=1
182 action='on'
183 break
185 off)
186 action='off'
187 break
190 usage "Invalid argument ${i}"
191 exit 1
194 rcfile=${i}
196 esac
197 shift
198 done
200 case ${action} in
201 show)
202 if [ ${force} -eq 1 ]; then
203 usage "-f flag requires 'on' or 'off'"
205 if [ ! ${rcfile} ]; then
206 printf "\tService\t\t\tState\n"
207 printf "\t=======\t\t\t=====\n"
208 for i in `(cd /etc/rc.d; rcorder -k ${KEYWORD} *)`; do
209 display ${i}
210 done
211 else
212 if exists ${rcfile} && is_valid ${rcfile}; then
213 display ${rcfile}
214 exit $?
215 else
216 usage "Invalid rcfile: ${rcfile}"
219 exit 0
222 if exists ${rcfile}; then
223 on ${rcfile}
226 off)
227 if exists ${rcfile}; then
228 off ${rcfile}
231 esac