#!/bin/bash
# 
# mdmdp		This starts, stops, and reloads the mdmpd-based
#		multipath device monitoring and management facility
#
# chkconfig: - 15 85
# description: multipath device monitoring and management
#
# Copyright 2002 Red Hat, Inc.
#
### BEGIN INIT INFO
# Default-Start: 
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: Start and stop the MD multipath monitor
# Description: The mdmpd service checks the status of all MD based multipath
#	devices on the system.  If a path to a device goes down, it removes
#	that path from the md multipath array.  It also periodically attempts
#	to contact the device via the down path.  When it succeeds repeatedly,
#	it re-adds the downed path to the multipath array.  There is a roughly
#	60 second delay between when a path becomes live again and when mdmpd
#	adds it back into service.
### END INIT INFO


PATH=/sbin:/usr/sbin:$PATH
RETVAL=0

prog=mdmpd

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

# Make sure configuration file exists and has information we can use
# MAILADDR or PROGRAM or both must be set in order to run mdadm --monitor
#[ -f /etc/mdadm.conf ] || exit 0
#grep '^\(MAILADDR\|PROGRAM\) .' /etc/mdadm.conf >/dev/null 2>&1 || exit 0


usage ()
{
    echo "Usage: service $prog {start|stop|status|restart|condrestart}"
    RETVAL=1
}


start ()
{
    [ `id -u` -eq 0 ] || return 4
    [ -x /sbin/mdmpd ] || return 1
    [ -f /var/lock/subsys/$prog ] && return 0
    ulimit -S -c 0 >/dev/null 2>&1
    # clean up in case we went down unexpectedly
    rm -f /var/run/$prog/*
    echo -n $"Starting $prog: "
    daemon mdmpd
    # hack: wait for mdadm to die, assume success if it doesn't die quickly
    usleep 100000
    pid=`pidof -o $$ -o $PPID -o %PPID -x $prog`
    if [ -n "$pid" ] ; then
	echo_success
        touch /var/lock/subsys/$prog
	echo "$pid" > /var/run/$prog.pid
	RETVAL=0
    else
	echo_failure
	RETVAL=1
    fi
    echo
}

stop ()
{
    [ `id -u` -eq 0 ] || return 4
    [ ! -f /var/lock/subsys/$prog ] && return 0
    echo -n $"Stopping $prog: "
    killproc $prog
    echo
    rm -f /var/run/$prog.pid
    rm -f /var/lock/subsys/$prog
}

restart ()
{
    stop
    start
}

condrestart ()
{
    [ -e /var/lock/subsys/$prog ] && restart || return 0
}


case "$1" in
    start) start; RETVAL=$? ;;
    stop) stop; RETVAL=$? ;;
    status) status $prog; RETVAL=$? ;;
    restart|force-reload) restart; RETVAL=$? ;;
    condrestart|try-restart|reload) condrestart; RETVAL=$? ;;
    *) usage; RETVAL=2 ;;
esac

exit $RETVAL
