#!/bin/sh

# Oracle Cluster Ready Services 10.1.0.3 Patching script
#
# Requirements
# - Must be invoked as root
# - Should be invoked once per node
# - Should not be invoked while any CRS daemons in the home are up
#
# Shared CRS Homes
# - Shut down all CRS daemons using the CRS home before running
#   the script on any node. Otherwise active binaries may get overwritten,
#   and daemons could unexpectedly crash.
#
# If the script fails, it should be rerun until it succeeds.
#
# Overall Patch flow information:
# - Invoke install action, which will copy all patched files
#   into $ORA_CRS_HOME/install/patch10103/
# - On each node, stop the CRS daemons, and then run this script.
#   It is acceptable to do each node in series to achieve a rolling 
#   upgrade, but it is not recommended to patch multiple homes 
#   simultaneously.

usage()
{
   printf "Usage:"
   printf "root10103.sh [-paramfile <input file for CRS parameter values>] "
}

ORA_CRS_HOME=%s_OracleHome%
CRS_ORACLE_OWNER=%OwnerId%
if [ -z "$MAKE" ]; then MAKE="%s_OSDunixMakePath%"; fi

if [ -z "$CP" ]; then CP=/bin/cp; fi
if [ -z "$LS" ]; then LS=/bin/ls; fi
if [ -z "$LN" ]; then LN=/bin/ln; fi # Perform a hard link if OS can do it
if [ -z "$LNS" ]; then LNS="$LN -s"; fi # Perform soft link
if [ -z "$MV" ]; then MV=/bin/mv; fi
if [ -z "$MVF" ]; then MVF="$MV -f"; fi
if [ -z "$CAT" ]; then CAT=/bin/cat; fi
if [ -z "$CUT" ]; then CUT=/usr/bin/cut; fi
if [ -z "$RMF" ]; then RMF="/bin/rm -f"; fi
if [ -z "$ECHO" ]; then ECHO=/bin/echo; fi
if [ -z "$FIND" ]; then FIND=/usr/bin/find; fi
if [ -z "$CHOWN" ]; then CHOWN=/bin/chown; fi
if [ -z "$CHGRP" ]; then CHGRP=/bin/chgrp; fi
if [ -z "$CHMOD" ]; then CHMOD=/bin/chmod; fi
if [ -z "$MKDIR" ]; then MKDIR=/bin/mkdir; fi
if [ -z "$RMDIR" ]; then RMDIR=/bin/rmdir; fi
if [ -z "$SYNC" ]; then SYNC=/bin/sync; fi
if [ -z "$SLEEP" ]; then SLEEP=/bin/sleep; fi
if [ -z "$GREP" ]; then GREP=/usr/bin/grep; fi
if [ -z "$DIFF" ]; then DIFF=/bin/diff; fi
if [ -z "$AWK" ]; then AWK=/bin/awk; fi
if [ -z "$ID" ]; then ID=/usr/bin/id; fi

# Needs to print out (at least) the basename of all processes on the box
if [ -z "$PSE" ]; then PSE="/bin/ps -e"; fi

SYNCALL="$SYNC; $SYNC; $SYNC"

# This script is intended to be run by root.

