#!/bin/sh
#* ----------------------------------------------------------------------
#*               Proprietary and Confidential Information
#*
#* This material contains, and is part of a computer software program
#* which is, proprietary and confidential information owned by COMPUTER
#* ASSOCIATES INTERNATIONAL, INC.  The program, including this material,
#* may not be duplicated, disclosed or reproduced in whole or in part for
#* any purpose without the express written authorization of COMPUTER
#* ASSOCIATES. All authorized reproductions must be marked with this
#* legend.
#*

#*             Computer Associates International, Incorporated
#*                       Software Superior by Design
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
#*  SCCS Module Control Stamp
#* ----------------------------------------------------------------------
#
#* "@(#)kernparam_update_ipc.Linux	1.7  04/09/04 14:10:42"
#* "Copyright (C) 1992-2004 Computer Associates International, Inc.  All rights reserved."
#

# this file is created by the kernparmchk program if any parms are low

# -----------------------------------------------------------------------------
# debug_echo() is used to print debug messages with filter on debug_level control.
# -----------------------------------------------------------------------------

THIS=`basename $0`
debug_echo() {
   level=${1:=0}
   shift
   [ ${DEBUG_ON:-0} -ge $level ] && echo "$THIS: $@"
}

# -----------------------------------------------------------------------------
# Process the command line arguments
# -----------------------------------------------------------------------------
USAGE="Usage: $THIS [-v] [-d <debug_level>] [-i <kernparmchk_list_file>], where
   -v turns verbose mode on with debug_level set to 0 and enables debug mode.
   -d turns debug mode on with <debug_level> set to the specified integer value.
   It exits with code 0 if successfully terminated, 1 if aborted with an error."

set -- `getopt vd:i: $*`
if [ $? != 0 ]
then
   echo $USAGE
   exit 2
fi

for i in $*
do
   case $i in
     -v) DEBUG_ON=${DEBUG_ON:-1}; shift;;
     -d) DEBUG_ON=$2; shift 2;
         debug_echo 1 "debug mode is turned on with level=$DEBUG_ON";;
     --) shift; break;;
   esac
done

# -----------------------------------------------------------------------------
# Initialize the environment.
# -----------------------------------------------------------------------------
PATH=/sbin:/usr/sbin:/usr/bin:/bin
KERNPARMCHKFILE="${TMPDIR:-/tmp}/kernparmchk.out"
TMPFILE="${TMPDIR:-/tmp}/kernparmchk.tmp.$$"
rm -f $TMPFILE
if [ ! -r $KERNPARMCHKFILE ]; then
   debug_echo 1 "Could not find $KERNPARMCHKFILE file. Exiting..."
   exit 0
fi
export CA_KERNPARM=/etc/sysctl.conf  # file that contains values for the sysctl cmd

