#!/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  
echo $HOME
echo $USER

PROG=/opt/bdfz/jyn/$SNAME  
KO_PATH=/opt/bdfz/jyn/ZDFY001.ko
PRELOAD_PATH=/opt/bdfz/jyn/zyhook.so

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

	# if test -e $PRELOAD_PATH; then
	# 	if ! test -e /etc/ld.so.preload; then
	# 		touch /etc/ld.so.preload
	# 		chmod 777 /etc/ld.so.preload
	# 	fi

	# 	if ! grep -q $PRELOAD_PATH  /etc/ld.so.preload; then
	# 		echo $PRELOAD_PATH >> /etc/ld.so.preload
	# 	fi
	# 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
	echo 99999999 > /proc/sys/fs/inotify/max_user_watches
	$PROG >/dev/null 2>&1 &

#check the daemon status first  
#	if test -e $KO_PATH ; then
#		insmod /opt/bdfz/jyn/ZDFY001.ko
#		if [ "$?" == "0" ] ; then
#			$PROG  
#		else 
#			echo "$KO_PATH install failed"
#		fi
#	else
#		echo "$KO_PATH not exists"
#	fi
}  

#stop function  
stop() {
	$PROG stopSelfProtect
    kill -9 `pidof $SNAME`
	echo 8192 > /proc/sys/fs/inotify/max_user_watches      
#	PID=`pidof $SNAME`
#	if [ "$PID" = "" ] ; then
#		echo "$SNAME not running"
#	else
#		kill -9 `pidof $SNAME`
#	fi

	if test -e /etc/ld.so.preload  &&  grep -q $PRELOAD_PATH /etc/ld.so.preload ; then
		sed -i '/\/opt\/bdfz\/jyn\/zyhook.so/d' /etc/ld.so.preload
	fi

	if test -e $KO_PATH ; then
		rmmod $KO_PATH 
	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  
