#! /bin/sh
#
# chkconfig:		12345 89 89
# description:		A software watchdog
# rc file author:	Marc Merlin <marcsoft@merlins.org>
#			Henning P. Schmiedehausen <hps@tanstaafl.de>
# Note that even though chkconfig says that this should be run at runlevel 1,
# RH by default won't do this, so the RPM applies an ugly patch to
# /etc/rc.d/init.d/single so that if you go from RL 3 to RL 1, watchdog is
# restarted anyway (if it's not, it can cause the kernel to reboot your machine
# depending on whether your kernel was compiled with CONFIG_WATCHDOG_NOWAYOUT)
#
# I have filed a bug with RH about this, and I hope they will change their
# single script to allow for other scripts to be run in RL 1.

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

[ -x /usr/sbin/watchdog -a -e /etc/watchdog.conf ] || exit 0

WATCHDOG_OOM_ADJUST="-1000"
VERBOSE="no"
if [ -f /etc/sysconfig/watchdog ]; then
	. /etc/sysconfig/watchdog
fi

adjust_oom() {
	if [ -e /var/run/watchdog.pid ]; then
		for pid in $(cat /var/run/watchdog.pid); do
			if [ -w "/proc/$pid/oom_score_adj" ]; then
				echo "$WATCHDOG_OOM_ADJUST" > "/proc/$pid/oom_score_adj" 2> /dev/null || :
			fi
		done
	fi
}

start() {
  	if [ ! -f /var/lock/subsys/watchdog ]; then
		msg_starting watchdog
		busy
		if [ -z "$WATCHDOG_MODULES" ]; then
			# preload software module
			modprobe -s softdog > /dev/null 2>&1
		else
			for module in $WATCHDOG_MODULES; do
				if [ "$module" = "ipmi_watchdog" ]; then
					modprobe -s ipmi_si > /dev/null 2>&1
					modprobe -s ipmi_devintf > /dev/null 2>&1
				fi
				modprobe -s $module > /dev/null 2>&1
			done
		fi

		if [ "${VERBOSE}" = "yes" ]; then
			daemon watchdog -v
		else
			daemon watchdog
		fi
		RETVAL=$?
		adjust_oom
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/watchdog
	else
		msg_already_running watchdog
	fi
}

stop() {
  	if [ -f /var/lock/subsys/watchdog ]; then
		msg_stopping watchdog
		killproc watchdog
		# If you compiled your kernel with CONFIG_WATCHDOG_NOWAYOUT, you may
		# not want  to remove the module  as sometimes /etc/rc.d/init.d/halt
		# will hang on umounting some remote nfs partition or for some other
		# reason, and you may then want the kernel to reboot by itself.
		# However, this means that if you stop watchdog, your system has one
		# minute to reboot cleanly, or it will be rebooted by the kernel. If
		# this behavior  isn't what you  want, just uncomment  the following
		# lines
		if [ "$WATCHDOG_UNLOAD" = "yes" ]; then
			if [ -z "$WATCHDOG_MODULES" ]; then
				# try to unload software module
				rmmod -s softdog > /dev/null 2>&1
			else
				for module in $WATCHDOG_MODULES; do
					rmmod -s $module > /dev/null 2>&1
				done
			fi
		fi

		rm -f /var/lock/subsys/watchdog >/dev/null 2>&1
	else
		msg_not_running watchdog
	fi
}

condrestart() {
	if [ -f /var/lock/subsys/watchdog ]; then
		stop
		start
	else
		msg_not_running watchdog
		RETVAL=$1
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|force-reload)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
	status watchdog
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
