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

# Usage: filesystem_full [-c caller_string] [-w]
#
# Exits 99 if any of the file systems holding
# the directories /var/opt/SUNWcluster/run, $CLUSTERVAR are full.
#
# Exits 0 means all okay.
#
# Other non-zero exit means other unknown problem.
#
# The optional arguments are used only when file system full is found.
# They mean:
#    -c call_string	Use caller_string (one shell word) in the error message
#			that is issued.
#    -w			After printing the error, block waiting until space
#			is available, before exitting 99.
#
# Make sure that $CLUSTERVAR is not full.
# If it's full, then it is possible that the fault probe
# that called us got a false positive, i.e., that the lack of
# space in $CLUSTERVAR caused some shell script or program to 
# compute a bogus result.  
# For example, a script might redirect the output of a probe
# into a temporary file and then grep that file: but if the
# file couldn't get created, the conclusion from the grep may
# be invalid.

usagehalt()
{
    echo "Usage: ${prog} [-c caller_string] [-s sleepsecs]"
    exit 1
}

# Usage: try doprinterror
# Try the no space test just once.  Iff doprinterror==1, print an
# error message.  Return 99 for no space, 0 for have space.
#
try()
{
    typeset doprint nospacedirs dir checknospace 
    doprint=$1
    nospacedirs="${CLUSTERVAR}"
    for dir in ${nospacedirs} 
    do
	if [ -r /bin/sh ]; then
	    checknospace=${dir}/${prog}.$$
	    /bin/cp -p /bin/sh ${checknospace} 2>/dev/null
	    cprc=$?
	    /bin/rm -f ${checknospace} >/dev/null 2>&1
	    if [ ${cprc} -ne 0 ]; then
	        if [ ${doprint} -eq 1 ]; then
		    echo "File system for ${dir} may be full, so ${callerstr} fault probe is not taking any action"
		fi
		return 99
	    fi
	fi
    done
    return 0
}


# 
# Main program
#

prog=`/bin/basename $0`
exitrc=0

FMBIN=/opt/SUNWcluster/ha/nfs
. ${FMBIN}/nfs_common_util 

callerstr="calling"
dowait=0
while getopts c:w C
do
    case $C in
    c )	    callerstr=${OPTARG} ;;
    w )	    dowait=1 ;;
    \?)	    usagehalt ;;
    esac
done

try 1
if [ $? -eq 99 ]; then
    exitrc=99
    if [ ${dowait} -eq 1 ]; then
	while true ; do
	    sleep 600
	    if try 0 ; then
		break
	    fi
	done
    fi
fi

exit ${exitrc}
