1#!/bin/sh
2
3if pidof wpa_supplicant hostapd valgrind.bin hlr_auc_gw > /dev/null; then
4    RUNNING=yes
5else
6    RUNNING=no
7fi
8
9sudo killall -q hostapd
10sudo killall -q wpa_supplicant
11for i in `pidof valgrind.bin`; do
12    if ps $i | grep -q -E "wpa_supplicant|hostapd"; then
13	sudo kill $i
14    fi
15done
16sudo killall -q wlantest
17if grep -q hwsim0 /proc/net/dev; then
18    sudo ip link set hwsim0 down
19fi
20
21if [ -e /tmp/hlr_auc_gw.sock ]; then
22    if which socat > /dev/null; then
23	echo TERMINATE | socat - UNIX-SENDTO:/tmp/hlr_auc_gw.sock
24	sleep 0.1
25    fi
26fi
27sudo killall -q hlr_auc_gw
28
29if [ "$RUNNING" = "yes" ]; then
30    # give some time for hostapd and wpa_supplicant to complete deinit
31    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
32	if ! pidof wpa_supplicant hostapd valgrind.bin hlr_auc_gw > /dev/null; then
33	    break
34	fi
35	if [ $i -gt 10 ]; then
36	    echo "Waiting for processes to exit (1)"
37	    sleep 1
38	else
39	    sleep 0.06
40	fi
41    done
42fi
43
44if pidof wpa_supplicant hostapd hlr_auc_gw > /dev/null; then
45    echo "wpa_supplicant/hostapd/hlr_auc_gw did not exit - try to force them to die"
46    sudo killall -9 -q hostapd
47    sudo killall -9 -q wpa_supplicant
48    sudo killall -9 -q hlr_auc_gw
49    for i in `seq 1 5`; do
50	if pidof wpa_supplicant hostapd hlr_auc_gw > /dev/null; then
51	    echo "Waiting for processes to exit (2)"
52	    sleep 1
53	else
54	    break
55	fi
56    done
57fi
58
59for i in `pidof valgrind.bin`; do
60    if ps $i | grep -q -E "wpa_supplicant|hostapd"; then
61	echo "wpa_supplicant/hostapd(valgrind) did not exit - try to force it to die"
62	sudo kill -9 $i
63    fi
64done
65
66count=0
67for i in /tmp/wpas-wlan0 /tmp/wpas-wlan1 /tmp/wpas-wlan2 /tmp/wpas-wlan5 /var/run/hostapd-global /tmp/hlr_auc_gw.sock /tmp/wpa_ctrl_* /tmp/eap_sim_db_*; do
68    count=$(($count + 1))
69    if [ $count -lt 7 -a -e $i ]; then
70	echo "Waiting for ctrl_iface $i to disappear"
71	sleep 1
72    fi
73    if [ -e $i ]; then
74	echo "Control interface file $i exists - remove it"
75	sudo rm $i
76    fi
77done
78
79if grep -q mac80211_hwsim /proc/modules 2>/dev/null ; then
80    sudo rmmod mac80211_hwsim
81    sudo rmmod mac80211
82    sudo rmmod cfg80211
83    # wait at the end to avoid issues starting something new immediately after
84    # this script returns
85    sleep 1
86fi
87