Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 26 Feb 2006 16:57:14 -0800
From:      Alexander Botero-Lowry <alex@foxybanana.com>
To:        freebsd-rc@freebsd.org
Subject:   chkconfig
Message-ID:  <44024E6A.3060001@foxybanana.com>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------070109070708080802060008
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi there. A while ago I largely rewrote sbin/chkconfig from NetBSD (not 
installed by default because it's broken) to work and to be a little 
more usable in general.

While formally chkconfig(8) from netbsd would only read state from 
/etc/rc.conf.d my chkconfig will read state from any location that 
rc.subr can read from. (using checkyesno). My chkconfig still writes 
state to /etc/rc.conf.d/<name>.

It was developed with FreeBSD support in consideration (as I mostly run 
FreeBSD these days), but has never really under gone any testing, in 
part do to some issues in abi/clearvar/cleantmp which were recently fixed.

It does not use grep, or sed ,or awk, or any other tools from /usr (on 
netbsd it was in /sbin, so i thought it shouldn't use anything in /usr). 
The chkconfig(8) in netbsd cvs uses grep.

I've attached it for anyone interested and would love to get some 
feedback (even the bad kind). I'm curious if it's feasible to include it 
in the base system as well.



--------------070109070708080802060008
Content-Type: text/plain;
 name="varname.sh"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="varname.sh"

#!/bin/sh
#
# Copyright 2005-2006, Alexander Botero-Lowry
#
# Original copyright and license:
#
# Copyright (c) 2001 Zembu Labs, Inc.
# All rights reserved.
#
# Author: Dan Mercer <dmercer@zembu.com>
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#       This product includes software developed by Alexander Botero-Lowry
# 4. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission
#
# THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
# RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
# CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# display() and the service list are products of chkconfig.sh.
# chkconfig.sh falls under the following copyright.
#
# This product includes software developed by Zembu Labs, Inc.
#
# TODO: 
#  Implement sort by state
#  Find a cleaner way to change state
#
# $Id: varname.sh,v 1.8 2005/08/18 07:08:18 alex Exp $
#

. /etc/rc.subr

#
# display() 
#   $1 = path to rc script
#   $2 = verbose 
#
# Gives a basic summar of services, if $2 is defined then no
# text is given, only a return code.  
#

display() {
   load_rc_config $1

   run_rc_script $1 rcvar | while read -r line; do
       case $line in
       \$*=*)
	   var1=${line#$}
	   var2=${var1%%=*}
	   if checkyesno $var2; then
        	   if [ -z $2 ]; then
			printf "\t%-16s\t\ton\n" ${var2%_enable}
		   fi
		   return 0
   	   else 
		   if [ -z $2 ]; then
        	   	printf "\t%-16s\t\toff\n" ${var2%_enable}
		   fi
		   return 1
   	   fi
	   ;;
       esac
   done
}

#
# This function is unused, but in theory would be a good foundation for
# a function to sort by state. One would have to create two lists of
# services, on and off, and then display them in order.
#
# This method is rather ineffient.
#

display_by_state() {

load_rc_config $1

   run_rc_script $1 rcvar | while read -r line; do
       case $line in
       \$*=*)
           var1=${line#$}
           var2=${var1%%=*}
           if checkyesno $var2; then
                   if [ -z $2 ]; then
                        printf "\t%-16s\t\ton\n" ${var2%_enable}
                   fi
                   return 0
           else
                   if [ -z $2 ]; then
                        printf "\t%-16s\t\toff\n" ${var2%_enable}
                   fi
                   return 1
           fi
           ;;
       esac
   done
}

#
# change_state() 
#    $1 = rc.d script
#    $2 = state
#
# changes the state of a service from on to off. 
#

change_state() {
   filename=$1
   state=$2
   if [ "$USER" != "root" ]; then
       echo "ERROR: Root privelages are required to change the state of an rc \
script"

       exit 1
   fi 

   if [ -z $force ]; then
      echo "A state file does not exist. Use -f to create a state file." 
      exit 1
   fi

   if [ ! -d /etc/rc.conf.d ]; then
      mkdir /etc/rc.conf.d
   fi

   if [ "$state" = "on" ]; then
      mode="YES"
   else
      mode="NO"
   fi 

   load_rc_config /etc/rc.d/$filename

#
## FIXME: the vailidty of this on freebsd is questionable at best
#
   run_rc_script /etc/rc.d/$filename rcvar | while read -r line; do
       case $line in
       \$*=*)
           var1=${line#$}
           var2=${var1%%=*}
	   echo "${var2}=$mode">/etc/rc.conf.d/$filename
	   ;;
       esac
   done
}

#
# is_valid_option()
#  $1 = option
#
# Determins whether the option passed as $1 is a valid option with 
# which to change state.
#

is_valid_option() {
   if [ "$1" = "on" ]; then
      return 0
   elif [ "$1" = "off" ]; then
      return 0
   else
      return 1
  fi
}

#
# is_valid_name()
#   $1 = rc script
#
# Determins whether the given rc script is a valid rc.d script 
# does this by use of checking for rc.subr
#
# This is not optimal
#

is_valid_name() {
   if [ ! -f /etc/rc.d/$1 ]; then
      return 1
   fi

   cat /etc/rc.d/$1 | while read -r line; do
       case $line in
	     ". /etc/rc.subr")
		echo 0
		;;
       esac
   done
}

#
## FIXME: sort_by_state is broken
#

if [ "$1" = "-s" ]; then 
   sort_by_state=1
   shift
elif [ "$1" = "-f" ]; then
   force=1
   shift
fi


if [ -z $1 ] ; then
   printf "\tService\t\t\t\tState\n"
   printf "\t=======\t\t\t\t=====\n"
   rc_order_path="/etc/rc.d/*"
elif [ "`is_valid_name $1`" = "0" ]  && [ -z $2 ]; then
   rc_order_path="/etc/rc.d/$1"
   verbose=no
elif [ "`is_valid_name $1`" = "0" ] && `is_valid_option $2`; then
     change_state $1 $2
elif [ "`is_valid_name $1`" = "0" ] && ! `is_valid_option $2`; then
    echo "Valid options are on or off."
else 
   echo "The rc script: $1 is not a valid rc.d script."
   exit 1
fi

for rc in `/sbin/rcorder -s nostart $rc_order_path 2>/dev/null`; do
    if [ "`is_valid_name ${rc#/etc/rc.d/}`" = "0" ]; then
       display $rc $verbose
    fi
done


--------------070109070708080802060008--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?44024E6A.3060001>