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