Skip to contentSkip to conversation

wpa-supplicant.sh: updated to modern init script format, refactoring, and new restart functionality

PATCH Superseded Andrew Wichmann 2 participants 2 messages Feb 29, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wpa-supplicant.sh: updated to modern init script format, refactoring, and new restart functionality

Refactored the script by placing the logic of starting and stopping into separate functions, added a switch statement to act on $1, then refactored multiple if statements into one line tests. Check_socket function was extracted to increase the readability of the start function. Created restart functionality by calling stop then start
Added a BEGIN INIT INFO comment section (should be verified for accuracy)
Sourced the rcS default file to use the VERBOSE variable instead of VERBOSITY
Defined WPA_COMMON_CTRL_IFACE = "/var/run/wpa_supplicant" that was previously acted upon but not defined.

---
 .../wpa-supplicant/wpa-supplicant.sh               | 91 +++++++++++++---------
 1 file changed, 54 insertions(+), 37 deletions(-)
meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +54 -37
index 5c9e5d3..3623cbe 100644
@@ -1,72 +1,62 @@
-#!/bin/sh
+#!/bin/sh -e
+### BEGIN INIT INFO
+# Provides:			wpa_supplicant
+# Required-Start:	$local_fs
+# Required-Stop:	$local_fs
+# Default-Start:	2 3 4 5
+# Default-Stop:		0 1 6
+# Short-Description: A script to start, stop, or restart the wpa_supplicant daemon.
+### END INIT INFO
 
 
+IFACE="wlan0"
 WPA_SUP_BIN="/usr/sbin/wpa_supplicant"
 WPA_SUP_PNAME="wpa_supplicant"
 WPA_SUP_PIDFILE="/var/run/wpa_supplicant.$IFACE.pid"
 WPA_SUP_OPTIONS="-B -P $WPA_SUP_PIDFILE -i $IFACE"
+WPA_COMMON_CTRL_IFACE="/var/run/wpa_supplicant"
+IF_WPA_CONF="/etc/wpa_supplicant.conf"
 
-VERBOSITY=0
 
+[ -s "/etc/default/rcS" ] && . "/etc/default/rcS"
 
-if [ -s "$IF_WPA_CONF" ]; then
-	WPA_SUP_CONF="-c $IF_WPA_CONF"
-else
-	exit 0
-fi
+[ ! -s "$IF_WPA_CONF" ] && exit 0
+WPA_SUP_CONF="-c $IF_WPA_CONF"
 
 if [ ! -x "$WPA_SUP_BIN" ]; then
 	
-	if [ "$VERBOSITY" = "1" ]; then
-		echo "$WPA_SUP_PNAME: binaries not executable or missing from $WPA_SUP_BIN"
-	fi
+	[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: binaries not executable or missing from $WPA_SUP_BIN"
 	
 	exit 1
 fi
 
-if [ "$MODE" = "start" ] ; then
+start() {
 	# driver type of interface, defaults to wext when undefined
 	if [ -s "/etc/wpa_supplicant/driver.$IFACE" ]; then
 		IF_WPA_DRIVER=$(cat "/etc/wpa_supplicant/driver.$IFACE")
 	elif [ -z "$IF_WPA_DRIVER" ]; then
 		
-		if [ "$VERBOSITY" = "1" ]; then
-			echo "$WPA_SUP_PNAME: wpa-driver not provided, using \"wext\""
-		fi
-		
+		[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: wpa-driver not provided, using \"wext\""
+	
 		IF_WPA_DRIVER="wext"
 	fi
 	
 	# if we have passed the criteria, start wpa_supplicant
 	if [ -n "$WPA_SUP_CONF" ]; then
 		
-		if [ "$VERBOSITY" = "1" ]; then
-			echo "$WPA_SUP_PNAME: $WPA_SUP_BIN $WPA_SUP_OPTIONS $WPA_SUP_CONF -D $IF_WPA_DRIVER"
-		fi
-		
+		[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: $WPA_SUP_BIN $WPA_SUP_OPTIONS $WPA_SUP_CONF -D $IF_WPA_DRIVER"
+
 		start-stop-daemon --start --quiet \
 			--name $WPA_SUP_PNAME --startas $WPA_SUP_BIN --pidfile $WPA_SUP_PIDFILE \
 			--  $WPA_SUP_OPTIONS $WPA_SUP_CONF -D $IF_WPA_DRIVER
 	fi
+	check_socket
+}
 
-	# if the interface socket exists, then wpa_supplicant was invoked successfully
-	if [ -S "$WPA_COMMON_CTRL_IFACE/$IFACE" ]; then
-	
-		if [ "$VERBOSITY" = "1" ]; then
-			echo "$WPA_SUP_PNAME: ctrl_interface socket located at $WPA_COMMON_CTRL_IFACE/$IFACE"
-		fi
-
-		exit 0
-		
-	fi
-	
-elif [ "$MODE" = "stop" ]; then
-
+stop() {
 	if [ -f "$WPA_SUP_PIDFILE" ]; then
 		
-		if [ "$VERBOSITY" = "1" ]; then
-			echo "$WPA_SUP_PNAME: terminating $WPA_SUP_PNAME daemon"
-		fi
+		[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: terminating $WPA_SUP_PNAME daemon"
 		
 		start-stop-daemon --stop --quiet \
 			--name $WPA_SUP_PNAME --pidfile	$WPA_SUP_PIDFILE
@@ -79,7 +69,34 @@ elif [ "$MODE" = "stop" ]; then
 			rm -f $WPA_SUP_PIDFILE
 		fi
 	fi
+	exit 0
+}
 
-fi
+check_socket(){
+	# if the interface socket exists, then wpa_supplicant was invoked successfully
+	if [ -S "$WPA_COMMON_CTRL_IFACE/$IFACE" ]
+	then
+		[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: ctrl_interface socket located at$WPA_COMMON_CTRL_IFACE/$IFACE"
+		exit 0
+	fi
+	exit 1
+}
+
+case "$1" in
+	start)
+		start
+		;;
+	stop)
+		stop
+		;;
+	restart)
+		stop
+		start
+		;;
+	*)
+		echo "Usage: /etc/init.d/wpa_supplicant {start|stop|restart}"
+		exit 1
+
+esac
 
-exit 0
+exit 1
Signature · 1 line
1.9.1
#1 lore raw
Quoted 5 lines

Am 29.02.2016 um 18:49 schrieb Andrew Wichmann <[email protected]>:

Refactored the script by placing the logic of starting and stopping into separate functions, added a switch statement to act on $1, then refactored multiple if statements into one line tests.

Why don't re-use the one the Debian folks provide (http://lists.openembedded.org/pipermail/openembedded-core/2016-February/118235.html)?
This would free resources to develop things we cannot steal from others ;)

Cheers

Quoted 153 lines

Check_socket function was extracted to increase the readability of the start function. Created restart functionality by calling stop then start
Added a BEGIN INIT INFO comment section (should be verified for accuracy)
Sourced the rcS default file to use the VERBOSE variable instead of VERBOSITY
Defined WPA_COMMON_CTRL_IFACE = "/var/run/wpa_supplicant" that was previously acted upon but not defined.

---
.../wpa-supplicant/wpa-supplicant.sh | 91 +++++++++++++---------

1 file changed, 54 insertions(+), 37 deletions(-)
meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +10 -1
index 5c9e5d3..3623cbe 100644
@@ -1,72 +1,62 @@
-#!/bin/sh
+#!/bin/sh -e
+### BEGIN INIT INFO
+# Provides:			wpa_supplicant
+# Required-Start:	$local_fs
+# Required-Stop:	$local_fs
+# Default-Start:	2 3 4 5
+# Default-Stop:		0 1 6
+# Short-Description: A script to start, stop, or restart the wpa_supplicant daemon.
+### END INIT INFO
 
 
+IFACE="wlan0"

WPA_SUP_BIN="/usr/sbin/wpa_supplicant" WPA_SUP_PNAME="wpa_supplicant" WPA_SUP_PIDFILE="/var/run/wpa_supplicant.$IFACE.pid" WPA_SUP_OPTIONS="-B -P $WPA_SUP_PIDFILE -i $IFACE"

meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +5 -6
+WPA_COMMON_CTRL_IFACE="/var/run/wpa_supplicant"
+IF_WPA_CONF="/etc/wpa_supplicant.conf"
 
-VERBOSITY=0
 
+[ -s "/etc/default/rcS" ] && . "/etc/default/rcS"
 
-if [ -s "$IF_WPA_CONF" ]; then
-	WPA_SUP_CONF="-c $IF_WPA_CONF"
-else
-	exit 0
-fi
+[ ! -s "$IF_WPA_CONF" ] && exit 0
+WPA_SUP_CONF="-c $IF_WPA_CONF"

if [ ! -x "$WPA_SUP_BIN" ]; then

meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +1 -3
-	if [ "$VERBOSITY" = "1" ]; then
-		echo "$WPA_SUP_PNAME: binaries not executable or missing from $WPA_SUP_BIN"
-	fi
+	[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: binaries not executable or missing from $WPA_SUP_BIN"

exit 1
fi

meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +1 -1
-if [ "$MODE" = "start" ] ; then
+start() {
	# driver type of interface, defaults to wext when undefined
	if [ -s "/etc/wpa_supplicant/driver.$IFACE" ]; then
		IF_WPA_DRIVER=$(cat "/etc/wpa_supplicant/driver.$IFACE")
	elif [ -z "$IF_WPA_DRIVER" ]; then
meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +2 -4
-		if [ "$VERBOSITY" = "1" ]; then
-			echo "$WPA_SUP_PNAME: wpa-driver not provided, using \"wext\""
-		fi
-
+		[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: wpa-driver not provided, using \"wext\""
+
		IF_WPA_DRIVER="wext"
	fi

	# if we have passed the criteria, start wpa_supplicant
	if [ -n "$WPA_SUP_CONF" ]; then
meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +2 -4
-		if [ "$VERBOSITY" = "1" ]; then
-			echo "$WPA_SUP_PNAME: $WPA_SUP_BIN $WPA_SUP_OPTIONS $WPA_SUP_CONF -D $IF_WPA_DRIVER"
-		fi
-
+		[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: $WPA_SUP_BIN $WPA_SUP_OPTIONS $WPA_SUP_CONF -D $IF_WPA_DRIVER"
+
		start-stop-daemon --start --quiet \
			--name $WPA_SUP_PNAME --startas $WPA_SUP_BIN --pidfile $WPA_SUP_PIDFILE \
			--  $WPA_SUP_OPTIONS $WPA_SUP_CONF -D $IF_WPA_DRIVER
	fi
meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +3 -13
+	check_socket
+}
 
-	# if the interface socket exists, then wpa_supplicant was invoked successfully
-	if [ -S "$WPA_COMMON_CTRL_IFACE/$IFACE" ]; then
-
-		if [ "$VERBOSITY" = "1" ]; then
-			echo "$WPA_SUP_PNAME: ctrl_interface socket located at $WPA_COMMON_CTRL_IFACE/$IFACE"
-		fi
-
-		exit 0
-
-	fi
-
-elif [ "$MODE" = "stop" ]; then
-
+stop() {
	if [ -f "$WPA_SUP_PIDFILE" ]; then
meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +1 -3
-		if [ "$VERBOSITY" = "1" ]; then
-			echo "$WPA_SUP_PNAME: terminating $WPA_SUP_PNAME daemon"
-		fi
+		[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: terminating $WPA_SUP_PNAME daemon"
		start-stop-daemon --stop --quiet \
			--name $WPA_SUP_PNAME --pidfile	$WPA_SUP_PIDFILE
meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa-supplicant.sh +29 -3
@@ -79,7 +69,34 @@ elif [ "$MODE" = "stop" ]; then
 			rm -f $WPA_SUP_PIDFILE
 		fi
 	fi
+	exit 0
+}
 
-fi
+check_socket(){
+	# if the interface socket exists, then wpa_supplicant was invoked successfully
+	if [ -S "$WPA_COMMON_CTRL_IFACE/$IFACE" ]
+	then
+		[ "$VERBOSE" = "yes" ] && echo "$WPA_SUP_PNAME: ctrl_interface socket located at$WPA_COMMON_CTRL_IFACE/$IFACE"
+		exit 0
+	fi
+	exit 1
+}
+
+case "$1" in
+	start)
+		start
+		;;
+	stop)
+		stop
+		;;
+	restart)
+		stop
+		start
+		;;
+	*)
+		echo "Usage: /etc/init.d/wpa_supplicant {start|stop|restart}"
+		exit 1
+
+esac
 
-exit 0
+exit 1
--

1.9.1

Quoted 5 lines

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

--
Jens Rehsack - [email protected]