#!/bin/sh
#############################################################################
#
# ipmi:		OpenIPMI Driver helper script
#
# Authors:	Jan Safranek <jsafrane@redhat.com>
#
# Based on IPMI init script by:
#               Matt Domsch <Matt_Domsch@dell.com>
#               Chris Poblete <Chris_Poblete@dell.com>
#
# Status return code bits
# no bits set = no errors
# bit 0 set = minimum modules aren't loaded
# bit 1 set = requested feature module isn't loaded
# bit 2 set = /dev/ipmi0 (or /dev/imb if using that instead) doesn't exist
# bit 3 set = /dev/watchdog doesn't exist
# bit 4 set = lockfile doesn't exist
# bit 5 set = modules are loaded even when asked to be unloaded

CONFIGFILE=/etc/sysconfig/ipmi
# source config info
[ -r ${CONFIGFILE} ] && . ${CONFIGFILE}

#############################################################################
# GLOBALS
#############################################################################
MODULE_NAME="ipmi"
INTF_NUM=0

IPMI_SMB_MODULE_NAME="ipmi_smb"
IPMI_SI_MODULE_NAME="ipmi_si"

MODULES_INTERFACES=""
[ "${DEV_IPMI}" = "yes" ] && MODULES_INTERFACES="${MODULES_INTERFACES} ipmi_devintf"
[ "${IPMI_IMB}" = "yes" ] && MODULES_INTERFACES="${MODULES_INTERFACES} ipmi_imb"

MODULES_FEATURES=""
[ "${IPMI_WATCHDOG}" = "yes" ] && MODULES_FEATURES="${MODULES_FEATURES} ipmi_watchdog"
[ "${IPMI_POWEROFF}" = "yes" ] && MODULES_FEATURES="${MODULES_FEATURES} ipmi_poweroff"

MODULES_HW=""
[ "${IPMI_SI}"  = "yes" ] && MODULES_HW="${MODULES_HW} ${IPMI_SI_MODULE_NAME}"
[ "${IPMI_SMB}" = "yes" ] && MODULES_HW="${MODULES_HW} ${IPMI_SMB_MODULE_NAME}"

MODULES_BASE="ipmi_msghandler"
MODULES="${MODULES_INTERFACES} ${MODULES_FEATURES} ${MODULES_HW} ${MODULES_BASE}"

RETVAL=0
LOCKFILE=/var/lock/subsys/ipmi
DEV_IPMI_TIMEOUT=150

#############################################################################

modules_loaded_verbose()
{
	OnePlusLoaded=0
	OnePlusUnloaded=0
	for m in $@; do
		if /sbin/lsmod | grep -q $m ; then
			echo "$m module loaded."
			OnePlusLoaded=1
		else
			echo "$m module not loaded."
			OnePlusUnloaded=1
		fi
	done
}

modules_loaded()
{
	OnePlusLoaded=0
	OnePlusUnloaded=0
	for m in $@; do
		if /sbin/lsmod | grep -q $m ; then
			OnePlusLoaded=1
		else
			OnePlusUnloaded=1
		fi
	done
}

device_node_exists ()
{
	if [ -e "$1" ]; then
		echo "$1 exists."
		return 1
	fi
	echo "$1 does not exist."
	return 0
}

minimum_modules_loaded()
{
        rc_base=1
	rc_hw=1
        modules_loaded_verbose "${MODULES_BASE}"
	[ ${OnePlusLoaded} -eq 0 ] && rc_base=0
	    
	modules_loaded_verbose "${MODULES_HW}"
	[ ${OnePlusLoaded} -eq 0 ] && rc_hw=0
	
	return $((rc_base && rc_hw))
}

#############################################################################

load_si()
{
	if [ "${IPMI_SI}" = "yes" ]; then
		devType=""
		which systemd-detect-virt
		systemd_detect_virt_exit=$?
		if [ $systemd_detect_virt_exit -eq 0 ];then
			devType="$(systemd-detect-virt)"
		else
			echo "systemd-detect-virt is not exit, can't identify virtual and physical machines," \
				"modprobe ipmi_si with ports=0xca2"
		fi
		if [ "$devType" = "none" ];then
			modprobe ${IPMI_SI_MODULE_NAME} > /dev/null 2>&1
		else
			modprobe ${IPMI_SI_MODULE_NAME} ports=0xca2 > /dev/null 2>&1
		fi
		modules_loaded ${IPMI_SI_MODULE_NAME}
		[ ${OnePlusLoaded} -ne 1 ] && RETVAL=$((RETVAL | 1))
	fi
}

load_smb()
{
	if [ "${IPMI_SMB}" = "yes" ]; then
		modprobe ${IPMI_SMB_MODULE_NAME} > /dev/null 2>&1
		modules_loaded ${IPMI_SMB_MODULE_NAME}
		[ ${OnePlusLoaded} -ne 1 ] && RETVAL=$((RETVAL | 1))
	fi
}

load_hw_modules()
{
	load_si
	load_smb
}

