#!/bin/bash

#
# Copyright (C) 2009 McAfee, Inc.  All rights reserved.
#

# chkconfig: 35 00 99
# description: Start the McAfee Solidifier service

### BEGIN INIT INFO
# Provides:		scsrvc
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Stop-After:         network
# Default-Start:	3 5
# Default-Stop:		0 1 2 4 6 S
# Description: Start the McAfee Solidifier service
### END INIT INFO

# Variable Initializations

# For 'tasklist_lock'
symbol1="tasklist_lock"
pattern1="@@TASKLIST_LOCK@@"
conf_file_var1="TaskListLockAddr"

# For 'find_task_by_pid_ns' 
symbol2="find_task_by_pid_ns"
pattern2="@@FIND_TASK_BY_PID_NS@@"
conf_file_var2="FindTaskByPidNsAddr"

# For systemcall table 
# ********************
symbol3="sys_call_table"
pattern3="@@SYS_CALL_TABLE@@"
conf_file_var3="SysCallTableAddr"

# For 'schedule_tail' 
# *******************
symbol4="schedule_tail"
pattern4="@@SCHEDULE_TAIL@@"
conf_file_var4="ScheduleTailAddr"

# For 'ret_from_fork'
# *******************
symbol5="ret_from_fork"
pattern5="@@RET_FROM_FORK@@"
conf_file_var5="RetFromForkAddr"

# For 'formats'
# *************
symbol6="formats"
pattern6="@@FORMATS@@"
conf_file_var6="FormatsAddr"

# For 'ia32_sys_call_table'
# *************
symbol7="ia32_sys_call_table"
pattern7="@@IA32_SYS_CALL_TABLE@@"
conf_file_var7="IA32SysCallTableAddr"

# For 'ia32_sys_call_table'
# *************
symbol8="ia32_sysret"
pattern8="@@IA32_SYSRET@@"
conf_file_var8="IA32SysretAddr"

# For 'kern_path_parent'
# *************
symbol9="kern_path_parent"
pattern9="@@KERN_PATH_PARENT_ADDR@@"
conf_file_var9="KernPathParentAddr"


# For all symbols search here 
symbol_file_proc="/proc/kallsyms"
symbol_file_map="/boot/System.map-`uname -r`"

#
_conf_file_dir="/etc/mcafee/solidcore"
_conf_file="$_conf_file_dir/solidcore.conf"
_conf_file_tmp="$_conf_file_dir/solidcore.conf.tmp"

#
eof_pattern="SOLIDCORE_CONF_EOF"

#
IS_MA_5_ENABLE="NO"

if [ "$IS_MA_5_ENABLE" = "YES" ]; then
	EVT_BIN="bin/scnexgen-pl"
else
	EVT_BIN="bin/scevtgen"
fi

set_sid() {
	_filename=$1
	_service_pid=`cat $_filename|awk -F: '{print $1}'`
	dprintf "_service_pid is $_service_pid$END_CHAR"
    }    

set_watcher_pid() {
	_filename=$1
	_watcher_pid=`cat $_filename|awk -F: '{print $2}'`
	dprintf "_watcher_pid is $_watcher_pid$END_CHAR"
    }

is_service_running() {
	if [ "$SERVICE_SCOPE" = "FULL" ]; then
		ps -aefww | grep "bin/scsrvc" | grep -v grep > /dev/null 2>&1
	else
		ps -aefww | grep "${EVT_BIN}" | grep -v grep > /dev/null 2>&1
	fi

	return $?
    }

# $1 is the lock/pid file
checkservice() {
	evt_string=""
	[ "$SERVICE_SCOPE" = "EVT" ] && evt_string="Event Generation"

	CUR_UID=`id | cut -b 5`
	if [ "$CUR_UID" != 0 ]; then
	      echo "Failed to start/stop McAfee Solidifier $evt_string service. Insufficient privileges."
	      exit 1
	fi

	_filename=$1
	if [ -f $_filename ]; then
	    set_sid $_filename
	    
	    if [ "x$_service_pid" != "x" ]
	    then
		if [ $_service_pid -eq -1 ]; then
		    dprintf "Found -1 in pid file$END_CHAR"
		    return 2
		fi

		dprintf "Checking for service pid $_service_pid$END_CHAR"

		if [ "$SERVICE_SCOPE" = "FULL" ]; then
			ps -fwwg $_service_pid | grep "bin/scsrvc" | grep -v grep > /dev/null 2>&1
		else
			ps -fwwg $_service_pid | grep "${EVT_BIN}" | grep -v grep > /dev/null 2>&1
		fi

		if [ $? -eq 0 ]; then
		    dprintf "$evt_string Service is running with pid $_service_pid$END_CHAR"
		    return 0 # service running
		fi
	    else    
		dprintf "_service_pid is Empty.$END_CHAR"
	    fi
	fi
	return 1 # service not running
    }

get_linux_arch ()
    {
	b=`uname -m 2>/dev/null`
	# For some old linux'es, -i option is not supported with uname. We default
	# to X86 in such a case until we actually start supporting these distros on
	# AMD64.
	if [ $? -ne 0 ]; then
	    host_arch="X86"
	else
	    case $b in
	    i386)  host_arch="X86";;
	    i486)  host_arch="X86";;
	    i586)  host_arch="X86";;
	    i686)  host_arch="X86";;
	    amd64) host_arch="AMD64";;
	    x86_64)host_arch="AMD64";;
	    *)echo "Unsupported Architecture $b found on machine.";;
	    esac
	fi
    }

get_solaris_arch ()
    {
	a=`isainfo -k`
	case $a in
	i386)   host_arch="X86";;
	amd64)  host_arch="AMD64";;
	sparcv9)host_arch="SPARC64";;
	*)echo "Unsupported Architecture $a found on machine.";;
	esac
    }

get_aix_arch ()
    {
	c=`getconf KERNEL_BITMODE`
	case $c in
	32)host_arch="PPC32";;
	64)host_arch="PPC64";;
	*)echo "Unsupported Architecture $c found on machine.";;
	esac
    }

get_hpux_arch ()
    {
	d=`uname -m`
	case $d in
	ia64)host_arch="IA64";;
	*)host_arch="PA64";;
	esac
    }

get_host_os ()
    {
	# Here we will find the host machine OS
	OS=`uname -s`
	case $OS in
	SunOS)
	    H_OS="SOLARIS"
	    ;;
	Linux)
	    H_OS="LINUX"
	    ;;
	HP-UX)
	    H_OS="HPUX"
	    ;;
	AIX)
	    H_OS="AIX"
	    ;;
	*)
	    echo "Unsupported Host OS: ($OS) found on this machine.";
	    return 1
	    ;;
	esac
    }

