xref: /netbsd/sbin/chkconfig/chkconfig.sh (revision b635f565)
1#!/bin/sh
2#
3#	$NetBSD: chkconfig.sh,v 1.3 2004/01/05 23:23:32 jmmv Exp $
4#
5# Copyright (c) 2001 Zembu Labs, Inc.
6# All rights reserved.
7#
8# Author: Dan Mercer <dmercer@zembu.com>
9#
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.
24#
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.
35#
36
37# chkconfig - configuration state checker
38#
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.
43#
44# chkconfig makes use of the '-k' flag to rcorder. It will not work
45# with versions of rcorder that do not support '-k'.
46#
47# Dan Mercer <dmercer@zembu.com>
48
49. /etc/rc.subr
50
51display()
52{
53	# output $1 with 'on' or 'off' based on the return of checkyesno()
54	# Returns 0 for yes, 1 for no.
55
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
64	fi
65}
66
67exists()
68{
69	# Returns true if an executable named $1 exists
70	# in /etc/rc.d/
71
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
79	fi
80}
81
82is_valid()
83{
84	# Returns true if $1 appears to be a valid NetBSD
85	# rc script.
86
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
97		fi
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
107			fi
108		fi
109	fi
110	return 1
111}
112
113add_keyword()
114{
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.
118
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
126	fi
127}
128
129usage()
130{
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.
134
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
141}
142
143on()
144{
145	_name=$1
146	if [ ${force} -eq 1 ]; then
147		add_keyword ${_name}
148	fi
149
150	if is_valid ${_name}; then
151		output="/etc/rc.conf.d/${_name}"
152		echo "${_name}=YES" > "${output}"
153	fi
154	return 0
155}
156
157off()
158{
159	_name=$1
160	if [ ${force} -eq 1 ]; then
161		add_keyword ${_name}
162	fi
163
164	if is_valid ${_name}; then
165		output="/etc/rc.conf.d/${_name}"
166		echo "${_name}=NO" > "${output}"
167	fi
168	return 0
169}
170
171KEYWORD='chkconfig'
172action='show'
173force=0
174
175for i
176do
177	case $1 in
178	-f)
179		force=1
180		;;
181	on)
182		action='on'
183		break
184		;;
185	off)
186		action='off'
187		break
188		;;
189	-*)
190		usage "Invalid argument ${i}"
191		exit 1
192		;;
193	*)
194		rcfile=${i}
195		;;
196	esac
197	shift
198done
199
200case ${action} in
201	show)
202		if [ ${force} -eq 1 ]; then
203			usage "-f flag requires 'on' or 'off'"
204		fi
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}"
217			fi
218		fi
219		exit 0
220		;;
221	on)
222		if exists ${rcfile}; then
223			on ${rcfile}
224		fi
225		;;
226	off)
227		if exists ${rcfile}; then
228			off ${rcfile}
229		fi
230		;;
231esac
232