net/mwan3: remove pid file use pgrep to get pid

If mwan3track will not stop immediately after sending the kill signal,
the clean_up handler will delete the pid file later while the new mwan3track is
already running.

This could result in a situation that mwan3track is running
more then once because the old mwan3track service could not be killed,
because the pid file is missing.

Using pgrep to kill all mwan3track for the tracked interface and not using
pid file should fix this issue.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
Florian Eckert 2017-05-08 11:43:27 +02:00
parent 0ecc08784c
commit 1fd3f85d4f
4 changed files with 14 additions and 29 deletions

View file

@ -390,10 +390,7 @@ mwan3_track()
} }
config_list_foreach $1 track_ip mwan3_list_track_ips config_list_foreach $1 track_ip mwan3_list_track_ips
if [ -e /var/run/mwan3track-$1.pid ] ; then kill $(pgrep -f "mwan3track $1") &> /dev/null
kill $(cat /var/run/mwan3track-$1.pid) &> /dev/null
fi
if [ -n "$track_ips" ]; then if [ -n "$track_ips" ]; then
[ -x /usr/sbin/mwan3track ] && /usr/sbin/mwan3track $1 $2 $track_ips & [ -x /usr/sbin/mwan3track ] && /usr/sbin/mwan3track $1 $2 $track_ips &
fi fi
@ -401,18 +398,13 @@ mwan3_track()
mwan3_track_signal() mwan3_track_signal()
{ {
local pid status local pid
if [ -f "/var/run/mwan3track-${1}.pid" ]; then pid="$(pgrep -f "mwan3track $1")"
pid="$(cat "/var/run/mwan3track-${1}.pid")" if [ "${pid}" != "" ]; then
status="$(pgrep -f mwan3track | grep "${pid}")" kill -USR1 "${pid}"
if [ "${status}" != "" ]; then
kill -USR1 "${pid}"
else
$LOG warn "Unable to send signal USR1 to mwan3track on interface $1 with pid ${pid}"
fi
else else
$LOG warn "Unable to find \"/var/run/mwan3track-${1}.pid\" file for mwan3track on interface $1" $LOG warn "Unable to send signal USR1 to mwan3track on interface $1 with pid ${pid}"
fi fi
} }

View file

@ -34,16 +34,12 @@ get_mwan3_status() {
local iface="${1}" local iface="${1}"
local iface_select="${2}" local iface_select="${2}"
local running="0" local running="0"
local pid="" local pid
local status=""
if [ "${iface}" = "${iface_select}" ] || [ "${iface_select}" = "" ]; then if [ "${iface}" = "${iface_select}" ] || [ "${iface_select}" = "" ]; then
if [ -f "${MWAN3_PID_FILE}-${iface}.pid" ]; then pid="$(pgrep -f "mwan3track $iface_selected")"
pid="$(cat "${MWAN3_PID_FILE}-${iface}.pid")" if [ "${pid}" != "" ]; then
status="$(pgrep -f mwan3track | grep "${pid}")" running="1"
if [ "${status}" != "" ]; then
running="1"
fi
fi fi
json_add_object "${iface}" json_add_object "${iface}"

View file

@ -42,9 +42,7 @@ ifdown()
ACTION=ifdown INTERFACE=$1 /sbin/hotplug-call iface ACTION=ifdown INTERFACE=$1 /sbin/hotplug-call iface
if [ -e /var/run/mwan3track-$1.pid ] ; then kill $(pgrep -f "mwan3track $1") &> /dev/null
kill $(cat /var/run/mwan3track-$1.pid)
fi
} }
ifup() ifup()

View file

@ -10,8 +10,9 @@ IFDOWN_EVENT=0
clean_up() { clean_up() {
$LOG notice "Stopping mwan3track for interface \"${INTERFACE}\"" $LOG notice "Stopping mwan3track for interface \"${INTERFACE}\""
rm "/var/run/mwan3track-${INTERFACE}.pid" &> /dev/null if [ "$(pgrep -f "mwan3track ${INTERFACE}")" = "" ]; then
rm -rf "/var/run/mwan3track/${INTERFACE}" &> /dev/null rm -rf "/var/run/mwan3track/${INTERFACE}" &> /dev/null
fi
if [ -z "$(ls -A "/var/run/mwan3track")" ]; then if [ -z "$(ls -A "/var/run/mwan3track")" ]; then
rm -rf "/var/run/mwan3track" rm -rf "/var/run/mwan3track"
fi fi
@ -31,7 +32,6 @@ main() {
INTERFACE=$1 INTERFACE=$1
DEVICE=$2 DEVICE=$2
echo "$$" > /var/run/mwan3track-$1.pid
mkdir -p /var/run/mwan3track/$1 mkdir -p /var/run/mwan3track/$1
trap clean_up SIGINT SIGTERM trap clean_up SIGINT SIGTERM
trap if_down SIGUSR1 trap if_down SIGUSR1
@ -103,7 +103,6 @@ main() {
if [ $score -eq $up ]; then if [ $score -eq $up ]; then
$LOG notice "Interface $1 ($2) is online" $LOG notice "Interface $1 ($2) is online"
env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface env -i ACTION=ifup INTERFACE=$1 DEVICE=$2 /sbin/hotplug-call iface
rm /var/run/mwan3track-$1.pid
rm -rf "/var/run/mwan3track/${1}" &> /dev/null rm -rf "/var/run/mwan3track/${1}" &> /dev/null
exit 0 exit 0
fi fi