start_watchdog_common()
{
	load_hw_modules
	modprobe ipmi_watchdog ${IPMI_WATCHDOG_OPTIONS} > /dev/null 2>&1
	modules_loaded ipmi_watchdog
	[ ${OnePlusUnloaded} -ne 0 ] &&
		RETVAL=$((RETVAL | 2)) &&
		echo "Watchdog startup failed: cannot load ipmi_watchdog module" &&
		return
}

start_watchdog_quiet()
{
	[ "${IPMI_WATCHDOG}" != "yes" ] &&
		return
	start_watchdog_common
}

start_watchdog()
{
	[ "${IPMI_WATCHDOG}" != "yes" ] &&
		RETVAL=$((RETVAL | 2)) &&
		echo "Watchdog not configured" &&
		return
	start_watchdog_common
}

stop_watchdog()
{
	modprobe -q -r ipmi_watchdog > /dev/null 2>&1
	modules_loaded ipmi_watchdog
	if [ ${OnePlusLoaded} -ne 0 ]; then
	    RETVAL=$((RETVAL | 32))
	    echo "Watchog shutdown failed: cannot unload ipmi_watchdog module"
	fi
}

stop_watchdog_quiet()
{
	modprobe -q -r ipmi_watchdog > /dev/null 2>&1
	modules_loaded ipmi_watchdog
	if [ ${OnePlusLoaded} -ne 0 ]; then
	    RETVAL=$((RETVAL | 32))
	fi
}

start_powercontrol_common()
{
	local poweroff_opts=""
	load_hw_modules
	if [ "${IPMI_POWERCYCLE}" == "yes" ]; then
	    modinfo ipmi_poweroff 2>/dev/null | grep -q poweroff_control && \
		poweroff_opts="poweroff_control=2"
	    modinfo ipmi_poweroff 2>/dev/null | grep -q poweroff_powercycle && \
		poweroff_opts="poweroff_powercycle=1"
	fi
	modprobe ipmi_poweroff "${poweroff_opts}" > /dev/null 2>&1
	modules_loaded ipmi_poweroff
	[ ${OnePlusUnloaded} -ne 0 ] &&
		RETVAL=$((RETVAL | 2)) &&
		echo "Powercontroll startup failed: cannot load ipmi_poweroff module" &&
		return
}

start_powercontrol_quiet()
{
	[ "${IPMI_POWEROFF}" != "yes" ] &&
		return
	start_powercontrol_common
}

start_powercontrol()
{
	[ "${IPMI_POWEROFF}" != "yes" ] &&
		RETVAL=$((RETVAL | 2)) &&
		echo "Powercontroll not configured" &&
		return
	start_powercontrol_common
}

stop_powercontrol()
{
	modprobe -q -r ipmi_poweroff > /dev/null 2>&1
	modules_loaded ipmi_poweroff
	if [ ${OnePlusLoaded} -ne 0 ]; then
	    RETVAL=$((RETVAL | 32))
	    echo "Powercontroll shutdown failed: cannot unload ipmi_poweroff module"
	fi
}

stop_powercontrol_quiet()
{
	modprobe -q -r ipmi_poweroff > /dev/null 2>&1
	modules_loaded ipmi_poweroff
	[ ${OnePlusLoaded} -ne 0 ] && RETVAL=$((RETVAL | 32))
}

#############################################################################
unload_all_ipmi_modules()
{
	stop_watchdog_quiet
	stop_powercontrol_quiet
	for m in ${MODULES}; do
		modprobe -q -r ${m} > /dev/null 2>&1
	done
}

unload_ipmi_modules_leave_features()
{
	for m in ${MODULES_INTERFACES}; do
		modprobe -q -r ${m} > /dev/null 2>&1
	done
	lsmod | grep -Eq "ipmi_(poweroff|watchdog)"
	if [ "$?" -ne "0" ]; then
		stop_watchdog_quiet
		stop_powercontrol_quiet
		for m in ${MODULES}; do
			modprobe -q -r ${m} > /dev/null 2>&1
		done
	fi
}

#############################################################################
load_ipmi_modules ()
{
	local locdelay
	modprobe ipmi_msghandler > /dev/null 2>&1
	modules_loaded ipmi_msghandler
	[ ${OnePlusLoaded} -ne 1 ] && unload_all_ipmi_modules && RETVAL=$((RETVAL | 1)) && return
	load_hw_modules
	[ $((RETVAL & 1)) -eq 1 ] && unload_all_ipmi_modules && RETVAL=$((RETVAL | 1)) && return

	if [ "${DEV_IPMI}" = "yes" ]; then
		modprobe ipmi_devintf > /dev/null 2>&1
		modules_loaded ipmi_devintf
		RETVAL=$((RETVAL & ~2))
		if [ ${OnePlusLoaded} -eq 1 ]; then
			# udev can take several seconds to create /dev/ipmi0, 
			# but it happens asynchronously, so delay here
			locdelay=${DEV_IPMI_TIMEOUT}
			while [ ! -e /dev/ipmi${INTF_NUM} -a ${locdelay} -gt 0 ]; do
				locdelay=$((locdelay - 1))
				sleep 0.1
			done
		else
			RETVAL=$((RETVAL | 2))
		fi
	fi

	if [ "${IPMI_IMB}" = "yes" ]; then
		modprobe ipmi_imb > /dev/null 2>&1
		modules_loaded ipmi_imb
		RETVAL=$((RETVAL & ~2))
		[ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))
		if [ ${OnePlusLoaded} -eq 1 ]; then
			DEVMAJOR=`cat /proc/devices | awk '/imb/{print $1}'`
			rm -f /dev/imb
			mknod -m 0600 /dev/imb c ${DEVMAJOR} 0 || RETVAL=$((RETVAL | 4))
		fi
	fi

	# Per Corey Minyard, essentially no one uses ipmi_radisys
	# and we don't want to encourage its further use
	# so it won't be handled here.
	return
}

