#!/bin/bash  
#  
# Description:  This shell script takes care of starting and stopping dennis  
#  
# Source function library  

#the service name  for example: dennis  
SNAME=filemonitor

#the full path and name of the daemon program  
#Warning: The name of executable file must be identical with service name  

PROG=/opt/apps/com.vsecure.chenxinsd/files/$SNAME  
PRELOAD_PATH=/opt/apps/com.vsecure.chenxinsd/files/zyhook.so
PRELOAD_NAME=zyhook.so
PRELOAD_CFG=/etc/ld.so.preload
KO_PATH=/opt/apps/com.vsecure.chenxinsd/files/ZDFY001.ko
LD_HOOK_PATH=`ldconfig -p | grep libc.so | head -n 1 |  awk '{print $NF}'`
LD_HOOK_PATH=`dirname $LD_HOOK_PATH`

# start function  
start() {
	if test -e $KO_PATH ; then
		insmod $KO_PATH
	fi

    if test -e $PRELOAD_PATH; then
        if ! test -e $PRELOAD_CFG; then
            touch $PRELOAD_CFG
            chmod 777 $PRELOAD_CFG
        fi

        cp -b $PRELOAD_PATH $LD_HOOK_PATH

        sed -i "/$PRELOAD_NAME/d" $PRELOAD_CFG
        echo $LD_HOOK_PATH/$PRELOAD_NAME >> $PRELOAD_CFG
    fi

	if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
		echo never > /sys/kernel/mm/transparent_hugepage/enabled
	fi

	if test -f /sys/kernel/mm/redhat_transparent_hugepage/enabled; then
		echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled
	fi

	sysctl vm.overcommit_memory=1
    sysctl fs.inotify.max_user_instances=1024
    sysctl fs.inotify.max_user_watches=262144
	$PROG >/dev/null 2>&1 &
}  

#stop function  
stop() {
	$PROG stopSelfProtect
    kill -9 `pidof $SNAME`

    if test -e $PRELOAD_CFG; then
		sed -i "/$PRELOAD_NAME/d" $PRELOAD_CFG
    fi

	if test -e $KO_PATH ; then
		rmmod $KO_PATH 
	fi

    if test -e $LD_HOOK_PATH/$PRELOAD_NAME; then
        rm $LD_HOOK_PATH/$PRELOAD_NAME
    fi
}  

case "$1" in  
start)  
start  
;;  
stop)  
stop  
;;  
reload|restart)  
stop  
start  
;;  
status)  
status $SNAME  
;;  
*)  
echo $"Usage: $0 {start|stop|restart|status}"  
exit 1  
esac  
