From 76c6a530ed8b64f0cd6425aa0d6f45da560d20f6 Mon Sep 17 00:00:00 2001 From: Rene Rebe Date: Thu, 20 May 2004 09:10:40 +0000 Subject: [PATCH] Rene Rebe: make ROCK Net a new experience: - speedup - register active interfaces in /var/run/rocknet/active-interfaces - make if{up,down} warn when the interface is active/unactive (supressable with -force) [2004051319074012257] (https://www.rocklinux.net/submaster) git-svn-id: http://www.rocklinux.org/svn/rock-linux/trunk@3029 c5f82cb5-29bc-0310-9cd0-bff59a50e3bc --- package/base/sysfiles/ifup.sh | 52 ++++++++++++++++--- package/base/sysfiles/rocknet.sh | 30 +++++++++-- package/base/sysfiles/rocknet_modules_base.sh | 2 + 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/package/base/sysfiles/ifup.sh b/package/base/sysfiles/ifup.sh index c93545f19..4cc369af8 100644 --- a/package/base/sysfiles/ifup.sh +++ b/package/base/sysfiles/ifup.sh @@ -1,20 +1,56 @@ #!/bin/sh +rocknet_tmp_base="/var/run/rocknet" +[ -d $rocknet_tmp_base ] || mkdir -p $rocknet_tmp_base + +var_contains() { + local tmp; + eval "tmp=\"\$$1\"" + + [ "${tmp/$3$2/}" != "$tmp" ] +} + usage() { - echo "Usage: $0 interface [ profile ]" + echo "Usage: $0 interface [ profile ] [ -force ]" exit 1 } -[ "$1" ] || usage +force=0 -if [ "$2" = "" ] ; then - profile="`cat /etc/network/profile 2> /dev/null`" -else - profile="$2" -fi +if="$1" ; shift ; [ "$if" ] || usage +profile="" +action="${0/#*\/if}" + +while [ "$1" ] ; do + case $1 in + -force) force=1 ;; + *) [ "$profile" = "" ] && profile="$1" || usage ;; + esac + shift +done +[ "$profile" ] || profile="`cat /etc/network/profile 2> /dev/null`" profile=${profile:-default} -/etc/network/rocknet "$profile" "$1" "${0#/sbin/if}" +# sanity checks (...) + +if [ $force -eq 0 ] ; then + active_interfaces="`cat $rocknet_tmp_base/active-interfaces 2>/dev/null`" + if test $action = "up" && var_contains active_interfaces ',' "$1($profile)" + then + echo "Interface $if($profile) is already listed active, it is probably a good idea to" + echo "take it down before activating it. Use -force to suppress this warning." + exit 2 + fi + + if test $action = "down" && ! var_contains active_interfaces ',' "$1($profile)" + then + echo "Interface $if($profile) is not listed active, it is probably a good idea to" + echo "activate it before deactivating it. Use -force to suppress this warning." + exit 2 + fi +fi + +/etc/network/rocknet "$profile" "$if" "$action" echo "$profile" > /etc/network/profile diff --git a/package/base/sysfiles/rocknet.sh b/package/base/sysfiles/rocknet.sh index 6b743154f..02ba75ca8 100755 --- a/package/base/sysfiles/rocknet.sh +++ b/package/base/sysfiles/rocknet.sh @@ -13,6 +13,7 @@ code_snipplets_counter=0 lineno=0 ignore=0 +global=1 if [ "$3" != "up" -a "$3" != "down" ]; then echo "Usage: $0 { profile | default } { interface | auto } { up | down }" @@ -61,6 +62,20 @@ status() { echo "$*" } +# +# register / unregister active interfaces for user input validation +# + +register() { + echo -n "${1}," >> $rocknet_tmp_base/active-interfaces +} + +unregister () { + active_interfaces="`cat $rocknet_tmp_base/active-interfaces 2>/dev/null`" + active_interfaces="${active_interfaces//${1},/}" + echo -n "$active_interfaces" > $rocknet_tmp_base/active-interfaces +} + for x in "$rocknet_base"/modules/*.sh; do . "$x"; done while read cmd para @@ -71,7 +86,14 @@ do para="$( echo "$para" | sed 's,[\*\?],\\&,g' )" if declare -f public_$cmd > /dev/null then - public_$cmd $para + # optimization: Only execute commands when they are + # inside an unignored interface section ... + if [ $cmd = "interface" ] ; then + public_$cmd $para + global=0 + elif [ $ignore -eq 0 -o $global -gt 0 ] ; then + public_$cmd $para + fi else error "Unknown statement in config file: $cmd" fi @@ -88,6 +110,8 @@ done < <( done | sort ) -[ "$pmatched" = 0 ] && error "Unknown profile: $profile" -[ "$imatched" = 0 ] && error "Unknown interface for profile: $interface" +[ "$pmatched" = 0 ] && \ + error "Unknown profile: '$profile'" +[ "$pmatched" = 1 -a "$imatched" = 0 ] && \ + error "Unknown interface for profile: '$interface'" diff --git a/package/base/sysfiles/rocknet_modules_base.sh b/package/base/sysfiles/rocknet_modules_base.sh index 870c63eec..19025c56d 100644 --- a/package/base/sysfiles/rocknet_modules_base.sh +++ b/package/base/sysfiles/rocknet_modules_base.sh @@ -47,6 +47,8 @@ public_interface() { if [ "$ignore" = 0 ] ; then imatched=1 status "Interface / profile matched: $if($prof)" + addcode up 9 9 "register $if\($prof\)" + addcode down 9 9 "unregister $if\($prof\)" fi }