#!/bin/bash
# vstorage hotplugd rc script
###
# chkconfig: 2345 48 80
# description: vstorage hotplug daemon
###

### BEGIN INIT INFO
# Provides: pstorage-hotplugd
# Provides: vstorage-hotplugd
# required-start: $local_fs
# required-stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO

# Source function library.
source /usr/libexec/vstorage/service-lib.sh

HOTPLUGD_BIN=/usr/bin/megaraid-mon
HOTPLUGD_NAME=megaraid-mon
[ -x "$HOTPLUGD_BIN" ] || exit 0

CONFIG_DIR="/etc/vstorage"
HOTPLUG_CONFIG="${CONFIG_DIR}/hotplug.config"

[ -f "$HOTPLUG_CONFIG" ] && source "$HOTPLUG_CONFIG"
[ "$ENABLE_HOTPLUG" = yes ] || exit 0

HOTPLUGD_OPTS=""
[ "$RAID_WRITEBACK" = yes ] && HOTPLUGD_OPTS="--writeback"

LOGFILE="/var/log/vstorage/hotplugd.log"
PID_FILE="/var/run/vstorage/hotplugd.pid"
RETVAL=0

daemonplus() {
	exec </dev/null >/dev/null 2>&1
	"$HOTPLUGD_BIN" -l "$LOGFILE" $HOTPLUGD_OPTS &
	echo $! > "$PID_FILE"
	disown -h
	exit 0
}

killprocplus() {
	SIGSPEC="$1"
	if [ -f "$PID_FILE" ]; then
		killproc -p "$PID_FILE" "$HOTPLUGD_NAME" "$SIGSPEC"
		return $?
	else
		echo_failure
		return 1
	fi
}

hotplugd_check_pid_name() {
	pid=$(cat "$PID_FILE")
	bin=$(cat /proc/"$pid"/cmdline 2>/dev/null | awk -F'\0' '{print $1}' 2>/dev/null)
	[ "$bin" = "$HOTPLUGD_BIN" ]
}

checkproc() {
	__pids_var_run ignored "$PID_FILE"
	code="$?"
	case "$code" in
		0) # program is running (likely)
			if hotplugd_check_pid_name; then
				# program is running
				return 0
			else
				# program is dead, pid file exists
				return 1
			fi
			;;
		*)
			return "$code"
			;;
	esac
}

# See how we were called.
case "$1" in
  start)
	if [ ! -e /sys/module/megaraid_sas ]; then
		modprobe megaraid_sas
		n=10
		while [ "$n" -gt 0 ]; do
			MAJOR=$(grep megaraid_sas_ioctl /proc/devices | cut -d' ' -f1)
			[ "$MAJOR" ] && break
			sleep 3
			n=$(expr "$n" - 1)
		done
	fi
	# retrigger block device events because they may appear before changes
	# in the config
	/sbin/udevadm trigger --subsystem-match=block
	echo -n "Starting $HOTPLUGD_NAME: "
	checkproc
	case $? in
		1) # program is dead, pid file exists
			rm -f "$PID_FILE"
			;;
		3) # program is not running
			;;
		0) # program is running
			echo_failure
			echo
			echo "$HOTPLUGD_NAME is already running"
			RETVAL=1
			;;
		4) # failed to read pid file
			echo_failure
			echo
			echo "failed to read pid file"
			RETVAL=1
			;;
	esac

	if [ $RETVAL -eq 0 ]; then
		daemonplus &
		wait $!
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			touch /var/lock/subsys/"$HOTPLUGD_NAME"
			echo_success
		else
			echo_failure
		fi
		echo
	fi
	;;
  stop)
	echo -n "Shutting down $HOTPLUGD_NAME: "
	killprocplus
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/"$HOTPLUGD_NAME" "$PID_FILE"
	;;
  status)
	echo -n "Checking $HOTPLUGD_NAME: "
	checkproc
	RETVAL=$?
	case $RETVAL in
		1) # program is dead, pid file exists
			echo "dead"
			;;
		3) # program is not running
			echo "not running"
			;;
		0) # program is running
			PID=$(cat "$PID_FILE" 2>/dev/null)
			echo "running, pid $PID"
			;;
		4) # failed to read pid file
			echo "failed to read pid file"
			;;
	esac
	;;
  reload)
	echo -n "Reloading $HOTPLUGD_NAME: "
	killprocplus -HUP
	RETVAL=$?
	echo
	;;
  force-reload)
	# new configuration takes effect only after restart
	$0 stop
	$0 start
	RETVAL=$?
	;;
  restart)
	$0 stop
	$0 start
	RETVAL=$?
	;;
  condrestart)
	checkproc
	if [ $? -ne 3 ]; then
		$0 stop
		$0 start
		RETVAL=$?
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|condrestart|status}"
	exit 2
esac

exit $RETVAL

