Home | History | Annotate | Download | only in common_files
      1 #
      2 # Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
      3 # Use is subject to license terms.
      4 #
      5 # CDDL HEADER START
      6 #
      7 # The contents of this file are subject to the terms of the
      8 # Common Development and Distribution License (the "License").
      9 # You may not use this file except in compliance with the License.
     10 #
     11 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     12 # or http://www.opensolaris.org/os/licensing.
     13 # See the License for the specific language governing permissions
     14 # and limitations under the License.
     15 #
     16 # When distributing Covered Code, include this CDDL HEADER in each
     17 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     18 # If applicable, add the following below this CDDL HEADER, with the
     19 # fields enclosed by brackets "[]" replaced with your own identifying
     20 # information: Portions Copyright [yyyy] [name of copyright owner]
     21 #
     22 # CDDL HEADER END
     23 #
     24 # ident	"%Z%%M%	%I%	%E% SMI"
     25 #
     26 
     27 # proc.drv_utils -- common code for driver add/remove
     28 #
     29 # pkg_drvadd - add a driver, has the same syntax as add_drv(1M)
     30 #
     31 #  EXAMPLES:
     32 #    pkg_drvadd mydrv
     33 #    pkg_drvadd -i '"pciex0000,0001"' mydrv
     34 #    pkg_drvadd -i '"pciex0000,0001"' -m '* 0666 root sys' mydrv
     35 #    pkg_drvadd -i '"pciex0000,0001" "pciex0000,0002"' mydrv
     36 #    ALIASES1='"pciex0000,0001" "pciex0000,0002"'
     37 #    ALIASES2='"pciex0000,0003" "pciex0000,0004"'
     38 #    pkg_drvadd -n -b "/" -m '* 0666 root sys' -c scsi \
     39 #	-i "'$ALIASES1 $ALIASES2'" -v  mydrv
     40 #
     41 # pkg_drvrem - remove list of drivers
     42 #   ARG1-ARG[last] = driver names
     43 #  e.g.
     44 #    pkg_drvrem mydrv1 mydrv2
     45 #
     46 # pkg_drvadd and pkg_drvrem will perform necessary clean-up and
     47 # return a non-zero exit code on a fatal failure.
     48 #
     49 
     50 PATH="/usr/bin:/usr/sbin:${PATH}"
     51 export PATH
     52 
     53 # setup the default basedir, can be overridden with -b
     54 if [ "${BASEDIR:=/}" != "/" ]
     55 then
     56 	BD_OPT="-b ${BASEDIR}"
     57 fi
     58 BD_OPT_DEFAULT="${BD_OPT}"
     59 BASEDIR_DEFAULT="${BASEDIR}"
     60 
     61 # returns 1 if driver not already installed
     62 not_installed() {
     63 	DRIVER=$1
     64 	grep "^${DRIVER} " ${BASEDIR}/etc/name_to_major > /dev/null 2>&1
     65 	if [ "$?" -eq 0 ]; then
     66 		return 1
     67 	else
     68 		return 0
     69 	fi
     70 }
     71 
     72 # get the driver name from the args
     73 driver_name() {
     74 	shift `expr $# - 1`
     75 	DRV=$1
     76 }
     77 
     78 # see if the basedir was overriden in the args
     79 basedir_override() {
     80 	OPTIND=1
     81 	while getopts fnvb:c:i:m:p:P: OPT 2>/dev/null; do
     82 		case "${OPT}" in
     83 		b)	BASEDIR="${OPTARG}"
     84 			BD_OPT="-b \"${OPTARG}\""
     85 			OPTIND=1
     86 			return
     87 			;;
     88 		esac
     89 	done
     90 	OPTIND=1
     91 
     92 	# if we get this far, reset to the default
     93 	BASEDIR="${BASEDIR_DEFAULT}"
     94 	BD_OPT="${BD_OPT_DEFAULT}"
     95 }
     96 
     97 pkg_drvadd() {
     98 	STATUS=0
     99 
    100 	driver_name "$@"
    101 	basedir_override "$@"
    102 
    103 	# if the driver isn't installed, we'll add_drv. If it's already
    104 	# installed (i.e. upgrade), we'll update_drv -a
    105 	if not_installed ${DRV} ; then
    106 		CMD="add_drv"
    107 		UPDATE=0
    108 		CHECK_ADD=0
    109 	else
    110 		CMD="update_drv"
    111 		UPDATE=1
    112 		CHECK_ADD=1
    113 	fi
    114 
    115 	CMD="${CMD} ${BD_OPT}"
    116 
    117 	while getopts fnvb:c:i:m:p:P: OPT 2>/dev/null; do
    118 		case "${OPT}" in
    119 		# -c is only supported in add_drv
    120 		c)	if [ ${UPDATE} -eq 0 ]; then
    121 				CMD="${CMD} -c ${OPTARG}"
    122 			fi
    123 			;;
    124 		# -n is only supported in add_drv
    125 		n)	if [ ${UPDATE} -eq 0 ]; then
    126 				CMD="${CMD} -${OPT}"
    127 			fi
    128 			;;
    129 		f | v)	CMD="${CMD} -${OPT}"
    130 			;;
    131 		m | p | P)
    132 			if [ ${CHECK_ADD} -eq 1 ]; then
    133 				CMD="${CMD} -a"
    134 				CHECK_ADD=0
    135 			fi
    136 			CMD="${CMD} -${OPT} \"${OPTARG}\""
    137 			;;
    138 		# only use the aliases which aren't already in
    139 		# etc/driver_aliases
    140 		i)	TMPALIAS=`echo ${OPTARG} | /usr/bin/sed -e "s/'//g"`
    141 			ALIASDIR="${BASEDIR}"/etc/driver_aliases
    142 			FOUND=0
    143 			for I in ${TMPALIAS}; do
    144 				/usr/bin/egrep -s "^${DRV}[  ]+${I}" ${ALIASDIR}
    145 				if [ $? -ne 0 ]; then
    146 					if [ ${CHECK_ADD} -eq 1 ]; then
    147 						CMD="${CMD} -a"
    148 						CHECK_ADD=0
    149 					fi
    150 					if [ ${FOUND} -eq 0 ]; then
    151 						CMD="${CMD} -i '"
    152 						FOUND=1
    153 					fi
    154 					CMD="${CMD} ${I}"
    155 				fi
    156 			done
    157 			if [ ${FOUND} -ne 0 ]; then
    158 				CMD="${CMD}'"
    159 			fi
    160 			;;
    161 		\?)	echo "pkg_drvadd(): Unsupported option -${OPT}"
    162 			return
    163 			;;
    164 		esac
    165 	done
    166 
    167 	CMD="${CMD} ${DRV}"
    168 	eval ${CMD}
    169 	if [ $? -ne 0 ]; then
    170 		echo "pkg_drvadd(): Failed \"${CMD}\"!\n" >&2
    171 		STATUS=1
    172 	fi
    173 
    174 	return ${STATUS}
    175 }
    176 
    177 pkg_drvrem() {
    178 	STATUS=0
    179 	CMD="rem_drv"
    180 
    181 	basedir_override "$@"
    182 	CMD="${CMD} ${BD_OPT}"
    183 
    184 	if [ "$1" =  "-b" ]; then
    185 		shift 2
    186 	fi
    187 
    188 	while [ $# -ne 0  ]
    189 	do
    190 		DRV=$1
    191 		if not_installed ${DRV} ; then
    192 			echo "driver ${DRV} not installed"
    193 		else
    194 			eval "${CMD} ${DRV}"
    195 			if [ $? -ne 0 ]; then
    196 				echo "pkg_drvrem(): Failed \"${CMD} ${DRV}\"!\n" >&2
    197 				STATUS=1
    198 			fi
    199 		fi
    200 		shift
    201 	done
    202 
    203 	return ${STATUS}
    204 }
    205