get_host_arch ()
    {
	case $H_OS in
	SOLARIS)
	    get_solaris_arch
	    ;;
	LINUX)
	    get_linux_arch
	    ;;
	AIX)
	    get_aix_arch
	    ;;
	HPUX)
	    get_hpux_arch
	    ;;
	*)
	    echo "Invalid Host OS ($H_OS) found on this machine."
	    host_arch="NONE"
	    return 1
	    ;;
	esac
    }

dprintf()
    {
	if [ $DEBUG ]
	then
	    _logfile=/tmp/.scsrvc.log.$$
	    $PRINT_COMMAND "$*" >> $_logfile
	fi
    }

lprintf()
    {
	if [ $DEBUG ]
	then
	    _logfile=/tmp/.scsrvc.log.$$
	    $PRINT_COMMAND "$*" | tee -a $_logfile
	else
	    $PRINT_COMMAND "$*"
	fi
    }

is_driver_loaded()
{
    ret=1

    if [ "$H_OS" = "LINUX" ]; then
	lsmod | grep $SOLIDCORE_DRV >/dev/null 2>&1
	ret=$?
    fi

    return $ret
}

# $1 is the file name
# $2 is the number of reties to attempt before it give up and return non zero
# $3 is the sleep time before next retry
check_lock_file()
    {
	_filename=$1
	_retires=$2
	_sleep_in_seconds=$3
	_iter=1
	_test="test -s"
	# first check if the file exists and has some content
	$_test $_filename >/dev/null 2>&1
	while [ $? -ne 0 -a $_iter -le $_retires ]
	do
	    dprintf "$_filename doesn't exist yet. "
	    dprintf "Sleeping for $_sleep_in_seconds$END_CHAR"
	    sleep $_sleep_in_seconds
	    _iter=`expr $_iter + 1`
	    $_test $_filename >/dev/null 2>&1
	done

	if [ $_iter -gt $_retires ]; then
	    # file still doesn't exist.. iterations over
	    dprintf "$_filename doesn't exist. Iterations over.$END_CHAR"
	    return 1
	fi

	# if iterations are not over yet .. validate the service pid
	checkservice $_filename >/dev/null 2>&1
	ret=$?
	while [ $ret -ne 0 -a $_iter -le $_retires ]
	do
	    [ $ret -eq 2 ] && break
	    dprintf "Service pid not yet validated. "
	    dprintf "Sleeping for $_sleep_in_seconds$END_CHAR"
	    sleep $_sleep_in_seconds
	    _iter=`expr $_iter + 1`
	    checkservice $_filename >/dev/null 2>&1
	    ret=$?
	done

	checkservice $_filename >/dev/null 2>&1
	return $?
    }

# $1 is the lock/pid file
killservice() {
	_filename=$1

	set_sid $_filename

	if [ "$SERVICE_SCOPE" = "FULL" ] && [ "$H_OS" = "SOLARIS" ] && [ "`uname -r`" = "5.10" ]; then
	    if [ -z "$SMF_FMRI" ]; then
		/usr/bin/svcs scsrvc > /dev/null 2>&1
		if [ $? -eq 0 ]; then
		    # Don't attempt to stop the service through svcadm if its in
		    # 'maintenance' state.
		    is_svc_state_corrupted
		    if [ $? -ne 0 ]; then
			/usr/sbin/svcadm disable -t scsrvc > /dev/null 2>&1
			return $?
		    fi    
		fi
	    fi
	fi

	dprintf "Going to kill service pid $_service_pid$END_CHAR"
	if [ "$SERVICE_SCOPE" = "FULL" ]; then
	    kill -USR1 -$_service_pid > /dev/null 2>&1
	else
	    kill -USR2 -$_service_pid > /dev/null 2>&1
	fi

	return $?
    }

# $1 is the lock/pid file
stopservice() {
	evt_string=""
	[ "$SERVICE_SCOPE" = "EVT" ] && evt_string="Event Generation"

	dprintf "GOING TO STOP $evt_string SERVICE$END_CHAR"
	_filename=$1
	_retires=50
	_sleep_in_seconds=2
	_iter=1

	lock_file_exists="no"
	lock_file_empty="yes"

	if [ -f $_filename ]; then
	    lock_file_exists="yes"
	fi

	if [ "$lock_file_exists" = "yes" ]; then
	    set_sid $_filename

	    if [ "x$_service_pid" != "x" ]; then
		lock_file_empty="no"
	    fi	
	fi	
	    
	#Handle the case where lockfile does not exist or exists but is empty.
	if [ "$lock_file_exists" = "no" -o "$lock_file_empty" = "yes" ]; then
	    is_service_running
	    if [ $? -eq 0 ]; then
		for pid in `ps -aefww | grep -e "${EVT_BIN}" | grep -v grep | awk '{print $2}'`
		do
		    kill -USR2 $pid
		done

		if [ "$SERVICE_SCOPE" = "FULL" ]; then
		    for pid in `ps -aefww | grep -e "/bin/scsrvc" | grep -v grep | awk '{print $2}'`
		    do
			kill -KILL $pid
		    done
		fi
	        # check that the service is actually stopped
	        # wait for a while and keep checking the service
	        is_service_running
	        while [ $? -eq 0 -a $_iter -le $_retires ]
	        do
		    # service still running
		    dprintf "$evt_string Service is still running.$END_CHAR"
		    dprintf "Going to sleep for $_sleep_in_seconds$END_CHAR"
		    sleep $_sleep_in_seconds
		    _iter=`expr $_iter + 1`
	            is_service_running
		done
		if [ $_iter -gt $_retires ]; then
		    lprintf "Error stopping McAfee Solidifier $evt_string service.$END_CHAR"
		    return 1		
		else
		    lprintf "McAfee Solidifier $evt_string service stopped successfully.$END_CHAR"
		    [ "$SERVICE_SCOPE" = "FULL" ] && rm -f $_filename >/dev/null 2>&1
		    return 0
		fi
	    else    
		lprintf "McAfee Solidifier $evt_string service is not running.$END_CHAR"
		return 0
	    fi    
	fi	

	checkservice $_filename
	RETCODE=$?
	if [ $RETCODE -ne 0 ]; then
	    lprintf "McAfee Solidifier $evt_string service is not running.$END_CHAR"
	    # In case of windriver we are overwriting return val to 0,
	    # So that it does not break rpm uninstall workflows.
            if [ "$IS_WRL" = "YES" ]; then
	        RETCODE=0
	    fi
	    return $RETCODE
	fi

	killservice $_filename
	RETCODE=$?
	if [ $RETCODE -ne 0 ]; then
	    is_driver_loaded
	    if [ $? -eq 0 -a $RETCODE -eq 1 ]; then
		lprintf "Stopping McAfee Solidifier $evt_string service: Permission Denied.$END_CHAR"
	    else
	    	lprintf "Error stopping McAfee Solidifier $evt_string service.$END_CHAR"
	    fi
	else
	    # check that the service is actually stopped
	    # wait for a while and keep checking the service-pid
	    checkservice $_filename >/dev/null 2>&1
	    while [ $? -eq 0 -a $_iter -le $_retires ]
	    do
		# service still running
		dprintf "$evt_string Service is still running.$END_CHAR"
		dprintf "Going to sleep for $_sleep_in_seconds$END_CHAR"
		sleep $_sleep_in_seconds
		_iter=`expr $_iter + 1`
		checkservice $_filename >/dev/null 2>&1
		if [ $? -eq 0 ] && [ "$SERVICE_SCOPE" = "FULL" ]; then
		    # try to kill again
		    killservice $_filename
		else
		    # check again
		    checkservice $_filename >/dev/null 2>&1
		fi
	    done

	    checkservice $_filename >/dev/null 2>&1
	    RETCODE=$?
	    if [ $RETCODE -ne 0 ]; then
		lprintf "McAfee Solidifier $evt_string service stopped successfully.$END_CHAR"
		[ "$SERVICE_SCOPE" = "FULL" ] && rm -f $_filename >/dev/null 2>&1
		[ "$H_OS" = "LINUX" ] && is_platform_not_ubuntu && rm -f /var/lock/subsys/scsrvc
		RETCODE=0
	    else
		is_driver_loaded
	        if [ $? -eq 0 -a $RETCODE -eq 1 ]; then
			lprintf "Stopping McAfee Solidifier $evt_string service: Permission Denied.$END_CHAR"
	    	else
	    		lprintf "Error stopping McAfee Solidifier $evt_string service.$END_CHAR"
	    	fi

		RETCODE=1
	    fi
	fi

	if [ "$SERVICE_SCOPE" = "FULL" ]; then
	    _home_dir=`cat $_conf_file | grep InstallDir | awk -F' ' '{print $3}' `

	    COVFILE="/$_home_dir/run/solidcoreS3.cov"

	    COVERAGE="NO"
	    [ -f $COVFILE ] && COVERAGE="YES"

	    if [ "$COVERAGE" = "YES" ]; then
		export COVFILE
	        if [ "$H_OS" = "LINUX" ]; then
		    COVGET="/opt/BullseyeCoverage/bin/covgetkernel"
		    [ -f $COVGET ] && $COVGET > /dev/null 2>&1
		fi
	    fi
	fi

	return $RETCODE
    }

