change signal trap

fix waiting for pid file loop
This commit is contained in:
garywill 2018-08-31 18:41:06 +08:00 committed by garywill
parent f816160fc1
commit 47844d7049
1 changed files with 34 additions and 28 deletions

View File

@ -382,8 +382,8 @@ cleanup_lock() {
rm -f $COUNTER_LOCK_FILE rm -f $COUNTER_LOCK_FILE
} }
LOCK_FILE=$TMPDIR/lnxrouter.all.lock
init_lock() { init_lock() {
local LOCK_FILE=/tmp/lnxrouter.all.lock
# we initialize only once # we initialize only once
[[ $LOCK_FD -ne 0 ]] && return 0 [[ $LOCK_FD -ne 0 ]] && return 0
@ -952,9 +952,9 @@ stop_redsocks() {
_cleanup() { _cleanup() {
local PID x local PID x
trap "" SIGINT SIGUSR1 SIGUSR2 EXIT trap "" SIGINT SIGUSR1 SIGUSR2 EXIT SIGTERM
mutex_lock mutex_lock
disown -a #disown -a
# kill haveged_watchdog # kill haveged_watchdog
[[ -n "$HAVEGED_WATCHDOG_PID" ]] && kill $HAVEGED_WATCHDOG_PID [[ -n "$HAVEGED_WATCHDOG_PID" ]] && kill $HAVEGED_WATCHDOG_PID
@ -963,7 +963,7 @@ _cleanup() {
for x in $CONFDIR/*.pid; do for x in $CONFDIR/*.pid; do
# even if the $CONFDIR is empty, the for loop will assign # even if the $CONFDIR is empty, the for loop will assign
# a value in $x. so we need to check if the value is a file # a value in $x. so we need to check if the value is a file
[[ -f $x ]] && kill -9 $(cat $x) [[ -f $x ]] && kill $(cat $x)
done done
rm -rf $CONFDIR rm -rf $CONFDIR
@ -986,6 +986,7 @@ _cleanup() {
rm -rf $COMMON_CONFDIR rm -rf $COMMON_CONFDIR
rm -f $LOCK_FILE
else else
echo "Exiting: This is NOT the only running instance" echo "Exiting: This is NOT the only running instance"
fi fi
@ -1045,6 +1046,7 @@ die() {
# send die signal to the main process # send die signal to the main process
[[ $BASHPID -ne $$ ]] && kill -USR2 $$ [[ $BASHPID -ne $$ ]] && kill -USR2 $$
# we don't need to call cleanup because it's traped on EXIT # we don't need to call cleanup because it's traped on EXIT
#cleanup
exit 1 exit 1
} }
@ -1226,7 +1228,7 @@ fi
# if the user press ctrl+c or we get USR1 signal # if the user press ctrl+c or we get USR1 signal
# then run clean_exit() # then run clean_exit()
trap "clean_exit" SIGINT SIGUSR1 trap "cleanup" SIGINT SIGUSR1 SIGTERM
# if we get USR2 signal then run die(). # if we get USR2 signal then run die().
trap "die" SIGUSR2 trap "die" SIGUSR2
@ -1394,7 +1396,7 @@ fi
echo "Target interface is ${TARGET_IFACE}" echo "Target interface is ${TARGET_IFACE}"
mutex_lock mutex_lock
trap "cleanup" EXIT #trap "cleanup" EXIT
CONFDIR=$(mktemp -d $TMPDIR/lnxrouter.${TARGET_IFACE}.conf.XXXXXXXX) CONFDIR=$(mktemp -d $TMPDIR/lnxrouter.${TARGET_IFACE}.conf.XXXXXXXX)
#echo "Config dir: $CONFDIR" #echo "Config dir: $CONFDIR"
echo "PID: $$" echo "PID: $$"
@ -1688,11 +1690,13 @@ if [[ $NO_DNSMASQ -eq 0 ]]; then
dnsmasq -C $CONFDIR/dnsmasq.conf -x $CONFDIR/dnsmasq.pid -l $CONFDIR/dnsmasq.leases || die & dnsmasq -C $CONFDIR/dnsmasq.conf -x $CONFDIR/dnsmasq.pid -l $CONFDIR/dnsmasq.leases || die &
while [[ ! -f $CONFDIR/dnsmasq.pid ]]; do while [[ ! -f $CONFDIR/dnsmasq.pid ]]; do
sleep 1 sleep 1
echo -n "dnsmasq PID: " ; cat $CONFDIR/dnsmasq.pid
done done
echo -n "dnsmasq PID: " ; cat $CONFDIR/dnsmasq.pid
umask $SCRIPT_UMASK umask $SCRIPT_UMASK
fi fi
if [[ $WIFI_IFACE ]]; then if [[ $WIFI_IFACE ]]; then
if [[ $NO_HAVEGED -eq 0 ]]; then if [[ $NO_HAVEGED -eq 0 ]]; then
@ -1713,31 +1717,33 @@ if [[ $WIFI_IFACE ]]; then
$STDBUF_PATH hostapd $HOSTAPD_DEBUG_ARGS -B $CONFDIR/hostapd.conf -P $CONFDIR/hostapd.pid || die & $STDBUF_PATH hostapd $HOSTAPD_DEBUG_ARGS -B $CONFDIR/hostapd.conf -P $CONFDIR/hostapd.pid || die &
while [[ ! -f $CONFDIR/hostapd.pid ]]; do while [[ ! -f $CONFDIR/hostapd.pid ]]; do
sleep 1 sleep 1
echo -n "hostapd PID: " ; cat $CONFDIR/hostapd.pid
done done
#HOSTAPD_PID=$! #HOSTAPD_PID=$!
#echo $HOSTAPD_PID > $CONFDIR/hostapd.pid #echo $HOSTAPD_PID > $CONFDIR/hostapd.pid
if ! wait $HOSTAPD_PID; then echo -n "hostapd PID: " ; cat $CONFDIR/hostapd.pid
echo -e "\nError: Failed to run hostapd, maybe a program is interfering." >&2
if networkmanager_is_running; then
echo "If an error like 'n80211: Could not configure driver mode' was thrown" >&2 fi
echo "try running the following before starting lnxrouter:" >&2
if [[ $NM_OLDER_VERSION -eq 1 ]]; then KEEP_RUNNING_PID=
echo " nmcli nm wifi off" >&2 keep_running() {
else
echo " nmcli r wifi off" >&2
fi
echo " rfkill unblock wlan" >&2
fi
die
fi
else
# need loop to keep this script running
while :; do while :; do
sleep 9999 sleep 9999
done done
fi }
clean_exit sleep 2
# need loop to keep this script running
keep_running &
KEEP_RUNNING_PID=$!
echo $KEEP_RUNNING_PID > $CONFDIR/keep_running.pid
#jobs
wait $KEEP_RUNNING_PID
#clean_exit
#cleanup