#!/bin/sh
#
# postrootpatch.sh
#
# Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      postrootpatch.sh - Post patch script for starting CRS stack after
#                         a bug has been patched
#
#    DESCRIPTION
#      Post bug patching script for starting CRS stack
#
#    NOTES
#    This is run once per node during the Oracle CRS one-off patch process.
#    This script does the following:
#    1) Copies the CRS init script to init.d for init process
#    2) Locks the CRS home
#    3) Starts the CRS stack
#    This is run during CRS installation and not during RAC
#
#    -nostart: performs only step 2
#    -startonly: performs step 1 and 3, without step 2.
# 
#    To support applying multiple patches, the -nostart option should be used
#    after each patch, and finally after the last patch the -startonly option
#    should be used. 
#
#    MODIFIED   (MM/DD/YY)
#    ksviswan   12/11/09 - Remove the new message display for auto patch to work
#    jcreight   10/21/09 - Backport jcreight_bug-6986682 from main
#    gnagiah    08/27/09 - 8531031 some files not instantiated properly
#    gnagiah    08/20/09 - 8781354 fix chmod shell buffer limits
#    jcreight   03/18/09 - Fix bug 8337092
#    gdbhat     03/06/09 - Backport rajayar_reboot_4587300 from main
#    smatpadi   03/31/08 - Bug 6831670
#    vrai       06/22/07 - XbranchMerge vrai_bug-5547807 from main
#    jtellez    03/14/07 - Modified history tag added
#

CP=/bin/cp
MV=/bin/mv
CHOWN=/bin/chown
CHGRP=/bin/chgrp
CHMOD=/bin/chmod
ECHO=/bin/echo
LS=/bin/ls
KILL=/bin/kill
SLEEP=/bin/sleep
LN=/bin/ln
LNS="/bin/ln -s"
RM=/bin/rm
RMF="/bin/rm -f"
CAT=/bin/cat
MKDIR=/bin/mkdir
MKDIR="/bin/mkdir -p"
GREP=/bin/grep
CAT=/bin/cat
TOUCH=/bin/touch
AWK=/bin/awk
SYNC=/bin/sync
IDCMD=/usr/bin/id
FIND=/usr/bin/find
XARGS=/usr/bin/xargs

NOLOCK=false
NOSTART=false

###Function for displaying usage of this script
usage()
{
   printf "Usage:"
   printf "$0 -crshome <Oracle CRS home path> [-nostart] [-startonly]"
   printf " where "
   printf "  -nostart: don't start the CRS stack, only lock CRS home"
   printf "  -startonly: only copy init scripts and start the CRS stack"
   printf "  when applying multiple patches, use -nostart after each one, and"
   printf "    finally -startonly after the last patch"

}

##Function for parsing the command line arguments
parseArgs()
{
   USER_ARGS=$*

   while [ $# -gt 0 ];
   do
    if [ $1 = "-crshome" ]; then
       if [ $# -ge 2 ]; then
	 ORA_CRS_HOME=$2;
         shift;
       else
	 usage;
	 exit 2
       fi
     elif [ $1 = "-nostart" ]; then
       NOSTART=true
     elif [ $1 = "-startonly" ]; then
       NOLOCK=true
     else
       usage;
       exit 2
     fi
     shift;
   done
}

# This script is intended to be run by root.

RUID=`$IDCMD|$AWK -F\( '{print $2}'|$AWK -F\) '{print $1}'`
if [ ${RUID} != "root" ];then
  $ECHO "You must be logged in as root to run $0."
  $ECHO "Log in as root and restart $0 execution."
  exit 1
fi

# File that $LOGMSG in init.cssd will write information to
MSGFILE=/var/adm/messages

##Parse command line arguments passed to this script
parseArgs $*

ORACLE_HOME=$ORA_CRS_HOME
export ORA_CRS_HOME
export ORACLE_HOME
CH=$ORA_CRS_HOME

if [ -z "$CH" ]; then
  usage;
  exit 1;
fi

CRSCTL=$CH/bin/crsctl

# Definitions to Add/Remove inittab entries
ADDITAB="$CAT \$IT | $GREP -v init.evmd | $GREP -v init.cssd | $GREP -v init.crsd > \$IT.no_crs || { $ECHO $?; exit 1; } ; \
        $CAT \$IT.no_crs $CH/crs/admin/inittab > \$IT.tmp || { $ECHO $?; exit 1; }; \
        $CP \$IT.tmp \$IT.crs || { $ECHO $?; exit 1; }; \
        \$SYNC ; \$SYNC ; \$SYNC ; \
        $MV \$IT.tmp \$IT || { $ECHO $?; exit 1; };\
        \$SYNC ; \$SYNC ; \$SYNC "
RMITAB="$CAT \$IT | $GREP -v init.evmd | $GREP -v init.cssd | $GREP -v init.crsd > \$IT.tmp || { $ECHO $?; exit 1; } ; \
        $CP \$IT.tmp \$IT.no_crs || { $ECHO $?; exit 1; }; \
        \$SYNC ; \$SYNC ; \$SYNC ; \
        $MV \$IT.tmp \$IT || { $ECHO $?; exit 1; }; \
        \$SYNC ; \$SYNC ; \$SYNC "

CLSCFG_MISCNT=""

UNAME=/bin/uname
PLATFORM=`$UNAME`

case $PLATFORM in
Linux) DIRNAME=/usr/bin/dirname
       ID=/etc/init.d
       RCSDIR="/etc/rc.d/rc3.d /etc/rc.d/rc5.d"
       RCKDIR="/etc/rc.d/rc0.d /etc/rc.d/rc1.d /etc/rc.d/rc2.d /etc/rc.d/rc4.d /etc/rc.d/rc6.d"
       RCALLDIR="/etc/rc.d/rc0.d /etc/rc.d/rc1.d /etc/rc.d/rc2.d /etc/rc.d/rc3.d /etc/rc.d/rc4.d /etc/rc.d/rc5.d /etc/rc.d/rc6.d"

       RC_START=S96
       RC_KILL=K19
       RC_KILL_OLD=K96
       IT=/etc/inittab
       INIT=/sbin/init
       ;;
HP-UX) 
       DIRNAME=/bin/dirname
       ID=/sbin/init.d
       RCSDIR="/sbin/rc3.d"
       RCKDIR="/sbin/rc2.d"
       RCALLDIR="/sbin/rc2.d /sbin/rc3.d"

       RC_START=S960
       RC_KILL=K190
       RC_KILL_OLD=K001
       IT=/etc/inittab
       INIT=/sbin/init
       HPUXCHOWN="$CHOWN root $CH/lib*/libttsh11.*"
       HPUXCHMOD="$CHMOD 555 $CH/lib*/libhasgen11.* $CH/lib*/libttsh11.* $CH/lib*/libdbcfg11.* $CH/lib*/libocr11.* $CH/lib*/libocrb11.* $CH/lib*/libocrutl11.* $CH/lib*/libsrvm11.* $CH/lib*/libsrvmhas11.* $CH/lib*/libsrvmocr11.* $CH/lib*/libuini11.*"
       ;;