# Register scdrv in HP-UX
register_driver ()
    {
	# Skip if already loaded.
	kmadmin -Q ${SOLIDCORE_DRV} > /dev/null 2>&1
	if [ $? -eq 0 ]; then
	    return 0
	fi
	
	cd $_home_dir/kmod
	kminstall -u ${SOLIDCORE_DRV} >/dev/null 2>&1
	config -M ${SOLIDCORE_DRV} -u >/dev/null 2>&1

	if [ $? != 0 ]; then
	    echo "Registration of ${SOLIDCORE_DRV} failed."
	    return 1
	fi    
    }

# Create /dev/scdrv device in HP-UX
create_device ()
    {
	DRV=`lsdev | grep $SOLIDCORE_DRV`
	if [ ! -z "$DRV" ]; then
	    major_c=`echo $DRV|awk '{print $1}'`
	    rm -f $DRVFILE > /dev/null 2>&1
	    mknod $DRVFILE c $major_c 0
	    ret=$?

	    if [ $ret -ne 0 ]; then
		echo "ERROR: Failed to create device $DRVFILE."
		return $ret
	    fi
	else
	    echo "McAfee Solidifier module $SOLIDCORE_DRV is not registered."
	    return 2
	fi
    }

is_svc_state_corrupted () {
	state=`svcs scsrvc|grep scsrvc|awk '{print $1}'`
	if [ "$state" = "maintenance" ]; then
	    return 0
	else
	    return 1
	fi	
    }

