#!/bin/sh
#
# ident "@(#)nfs_start_daemons.sh 1.12     03/09/02 SMI"
#
# Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# cluster/src/cmd/ha-services/nfs/nfs_start_daemons.sh
#
# Called to start statd, lockd or nfsd.
# Computes the correct way to start them with the
# right arguments, propogates status to caller.
# Called with one argument statd, lockd, nfsd
# or mountd
#
# The output of this command is redirected into a file
# useful only for debugging. So we are free to use things
# like "echo" etc. The user never sees the output. The caller
# (presumably some HANFS RT method) uses the exit code from
# here to syslog() some stuff anyways.
#
# This script is called while holding the HA-NFS state lock
# so we need not worry about it changing from under us.
#
# We need not worry about the processes in question which we
# are attempting to start, the caller has already killed them
# (with SIGKILL). You can only do so much double checking...
#
# Most of the code here is copied from HA1.3
#

HANFS_DIR=/var/run/.hanfs

if [ $# -ne 1 ]
then
	echo invalid argument list $*
	exit 1
fi

daemon=$1
hanfs_state_file=$HANFS_DIR/nfs.state
statd_args_file=$HANFS_DIR/statd.args

# To make this script to be idempotent,
# make sure hanfs_state_file exists
/usr/bin/touch $hanfs_state_file

if [ ! -r $hanfs_state_file ]
then
	echo Can not read $hanfs_state_file
	exit 1
fi

# See which daemon needs to be started up
case $daemon in
	'statd')
		# Need to assemble the -p and -a options
		rm -f $statd_args_file
		# First assemble the -p options

		awk ' {print $3} ' < $hanfs_state_file |
		sort | uniq |
		while read path
		do
        		if [ -d $path ]
        		then
                		echo "-p $path \c" >> $statd_args_file
				else
						echo invalid statmon path $path
						exit 1
        		fi
		done

		# Now the -a options. These are the ipaddresses passed to
		# statd via the -a options. The code basically parses out
		# the first 3 fields (RG, R, StatmonPath) from the state
		# File, what is left is the ipaddresses.
		#

		awk ' { for(i=4; i <= NF; i++) print $i} ' < $hanfs_state_file |
		sort | uniq |
		while read ipaddr
		do
        		echo "-a $ipaddr \c" >> $statd_args_file
		done

		# The statd.args file is created IFF there are
		# additional arguments to statd
		if [ -r $statd_args_file ]
		then
			echo statd args are `cat $statd_args_file`
        		/usr/lib/nfs/statd `cat $statd_args_file`
			rc=$?
		else
			echo No additional statd arguments
        		/usr/lib/nfs/statd
			rc=$?
		fi

		exit $rc

		;;	# End of statd case

	'nfsd')
		# The comment from HA1.3 code is copied verbatim here for
		# reference.  Note that we no longer start nfsd explicitly
		# in the TS scheduling class in order to co-exist with SRM.

		#
		#
		# Search the /etc/init.d/nfs.server command file for the line
		# that starts up the nfsd, in case the system administrator
		# has customized that line, for example, by raising the
		# number of daemon threads or by using the other nfsd
		# switches, such as what network protocols to use.
		# If there are multiple nfsd lines in the nfs.server
		# command file, just use the first.
		# Thus, if the system administrator has increased the
		# number of servers in /etc/init.d/nfs.server, we'll use
		# that value.
		# According to SMCC NFS Server Performance and Tuning Guide:
		# "The default setting, 16, in Solaris 2.4 software environments
		#  results in poor NFS response times.  Scale the setting with
		#  the number of processors and networks.  Increase the number of
		#  NFS server threads by editing the invocation of nfsd in
		#  /etc/init.d/nfs.server."
		# That is, the manual recommends to the administrator that the way
		# to tune the nfsd is to edit the /etc/init.d/nfs.server script,
		# thus, HA-NFS tries to honor any such edits.
		#

		DEFAULT_NFSDCMD="/usr/lib/nfs/nfsd -a 16"
		NFSDCMD="`egrep '^[^#]*/usr/lib/nfs/nfsd' /etc/init.d/nfs.server \
				2>/dev/null | head -1`"
		if [ -z "$NFSDCMD" ]; then
			NFSDCMD="$DEFAULT_NFSDCMD"
		fi
		echo nfsd command is $NFSDCMD
		$NFSDCMD
		rc=$?
		if [ $rc -ne 0 ]
		then
			echo nfsd start failed with $rc
			exit 1
		fi
		exit 0

		;; # End of nfsd case

	'lockd')
		# Do the same as in nfsd case by extracting the lockd startup
		# command in /etc/init.d/nfs.client
		# Strip the last '&' from the command line using sed.

		# Get the OS release. We need to differentiate between
		# solaris 8 and later solaris releases, because 
		# /etc/default/nfs facility is available on solaris 9
		# and later only.
		OS="`/usr/bin/uname -sr`"

		# Get the value of LOCKD_GRACE_PERIOD from /etc/default/nfs
		# only if the Solaris release is 5.9 and above.
		if [ "$OS" != "SunOS 5.8" ]; then
			lockd_grace_period="`egrep LOCKD_GRACE_PERIOD /etc/default/nfs \
				2>/dev/null | head -1 | awk -F= '{print $2}'`"
		fi
		LOCKDCMD="`egrep '^[^#]*/usr/lib/nfs/lockd' /etc/init.d/nfs.client\
				2>/dev/null | tail -1 | sed 's/ &/ /'`"

		# This is highly unlikely. Just in case though.
		if [ -z "$LOCKDCMD" ]; then
			LOCKDCMD="/usr/lib/nfs/lockd"
		fi

		# Check if grace period is specified. If its specified, we do
		# retain the command obtained. This is true for both S8 and S9 
		# (or later), because in S9 (or later) the value given with 
		# command line overwrites the value in /etc/default/nfs.
		echo $LOCKDCMD | grep "\-g" > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			echo Grace period not specified
			if [ "$OS" != "SunOS 5.8" ] && [ $lockd_grace_period -ne 45 ]
			then
				# if not S8 and lockd_grace_period value is 
				# not a default(45s), then use LOCKDCMD, bcoz
				# lockd automatically takes care of starting
				# with apppropriate tunables.
				echo $OS LOCKD_GRACE_PERIOD $lockd_grace_period
			else
				# Could be S8 OR S9(or later) with default
				# lock_grace_period value. In both cases, we
				# can start lockd with a 90 sec grace period 
				# as admin. did not wish to change the default.
				echo $OS
				LOCKDCMD=`echo $LOCKDCMD |
				sed 's/\/usr\/lib\/nfs\/lockd/\/usr\/lib\/nfs\/lockd -g 90/g'`
			fi
		fi
		echo lockd command is $LOCKDCMD
		eval $LOCKDCMD
		rc=$?
		if [ $rc -ne 0 ]
		then
			echo lockd start failed with $rc
			exit 1
		fi
		exit 0

		;; # End of lockd case

	'mountd')
		# No customization is needed in this case
		# simply call mountd

		/usr/lib/nfs/mountd
		rc=$?
		if [ $rc -ne 0 ]
		then
			echo mountd start failed with $rc
			exit 1
		fi
		exit 0

		;; # End of mountd case

	*)
		echo Unknown daemon $daemon
		exit 1

		;;
esac

echo Why are we here
exit 1

