[arch-projects] [netcfg] [PATCH 4/4] Wait actively in {start, stop}_wpa

Henrik Hallberg henrik at k2h.se
Mon Jul 2 17:07:20 EDT 2012


Check .pid file regularly instead of waiting a second blindly. Saves up
to a second of wall time per call.
---
 src/8021x | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/8021x b/src/8021x
index d143b30..b78dcf9 100644
--- a/src/8021x
+++ b/src/8021x
@@ -38,18 +38,23 @@ start_wpa()
     fi
 
     wpa_supplicant -B -P "/run/wpa_supplicant_${INTERFACE}.pid" -i "$INTERFACE" -D "$WPA_DRIVER" "$WPA_CONF" $WPA_OPTS
-    sleep 1
 
-    if [[ ! -f "/run/wpa_supplicant_${INTERFACE}.pid" ]]; then
-        return 1
-    fi
+    # wait up to one second until pid file appears
+    timeout_wait 1 "[[ -f \"/run/wpa_supplicant_${INTERFACE}.pid\" ]]";
+
+    # Return success if pid file exists, else failure
+    return $?
 }
 
 stop_wpa()
 {
     wpa_cli -p "$WPA_CTRL_PATH" -i "$1" terminate &> /dev/null
-    sleep 1         # JP: need this else the file tends to disappear after [[ -f ... ]] but before cat...
-                    # see <http://bbs.archlinux.org/viewtopic.php?pid=515667#p515667>
+
+    # sleep up to one second, waiting for pid file to be removed
+    # JP: need to sleep else the file tends to disappear after [[ -f ... ]] but before cat...
+    # see <http://bbs.archlinux.org/viewtopic.php?pid=515667#p515667>
+    timeout_wait 1 "[[ ! -f \"/run/wpa_supplicant_$1.pid\" ]]"
+
     if [[ -f "/run/wpa_supplicant_$1.pid" ]]; then
         kill "$(< "/run/wpa_supplicant_$1.pid")" &>/dev/null &
     fi
-- 
1.7.11.1



More information about the arch-projects mailing list