#!/bin/sh
#
# netconsole assisted netconsole module loading
#
# chkconfig:	2345 11 89
#
# description:	This initscript loads netconsole kernel module.
#
# $Id: netconsole.init,v 1.13 2011/06/06 06:22:47 arekm Exp $

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

# Get network config
. /etc/sysconfig/network

# Get service config - may override defaults
[ -f /etc/sysconfig/netconsole ] && . /etc/sysconfig/netconsole

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

# configtest itself
# must return non-zero if check failed
# output is discarded if checkconfig is ran without details
configtest() {
	# anything above 8 reports invalid argument
	if [ "$LOGLEVEL" -lt 0 -o "$LOGLEVEL" -gt 8 ]; then
		nls "Invalid kernel console loglevel."
		return 1
	fi

	if [ "$SRC_PORT" -le 0 ]; then
		nls "Invalid source port."
		return 1
	fi

	if [ ! -e "/sys/class/net/${DEVICE}" ]; then
		nls "Can't find ${DEVICE} - invalid."
		return 1
	fi

	if [ "$TGT_PORT" -le 0 ]; then
		nls "Invalid target port."
		return 1
	fi

	if [ -z "$TGT_IP" ]; then
		nls "Unspecified target address."
		return 1
	fi

	return 0
}

# wrapper for configtest
checkconfig() {
	local details=${1:-0}

	if [ $details = 1 ]; then
		# run config test and display report (status action)
		show "Checking %s configuration" "netconsole"; busy
		local out
		out=$(configtest 2>&1)
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			ok
		else
			fail
		fi
		[ "$out" ] && echo >&2 "$out"
	else
		# run config test and abort with nice message if failed
		# (for actions checking status before action).
		configtest >/dev/null 2>&1
		RETVAL=$?
		if [ $RETVAL != 0 ]; then
			show "Checking %s configuration" "netconsole"; fail
			nls 'Configuration test failed. See details with %s "checkconfig"' $0
			exit $RETVAL
		fi
	fi
}

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/netconsole ]; then
		msg_already_running "netconsole"
		return
	fi

	checkconfig

	if [ -z "${TGT_MAC}" ]; then
		LC_ALL=C /bin/ping -nq -c 3 "${TGT_IP}" -I "${DEVICE}" 1>/dev/null 2>/dev/null
		ret=$?
		# ping worked, try arp
		if [ $ret -eq 0 ]; then
			TGT_MAC="$(LC_ALL=C arp -an -i ${DEVICE} ${TGT_IP} | egrep -v 'incomplete|no match' | awk '{print $4}')"
		fi
	elif [ "${TGT_MAC}" == "broadcast" ]; then
		TGT_MAC=''
	fi

	msg_starting "netconsole"
	modprobe netconsole netconsole=${SRC_PORT}@${SRC_IP}/${DEVICE},${TGT_PORT}@${TGT_IP}/${TGT_MAC}
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
		ok
		dmesg -n ${LOGLEVEL}
		touch /var/lock/subsys/netconsole
	else
		fail
	fi
}

stop() {
	if [ ! -f /var/lock/subsys/netconsole ]; then
		msg_not_running "netconsole"
		return
	fi

	msg_stopping "netconsole"
	modprobe -r netconsole
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
		rm -f /var/lock/subsys/netconsole
		ok
	else
		fail
	fi
}

condrestart() {
	if [ ! -f /var/lock/subsys/netconsole ]; then
		msg_not_running "netconsole"
		RETVAL=$1
		return
	fi

	checkconfig
	stop
	start
}

status() {
	local res=$(lsmod | awk '$1 == "netconsole" {print $1}')
	if [ -z "$res" ]; then
		echo "netconsole kernel module not loaded"
		RETVAL=1
	else
		echo "netconsole kernel module is loaded"
	fi
}

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

exit $RETVAL
