#!/bin/sh
#
# nfslock       This shell script takes care of starting and stopping
#               the NFS file locking service.
#
# chkconfig: - 14 86
# description: NFS is a popular protocol for file sharing across \
#	       TCP/IP networks. This service provides NFS file \
#	       locking functionality.
# probe: true

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
# Check that networking is up.
SourceIfNotEmpty /etc/sysconfig/network && [ "$NETWORKING" != no ] || exit

[ -x /sbin/rpc.lockd -a -x /sbin/rpc.statd ] || exit

# Locking only for kernel < 2.2.18
OS_RELEASE=`uname --release`
OS_RELEASE_MINOR=`echo "$OS_RELEASE" | sed 's/\(^[0-9]\)\.\([0-9]*\).*/\2/'`
OS_RELEASE_VERSION=`echo "$OS_RELEASE" | sed 's/\(^[0-9]\)\.\([0-9]*\)\.\([0-9]*\).*/\3/'`
if [ "$OS_RELEASE_MINOR" -gt 2 ]; then
	NEEDLOCK=
elif [ "$OS_RELEASE_MINOR" -eq 2 -a "$OS_RELEASE_VERSION" -ge 18 ]; then
	NEEDLOCK=
else
	NEEDLOCK=1
fi

LOCKFILE=/var/lock/subsys/nfslock
RETVAL=0

start()
{
	fgrep -qs lockdctl /proc/ksyms || exit 0

	# Start daemons.
	if [ -n "$NEEDLOCK" ]; then
		echo -n "Starting NFS lockd: "
		daemon rpc.lockd
		RETVAL=$?
		echo
	fi

	echo -n "Starting NFS statd: "
	daemon rpc.statd
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	return $RETVAL
}

stop()
{
	# Stop daemons.
	if [ -n "$NEEDLOCK" ]; then
		echo -n "Shutting down NFS lockd: "
		killproc lockd
		echo
	fi

	echo -n "Shutting down NFS statd: "
	killproc rpc.statd
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	return $RETVAL
}

restart()
{
	stop
	start
}

probe()
{
	if [ ! -e "$LOCKFILE" ]; then
		echo start
		exit 0
	fi
	/sbin/pidof rpc.statd &>/dev/null; STATD="$?"
	if [ -n "$NEEDLOCK" ]; then
		/sbin/pidof lockd &>/dev/null; LOCKD="$?"
	else
		LOCKD=0
	fi
	if [ $STATD = 1 -o $LOCKD = 1 ]; then
		echo restart
		exit 0
	fi
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		if [ -n "$NEEDLOCK" ]; then
			status lockd
		fi
		status rpc.statd
		RETVAL=$?
		;;
	probe)
		probe
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