RUID=`$ID|$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

##
## Step 1: Read in a config file if requested.
##
if [ $# -gt 2 ]; then
   usage;
   exit 2
fi

USE_FILE=false
while [ $# -gt 0 ];
do
  if [ $1 = "-paramfile" ]; then
    USE_FILE=true
    if [ $# -ge 2 ]; then
      CRS_ENV_FILE=$2
    fi
  fi
  shift;
done

if $USE_FILE;
then
  if [ ! -f $CRS_ENV_FILE ]; then
     $ECHO "The input file($CRS_ENV_FILE) specifying the values for CRS variable is not found"
     exit 1
  fi

  ## Make sure that this input file has only "LHS=RHS" and empty lines
  ## The file should have only LHS=RHS and empty lines.
  ## Any other lines will be considered improper input for root script
  awkOutput=`$AWK 'BEGIN { Ni=0 } \
                     /[A-Z_]\=[a-zA-Z0-9/_-\"\b]/ { next } \
                     /^$/   { next } \
                     /^#/   { next } \
                            { printf("\"%s\"\n", $0); next }' $CRS_ENV_FILE`

  if [ "x$awkOutput" != "x" ]; then
     $ECHO "The following unexpected CRS parameters found in input: $awkOutput"
     exit 1
  fi

  . $CRS_ENV_FILE

  $ECHO "File ($CRS_ENV_FILE) is used for setting CRS variables."
  $ECHO "CRS_ORACLE_OWNER=$CRS_ORACLE_OWNER"
  $ECHO "ORA_CRS_HOME=$ORA_CRS_HOME"
  $ECHO "MAKE=$MAKE"
fi

export ORA_CRS_HOME
ORACLE_OWNER=$CRS_ORACLE_OWNER
CH=$ORA_CRS_HOME

# PATCHDIR, and commands for finding things within it
PATCHDIR=$CH/install/patch10103/
FINDPATCHDIRS="$FIND $PATCHDIR -type d -depth"
FINDPATCHFILES="$FIND . -type f"

# Some of the copies below could go horribly wrong if CH
# is not set, or is set to something nonexistant.
if [ ! -d "$CH" ]; then
  $ECHO Unable to find ORA_CRS_HOME=$CH.
  exit 1;
fi

if [ ! -x "$MAKE" ]; then
  $ECHO Make executable not found. MAKE=$MAKE
  exit 1;
fi

##
## Step 2: Get definitions for all platform-specific variables
##
UNAME='/bin/uname'
PLATFORM=`$UNAME`

case $PLATFORM in
SunOS)
  DIRNAME=/bin/dirname
  ID=/etc/init.d
  RCDIR="/etc/rc3.d"
  RC_START=S96
  RC_KILL=K96
  IT=/etc/inittab
  INIT=/sbin/init

  LIBSKGXN=libskgxn2.so
  LIBSKGXNS=libskgxns.so
  ;;
Linux)
  DIRNAME=/usr/bin/dirname
  ID=/etc/init.d
  RCDIR="/etc/rc.d/rc2.d /etc/rc.d/rc3.d /etc/rc.d/rc5.d"
  RC_START=S96
  RC_KILL=K96
  IT=/etc/inittab
  INIT=/sbin/init
  GREP=/bin/grep

  LIBSKGXN=libskgxn2.so
  LIBSKGXNS=libskgxns.so
  ;;
HP-UX)
  MACH_HARDWARE=`/bin/uname -m`
  if [ "$MACH_HARDWARE" = "ia64" ]; then
    SO_EXT=so
  else
    SO_EXT=sl
  fi 

  DIRNAME=/bin/dirname
  ID=/sbin/init.d
  RCDIR="/sbin/rc3.d"
  RC_START=S960
  RC_KILL=K960
  IT=/etc/inittab
  INIT=/sbin/init

  LIBSKGXN=libskgxn2.$SO_EXT
  LIBSKGXNS=libskgxns.$SO_EXT
  ;;
AIX)
  DIRNAME=/bin/dirname
  ID=/etc
  RCDIR="/etc/rc.d/rc2.d"
  RC_START=S96
  RC_KILL=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"

  LIBSKGXN=libskgxn2.a
  LIBSKGXNS=libskgxns.a
  ;;
OSF1)
  DIRNAME=/usr/bin/dirname
  ID="/sbin/init.d"
  RCDIR="/sbin/rc3.d"
  RC_START=S96
  RC_KILL=K96
  IT=/etc/inittab
  INIT=/sbin/init

  LIBSKGXN=libskgxn2.so
  LIBSKGXNS=libskgxns.so
  ;;
*) 
  /bin/echo "ERROR: Unknown Operating System"
  exit -1
  ;;
esac

##
## Step 3: Verify that the CRS daemons are down.
##

$PSE | $GREP -i 'o[c]ssd'    > /tmp/ocssd.ps
$PSE | $GREP -i 'c[r]sd.bin' > /tmp/crsd.ps
$PSE | $GREP -i 'e[v]md.bin' > /tmp/evmd.ps
PROCS=`$CAT /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps`
if [ ! -z "$PROCS" ]; then
  $ECHO Please shut down the following processes before running this script.
  $CAT /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps
  $RM /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps
  exit 1
fi

$RM /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps

##
## Step 4: Apply all of the patched files to the CRS Home.
##
## The patched files only need to be dealt with on the first node
## if the CRS home is shared. Otherwise they are done on all node.
## A small number of patched files will be generated via make
## on the customer site.
##
if [ -d $PATCHDIR ]; then

  # Converts the patched libskgxn2.so to a form appropriate to
  # the platform. This is expressly intended for all platforms.
  if [ -f $PATCHDIR/lib/$LIBSKGXN ]; then
    if [ -f $CH/lib/$LIBSKGXNS ]; then
      $MVF $PATCHDIR/lib/$LIBSKGXN $PATCHDIR/lib/$LIBSKGXNS
    elif [ ! -f $CH/lib/$LIBSKGXN ]; then
      $MVF $PATCHDIR/lib/$LIBSKGXN $PATCHDIR/lib/$LIBSKGXNS
    fi
  fi
  if [ -f $PATCHDIR/lib32/$LIBSKGXN ]; then
    if [ -f $CH/lib32/$LIBSKGXNS ]; then
      $MVF $PATCHDIR/lib32/$LIBSKGXN $PATCHDIR/lib32/$LIBSKGXNS
    elif [ ! -f $CH/lib32/$LIBSKGXN ]; then
      $MVF $PATCHDIR/lib32/$LIBSKGXN $PATCHDIR/lib32/$LIBSKGXNS
    fi
  fi
     
  $RMF $CH/install/files10103.log
  cd $PATCHDIR

  for f in `$FINDPATCHFILES`
  do
    $MVF $f $CH/$f
    if [ ! $? ]
    then
      $ECHO Unable to copy file $f to $CH/$f.
      exit 1;
    fi
    $ECHO $f >> $CH/install/files10103.log
  done

  # In some configurations, we will have just an skgxn2 library. This
  # change converts them to have symlinks from skgxn2 to skgxns to simplify
  # later patch updates. (makes them consistent with other platforms)
  if [ ! -f $CH/lib/$LIBSKGXNS ]; then
    if [ -f $CH/lib/$LIBSKGXN ]; then
      $MVF $CH/lib/$LIBSKGXN $CH/lib/$LIBSKGXNS
      $LNS $CH/lib/$LIBSKGXNS $CH/lib/$LIBSKGXN
    fi
  fi
  if [ -d $CH/lib32 ]; then
    if [ ! -f $CH/lib32/$LIBSKGXNS ]; then
      if [ -f $CH/lib32/$LIBSKGXN ]; then
        $MVF $CH/lib32/$LIBSKGXN $CH/lib32/$LIBSKGXNS
        $LNS $CH/lib32/$LIBSKGXNS $CH/lib32/$LIBSKGXN
      fi
    fi
  fi

  cd $CH

  # Now clear out all of the empty directories in $PATCHDIR.
  # (This could be done with an rm -rf, but the following is safer.)
  for f in `$FINDPATCHDIRS`
  do
    # Try to gracefully remove the directory. 
    $RMDIR $f
    if [ ! $? ]
    then
      $ECHO Unable to remove directory $f.
      exit 1;
    fi
  done

  # We need to do a small amount of relinking on customer site. Only
  # perform this on the first node, if the CRS home is shared.
  $ECHO Relinking some shared libraries.

  $MAKE -f $CH/network/lib/ins_net_client.mk ntcontab.o \
    ORACLE_HOME=$CH  >> $CH/install/make.log
  if [ ! $? ]; then
    $ECHO Relinking failed at ntcontab.o. See $CH/install/make.log.
    exit 1
  fi

  $MAKE -f $CH/network/lib/ins_net_client.mk nnfgt.o \
    ORACLE_HOME=$CH  >> $CH/install/make.log
  if [ ! $? ]; then
    $ECHO Relinking failed at nnfgt.o. See $CH/install/make.log.
    exit 1
  fi

  $MAKE -f $CH/network/lib/ins_net_client.mk client_sharedlib \
    ORACLE_HOME=$CH >> $CH/install/make.log
  if [ ! $? ]; then
    $ECHO Relinking failed at client_sharedlib. See $CH/install/make.log.
    exit 1
  fi

  $ECHO Relinking complete.
fi

cd $CH

##
## Step 5: Reapply all permissions changes normally done in the CRS Home.
##

# set ownership to root, but give execute permissions to all
$CHOWN root $CH/bin/crs*
$CHMOD 555  $CH/bin/crs*

# only owner(root) should ever invoke crsd
$CHMOD 744  $CH/bin/crsd
$CHMOD 744  $CH/bin/crsd.bin

# Set root ownership on the CRS Home
$CHMOD 755 $CH
$CHOWN root $CH

# Create $CH/log. A small number of things will use this directory
# for diagnostics.
if [ ! -d $CH/log ]; then
  $MKDIR $CH/log
fi
$CHOWN $ORACLE_OWNER $CH/log
$CHMOD 755 $CH/log

# Writable by the dba group but can only be deleted by the crshome owner
$CHMOD 1770 evm/log

# Everyone should be able to insert, but they should not be able to
# delete the directory.
$CHOWN $ORACLE_OWNER $CH/crs/public
$CHMOD 1777 $CH/crs/public

# Check that the parent directories of $CH are ownable by root.
# Any parent not owned by root may allow a non-root user to invoke
# actions as root.
d=$CH
while [ "$d" != "/" ]
do
    set -$- `$LS -ld $d`
    case $3 in
    root)   ;;      # ok
    *)      $ECHO "WARNING: directory '$d' is not owned by root";;
    esac
    d=`$DIRNAME $d`
done

##
## Step 6: Recopy things that live outside of the CRS Home
##
## NOTE: Step 6 and Step 7 are done with an enhanced level of paranoia
##       compared to previous incarnations in root.sh. The following
##       methods of modifying inittab are performed intentionally 
##       in hopes of avoiding situations where inittab gets truncated
##       to a 0 byte file, since this is especially hard to repair.
##       Some additional complexity is here because of similar reasons
##       to why we do not have or support a manual startup of CRS.
##

case $PLATFORM in
AIX)
  # Remove CRS entries from inittab
  $ECHO Preparing to recopy patched init and RC scripts.
  $CP $IT $IT.pre10103 || { $ECHO $? ; exit 1; }
  eval $RMITAB
  $CP $IT $IT.nocrs10103 || { $ECHO $? ; exit 1; }
  eval $SYNCALL
  $INIT q
  eval $SYNCALL
  ;;
*)
  # Remove CRS entries from inittab
  $ECHO Preparing to recopy patched init and RC scripts.
  $CP $IT $IT.pre10103 || { $ECHO $? ; exit 1; }
  ($CAT $IT | $GREP -v -i "init.evmd" | $GREP -v -i "init.cssd" | \
   $GREP -v -i "init.crs"  | $GREP -v -i "init.crsd" > $IT.nocrs10103) \
        || { $ECHO $? ; exit 1; }
  $CP $IT.nocrs10103 $IT.tmp || { $ECHO $? ; exit 1; }
  eval $SYNCALL
  $MVF $IT.tmp $IT || { $ECHO $? ; exit 1; }
  eval $SYNCALL
  $INIT q
  eval $SYNCALL
  ;;
esac

# Give init a brief amount of time to acknowledge the change.
$SLEEP 10

# Recopy the RC and init scripts.
$ECHO Recopying init and RC scripts.
$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; }

for rc in $RCDIR
do
  $RMF $rc/"$RC_START"init.crs $rc/"$RC_KILL"init.crs
  $LN $ID/init.crs $rc/"$RC_START"init.crs || { $ECHO $?; exit 1; }
  $LN $ID/init.crs $rc/"$RC_KILL"init.crs  || { $ECHO $?; exit 1; }
done

eval $SYNCALL

##
## Step 7: Now try to start up the CRS daemons again.
##
## WARNING: Doing this manually is discouraged.
##

$ID/init.crs start
$ID/init.cssd startcheck
if [ ! $? ]; then
  $ECHO Failed at startcheck. See the system message file for details.
  exit 1;
fi

case $PLATFORM in
AIX)
  $ECHO Starting up the CRS daemons.
  eval $ADDITAB  
  eval $SYNCALL
  $INIT q
  eval $SYNCALL
  ;;
*)
  $ECHO Starting up the CRS daemons.
  $CAT $IT.nocrs10103 $CH/crs/admin/inittab > $IT.tmp || { $ECHO $?; exit 1; }
  eval $SYNCALL
  $MVF $IT.tmp $IT || { $ECHO $?; exit 1; }
  eval $SYNCALL
  $INIT q
  eval $SYNCALL
  ;;
esac

works=false
# Wait a few minutes to see if the CRS daemons come up. On some systems if
# the init daemon is relatively unresponsive this can take 5 or more minutes.
$ECHO "Waiting for the patched CRS daemons to start."
$ECHO "  This may take a while on some systems."
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 "."
  $SLEEP 30
done

if $works
then
  $ECHO "10.1.0.3 patch successfully applied."
else
  $ECHO "Timed out waiting for the CRS daemons to start. Look at the"
  $ECHO "system message file and the CRS log files for diagnostics."
  exit 1
fi

