changeset 2231:78eedd75ccca draft

Check that we have ctrl_interface defined in wpa_supplicant.conf and pass this parameter to wpa_cli(8). If not set, warn about not interacting with wpa_supplicant(8).
author Roy Marples <roy@marples.name>
date Mon, 20 Jan 2014 15:01:53 +0000
parents 6f64cbf7fa50
children 97f0168074a3
files dhcpcd-hooks/10-wpa_supplicant dhcpcd-run-hooks.in
diffstat 2 files changed, 61 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/dhcpcd-hooks/10-wpa_supplicant	Mon Jan 20 09:29:14 2014 +0000
+++ b/dhcpcd-hooks/10-wpa_supplicant	Mon Jan 20 15:01:53 2014 +0000
@@ -5,8 +5,10 @@
 
 if [ -z "$wpa_supplicant_conf" ]; then
 	for x in \
+		/etc/wpa_supplicant/wpa_supplicant-"$interface".conf \
+		/etc/wpa_supplicant/wpa_supplicant.conf \
+		/etc/wpa_supplicant-"$interface".conf \
 		/etc/wpa_supplicant.conf \
-		/etc/wpa_supplicant/wpa_supplicant.conf \
 	; do
 		if [ -s "$x" ]; then
 			wpa_supplicant_conf="$x"
@@ -16,11 +18,42 @@
 fi
 : ${wpa_supplicant_conf:=/etc/wpa_supplicant.conf}
 
+wpa_supplcant_ctrldir() {
+	local dir
+
+	dir=$(key_get_value "[[:space:]]*ctrl_interface=" \
+		"$wpa_supplicant_conf")
+	dir=$(trim "$dir")
+	case "$dir" in
+	DIR=*)
+		dir=${dir##DIR=}
+		dir=${dir%%[[:space:]]GROUP=*}
+		dir=$(trim "$dir")
+		;;
+	esac
+	printf %s "$dir"
+}
+
 wpa_supplicant_start()
 {
-	local err errn
+	local dir err errn
 
-	wpa_cli -i "$interface" status >/dev/null 2>&1 && return 0
+	# Pre flight checks
+	if [ ! -s "$wpa_supplicant_conf" ]; then
+		syslog warn \
+			"$wpa_supplicant_conf does not exist"
+		syslog warn "not interacting with wpa_supplicant(8)"
+		return 1
+	fi
+	dir=$(wpa_supplcant_ctrldir)
+	if [ -z "$dir" ]; then
+		syslog warn \
+			"ctrl_interface not defined in $wpa_supplicant_conf"
+		syslog warn "not interacting with wpa_supplicant(8)"
+		return 1
+	fi
+
+	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 && return 0
 	syslog info "starting wpa_supplicant"
 	err=$(wpa_supplicant -B -c"$wpa_supplicant_conf" -i"$interface" 2>&1)
 	errn=$?
@@ -33,14 +66,16 @@
 
 wpa_supplicant_reconfigure()
 {
-	local err errn
+	local dir err errn
 
-	if ! wpa_cli -i "$interface" status >/dev/null 2>&1; then
+	dir=$(wpa_supplcant_ctrldir)
+	[ -z "$dir" ] && return 1
+	if ! wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1; then
 		wpa_supplicant_start
 		return $?
 	fi
 	syslog info "reconfiguring wpa_supplicant"
-	err=$(wpa_cli -i"$interface" reconfigure 2>&1)
+	err=$(wpa_cli -p "$dir" -i "$interface" reconfigure 2>&1)
 	errn=$?
 	if [ $errn != 0 ]; then
 		syslog err "failed to reconfigure wpa_supplicant"
@@ -51,9 +86,11 @@
 
 wpa_supplicant_stop()
 {
-	local err errn
+	local dir err errn
 
-	wpa_cli -i "$interface" status >/dev/null 2>&1 || return 0
+	dir=$(wpa_supplcant_ctrldir)
+	[ -z "$dir" ] && return 1
+	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 || return 0
 	syslog info "stopping wpa_supplicant"
 	err=$(wpa_cli -i"$interface" terminate 2>&1)
 	errn=$?
@@ -63,9 +100,8 @@
 	fi
 	return $errn
 }
-	
 
-if [ "$ifwireless" = "1" -a -s "$wpa_supplicant_conf" ] && \
+if [ "$ifwireless" = "1" ] && \
     type wpa_supplicant >/dev/null 2>&1 && \
     type wpa_cli >/dev/null 2>&1
 then
--- a/dhcpcd-run-hooks.in	Mon Jan 20 09:29:14 2014 +0000
+++ b/dhcpcd-run-hooks.in	Mon Jan 20 15:01:53 2014 +0000
@@ -59,6 +59,21 @@
 	echo "$ifaces"
 }
 
+# Trim function
+trim()
+{
+	local var="$*"
+
+	var=${var#"${var%%[![:space:]]*}"}
+	var=${var%"${var##*[![:space:]]}"}
+	if [ -z "$var" ]; then
+		# So it seems our shell doesn't support wctype(3) patterns
+		# Fall back to sed
+		var=$(echo "$var" | sed -e 's/^[ \t]*//;s/[ \t]*$//')
+	fi
+	printf %s "$var"
+}
+
 # We normally use sed to extract values using a key from a list of files
 # but sed may not always be available at the time.
 key_get_value()