# -----------------------------------------------------------------------------
# start processing the input file generated by kernparamchk script
# -----------------------------------------------------------------------------
while read param curval newval
do
   debug_echo 2 "Got next entry: param=$param curval=$curval newval=$newval"
   #  See if dynamically settable 
   full=$(cd /proc/sys; find . -name "$param" -type f | sed 's/\//./g;s/^\.*//')
   if [ -n "$full" ] ; then
      #echo "$full = $newval" >> $CA_KERNPARM
      grep -v "$full = " $CA_KERNPARM > $TMPFILE 2>/dev/null;
      echo "$full = $newval" >> $TMPFILE
      cp $TMPFILE $CA_KERNPARM && rm -f $TMPFILE
      debug_echo 1 "Adding into $CA_KERNPARM: $full = $newval"
      continue
   else
      debug_echo 2 "Check if its special sem related param like semmsl semmns semopm semmni"
      if [ "$param##sem" != "$param" ]; then
         # Special processing for the parameters starting sem like semmsl etc
         shift $#
         set - `grep "kernel.sem" $CA_KERNPARM 2>/dev/null`
         if [ $# -ne 6 ]; then
            debug_echo 2 "kernel.sem missing in $CA_KERNPARM. Add it to update $param"
            set - `sysctl kernel.sem`
            if [ $# -ne 6 ]; then
               debug_echo 2 "Can not get existing sem parameters by any means."
            fi
         else debug_echo 2 "Found good kernel.sem in $CA_KERNPARM. $param is updated now."
         fi
         shift 2
         semmsl=$1; semmns=$2; semopm=$3; semmni=$4
         case $param in
            semmsl) semmsl=$newval;;
            semmns) semmns=$newval;;
            semopm) semopm=$newval;;
            semmni) semmni=$newval;;
            *) debug_echo 2 "Invalid kernel parameter starting with sem as $param";;
         esac
         grep -v "kernel\.sem" $CA_KERNPARM > $TMPFILE 2>/dev/null;
         echo "kernel.sem = $semmsl	$semmns	$semopm	$semmni" >> $TMPFILE
         cp $TMPFILE $CA_KERNPARM && rm -f $TMPFILE
         debug_echo 1 "Adding into $CA_KERNPARM: kernel.sem = $semmsl	$semmns	$semopm	$semmni"
      else
         debug_echo 2 "$param parameter could not be found in /proc/sys"
      fi
   fi

   #  Nope.  We will try to update kernel header files.  To put remaining 
   #  kernel parameter values in effect the kernel has to be rebuilt.
   MANIFEST=$(echo $param | tr 'a-z' 'A-Z')
   line=$(find ${TOPDIR:-/usr/src/linux}/include/linux -name '*.h' -exec grep "define[ 	]*$MANIFEST" /dev/null {} \; 2>/dev/null)
   if [ -n "$line" ] ; then
      hfile=$(echo "$line" | sed 's/:.*//g')
      nhfile="${TMPDIR:-/tmp}/$(basename $hfile).ca_new"
      [ ! -f ${hfile}.caorig ] && cp -p $hfile ${hfile}.caorig
      awk -v var="$MANIFEST" -v new="$newval" '
         BEGIN { field=-1;  }
         /#[   ]*define/ && $0 ~ var {
               for (k=1; k <= NF; k++) {
                  if (field == -1 && $k == var) field=k+1 ;
               }
               if (field > 0) {
                  for (k=1; k <= NF; k++) {
                     if (k == field) {
                        printf "%s ", new ;
                        field = -2 ;
                     } else
                        printf "%s ", $k ;
                  }
                    printf "\n" ;
               } else {
                   print $0 ;
               }
               next ;
         }
         { print } '  $hfile > $nhfile
      if [ -s "$nhfile" ] ; then
         mv -f $nhfile  $hfile
      fi
   fi
done < $KERNPARMCHKFILE 

# -----------------------------------------------------------------------------
# Add the sysctl command into the rc boot script, if needed.
# We want to make sure kernel parms get updated at boot time
# If we wait until unicenter starts, it will be too late.
# The old kernparam_update_ipc.Linux.c file used to do this.
# -----------------------------------------------------------------------------

sysctl -p $CA_KERNPARM > /dev/null 2>&1
debug_echo 1 "Explicitely updated current kernel as 'sysctl -p $CA_KERNPARM'"

if [ -f /etc/rc.d/boot.local ]
then
    # make a backup so deinstall script can restore the original
    if [ ! -f /etc/rc.d/boot.local.cai ]
    then
        cp /etc/rc.d/boot.local /etc/rc.d/boot.local.cai
    fi

    # see if we already have a sysctl call in the file
    grep -q "/sbin/sysctl -p $CA_KERNPARM" /etc/rc.d/boot.local
    if [ $? -eq 0 ]
	 then
        debug_echo 2 "Already found 'sysctl -p $CA_KERNPARM' in /etc/rc.d/boot.local"
        : # its already there
    else
        echo "/sbin/sysctl -p $CA_KERNPARM" >> /etc/rc.d/boot.local
        debug_echo 1 "Added entry 'sysctl -p $CA_KERNPARM' into /etc/rc.d/boot.local"
    fi
fi

exit 0