#############################################################################
start()
{
	load_ipmi_modules
	if [ ${RETVAL} -eq 0 ]; then
		touch ${LOCKFILE}
	else
		if [ $((RETVAL & 1)) -eq 1 ]; then
			echo "Startup failed."
		else
			touch ${LOCKFILE} && echo "Warning!?"
		fi
	fi
	start_watchdog_quiet
	start_powercontrol_quiet
}

#############################################################################
stop()
{
	unload_ipmi_modules_leave_features
	modules_loaded ${MODULES_INTERFACES}
	if [ ${OnePlusLoaded} -ne 0 ]; then
		RETVAL=$((RETVAL | 32))
		echo "Shutdown failed, something may be in use"
	else
		rm -f ${LOCKFILE}
	fi
}

stop_all()
{
	unload_all_ipmi_modules
	modules_loaded ${MODULES}
	if [ ${OnePlusLoaded} -ne 0 ]; then
		RETVAL=$((RETVAL | 32))
		echo "Shutdown failed, something may be in use"
	else
		rm -f ${LOCKFILE}
	fi
}

#############################################################################
restart()
{
	stop_all
	RETVAL=0
	start
}

#############################################################################

reload()
{
	stop_all
	RETVAL=0
	start
}

#############################################################################

status_all()
{
	minimum_modules_loaded
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 1))
	
	modules_loaded_verbose ${MODULES_FEATURES} ${MODULES_INTERFACES}
	[ ${OnePlusUnloaded} -ne 0 ] && RETVAL=$((RETVAL | 2))

	if [ "${DEV_IPMI}" = "yes" ]; then 
	    device_node_exists /dev/ipmi${INTF_NUM}
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 4))
	fi

	if [ "${IPMI_IMB}" = "yes" ]; then
	    device_node_exists /dev/imb
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 4))
	fi

	if [ "${IPMI_WATCHDOG}" = "yes" ]; then
	    device_node_exists /dev/watchdog
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 8))
	fi

	[ ! -e ${LOCKFILE} ] && RETVAL=$((RETVAL | 16))
}

status()
{
	minimum_modules_loaded
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 1))
	
	if [ "${DEV_IPMI}" = "yes" ]; then 
	    modules_loaded_verbose ipmi_devintf
	    [ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))

	    device_node_exists /dev/ipmi${INTF_NUM}
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 4))
	fi

	if [ "${IPMI_IMB}" = "yes" ]; then
	    device_node_exists /dev/imb
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 4))
	fi
}

status_watchdog()
{
	minimum_modules_loaded
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 1))

	modules_loaded_verbose ipmi_watchdog
	[ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))

	device_node_exists /dev/watchdog
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 8))
}

status_powercontrol()
{
	minimum_modules_loaded
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 1))

	modules_loaded_verbose ipmi_powercontrol
	[ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))
}

#############################################################################
usage ()
{
	echo $"Usage: $0 {start|stop|status" 1>&2
	echo $"          restart|condrestart|try-restart|reload|force-reload" 1>&2
	echo $"          start-watchdog|stop-watchdog|status-watchdog" 1>&2
	echo $"          start-powercontrol|stop-powercontrol|status-powercontrol" 1>&2
	echo $"          stop-all|status-all}" 1>&2
	RETVAL=2
}

condrestart ()
{
	[ -e ${LOCKFILE} ] && restart
}

#############################################################################
# MAIN
#############################################################################
case "$1" in
	start) start ;;
	stop)  stop ;;
	restart) restart ;;
	force-reload) reload ;;
	reload) reload ;;
	status)	status ;;
	status-all) status_all ;;
	condrestart) condrestart ;;
	try-restart) condrestart ;;
	start-watchdog) start_watchdog ;;
	stop-watchdog) stop_watchdog ;;
	status-watchdog) status_watchdog ;;
	start-powercontrol) start_powercontrol ;;
	stop-powercontrol) stop_powercontrol ;;
	status-powercontrol) status_powercontrol ;;
	stop-all) stop_all ;;
	*) usage ;;
esac

exit ${RETVAL}

#############################################################################
# end of file
#############################################################################