set_addr ()
{
    if [ $# -lt 3 ]; then
	lprintf "Insufficient number of args ($#) specified for set_addr."
	return 1
    fi
    
    symbol=$1
    pattern=$2
    conf_file_var=$3
     
    symbol_found_proc=0
    symbol_found_map=0

    # Skip if scdrv is already loaded.
    is_driver_loaded
    if [ $? -eq 0 ]; then
        dprintf "Module $SOLIDCORE_DRV already loaded. "
	dprintf "Skipping setting '$symbol' address.$END_CHAR"
	return 0;
    fi

        # Make sure the kernel proc/kallsym file exists on the system.
        ls $symbol_file_proc >/dev/null 2>&1
        if [ $? -ne 0 ]; then
	    lprintf "kernel symbol file '$symbol_file_proc' doesn't exist.$END_CHAR"
	else 
        # File found. Lets find the symbol addr from it.
    	symbol_addr_proc=`grep -w "$symbol\$" $symbol_file_proc | grep -vE "\[|\]" | head -1 | awk '{print $1}' 2>/dev/null`
        symbol_addr_proc=`echo $symbol_addr_proc`
	dprintf "'$symbol' address found from $symbol_file_proc is '$symbol_addr_proc'.$END_CHAR"
	fi


	# Make sure the file contains the symbol's address.
        if [ -z "$symbol_addr_proc" ]; then
            dprintf "Unable to find symbol $symbol in $symbol_file_proc.$END_CHAR"
	else 
	symbol_found_proc=1;
	fi


        # Make sure the kernel map/kallsym file exists on the system.
        ls $symbol_file_map >/dev/null 2>&1
        if [ $? -ne 0 ]; then
	    lprintf "kernel symbol file '$symbol_file_map' doesn't exist.$END_CHAR"
	else 
        # File found. Lets find the symbol addr from it.
    	symbol_addr_map=`grep -w "$symbol\$" $symbol_file_map | grep -vE "\[|\]" | head -1 | awk '{print $1}' 2>/dev/null`
        symbol_addr_map=`echo $symbol_addr_map`
	dprintf "'$symbol' address found from $symbol_file_map is '$symbol_addr_map'.$END_CHAR"
	fi


	# Make sure the file contains the symbol's address.
        if [ -z "$symbol_addr_map" ]; then
            dprintf "Unable to find symbol $symbol in $symbol_file_map.$END_CHAR"
	else 
	symbol_found_map=1;
	fi

	# if the symbol is found in both the files, validate it
	if [ "$symbol_found_proc" -eq "1" ]; then
	    if [ "$symbol_found_map" -eq "1" ]; then
		if [ "$symbol_addr_map" = "$symbol_addr_proc" ]; then
		    symbol_addr=$symbol_addr_proc
		else 
		    lprintf "symbol $symbol in $symbol_file_map does not match with symbol $symbol in $symbol_file_proc.$END_CHAR"
		    return 2
		fi 
	    else 
		symbol_addr=$symbol_addr_proc
	    fi 
	else
	    if [ "$symbol_found_map" -eq "1" ]; then
		symbol_addr=$symbol_addr_map
	    else
		lprintf "Unable to find symbol $symbol in $symbol_file_map and $symbol_file_proc.$END_CHAR"
		return 2
	    fi 
	fi

        # Skip if new value is already present.
        symbol_addr_cur=`cat $_conf_file | grep -w $conf_file_var | awk -F " = " '{print $2}'`
	symbol_addr_cur=`echo $symbol_addr_cur`
	# For removing any trailing space.
        if [ "$symbol_addr_cur" = "0x$symbol_addr" ]; then
	    dprintf "No need to update $symbol address 0x$symbol_addr in conf file.$END_CHAR"
	    return 0;
        fi
        # make sure the pattern to be replaced exists in the conf file.
	#cat $_conf_file|grep "$pattern" 1>/dev/null 2>&1
        #if [ $? -ne 0 ]; then
        #    lprintf "Unable to find pattern $pattern in $_conf_file.$END_CHAR"
        #    return 2
	#fi

        # Pattern found. Replace it with the new address of tasklist_lock.
        cat $_conf_file|sed -e "s/$conf_file_var\ =\ $symbol_addr_cur/$conf_file_var\ =\ 0x$symbol_addr/" 1> $_conf_file_tmp 2>/dev/null
        cat $_conf_file_tmp | grep $eof_pattern > /dev/null 2>&1
        if [ $? -ne 0 ] ; then
            lprintf "There is not enough space in filesystem that contains $_conf_file_dir."
            lprintf "Please make sure that atleast $size_orig bytes are available.$END_CHAR"
            rm -rf $_conf_file_tmp
            return 1
        fi
	mv -f $_conf_file_tmp $_conf_file
}

set_aix_install_TL_SP ()
{
    pattern="@@AIX61_INSTALL_TL_SP@@"
    conf_file_var="AIX61InstallTLSP"

    # Skip if scdrv is already loaded.
    genkex |grep $SOLIDCORE_DRV >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        dprintf "Module $SOLIDCORE_DRV already loaded. "
	dprintf "Skipping setting '$symbol' address.$END_CHAR"
	return 0;
    fi

    if [ "6.1" = `uname -v`.`uname -r` ]; then

	# Find OS TL & SP information
	aix61_tl_sp=`oslevel -sq 2>/dev/null| head -n 1 |  cut -f2 -d'-' 2>/dev/null`
        dprintf "AIX 6.1 TL is '$aix61_tl_sp'.$END_CHAR"
	if [ -z "$aix61_tl_sp" ]; then
            lprintf "Unable to find AIX 6.1 TL.$END_CHAR"
            lprintf "Error starting McAfee Solidifier service.$END_CHAR"
            return 2
	fi

        # Skip if new value is already present.
        if [ "`cat $_conf_file |grep -w "$conf_file_var"|awk -v awkvar=$pattern -F ' = ' '{print awkvar}'`" = "$aix61_tl_sp" ]; then
	    dprintf "No need to update $pattern $aix61_tl_sp in conf file.$END_CHAR"
	    return 0;
        fi

        # make sure the pattern to be replaced exists in the conf file.
	cat $_conf_file|grep "$pattern" 1>/dev/null 2>&1
        if [ $? -ne 0 ]; then
            lprintf "Unable to find pattern $pattern in $_conf_file.$END_CHAR"
            return 2
	fi

        # Pattern found. Replace it with the new address of tasklist_lock.
        cat $_conf_file|sed -e "s/$pattern/$aix61_tl_sp/" > $_conf_file_tmp
	mv -f $_conf_file_tmp $_conf_file

    fi
}

set_tasklist_lock_addr () 
{
    set_addr $symbol1 $pattern1 $conf_file_var1 
}

set_find_task_by_pid_ns_addr ()
{
    set_addr $symbol2 $pattern2 $conf_file_var2
}

set_sys_call_table_addr ()
{
    set_addr $symbol3 $pattern3 $conf_file_var3
}

set_schedule_tail_addr ()
{
    set_addr $symbol4 $pattern4 $conf_file_var4
}

set_ret_from_fork_addr ()
{
    set_addr $symbol5 $pattern5 $conf_file_var5
}

set_formats_addr ()
{
    set_addr $symbol6 $pattern6 $conf_file_var6
}

set_ia32_sys_call_table_addr ()
{
    set_addr $symbol7 $pattern7 $conf_file_var7
}

set_ia32_sysret_addr ()
{
    set_addr $symbol8 $pattern8 $conf_file_var8
}

set_kern_path_parent_addr ()
{
    set_addr $symbol9 $pattern9 $conf_file_var9
}

run_sanity ()
{
    SANITY_DIR=$1

    OUTPUT_FILE=/dev/null
    if [ $DEBUG ]
    then
	OUTPUT_FILE=/tmp/.scsrvc.log.$$
    fi

    dprintf "Executing build verification test...$END_CHAR"

    SC_KERNEL_UPGRADE=1 $SANITY_DIR/wrapper_script.sh >>$OUTPUT_FILE 2>&1
    if [ $? -ne 0 ]; then
	lprintf "Failed to execute build verification test.$END_CHAR"
    else
	result=`cat $SANITY_DIR/RESULTS`
	dprintf "Result: $result $END_CHAR"

	echo $result | grep FAILED > /dev/null 2>&1
	[ $? -eq 0 ] && lprintf "Build verification test failed.$END_CHAR"
    fi
}

is_platform_not_ubuntu ()
{
    if [ -f "/etc/lsb-release" -a ! -f "/etc/SuSE-release" ]; then
	    PLAT=`cat "/etc/lsb-release" | grep -w DISTRIB_DESCRIPTION | head -n 1 | awk -F"=" '{print $2}' | awk -F"\"" '{print $2}'|awk '{print $1}'`
	    if [ "$PLAT" = "Ubuntu" ]; then
		return 1
	    fi
    fi
    return 0    
}

check_and_restore_conf ()
{
#Check if solidcore.conf is currupted due to system crash.
    if [ ! -f $_conf_file ]; then
        dprintf "Orignal conf is missing.$END_CHAR";
        if [ -f $_conf_file_tmp ]; then
            cat $_conf_file_tmp | grep $eof_pattern > /dev/null 2>&1
            if [ $? -ne 0 ] ; then
                lprintf "Error restoring solidcore.conf. EOF marker is missing.$END_CHAR";
                lprintf "Error starting McAfee Solidifier service.$END_CHAR"
                return 1;
            fi
            mv $_conf_file_tmp $_conf_file
            if [ $? -ne 0 ] ; then
                lprintf "Error while restoring solidcore.conf.$END_CHAR";
                lprintf "Error starting McAfee Solidifier service.$END_CHAR"
                return 1;
            fi
            dprintf "Orignal conf is restored.$END_CHAR";
        else
            lprintf "Error restoring solidcore.conf. Conf files are missing.$END_CHAR";
            lprintf "Error starting McAfee Solidifier service.$END_CHAR"
            return 1;
        fi
    fi
}

# $1 is the lock/pid file
startservice() {
	_filename=$1

	evt_string=""
	[ "$SERVICE_SCOPE" = "EVT" ] && evt_string="Event Generation"

        #Check (and restore) if original solidcore.conf is missing.
        check_and_restore_conf
        check_ret=$?
        if [ $check_ret -ne 0 ] ; then
            return $check_ret
        fi


	#Handle the case where the lockfile .scsrvc-lock doesnt exist."
	if [ ! -f $_filename ]; then
	    is_service_running
	    if [ $? -eq 0 ]; then
		lprintf "Warning: McAfee Solidifier $evt_string service is already running, but the pid file '$_filename' does not exist.$END_CHAR"
		lprintf "Please restart the McAfee Solidifier service to fix this.$END_CHAR"
		return 0
	    fi    
	    if [ "$SERVICE_SCOPE" = "EVT" ]; then
		lprintf "Error: Cannot start McAfee Solidifier $evt_string service as the pid file '$_filename' does not exist.$END_CHAR"
		lprintf "Please restart the McAfee Solidifier service to fix this.$END_CHAR"
		return 1
	    fi
	else	
	    #Handle the case where the lockfile .scsrvc-lock exists but is empty."
	    set_sid $_filename
	    
	    if [ "x$_service_pid" = "x" ]; then
		is_service_running
		if [ $? -eq 0 ]; then
		    lprintf "Warning: McAfee Solidifier $evt_string service is already running, but the pid file '$_filename' is empty.$END_CHAR"
		    lprintf "Please restart the McAfee Solidifier service to fix this.$END_CHAR"
		    return 0
		fi
		if [ "$SERVICE_SCOPE" = "EVT" ]; then
		    lprintf "Error: Cannot start McAfee Solidifier $evt_string service as the pid file '$_filename' is empty.$END_CHAR"
		    lprintf "Please restart the McAfee Solidifier service to fix this.$END_CHAR"
		    return 1
		fi
	    else
		is_service_running
		if  [ $? -ne 0 ]; then
		    [ "$SERVICE_SCOPE" = "FULL" ] && rm -f $_filename >/dev/null 2>&1
		fi   
	    fi	
	fi	
	
	checkservice $_filename
	if [ $? -eq 0 ]; then
	    lprintf "McAfee Solidifier $evt_string service is already running.$END_CHAR"
	    return 0
	fi

    _home_dir=`cat $_conf_file | grep InstallDir | awk -F' ' '{print $3}' `

    if [ "$SERVICE_SCOPE" = "FULL" ] && [ "$H_OS" = "SOLARIS" ] && [ "`uname -r`" = "5.10" ]; then
	# In single user mode, don't use the svc framwork as it fails
	# to start service because of unsatisfied dependencies. 
	_run_level=`who -r | awk {'print $3'}`
	if [ "$_run_level" = "S" ]; then
	    dprintf "System is in single user mode.$END_CHAR"
	elif [ -z "$SMF_FMRI" ]; then
	    /usr/bin/svcs scsrvc > /dev/null 2>&1
	    if [ $? -ne 0 ]; then
		/usr/sbin/svccfg import /var/svc/manifest/system/scsrvc.xml > /dev/null 2>&1
		sleep 2
	    fi

	    /usr/sbin/svcadm enable -s scsrvc > /dev/null 2>&1
	    RETCODE=$?

	    if [ $RETCODE -ne 0 ]; then
		# When the service is killed by another process, svc.startd
		# moves it to the 'maintenance' state after which it doesnt
		# start without being reset.
		is_svc_state_corrupted

		if [ $? -eq 0 ]; then
		    /usr/sbin/svcadm clear scsrvc
		    RETCODE=$?
		    sleep 2
		fi	
	    fi

	    if [ $RETCODE -ne 0 ]; then
        	lprintf "Error starting McAfee Solidifier service.$END_CHAR"
	    else
		lprintf "McAfee Solidifier service started successfully.$END_CHAR"
	    fi

	    return $RETCODE
	fi
    fi
    dprintf "GOING TO START $evt_string SERVICE$END_CHAR"

    if [ "$SERVICE_SCOPE" = "FULL" ] && [ "$H_OS" = "HPUX" ]; then
	if [ "`uname -r`" != "B.11.23" -a "`uname -r`" != "B.11.31" ]; then
	    if [ -f "$KM_INIT_FILE" ]; then
		. $KM_INIT_FILE
	    fi	

	    # If KM_INIT is not set to 1 then DLKM initializations
	    # would not have taken place and we need to do it ourselves.
	    if [ "$KM_INIT" != 1 ]; then
		register_driver
		[ $? -ne 0 ] && return 1;
            fi 
	fi 

        # For All HP-UX Distros, create device on every service invocation
	create_device
	[ $? -ne 0 ] && return 1;
    fi

    # Populate tasklist_lock's address from kernel conf file.
    if [ "$SERVICE_SCOPE" = "FULL" ] && [ "$H_OS" = "LINUX" ]; then
	if [ "$IS_WRL" != "YES" ]
	then
		K_VER1=`uname -r | awk -F "-" '{print $1}' | awk -F "." '{print $1}'`
		K_VER2=`uname -r | awk -F "-" '{print $1}' | awk -F "." '{print $2}'`
		K_VER3=`uname -r | awk -F "-" '{print $1}' | awk -F "." '{print $3}'`

		if [ $K_VER1 -gt 2 ] || [ $K_VER1 -eq 2 -a $K_VER2 -gt 6 ] || [ $K_VER1 -eq 2 -a $K_VER2 -eq 6 -a $K_VER3 -ge 18 ]; then
			set_tasklist_lock_addr
			if [ $? -ne 0 ]; then
				echo "Error starting McAfee Solidifier service.$END_CHAR"
				return 2
			fi
		fi
	
		if [ $K_VER1 -gt 2 ] || [ $K_VER1 -eq 2 -a $K_VER2 -gt 6 ] || [ $K_VER1 -eq 2 -a $K_VER2 -eq 6 -a $K_VER3 -ge 31 ]; then
			set_find_task_by_pid_ns_addr
			if [ $? -ne 0 ]; then
				echo "Error starting McAfee Solidifier service.$END_CHAR"
				return 2
			fi
		fi

		set_sys_call_table_addr
		if [ $? -ne 0 ]; then
			echo "Error starting McAfee Solidifier service.$END_CHAR"
			return 2
		fi

		set_schedule_tail_addr
		if [ $? -ne 0 ]; then
			echo "Error starting McAfee Solidifier service.$END_CHAR"
			return 2
		fi

		set_ret_from_fork_addr
		if [ $? -ne 0 ]; then
			echo "Error starting McAfee Solidifier service.$END_CHAR"
			return 2
		fi

		set_formats_addr
		if [ $? -ne 0 ]; then
			echo "Error starting McAfee Solidifier service.$END_CHAR"
			return 2
		fi

		if [ "$host_arch" = "AMD64" ]; then
			set_ia32_sys_call_table_addr
			if [ $? -ne 0 ]; then
				echo "Error starting McAfee Solidifier service.$END_CHAR"
				return 2
			fi
        
			set_ia32_sysret_addr
			if [ $? -ne 0 ]; then
				echo "Error starting McAfee Solidifier service.$END_CHAR"
				return 2
			fi
		fi
        
	fi #/*if [ "$IS_WRL" != "YES" ]*/
	is_driver_loaded
	if [ $? -ne 0 -a ! -d $_home_dir/kmod/`uname -r` ]; then
	    #Kernel has been upgraded, trigger build
	    lprintf "$END_CHAR"
	    lprintf "McAfee Solidifier: Found new kernel, trying to locate/build appropriate module....$END_CHAR "
	    $_home_dir/dks/build_target.sh install > "$_home_dir/dks/build.log_`date +%Y_%m_%d_%H_%M_%S`" 2>&1

	    if [ $? -ne 0 ]; then
		lprintf "Failed to build module.$END_CHAR"
		lprintf "Error starting McAfee Solidifier service.$END_CHAR"
		return 2
	    fi
	fi

	# Fix for bug 21551.
	service rpcidmapd start >/dev/null 2>&1 
	RPCRET=$?

        # Throw warning on screen for Redhat 4 and 5
	REL_FILE="/etc/redhat-release"
	if [ -r $REL_FILE ]; then
	    LINUX_REL=`cat $REL_FILE| head -n 1 | awk -F " [(]" '{ print $1 }' | awk -F "." '{ print $1 }'`
            echo $LINUX_REL|grep "Enterprise Linux"|egrep 'release 4|release 5' >/dev/null 2>&1
            if [ $? -eq 0 -a $RPCRET -ne 0 ]; then
		lprintf "Warning: Unable to start service 'rpcidmapd'. ret code: $RPCRET.$END_CHAR"
            fi
        fi
    fi

    if [ "$SERVICE_SCOPE" = "FULL" ] && [ "$H_OS" = "AIX" ]; then
	set_aix_install_TL_SP
        if [ $? -ne 0 ]; then
            lprintf "Error starting McAfee Solidifier service.$END_CHAR"
	    return 2
        fi
    fi

    if [ "$SERVICE_SCOPE" = "FULL" ]; then
	COVFILE="/$_home_dir/run/solidcoreS3.cov"

	COVERAGE="NO"
	[ -f $COVFILE ] && COVERAGE="YES"

	if [ "$COVERAGE" = "YES" ]; then
	    export COVFILE
	    if [ "$H_OS" = "LINUX" ]; then
		COVMOD="/opt/BullseyeCoverage/run/linuxKernel/libcov-lkm.ko"
		[ -f $COVMOD ] && insmod $COVMOD > /dev/null 2>&1
	    fi
	fi

	is_driver_loaded
	if [ $? -ne 0 -a -f "$_home_dir/bin/scsrvc.new" ]; then
	    mv -f $_home_dir/bin/scsrvc.new $_home_dir/bin/scsrvc > /dev/null 2>&1
	    ret=$?
	    if [ $ret -ne 0 ]; then
		lprintf "Failed to restore new service binary, ret code: $ret.$END_CHAR"
		lprintf "Error starting McAfee Solidifier service.$END_CHAR"
	    fi

	    if [ "$H_OS" = "LINUX" ]; then
		rte_mode=`cat $_conf_file | grep -w RTEMode | awk '{print $3}'`
		if [ "$rte_mode" != "0x0" -a "$rte_mode" != "0" ]; then
		    run_sanity $_home_dir/sanity
		fi
	    fi
	fi
    fi

    if [ "$SERVICE_SCOPE" = "FULL" ]; then
	$_home_dir/bin/scsrvc
    else
	# The name killservice might seem out of place here. What we need here
	# is just to send SIGUSR2 to service so that watcher starts scevtgen.
	# This is what exactly is done by killservice.
	killservice $_filename
    fi

    RETCODE=$?
    if [ $RETCODE -ne 0 ]
    then
        lprintf "Error starting McAfee Solidifier $evt_string service.$END_CHAR"
    else
        # Check if the service started successfully and has set the pid
        # in the lock file
	if [ "$SERVICE_SCOPE" = "FULL" ]; then
	    check_lock_file $_filename 900 2
	else
	    check_lock_file $_filename 5 2
	fi

        RETCODE=$?
        if [ $RETCODE -ne 0 ]; then
            lprintf "Error starting McAfee Solidifier $evt_string service.$END_CHAR"
        else
            lprintf "McAfee Solidifier $evt_string service started successfully.$END_CHAR"
    	    [ "$H_OS" = "LINUX" ] && is_platform_not_ubuntu && touch /var/lock/subsys/scsrvc;
        fi
    fi
    return $RETCODE
}

# $1 is the lock/pid file
servicestatus() {
    _filename=$1

    #Handle the case where the lockfile .scsrvc-lock doesnt exist."
    if [ ! -f $_filename ]; then
	is_service_running
	if [ $? -eq 0 ]; then
	    lprintf "Warning: McAfee Solidifier service is running, but the pid file '$_filename' does not exist.$END_CHAR"
	    lprintf "Please restart the McAfee Solidifier service to fix this.$END_CHAR"
	    return 0
	else    
	    dprintf "Warning: McAfee Solidifier service is not running, and the pid file '$_filename' does not exist.$END_CHAR"
	    lprintf "McAfee Solidifier service is not running.$END_CHAR"
	    return 1
	fi    
    fi	

    #Handle the case where the lockfile .scsrvc-lock exists but is empty."
    set_sid $_filename
    
    if [ "x$_service_pid" = "x" ]; then
	is_service_running
	if [ $? -eq 0 ]; then
	    lprintf "Warning: McAfee Solidifier service is running, but the pid file '$_filename' is empty.$END_CHAR"
	    lprintf "Please restart the McAfee Solidifier service to fix this.$END_CHAR"
	    return 0
	else    
	    dprintf "Warning: McAfee Solidifier service is not running, but the pid file '$_filename' is empty.$END_CHAR"
	    lprintf "McAfee Solidifier service is not running.$END_CHAR"
	    return 1
	fi 
    fi	
					    
    checkservice $_filename
    RETCODE=$?
    if [ $RETCODE -eq 0 ]; then
	set_watcher_pid $_filename

	if [ "x$_watcher_pid" != "x" ]; then
	    dprintf "Checking for watcher pid $_watcher_pid$END_CHAR"
	    
	    ps -wwp $_watcher_pid | grep "scsrvc" | grep -v grep > /dev/null 2>&1
	    if [ $? -ne 0 ]; then 
		lprintf "McAfee Solidifier service is running, but watcher process is dead.$END_CHAR"
		lprintf "Please restart the McAfee Solidifier service to fix this.$END_CHAR"
	    else	
		lprintf "McAfee Solidifier service is running.$END_CHAR"
            fi		
	else    
            lprintf "_watcher_pid is Empty.$END_CHAR"
	    lprintf "McAfee Solidifier service is running, but watcher process is dead.$END_CHAR"
            lprintf "Please restart the McAfee Solidifier service to fix this.$END_CHAR"
	fi    
    else
        lprintf "McAfee Solidifier service is not running.$END_CHAR"
    fi
    return $RETCODE
}

get_systemctl_log ()
{
    case $1 in	 	 
	"start")ignore_string="stop";;	 	 
    "stop")ignore_string="start";;	 	 
    *)ignore_string="dummy message";;	 	 
    esac	 	 

    #This is quite dumb, so atleast make sure that "stop" doesn't print any prior msg for "start" and vice-a-versa	 	 
    systemctl status scsrvc.service -l | grep "scsrvc\[" | grep -v "$ignore_string" | tail -1 | awk -F "]: " '{print $2}'
}

