[arch-projects] [netcfg] [PATCH 1/2] Defensively remove temporary file in EXIT trap
Signed-off-by: Henrik Hallberg <halhen@k2h.se> --- scripts/netcfg-menu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/netcfg-menu b/scripts/netcfg-menu index d0db762..a3bab94 100755 --- a/scripts/netcfg-menu +++ b/scripts/netcfg-menu @@ -30,6 +30,7 @@ fi # if no default yet, use the first entry [[ -z "$DEFAULT" ]] && DEFAULT="${profiles[0]}" ANSWER=$(mktemp --tmpdir menu.XXXXXXXX) || exit 1 +trap "rm -f $ANSWER" EXIT # Set timeout if [[ -z "$1" ]]; then @@ -60,7 +61,6 @@ case $ret in exit_err "Abnormal ret code from dialog: $ret" ;; esac -rm -f "$ANSWER" # JP: add -f exit $ret # JP: exit with caught $? # vim: ft=sh ts=4 et sw=4: -- 1.7.11
Setting NETWORKS to 'last' or '@last' restarts the profiles that were running at the previous 'netcfg-daemon stop'. Signed-off-by: Henrik Hallberg <halhen@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
On Thu, Jun 21, 2012 at 2:52 PM, Henrik Hallberg <henrik@k2h.se> wrote:
Setting NETWORKS to 'last' or '@last' restarts the profiles that were running at the previous 'netcfg-daemon stop'.
I think I'd rather have two extra options for netcfg, namely 'save' and 'restore' and add a '[@]restore' target to netcfg-daemon, but besides that I have my doubts about this whole idea. Can anyone describe a good use case? When is it not okay to list all profiles you might need in NETWORKS=()? Further, suppose 1) You manually connect to a network not in your NETWORKS(). 2) You connect to it on a day-to-day basis because at every boot, netcfg-daemon connects to it. 3) One day, connecting fails and the network is lost from the saved state 4) You have to connect to the network again, manually I don't really like this style. Currently netcfg-daemon manages the networks it manages and can even co-exist with manual netcfg connections (as long as they don't share an interface there is really nothing to worry about). The only exception is the menu target, which makes it possible to connect to not explicitly specified targets, but since this behavior is entirely stateless I'm OK with it. What I think is the underlying request is support for locations. Of course netcfg profiles already provide support for locations on a per-interface basis, but apparently there is a need to group profiles into locations. If that is what this is all about, I am not in favor of a last-connected feature. Much rather I'd have support for meta-profiles/profile-groups in some way. Regards, - Jouke
On Fri, Jun 22, 2012 at 02:25:28AM +0200, Jouke Witteveen wrote:
Can anyone describe a good use case?
First, it seems like a sane default. I prefer less configuration and reasonable behaviour over more configuration and perfect results. This would change a configuration item to read up on, test and remember, to something that works well enough out-of-the-box. Second, when travelling and staying in different places, it would be convenient to use this hiberate-like restoration. For me, this strikes the balance between convenient and simple, and reasonable. Maybe there's a combination of netcfg-auto-* that does what I'm looking for? / H
On Fri, Jun 22, 2012 at 11:44 AM, Henrik Hallberg <henrik@k2h.se> wrote:
On Fri, Jun 22, 2012 at 02:25:28AM +0200, Jouke Witteveen wrote:
Can anyone describe a good use case?
First, it seems like a sane default. I prefer less configuration and reasonable behaviour over more configuration and perfect results. This would change a configuration item to read up on, test and remember, to something that works well enough out-of-the-box.
A somewhat meager argument, since it doesn't tell me when you would need this functionality.
Second, when travelling and staying in different places, it would be convenient to use this hiberate-like restoration.
Potentially useful indeed, but you can already just list all different places you go to in NETWORKS and it will connect to the first one that it can connect to. This works even better than the 'last' functionality, as it does away with the need to connect manually at every location change.
For me, this strikes the balance between convenient and simple, and reasonable. Maybe there's a combination of netcfg-auto-* that does what I'm looking for?
Just netcfg-auto-wireless should largely cover your concerns too. Nevertheless I gave your code some thought and since there was a bug report for the same issue, I decided to give it a shot. 2.8.5 contains the requested functionality. I started with some minor changes of your patch, but later decided to do the whole thing in a single commit. Thanks for your contribution, without it the functionality would not have arrived in 2.8.5. - Jouke
On Thu, Jun 21, 2012 at 02:52:16PM +0200, Henrik Hallberg wrote:
Signed-off-by: Henrik Hallberg <halhen@k2h.se> --- scripts/netcfg-menu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/scripts/netcfg-menu b/scripts/netcfg-menu index d0db762..a3bab94 100755 --- a/scripts/netcfg-menu +++ b/scripts/netcfg-menu @@ -30,6 +30,7 @@ fi # if no default yet, use the first entry [[ -z "$DEFAULT" ]] && DEFAULT="${profiles[0]}" ANSWER=$(mktemp --tmpdir menu.XXXXXXXX) || exit 1 +trap "rm -f $ANSWER" EXIT
This needs to be evaluated at the time the trap is called, not when the trap is declared. trap 'rm -f "$ANSWER"' EXIT Better yet, do this properly and avoid the need for a temporary file.
# Set timeout if [[ -z "$1" ]]; then @@ -60,7 +61,6 @@ case $ret in exit_err "Abnormal ret code from dialog: $ret" ;; esac -rm -f "$ANSWER" # JP: add -f exit $ret # JP: exit with caught $?
# vim: ft=sh ts=4 et sw=4: -- 1.7.11
Better yet, do this properly and avoid the need for a temporary file.
You're right; I'll submit a better suggestion in a minute. Forget [PATCH 1/2] (which I guess would have been better sent separately anyhow). Sorry. / H
participants (3)
-
Dave Reisner
-
Henrik Hallberg
-
Jouke Witteveen