Home | History | Annotate | Download | only in common_files
      1 #
      2 # Copyright 2009 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 
     25 # proc.drv_utils -- common code for driver add/remove
     26 #
     27 # pkg_drvadd - add a driver, has the same syntax as add_drv(1M)
     28 #
     29 #  EXAMPLES:
     30 #    pkg_drvadd mydrv
     31 #    pkg_drvadd -i '"pciex0000,0001"' mydrv
     32 #    pkg_drvadd -i '"pciex0000,0001"' -m '* 0666 root sys' mydrv
     33 #    pkg_drvadd -i '"pciex0000,0001" "pciex0000,0002"' mydrv
     34 #    ALIASES1='"pciex0000,0001" "pciex0000,0002"'
     35 #    ALIASES2='"pciex0000,0003" "pciex0000,0004"'
     36 #    pkg_drvadd -n -b "/" -m '* 0666 root sys' -c scsi \
     37 #	-i "'$ALIASES1 $ALIASES2'" -v  mydrv
     38 #
     39 # pkg_drvrem - remove list of drivers
     40 #   ARG1-ARG[last] = driver names
     41 #  e.g.
     42 #    pkg_drvrem mydrv1 mydrv2
     43 #
     44 # pkg_drvadd and pkg_drvrem will perform necessary clean-up and
     45 # return a non-zero exit code on a fatal failure.
     46 #
     47 
     48 PATH="/usr/bin:/usr/sbin:${PATH}"
     49 export PATH
     50 
     51 # setup the default basedir, can be overridden with -b
     52 if [ "${BASEDIR:=/}" != "/" ]
     53 then
     54 	BD_OPT="-b ${BASEDIR}"
     55 fi
     56 BD_OPT_DEFAULT="${BD_OPT}"
     57 BASEDIR_DEFAULT="${BASEDIR}"
     58 
     59 # returns 1 if driver not already installed
     60 not_installed() {
     61 	DRIVER=$1
     62 	grep "^${DRIVER} " ${BASEDIR}/etc/name_to_major > /dev/null 2>&1
     63 	if [ "$?" -eq 0 ]; then
     64 		return 1
     65 	else
     66 		return 0
     67 	fi
     68 }
     69 
     70 # get the driver name from the args
     71 driver_name() {
     72 	shift `expr $# - 1`
     73 	DRV=$1
     74 }
     75 
     76 # see if the basedir was overriden in the args
     77 basedir_override() {
     78 	OPTIND=1
     79 	while getopts fnvb:c:i:m:p:P: OPT 2>/dev/null; do
     80 		case "${OPT}" in
     81 		b)	BASEDIR="${OPTARG}"
     82 			BD_OPT="-b \"${OPTARG}\""
     83 			OPTIND=1
     84 			return
     85 			;;
     86 		esac
     87 	done
     88 	OPTIND=1
     89 
     90 	# if we get this far, reset to the default
     91 	BASEDIR="${BASEDIR_DEFAULT}"
     92 	BD_OPT="${BD_OPT_DEFAULT}"
     93 }
     94 
     95 pkg_drvadd() {
     96 	STATUS=0
     97 
     98 	driver_name "$@"
     99 	basedir_override "$@"
    100 
    101 	# if the driver isn't installed, we'll add_drv. If it's already
    102 	# installed (i.e. upgrade), we'll update_drv -a
    103 	if not_installed ${DRV} ; then
    104 		CMD="add_drv"
    105 		UPDATE=0
    106 		CHECK_ADD=0
    107 	else
    108 		CMD="update_drv"
    109 		UPDATE=1
    110 		CHECK_ADD=1
    111 	fi
    112 
    113 	CMD="${CMD} ${BD_OPT}"
    114 
    115 	while getopts fnvb:c:i:m:p:P: OPT 2>/dev/null; do
    116 		case "${OPT}" in
    117 		# -c is only supported in add_drv
    118 		c)	if [ ${UPDATE} -eq 0 ]; then
    119 				CMD="${CMD} -c ${OPTARG}"
    120 			fi
    121 			;;
    122 		# -n is only supported in add_drv
    123 		n)	if [ ${UPDATE} -eq 0 ]; then
    124 				CMD="${CMD} -${OPT}"
    125 			fi
    126 			;;
    127 		f | v)	CMD="${CMD} -${OPT}"
    128 			;;
    129 		m | p | P)
    130 			if [ ${CHECK_ADD} -eq 1 ]; then
    131 				CMD="${CMD} -a"
    132 				CHECK_ADD=0
    133 			fi
    134 			CMD="${CMD} -${OPT} \"${OPTARG}\""
    135 			;;
    136 		# update_drv accepts adding an existing alias without complaint
    137 		i)	if [ ${CHECK_ADD} -eq 1 ]; then
    138 				CMD="${CMD} -a"
    139 				CHECK_ADD=0
    140 			fi
    141 			CMD="${CMD} -${OPT} '${OPTARG}'"
    142 			;;
    143 		\?)	echo "pkg_drvadd(): Unsupported option -${OPT}"
    144 			return
    145 			;;
    146 		esac
    147 	done
    148 
    149 	CMD="${CMD} ${DRV}"
    150 	eval ${CMD}
    151 	if [ $? -ne 0 ]; then
    152 		echo "pkg_drvadd(): Failed \"${CMD}\"!\n" >&2
    153 		STATUS=1
    154 	fi
    155 
    156 	return ${STATUS}
    157 }
    158 
    159 pkg_drvrem() {
    160 	STATUS=0
    161 	CMD="rem_drv"
    162 
    163 	basedir_override "$@"
    164 	CMD="${CMD} ${BD_OPT}"
    165 
    166 	if [ "$1" =  "-b" ]; then
    167 		shift 2
    168 	fi
    169 
    170 	while [ $# -ne 0  ]
    171 	do
    172 		DRV=$1
    173 		if not_installed ${DRV} ; then
    174 			echo "driver ${DRV} not installed"
    175 		else
    176 			eval "${CMD} ${DRV}"
    177 			if [ $? -ne 0 ]; then
    178 				echo "pkg_drvrem(): Failed \"${CMD} ${DRV}\"!\n" >&2
    179 				STATUS=1
    180 			fi
    181 		fi
    182 		shift
    183 	done
    184 
    185 	return ${STATUS}
    186 }
    187