#!/bin/ksh
#
#pragma ident   "@(#)nfs_check_my_lockd_statd.sh 1.4   01/03/28 SMI"
#Copyright (C) 1997 Sun Microsystems, Inc.
#

cleanup()
{
    /bin/rm -f $PSTMP $TMPERR
}

checkdaemon()
{
    # Usage: checkdaemon daemonName rpcName
    # daemonName is the name to search for in the ps listing
    # rpcName is the name/programnumber to use for calling rpcinfo.
    # Any problems found cause non-zero exit from entire script.
    DAEMONNAME=$1
    RPCNAME=$2

    if [ -z ${RPCNAME} ]; then
	return 0
    fi
    ${FMBIN}/net_rpcprobe -T udp ${MYHOST} ${RPCNAME} > ${TMPERR} 2>&1
    if [ $? -eq 0 ]; then
        return 0
    fi
    log_info "$pre.4500" \
		"my daemon ${DAEMONNAME} is not responding to NULL rpc"
    /bin/ps -e >${PSTMP} 2>${TMPERR}
    if [ $? -ne 0 ]; then
        cleanup
        exit 1
    fi

    /bin/grep -w ${DAEMONNAME} ${PSTMP} >/dev/null 2>&1

    if [ $? -ne 0 ]; then
        log_info "${pre}.4501" "${DAEMONNAME} is not running"

        if [ "${DAEMONNAME}" = "rpcbind" ]; then

            # rpcbind is special, restart cannot be done without doing
            # warm start dance -- see rpcbind man page.
            if [ -f /tmp/portmap.file -o -f /tmp/rpcbind.file ]; then
                log_info "${pre}.5001" \
			"warm restart seems to be in progress by human administrator"
            else
                log_info "${pre}.4502" \
			"manual reboot of ${HA_LOCALHOST} may be needed"
            fi 
        fi
    fi
    cleanup
    exit 1
}

# Get the Source of All the Common Utilities
INCLUDE=.
FMBIN=/opt/SUNWcluster/ha/nfs
${INCLUDE} ${FMBIN}/nfs_common_util

ECH_TRAPSIGNALS="1 2 3 15"
trap "cleanup ; trap 0 ; exit 1" $ECH_TRAPSIGNALS

PROG=$(/bin/basename $0)
pre="SUNWcluster.nfs_check_my_lockd_statd"
PSTMP=${tmpdir}/${PROG}.ps.$$
TMPERR=${tmpdir}/${PROG}.err.$$
MYHOST=$(/bin/uname -n)

if [ -z ${tmpdir} ]; then
	tmpdir=/var/opt/SUNWcluster/run
fi

# Issue: if rpcbind isn't running, then system isn't really usable.
# Node suicide should be an option.  For now, we are too chicken to
# automatically commit suicide.  Instead, just log.
# Also, be careful about auto suicide: rpcbind has a mechanism to terminate it
# with a checkpoint that will be used when restarting it, and an administrator
# may be in the middle of using that facility!  Just restarting rpcbind
# without a checkpoint is useless, because rpc server programs register with
# rpcbind only once when they start up, they do not ever re-register.

if [[ -z ${HA_RPCPROG_rpcbind} || -z ${HA_RPCPROG_lockd} \
		|| -z ${HA_RPCPROG_statd} ]]; then
	initnfsenv
fi

checkdaemon rpcbind ${HA_RPCPROG_rpcbind}
checkdaemon lockd ${HA_RPCPROG_lockd}
checkdaemon statd ${HA_RPCPROG_statd}

cleanup
exit 0
