#!/bin/sh
#
# gssd		Start up and shut down RPCSEC GSS daemon
#
# chkconfig:	345 15 81
# description:	Starts user-level daemon that manages RPCSEC GSS contexts \
#		for the NFSv4 client.

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

# Get network config
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "RPC gssd"
		exit 1
	fi
else
	exit 0
fi

if [ "$1" != "stop" ]; then
	check_portmapper || { nls "Error: portmap isn't running" && exit 0; }
fi

# Get service config
[ -f /etc/sysconfig/nfsfs ] && . /etc/sysconfig/nfsfs

is_yes "${SECURE_NFS}" || exit 0

# List of kernel modules to load
[ -z "${SECURE_NFS_MODS}" ] && SECURE_NFS_MODS="des rpcsec_gss_krb5"

# /usr may be on NFS, fail silently, nfsfs will start it
[ -x /usr/sbin/rpc.gssd ] || exit 0

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/gssd ]; then
		if ! grep -q rpc_pipefs /proc/mounts ; then
			modprobe sunrpc >/dev/null 2>&1
			run_cmd "Mounting /var/lib/nfs/rpc_pipefs filesystem" \
				mount -t rpc_pipefs rpc_pipefs /var/lib/nfs/rpc_pipefs
		fi
		# Load rpcsec modules
		for i in ${SECURE_NFS_MODS} ; do 
			_modprobe single die $i
		done
		msg_starting "RPC gssd"
		daemon rpc.gssd $RPCGSSOPTIONS
		RETVAL=$?
		[ $RETVAL = 0 ] && touch /var/lock/subsys/gssd
	else
		msg_already_running "RPC gssd"
	fi
}

stop() {
	if [ -f /var/lock/subsys/gssd ]; then
		msg_stopping "RPC gssd"
		killproc rpc.gssd
		rm -f /var/lock/subsys/gssd
	else
		msg_not_running "RPC gssd"
	fi
}

# See how we were called.
RETVAL=0
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  status)
	status rpc.gssd
	RETVAL=$?
	;;
  restart|reload)
	stop
	start
	;;
  *)
	msg_usage "$0 {start|stop|restart|status}"
	exit 1
esac

exit $RETVAL