SunOS) MACH_HARDWARE=`/bin/uname -i`
       DIRNAME=/bin/dirname
       ID=/etc/init.d
       RCSDIR="/etc/rc3.d"
       RCKDIR="/etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rcS.d"
       RCALLDIR="/etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rcS.d"

       RC_START=S96
       RC_KILL=K19
       RC_KILL_OLD=K96
       IT=/etc/inittab  
       INIT=/sbin/init
       ;;
AIX)   DIRNAME=/bin/dirname
       ID=/etc
       RCSDIR="/etc/rc.d/rc2.d"
       RCKDIR="/etc/rc.d/rc2.d"
       RCALLDIR="/etc/rc.d/rc2.d"

       RC_START=S96
       RC_KILL=K19
       RC_KILL_OLD=K96
       IT=/etc/inittab
       INIT=/usr/sbin/init
       ADDITAB="$ECHO \"Adding daemons to inittab\";
                while read line; \
                do \
                rflds=\`echo \$line | cut -d ':' -f 1-3\`; \
                lfld=\`echo \$line | cut -d ':' -f 4-\`; \
                mkitab \$rflds:\"\$lfld\"; \
                done < $CH/crs/admin/inittab"
       RMITAB="while read line; \
                do \
                identifier=\`echo \$line | cut -d ':' -f 1\`; \
                rmitab \$identifier 2>/dev/null; \
                done < $CH/crs/admin/inittab"
    AIXCHMOD="$CHMOD o+r $CH/lib/libttsh11.* $CH/lib32/libttsh11.* $CH/lib/libocr11.* $CH/lib/libocrb11.* $CH/lib/libhasgen11.* $CH/lib/libocrutl11.* $CH/lib/libdbcfg11.* $CH/lib/libttsh11.*"
    AIXCHOWN="$CHOWN root $CH/lib/libhasgen11.* $CH/lib/libttsh11.* $CH/lib32/libttsh11.*"
       ;;
OSF1)  DIRNAME=/usr/bin/dirname
       ID="/sbin/init.d"
       RCSDIR="/sbin/rc3.d"
       RCKDIR="/sbin/rc3.d"
       RCALLDIR="/sbin/rc3.d"

       RC_START=S96
       RC_KILL=K19
       RC_KILL_OLD=K96
       IT=/etc/inittab
       INIT=/sbin/init
       ;;

*)     $ECHO "ERROR: Unknown Operating System"
       exit -1
       ;;
esac

# For the AIX platform we have to reset the lib premissions so that the OS can load the
# sharable libraries into the system cache.

case $PLATFORM in
AIX) $AIXCHMOD
     $AIXCHOWN
       ;;
HP-UX) $HPUXCHMOD
       $HPUXCHOWN
       if [ ! -d /var/opt/oracle/bin ];
       then
          $MKDIR /var/opt/oracle/bin
       fi
       $CHOWN root:sys /var/opt/oracle/bin
       $CHMOD 770 /var/opt/oracle/bin
       $CP -f $CH/bin/reboot_toc /var/opt/oracle/bin
       $CHMOD 770 /var/opt/oracle/bin/reboot_toc
       $CHOWN root:sys /var/opt/oracle/bin/reboot_toc
       ;;
*)
       ;;
esac

$ECHO "Checking to see if Oracle CRS stack is already up..."
if $CH/bin/crs_stat > /dev/null 2>&1
then
   $ECHO "Oracle CRS stack is already running."
   exit 0
fi

##Checking if CRS is being started by INIT
$ECHO "Checking to see if Oracle CRS stack is already starting"
$ID/init.cssd startcheck CSS
if [ "$?" = "0" ]; then
  $ECHO "Oracle CRS stack is already started and will shortly be running under init" 
  exit 1
fi

$CHMOD 644 $IT $IT.orig $IT.no_cssd $IT.cssd $IT.crs $IT.no_crs 2>/dev/null

# First Activate the Volume Groups containing Oracle Cluster Registry 
#repository and the Voting disk.
$CH/css/admin/init.cssd activatevg

if [ "$NOLOCK" = "false" ]; then

##--------------------------------------------
# Set permissions of CRS binary (Lock CRS home)
##--------------------------------------------

# Ensure that everything in the bin directory is now executable.
$FIND $CH/bin -print | $XARGS $CHMOD +x

# For all the files under bin, others should have only execute permission
$FIND $CH/bin -print | $XARGS $CHMOD o=x

# set ownership to root, but give execute permissions to all

$CHOWN root $CH/bin/crs* 
$CHMOD 551  $CH/bin/crs* 
$CHOWN root $CH/bin/racg* 
$CHMOD 551  $CH/bin/racg* 

$CHOWN root $CH/racg
$CHMOD 751  $CH/racg

$CHOWN root $CH/racg/admin 
$CHMOD 751  $CH/racg/admin

$CHMOD 551 $CH/racg/admin/racg*

# only owner(root) should ever invoke crsd

for d in . lib bin crs crs/init crs/profile crs/script crs/template crs/auth crs/mesg css/mesg evm/mesg racg/mesg srvm/mesg
do
    $CHOWN root $CH/$d 
done

# check directories above $CH are owned by root

d=$CH
while [ "$d" != "/" ]
do
    d=`$DIRNAME $d`
    set -$- `$LS -ld $d`
    case $3 in
    root)   ;;	    # ok
    *)	    $ECHO "WARNING: directory '$d' is not owned by root";;
    esac
done


fi # NOLOCK

if [ "$NOSTART" = "false" ]; then

# basic copies first...
$CP $CH/evm/admin/init.evmd $ID/init.evmd || { $ECHO $?; exit 1; }
$CP $CH/css/admin/init.cssd $ID/init.cssd || { $ECHO $?; exit 1; }
$CP $CH/crs/admin/init.crsd $ID/init.crsd || { $ECHO $?; exit 1; }
$CP $CH/crs/admin/init.crs $ID/init.crs || { $ECHO $?; exit 1; }

# Clean up any old init.crs scripts
for rc in $RCALLDIR
do
  $RMF $rc/"$RC_START"init.crs
  $RMF $rc/"$RC_KILL"init.crs
  $RMF $rc/"$RC_KILL_OLD"init.crs
done

# Install new ones
for rc in $RCSDIR
do
  $LNS $ID/init.crs $rc/"$RC_START"init.crs  || { $ECHO $?; exit 1; }
done
for rc in $RCKDIR
do
  $LNS $ID/init.crs $rc/"$RC_KILL"init.crs || { $ECHO $?; exit 1; }
done

$CP $IT $IT.orig || { $ECHO $?; exit 1; }

# Remove inittab entries, and wait a bit to be sure init realizes the change
eval $RMITAB
$INIT q
$SLEEP 10

# Prepare to start the daemons.
$ID/init.crs start

# Check to see if they are going to start.
$ID/init.cssd startcheck CSS
if [ "$?" != "0" ]; then
  $ECHO CRS daemons not set to start. See $MSGFILE for details.
  exit 1
fi

eval $ADDITAB
$INIT q

# Complete success. This is the last node of the install.
works=false
# Wait for CRSD and EVMD to start up
for i in 30 60 90 120 150 180 210 240 270 300 330 360 390 420 450 480 510 540 570 600
do
    if $CH/bin/crs_stat > /dev/null 2>&1
    then
      works=true
      break
    fi
 
    $ECHO "Waiting for the Oracle CRSD and EVMD to start"
    $SLEEP 30
done

if $works
then
    $ECHO "Oracle CRS stack installed and running under init(1M)" 
else
    $ECHO "Timed out waiting for the CRS stack to start."
    exit 1
fi 

# Startup was successful. Continue with the remainder.

else # NOSTART is set
    $ECHO "Oracle CRS stack not started"
    $ECHO "To start Oracle CRS stack later, invoke this script with -startonly"
fi # NOSTART

exit 0