get_systemctl_status ()
{
    systemctl is-active scsrvc.service > /dev/null
    [ $? -eq 0 ] && return 0

    systemctl is-failed scsrvc.service > /dev/null
    [ $? -eq 0 ] && return 2

    return 1
}

handle_systemctl_start ()
{
    get_systemctl_status
    [ $? -eq 0 ] && lprintf "McAfee Solidifier service is already running.$END_CHAR" && return 0

    systemctl start scsrvc.service

    get_systemctl_log
    get_systemctl_status
    case $? in
    	0)return 0;;
    	*)return 1;;
    esac
}

handle_systemctl_stop ()
{
    get_systemctl_status
    [ $? -eq 1 ] && lprintf "McAfee Solidifier service is not running.$END_CHAR" && return 0

    systemctl stop scsrvc.service

    get_systemctl_log
    get_systemctl_status
    case $? in
    	0)return 1;;
    	1)return 0;;
    	2)systemctl start scsrvc.service; return 1;;
    esac
}

handle_systemctl_restart ()
{
    service_pid1=`ps -aefww | grep -e "/bin/scsrvc" | grep -v grep | awk '{print $2}'| head -1`

    systemctl restart scsrvc.service

    get_systemctl_log
    get_systemctl_status
    case $? in
    	0)
	    same_service_pid="NO"
            for pid in `ps -aefww | grep -e "/bin/scsrvc" | grep -v grep | awk '{print $2}'`
            do

                if [ "$pid" = "$service_pid1" ] ; then
                    same_service_pid="YES"
                fi
            done
            if [ "$same_service_pid" = "YES" ] ; then
                dprintf "McAfee Solidifier service is not restarted.$END_CHAR"
                return 1
            else
                dprintf "McAfee Solidifier service is restarted successfully.$END_CHAR"
                return 0
            fi
	;;

    	*)  
            dprintf "McAfee Solidifier service is not restarted.$END_CHAR"
            return 1
        ;;
    esac
}

