#!/bin/ksh

#
# We need to take special care when updating fmd to ensure that the daemon
# always sees a consistent view of the world.  To do that, we temporarily
# disable fmd while backing out.  When backout is complete, we re-enable it.
#
# Backout is also special in that we may end up with an old (relative to
# this patch) DE trying to use state created by the DE being backed out.
# Down that road lies madness, so we're going to clear out DE state.  At
# present, this only applies to the cpumem-diagnosis DE.  If and when other
# DEs are added to the patch, their state should be erased as well.
#

SVCNAME=svc:/system/fmd:default

STOPFMD=1

DES2ERASE=cpumem-diagnosis      # list of DEs whose state is to be erased

if [ "$ROOTDIR" != "/" ]; then
        # This patch is being applied to an alternate root, which means that
        # we're not patching objects belonging to a running fmd.
        STOPFMD=0
fi

if [ $STOPFMD -eq 1 ] && ! svcs $SVCNAME >/dev/null 2>&1 ; then
        # fmd isn't registered as a service on this machine.  Either this
        # is a non-global zone, or something strange is going on.  Don't
        # attempt to disable it.
        STOPFMD=0
fi

if [ $STOPFMD -eq 1 ] ; then
        echo 'Temporarily disabling fmd(1M)'
        svcadm disable -t -s $SVCNAME
        if [ $? -ne 0 ] ; then
                echo "failed to disable fmd service for patch" >&2
                exit 1
        fi
fi

for de in $DES2ERASE ; do
        DEDIR=$ROOTDIR/var/fm/fmd/ckpt/$de

        if [ -d $DEDIR ] && [ $DEDIR/* != "$DEDIR/*" ] ; then
                echo "Resetting diagnosis engine state ($de)"
                rm $DEDIR/*
        fi
done

exit 0
