#!/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.
#*
#*                          Copyright (c) 1992-2009
#*             Computer Associates International, Incorporated
#*                       Software Superior by Design
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
#*  SCCS Module Control Stamp
#* ----------------------------------------------------------------------
#
#* "@(#)kernparmchk.Linux.sh	1.10  07/24/09 19:24:27"
#* "Copyright (C) 1992-2009 Computer Associates International, Inc.  All rights reserved."
#

# -----------------------------------------------------------------------------
# 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: $@"
}

# -----------------------------------------------------------------------------
# Initialize the environment.
# -----------------------------------------------------------------------------
PATH=/sbin:/usr/sbin:/usr/bin:/bin
KERNEL_PARM_OUT="${TMPDIR:=/tmp}/kernparmchk.out"
SYSCTL_OUT="${TMPDIR:=/tmp}/sysctl.out.$$"
THIS="kernparmchk"
KERNPARM_OKAY=0
KERNPARM_WARN=5
KERNPARM_ERROR=1
CONF_FILE="/etc/sysctl.conf"
KERNPARM_TYPE=0
trap "rm -f $SYSCTL_OUT 2>/dev/null"  1 2 15

# -----------------------------------------------------------------------------
# Process the command line arguments
# -----------------------------------------------------------------------------
USAGE="Usage: $THIS [-v] [-d <debug_level>] [-t <type>] [-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.
   -h prints this message.
   -t specifies the $THIS's behaviour;  If -t is missing, pre-r11 behaviour is 
      assumed. The -t INSTALL results in r11 install time behaviour.
   -i specifies kernparmchk.list file where the required kernel params are listed.
   If -i is missing, it defaults to \$CASHCOMP/csutils/config/kernparmchk.list.
   If \$CASHCOMP is not defined, it defaults to \$CAIGLBL0000/kernparmchk.list.
   It exits with code 0 if no changes, 1 if error, 5 if changes are detected."

set -- `getopt vhd:i:t: $*`
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";;
   -i) CONFIG_FILE=$2; shift 2
       if [ ! -f "$CONFIG_FILE" ]; then
          echo "$CONFIG_FILE specified by -i option is missing.  Exiting..."
          exit $KERNPARM_ERROR
       fi;;
   -h) echo $USAGE
       exit $KERNPARM_ERROR ;;
   -t) KERNPARM_T_ARG=$2; shift 2
       debug_echo 1 "The type argument is $KERNPARM_T_ARG"
       if [ "$KERNPARM_T_ARG" == "INSTALL" ] ; then
          KERNPARM_TYPE=1
       else
          echo "Incorrect type value ($KERNPARM_T_ARG) is specified with -t option.  Exiting..."
          exit $KERNPARM_ERROR
       fi;;
   --) shift; break;;
   esac
done

# -----------------------------------------------------------------------------
# Validate the existence of input config file. Consider appropriate defaults.
# -----------------------------------------------------------------------------
if [ -z "$CONFIG_FILE" ]; then
	if [ -n "$CASHCOMP"  -a -f $CASHCOMP/csutils/config/kernparmchk.list ]; then
	   debug_echo 2 "OK, CASHCOMP env variable is set. Ignore CIAGLBL0000 ..."
	   CONFIG_FILE="$CASHCOMP/csutils/config/kernparmchk.list"
	elif [ -n "$CAIGLBL0000" ];then
	   debug_echo 2 "OK, CASHCOMP is NULL but CAIGLBL0000 is set to $CAIGLBL0000"
	   CONFIG_FILE="$CAIGLBL0000/kernparmchk.list"
    else
	   echo "ERR: CAIGLBL0000 env variable is not set. Exiting..."
	   exit $KERNPARM_ERROR
	fi
fi
CONFIG_FILE_LOCAL="$CONFIG_FILE.local"

if [ -f "$CONFIG_FILE_LOCAL" ] ; then
   KERNEL_PARM_LIST="$CONFIG_FILE_LOCAL"
elif [ -f "$CONFIG_FILE" ] ; then
   KERNEL_PARM_LIST="$CONFIG_FILE"
else 
   debug_echo 1 "$CONFIG_FILE file is missing.  Exiting..."
   exit $KERNPARM_ERROR
fi

# -----------------------------------------------------------------------------
# check_value() is a required for 64 bit processing
# -----------------------------------------------------------------------------
check_value()
{
  len_p1=`echo $1 | wc -c`
  len_p2=`echo $2 | wc -c`

  [ $len_p1 -gt $len_p2 ] && return 1
  [ $len_p1 -lt $len_p2 ] && return 0

  is64bit="0"

  if [ "$len_p1" -gt 19 ];then
  # handle large values, which can't be handled by -gt operator
      is64bit=1
      cnt=0
      actual_check_len=`expr $len_p1 - 1`
      actual_check_len=${actual_check_len:-0}
      while [ "$cnt" -lt "$actual_check_len" ]
      do
          cnt=`expr $cnt + 1`
          ch1=`echo $1 | cut -c $cnt`
          ch2=`echo $2 | cut -c $cnt`
          if [ "$ch1" -gt "$ch2" ];then
              return 1
          elif [ "$ch1" -lt "$ch2" ];then
              return 0
          fi
      done
	return 1
  else
      [ $1 -ge $2 ] && return 1
  fi
  return 0

}

# -----------------------------------------------------------------------------
# sem_param() is a special function that checks the semaphore related parameters
# SEMMSL, SEMMNS, SEMOPM, SEMMNI for current and needed values.
# -----------------------------------------------------------------------------
sem_param()
{
   sem_name=$1
   sem_value=$2
   sem_field=$3
   debug_echo 2  "sem_param_name = $1 sem_param_value = $2 field = $3"
   larger_value=$sem_value
   sysline=`grep "kernel.sem" $SYSCTL_OUT 2>/dev/null`
   if [ -n "$sysline" ] ; then
      debug_echo 2  "sem_line = $sysline"
      IFS="="
      set $sysline
      IFS=$OLDIFS
      set $2
      shift `expr $sem_field - 1`
      debug_echo 2 "$sem_name: required setting=$sem_value, current setting=$1"
      if [ $KERNPARM_TYPE -eq 1 ] ; then
         cur_set=${1:-0}
         check_value ${1:-0} $larger_value
         if [ $? -ne 0 ];then
            larger_value=$1
         fi
         UPDATE_CONF=1
         file_line=`grep "kernel.sem" $CONF_FILE 2>/dev/null |grep -v "^[#;][.]*" | tail -1`
         if [ -n "$file_line" ] ; then
            IFS="="
            set $file_line
            IFS=$OLDIFS
            set $2
            shift `expr $sem_field - 1`
            debug_echo 2 "$sem_name: conf file setting=$1"
            check_value ${1:-0} $larger_value
            if [ $? -ne 0 ];then
                UPDATE_CONF=0
                larger_value=$1
            fi
         fi

         if [ $UPDATE_CONF -eq 1 ] ; then
            debug_echo 3 "Adding entry: $sem_name $cur_set $larger_value"
            if [ "$is64bit" = "1" ];then
                printf "%-9s %.0f %.0f\n" $sem_name $cur_set $larger_value >> $KERNEL_PARM_OUT
            else
                printf "%-9s %16u %16u\n" $sem_name $cur_set $larger_value >> $KERNEL_PARM_OUT
            fi
            rc_val=$KERNPARM_WARN
         fi
      else  #KERNPARM_TYPE
         check_value $sem_value $1
         if [ $? -ne 0 ];then
            if [ "$is64bit" = "1" ];then
               printf "%-9s %.0f %.0f\n" $sem_name $1 $sem_value >> $KERNEL_PARM_OUT
            else
               printf "%-9s %16u %16u\n" $sem_name $1 $sem_value >> $KERNEL_PARM_OUT
            fi
            rc_val=$KERNPARM_WARN
            debug_echo 3 "Adding entry: $sem_name $1 $sem_value"
         else
            debug_echo 3 "$sem_name needed value $sem_value is less than current $1"
         fi
      fi  #KERNPARM_TYPE
   else
      debug_echo 1 "kernel.sem is not in sysctl output"
   fi
}

# -----------------------------------------------------------------------------
# Process input config .list file and generate .out file for kernparam_update_ipc
# -----------------------------------------------------------------------------
if [ ! -f "$KERNEL_PARM_LIST" ] ; then
   debug_echo 1 "Cannot find $KERNEL_PARM_LIST file."
   exit $KERNPARM_ERROR
fi
debug_echo 1 "Input file: $KERNEL_PARM_LIST"
umask 022
rm -rf $KERNEL_PARM_OUT $SYSCTL_OUT 2>/dev/null
sysctl -a > $SYSCTL_OUT 2>/dev/null

OLDIFS="$IFS"
rc_val=$KERNPARM_OKAY
while read line 
do
   expr "$line" : "[ 	]*#" >/dev/null && continue
   IFS='=' 
   set $line
   IFS="$OLDIFS"
   parm_name=$1
   need_value=$2
   debug_echo 3 "next param: $parm_name needed_value: $need_value"
   if [ -z "$parm_name" -o -z "$need_value" ] ; then
      debug_echo 2 "Could not parse line \"$line\""
      continue
   fi
   larger_value=$need_value
   sysline=`grep "$parm_name" $SYSCTL_OUT | grep -v auto_ 2>/dev/null`
   if [ -n "$sysline" ] ; then
      debug_echo 3 "$parm_name found in sysdef $sysline"
      IFS="="
      set $sysline
      IFS="$OLDIFS"
      debug_echo 2 "$parm_name: required setting=$need_value, current setting=$2"
      if [ $KERNPARM_TYPE -eq 1 ] ; then
         cur_set=$2
         check_value ${2:-0} $larger_value
         if [ $? -ne 0 ];then
            larger_value=$2
         fi
         UPDATE_CONF=1
         #find the name from /proc
         fname=$(cd /proc/sys; find . -name "$parm_name" -type f | sed 's/\//./g;s/^\.*//')
         if [ -n "$fname" ] ; then
            file_line=`grep "$fname" $CONF_FILE 2>/dev/null |grep -v "^[#;][.]*" | tail -1`
            if [ -n "$file_line" ] ; then
               IFS="="
               set $file_line
               IFS="$OLDIFS"
               check_value ${2:-0} $larger_value
               if [ $? -ne 0 ];then
                  UPDATE_CONF=0
                  larger_value=$2
               fi
            fi
         fi
         if [ $UPDATE_CONF -eq 1 ] ; then
            debug_echo 1 "Adding entry: $parm_name $cur_set $larger_value"
            if [ "$is64bit" = "1" ];then
               printf "%-9s %.0f %.0f\n" $parm_name $cur_set $larger_value >> $KERNEL_PARM_OUT
            else
               printf "%-9s %16u %16u\n" $parm_name $cur_set $larger_value >> $KERNEL_PARM_OUT
            fi
            rc_val=$KERNPARM_WARN
         fi
      else #KERNPARM_TYPE
         check_value $need_value $2
         if [ $? -ne 0 ];then
            debug_echo 1 "Adding entry: $parm_name $2 $need_value"
            if [ "$is64bit" = "1" ];then
               printf "%-9s %.0f %.0f\n" $parm_name $2 $need_value >> $KERNEL_PARM_OUT
            else
               printf "%-9s %16u %16u\n" $parm_name $2 $need_value >> $KERNEL_PARM_OUT
            fi
            rc_val=$KERNPARM_WARN
         fi
      fi   #KERNPARM_TYPE
   else
      debug_echo 3 "$parm_name missing in sysdef stdout. Try special processing."
      #Check for special cases 
      IFS="$OLDIFS"
      case $parm_name in
         semmni )
                   sem_param "$parm_name" "$need_value" 4
                   ;;
         semopm )
                   sem_param "$parm_name" "$need_value" 3
                   ;;
         semmns )
                   sem_param "$parm_name" "$need_value" 2
                   ;;
         semmsl )
                   sem_param "$parm_name" "$need_value" 1
                   ;;
         maxfile)
                   sysline=`grep "fs.file-max" $SYSCTL_OUT 2>/dev/null`
                   if [ -n "$sysline" ] ; then
                      IFS="="
                      set $sysline
                      IFS="$OLDIFS"
                      debug_echo 2 "$parm_name: required setting=$need_value, current setting=$2"
                      if [ $KERNPARM_TYPE -eq 1 ] ; then
                         cur_set=$2
                         check_value ${2:-0} $larger_value 
                         if [ $? -ne 0 ];then
                            larger_value=$2
                         fi
                         UPDATE_CONF=1
                         fname="fs.file-max"
                            file_line=`grep "$fname" $CONF_FILE 2>/dev/null |grep -v "^[#;][.]*" | tail -1`
                            if [ -n "$file_line" ] ; then
                               IFS="="
                               set $file_line
                               IFS="$OLDIFS"
                               check_value ${2:-0} $larger_value 
                               if [ $? -ne 0 ];then
                                   UPDATE_CONF=0
                                   larger_value=$2
                               fi
                            fi
                         if [ $UPDATE_CONF -eq 1 ] ; then
                            debug_echo 1 "Adding entry: $parm_name $cur_set $larger_value"
                            printf "%-11s %14u %16u\n" file-max $cur_set $larger_value >> $KERNEL_PARM_OUT
                            rc_val=$KERNPARM_WARN
                         fi
                      else  #KERNPARM_TYPE
                         check_value $need_value $2 
                         if [ $? -ne 0 ];then
                            printf "%-11s %14u %16u\n" file-max $2 $need_value >> $KERNEL_PARM_OUT
                            rc_val=$KERNPARM_WARN
                         fi
                      fi    #KERNPARM_TYPE
                   else
                      debug_echo 1 "file-max is not in sysctl output"
                   fi
                   ;;
         *)
                   debug_echo 1 "Parameter $parm_name is not in sysctl output"
                   ;;
      esac 
   fi
done < $KERNEL_PARM_LIST

[ ${DEBUG_ON:-0} -eq 0 ] && rm -f $SYSCTL_OUT 2>/dev/null

debug_echo 1 "Exiting with return code $rc_val"

exit $rc_val