#Lets get the installation dir to set the .scsrvc-lock path.
IS_WRL=YES
if [ "$IS_WRL" != "YES" ]; then
    PID_FILE=/tmp/.scsrvc-lock
else
    _home_dir=`cat $_conf_file | grep InstallDir | awk -F' ' '{print $3}' `
    PID_FILE="$_home_dir/run/.scsrvc-lock"
fi

#Check if systemd or sysv 
IS_SYSTEMD=`ls -l /\sbin/\init | grep -i "systemd"`
if [ ! -z "$IS_SYSTEMD" ]; then
   IS_SYSTEMD_SUPPORTED="yes"
else
   IS_SYSTEMD_SUPPORTED="no"
fi

SOLIDCORE_DRV=scdrv
if [ "x$2" != "x" ]
then
    DEBUG=1
fi

get_host_os
[ $? -ne 0 ] && exit 1;

get_host_arch
[ $? -ne 0 ] && exit 1;

if [ "$H_OS" = "LINUX" ]; then
        size_orig=`stat -c %s $_conf_file`
fi


if [ "$H_OS" = "HPUX" ]; then 
    PRINT_COMMAND=echo
    END_CHAR=""
    
    PATH=$PATH:/sbin:/usr/bin:/usr/sbin
    export PATH

    DRVFILE=/dev/${SOLIDCORE_DRV}
    KM_INIT_FILE=/etc/rc.config.d/kminit
