#!/bin/ksh
#ident @(#)imha_stop_net	1.5 98/09/13 SMI
# Copyright 1998, Sun Microsystems, Inc.
# All Rights Reserved

# STOP_NET script for SIMS 3.x HA interface.  Brings SIMS down.
# Called by the HA framework with three arguments:
# $1: Comma separated list of logical hosts this physical host masters,
#     or null if service is off
# $2: Comma separated list of logical hosts this physical host is not
#     to master, or all logical hosts if this service is off.
# $3: Timeout allowed for stop, in seconds.

typeset MASTERED_HOSTS="$1"
typeset NOT_MASTERED_HOSTS="$2"
typeset -i TIMEOUT="$3"

prog_path=${0%/*}

. $prog_path/imha_common

getpids() {
    /bin/ps -ef | \
    /bin/grep "$1" | \
    /bin/grep -v /bin/grep | \
    /bin/awk '{print $2}'
}

# Following defines the number of seconds remaining in TIMEOUT allowed
# before panic sets in and we try killing imaccessd with kill -9.  This is
# brutal, but required so that imaccessd won't have files open on the shared
# disk and to prevent unmounting it.  Currently there is no way to set this
# parameter other than editing this shell script.  NOTE that any such
# editing is likely to be undone at the next upgrade or possibly even patch,
# when this script will be overwritten with the new version.

typeset -i panic=5 # Panic 5 seconds before timeout.

typeset -i timelimit=$TIMEOUT+$SECONDS-$panic

# Since "im.server stop" is idempotent in itself, we don't need to do
# any special checking, other than to make sure the scripts are available.

loglevel notice $(gettext 'HA framework requested SIMS stop.')

if [[ -f /etc/init.d/slapd ]]
then
	/etc/init.d/slapd stop
fi

if [[ -f /opt/SUNWmail/sbin/im.server ]]
then
	/opt/SUNWmail/sbin/im.server stop
fi

# Some IMTA SMTP processes may be hanging around, politely waiting
# for a remote client to finish sending mail.  Kill any such
# processes, since they will prevent the shared disk from being unmounted.

if [[ -f /tmp/SUNWimha.monitor ]]
then
	/opt/SUNWmail/sbin/imta process
fi


if [[ -f /opt/SUNWmail/sbin/imta ]]
then
	pids="$(/opt/SUNWmail/sbin/imta process |  /bin/awk '{print $2}')"
	if [[ ! -z pids ]]
	then
		loglevel debug $(printf "$(gettext 'These leftover imta processes will be killed: \"%s\"')" "$pids") ;
		/bin/kill -9 $pids
	fi
fi

if [[ -f /tmp/SUNWimha.monitor ]]
then
	/opt/SUNWmail/sbin/imta process
fi

	

imaccessd="ims/lib/imaccessd"
pids="$(getpids $imaccessd)";
while [[ ! -z "$pids" ]]
do
	sleep 1
	pids="$(getpids $imaccessd)";
	(( $timelimit < $SECONDS )) && [[ ! -z "$pids" ]] && kill -9 $pids
done

exit 0

# Local variables:
# mode:ksh
# ksh-indent:8
# ksh-group-offset:-8
# End:
