[arch-projects] [netcfg] [PATCH 2/2] Add 'last' NETWORKS option (FS#23015)

Henrik Hallberg henrik at k2h.se
Thu Jun 21 08:52:17 EDT 2012


Setting NETWORKS to 'last' or '@last' restarts the profiles that were running at the previous 'netcfg-daemon stop'.

Signed-off-by: Henrik Hallberg <halhen at k2h.se>
---
 docs/features.txt     |  3 +++
 scripts/netcfg-daemon | 38 +++++++++++++++++++++++++++++++++-----
 src/globals           |  1 +
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/docs/features.txt b/docs/features.txt
index f07376f..074ff86 100644
--- a/docs/features.txt
+++ b/docs/features.txt
@@ -45,6 +45,9 @@ background. For example:
 NETWORKS=(@adsl @mywireless lan)
 --------------------------------
 
+You can restart the profiles you had running at the previous shutdown
+using +NETWORKS=(last)+ or +NETWORKS=(@last)+.
+
 Next, add `net-profiles' to your +DAEMONS+ line in '/etc/rc.conf'.
 
 
diff --git a/scripts/netcfg-daemon b/scripts/netcfg-daemon
index 97ed1ef..f3855fe 100755
--- a/scripts/netcfg-daemon
+++ b/scripts/netcfg-daemon
@@ -4,6 +4,19 @@
 
 . /usr/lib/network/globals
 STATE_FILE="$STATE_DIR/netcfg-daemon"
+LAST_PROFILES_FILE="$PERSISTENT_STATE_DIR/last_profiles"
+
+function restore_last {
+	# When no last profiles are known, start silently
+	touch "$STATE_FILE"
+	if [[ -f "$LAST_PROFILES_FILE" ]]; then
+		while read prof; do
+			if /usr/bin/netcfg up "$prof"; then
+				echo "$prof" >> "$STATE_FILE"
+			fi
+		done <"$LAST_PROFILES_FILE"
+	fi
+}
 
 case "$1" in
 	start)
@@ -11,11 +24,23 @@ case "$1" in
 		[[ -e $STATE_FILE ]] && exit_err "netcfg-daemon is already started"
 		. /etc/conf.d/netcfg
 		[[ ${NETWORKS+x} != x ]] && exit_err "NETWORKS is not set in /etc/conf.d/netcfg"
-		if [[ ${#NETWORKS[@]} -eq 1 && $NETWORKS = menu ]]; then
-			/usr/bin/netcfg-menu ${NETWORKS_MENU_TIMEOUT-5} && \
-				mv "$STATE_DIR/menu" "$STATE_FILE"
-			exit $?
+
+		if [[ ${#NETWORKS[@]} -eq 1 ]]; then
+			case $NETWORKS in
+				menu)
+					/usr/bin/netcfg-menu ${NETWORKS_MENU_TIMEOUT-5} && \
+						mv "$STATE_DIR/menu" "$STATE_FILE"
+					exit $? ;;
+				last|@last)
+					if [[ ${NETWORKS:0:1} == "@" ]]; then
+						restore_last >/dev/null &
+					else
+						restore_last
+					fi
+					exit 0 ;;
+			esac
 		fi
+
 		for profile in "${NETWORKS[@]}"; do
 			if [[ "$profile" = "${profile#@}" ]]; then
 				if /usr/bin/netcfg check-iface "$profile"; then
@@ -36,6 +61,10 @@ case "$1" in
 	stop)
 		(( $(id -u) )) && exit_stderr "This script should be run as root."
 		[[ ! -e $STATE_FILE ]] && exit_err "netcfg-daemon was not started"
+
+		mkdir -p $(dirname "$LAST_PROFILES_FILE")
+		/usr/bin/netcfg current > "$LAST_PROFILES_FILE"
+
 		# Stop the profiles in the reverse order they were started.
 		tac "$STATE_FILE" | (
 			while read profile; do
@@ -68,4 +97,3 @@ case "$1" in
 	*)
 		echo "Usage: $0 {start|stop|restart|status}"
 esac
-
diff --git a/src/globals b/src/globals
index f8d30ef..baccbbe 100644
--- a/src/globals
+++ b/src/globals
@@ -14,6 +14,7 @@ SUBR_DIR="/usr/lib/network"
 HOOKS_DIR="$SUBR_DIR/hooks"
 CONN_DIR="$SUBR_DIR/connections"
 STATE_DIR="/run/network"
+PERSISTENT_STATE_DIR="/var/lib/network"
 
 
 
-- 
1.7.11



More information about the arch-projects mailing list