else
    PRINT_COMMAND=printf
    END_CHAR="\n"
fi

if [ ! -z "$2" ]; then
    lprintf "Usage: $0 { start | stop | status | restart }$END_CHAR"
    RETVAL=1
    exit $RETVAL;
fi

# Set locale information as it may not be set for init scripts
if [ "$H_OS" = "LINUX" ]; then
    [ -f /etc/profile.d/lang.sh ] && . /etc/profile.d/lang.sh
fi

host_distro="Unknown"
EL7="Red Hat Enterprise Linux Server release 7"
RELEASE_FILE="/etc/redhat-release"
if [ -r $RELEASE_FILE ]; then
    LINUX_REL=`cat $RELEASE_FILE | head -n 1 | awk -F " [(]" '{ print $1 }' | awk -F "." '{ print $1 }'`
    case $LINUX_REL in
    $EL7)host_distro="LEL7";;
    esac
fi

if [ "$host_distro" = "LEL7" ]; then
    IS_SYSTEMD_SUPPORTED="YES"
fi

SERVICE_SCOPE="FULL"

case "$1" in
    'start')
    if [ "$IS_SYSTEMD_SUPPORTED" = "yes" -a -z "$SCSRVC_EXEC_CONTEXT" ]; then	 	 
	handle_systemctl_start	 	 
    else
    	startservice $PID_FILE
    fi
    RETVAL=$?
    ;;

    'stop')
    if [ "$IS_SYSTEMD_SUPPORTED" = "yes" -a -z "$SCSRVC_EXEC_CONTEXT" ]; then
    	handle_systemctl_stop
    else
    	stopservice $PID_FILE
    fi
    RETVAL=$?
    ;;

    'start_evt')
    SERVICE_SCOPE="EVT"
    startservice $PID_FILE
    RETVAL=$?
    ;;

    'stop_evt')
    SERVICE_SCOPE="EVT"
    stopservice $PID_FILE
    RETVAL=$?
    ;;

    'restart')
    if [ "$H_OS" = "LINUX" ]; then
	trap 'dprintf "Signal trapped$END_CHAR"' 10 15
    elif [ "$H_OS" = "AIX" ]; then
	trap 'dprintf "Signal trapped$END_CHAR"' 15 30
    else
	trap 'dprintf "Signal trapped$END_CHAR"' 15 16 
    fi

    if [ "$IS_SYSTEMD_SUPPORTED" = "yes" ]; then
	handle_systemctl_restart 
    	RETVAL=$?
    else
    	stopservice $PID_FILE
	RETVAL1=$?
    	if [ "$H_OS" = "SOLARIS" ]; then
    		rem_drv $SOLIDCORE_DRV > /dev/null 2>&1
    		add_drv -v $SOLIDCORE_DRV > /dev/null 2>&1
    	fi
    	startservice $PID_FILE
    	RETVAL2=$?
    	if [ "$RETVAL1" != 0 -o "$RETVAL2" != 0 ]; then
		RETVAL=1;
    	else
		RETVAL=0;
    	fi
    fi

    ;;

    'status')
    servicestatus $PID_FILE
    RETVAL=$?
    ;;

    '-d')
    _home_dir=`cat $_conf_file | grep InstallDir | awk -F' ' '{print $3}' `

    if [ "$H_OS" = "LINUX" ]; then
        #Check (and restore) if original solidcore.conf is missing.
        check_and_restore_conf
        check_ret=$?
        if [ $check_ret -ne 0 ]; then
            exit $check_ret
        fi

	if [ ! -d $_home_dir/kmod/`uname -r` ]; then
	    #Kernel has been upgraded, trigger build
	    echo "Found new kernel.$END_CHAR "
	    $_home_dir/dks/build_target.sh install > "$_home_dir/dks/build.log_`date +%Y_%m_%d_%H_%M_%S`" 2>&1

	    if [ $? -ne 0 ]; then
		echo "Error starting McAfee Solidifier service.$END_CHAR"
		exit 2
	    fi
	fi

        if [ "$IS_WRL" != "YES" ]
	then 
	    K_VER1=`uname -r | awk -F "-" '{print $1}' | awk -F "." '{print $1}'`
	    K_VER2=`uname -r | awk -F "-" '{print $1}' | awk -F "." '{print $2}'`
	    K_VER3=`uname -r | awk -F "-" '{print $1}' | awk -F "." '{print $3}'`

	    if [ $K_VER1 -gt 2 ] || [ $K_VER1 -eq 2 -a $K_VER2 -gt 6 ] || [ $K_VER1 -eq 2 -a $K_VER2 -eq 6 -a $K_VER3 -ge 18 ]; then
	        set_tasklist_lock_addr
	        RET1=$?
	    else
	        RET1=0
	    fi
	
	    if [ $K_VER1 -gt 2 ] || [ $K_VER1 -eq 2 -a $K_VER2 -gt 6 ] || [ $K_VER1 -eq 2 -a $K_VER2 -eq 6 -a $K_VER3 -ge 31 ]; then
                set_find_task_by_pid_ns_addr
	        RET2=$?
	    else
	        RET2=0
	    fi

            set_sys_call_table_addr
	    RET3=$?

            set_schedule_tail_addr
	    RET4=$?

            set_ret_from_fork_addr
	    RET5=$?

            set_formats_addr
	    RET6=$?
    	
	    if [ "$host_arch" = "AMD64" ]; then
                set_ia32_sys_call_table_addr
	        RET7=$?
        
	        set_ia32_sysret_addr
	        RET8=$?
	    else
	        RET7=0
	        RET8=0
	    fi
        else
	    RET1=0;RET2=0;RET3=0;RET4=0;RET5=0;RET6=0;RET7=0;RET8=0;
	fi
	
	if [ $RET1 -eq 0 -a $RET2 -eq 0  -a $RET3 -eq 0 \
	     -a $RET4 -eq 0 -a $RET5 -eq 0 -a $RET6 -eq 0 \
	     -a $RET7 -eq 0 -a $RET8 -eq 0 ]; then
	
	    # Fix for bug 21551.
	    service rpcidmapd start  > /dev/null 2>&1 

    	    $_home_dir/bin/scsrvc -d
        fi
    elif [ "$H_OS" = "AIX" ]; then
    	set_aix_install_TL_SP
	RET1=$?

	if [ $RET1 -eq 0 ]; then
    	    $_home_dir/bin/scsrvc -d
	fi
    else
    	$_home_dir/bin/scsrvc -d
    fi

    ;;
			
    *)
    if [ "$H_OS" = "HPUX" ]; then
	case $1 in 
    	'start_msg')
    	echo "Starting McAfee Solidifier service"
	RETVAL=0
    	;;

    	'stop_msg')
    	echo "Stopping McAfee Solidifier service"
	RETVAL=0
    	;;

	*)
    	lprintf "Usage: $0 { start | stop | status | restart }$END_CHAR"
    	RETVAL=1
	esac
    else	
    	lprintf "Usage: $0 { start | stop | status | restart }$END_CHAR"
    	RETVAL=1
    fi
    ;;
esac

# Always return '1' for error in HP-UX
if [ "$H_OS" = "HPUX" ]; then 
    if [ $RETVAL -ne 0 ]; then
	RETVAL=1
    fi	
fi    
exit $RETVAL;
