OpenSolaris_b135/pkgdefs/SUNWfcprtr/preinstall

#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
MFSTSCAN=/lib/svc/bin/mfstscan
SVCCFG=/usr/sbin/svccfg
SVCADM=/usr/sbin/svcadm
SVCPROP=/usr/bin/svcprop

STOP_DELAY=60

MANIFEST=/var/svc/manifest/network/fcoe_config.xml
MANIFEST_DEATH=/etc/svc/fcoe_config.xml.death
SVCINST=svc:/network/fcoe_config:default
DEATHROW_FILE=/etc/svc/deathrow
FCPRTRPKG=SUNWfcprtr

#
# Helper function. Delete the manifest hash value.
#

svc_delhash()
{
	$SVCCFG delhash -d $MANIFEST >/dev/null 2>&1
	if [ "$?" != "0" ];then
		# this Solaris release doesn't have delhash command
		pg_name=`$MFSTSCAN -t $MANIFEST`
		if $SVCPROP -q -p $pg_name smf/manifest; then
			$SVCCFG -s smf/manifest delpg $pg_name
		fi
	fi
}

#
# Helper function. Handle services deathrow file.
#
svc_deathrow()
{
	DEATHROW_FILE=${PKG_INSTALL_ROOT}/etc/svc/deathrow
	MANIFEST_FILE=${PKG_INSTALL_ROOT}${MANIFEST}
	MANIFEST_FILE_DEATH=${PKG_INSTALL_ROOT}${MANIFEST_DEATH}

	ENTITIES=`SVCCFG_NOVALIDATE=1 $SVCCFG inventory ${MANIFEST_FILE}`
	for fmri in $ENTITIES; do
		# add to service deathrow file
		echo ${fmri} ${MANIFEST_DEATH} ${FCPRTRPKG} >> ${DEATHROW_FILE}
	done
	mv ${MANIFEST_FILE} ${MANIFEST_FILE_DEATH}
}

#
# Helper function. To hard code a deathrow file.
#
svc_deathrow_hardcode()
{
	DEATHROW_FILE=${PKG_INSTALL_ROOT}/etc/svc/deathrow

	echo "svc:/network/fcoe_config:default ${MANIFEST_DEATH} ${FCPRTRPKG}" >> ${DEATHROW_FILE}
	echo "svc:/network/fcoe_config ${MANIFEST_DEATH} ${FCPRTRPKG}" >> ${DEATHROW_FILE}
}

#
# Helper function. Wait the service to be disabled.
#
wait_disable() {
	while [ ${nsec:=0} -lt $STOP_DELAY ]; do
		state=`$SVCPROP -p restarter/state $SVCINST`
		if [ "$state" = "disabled" -o "$state" = "maintenance" ]; then
			nstate=`$SVCPROP -p restarter/next_state $SVCINST`
			if [ "$nstate" = "none" ]; then
				return 0
			fi
		fi
		/usr/bin/sleep 1
		nsec=`expr ${nsec} + 1`
	done
	return 1
}

if [ ! -f ${PKG_INSTALL_ROOT}${MANIFEST} ]; then
	#
	# Manifest file may get deleted before the running of this script
	# e.g., during upgrade
	#
	svc_deathrow_hardcode
	exit 0
fi

old_fmri=no

ENTITIES=`$SVCCFG inventory ${PKG_INSTALL_ROOT}${MANIFEST}`
for fmri in $ENTITIES; do
	if [ "$fmri" = "$SVCINST" ]; then
		old_fmri=yes
	fi
done

if [ "$old_fmri" = "no" ]; then
	# nothing to do as there is no old fmri
	exit 0
fi

#
# To remove the old fmri
#
if [ -r /etc/svc/volatile/repository_door ]; then
	smf_alive=yes
else
	smf_alive=no
fi

if [ "$PKG_INSTALL_ROOT" != "" -a "$PKG_INSTALL_ROOT" != "/" ]; then
	smf_alive=no
fi

if [ "$smf_alive" = "no" ]; then
	svc_deathrow
else
	ENTITIES=`$SVCCFG inventory $MANIFEST`
	for fmri in $ENTITIES; do
		# If this fmri refers to an instance, or a service
		$SVCPROP -p restarter/state $fmri >/dev/null 2>/dev/null
		if [ $? -eq 1 ]; then
			# a service
			$SVCCFG delete $fmri 2>/dev/null
			continue
		fi

		#
		# Disable the instance
		#
		$SVCADM disable $fmri 2>/dev/null
		if [ $? -eq 0 ]; then
			wait_disable
			if [ $? -eq 0 ]; then
				#disabled
				$SVCCFG delete -f $fmri 2>/dev/null
				continue
			fi
		fi

		#
		# Forcily removal
		#
		ctid=`$SVCPROP -p restarter/contract $fmri 2>/dev/null`
		tctid=`$SVCPROP -p restarter/transient_contract $fmri 2>/dev/null`

		$SVCCFG delete -f $fmri

		#
		# Kill remaining processes.
		#
		if [ -n "${tctid}" -a "${ctid}" -gt 1 ]; then
			# kill the stopper
			/usr/bin/pkill -9 -c $tctid
		fi
		if [ -n "${ctid}" -a "{ctid}" -gt 1 ]; then
			# kill remaining processes
			/usr/bin/pkill -9 -c $ctid
		fi
	done
	#
	# Delete manifest hash value
	#
	svc_delhash
	#
	# Nail it down
	#
	svc_deathrow
fi