Avoid forking needlessly.
authorRoy Marples <roy@marples.name>
Tue, 21 Apr 2009 15:37:24 +0000 (15:37 +0000)
committerRoy Marples <roy@marples.name>
Tue, 21 Apr 2009 15:37:24 +0000 (15:37 +0000)
libc.in
resolvconf.in

diff --git a/libc.in b/libc.in
index dcf6e7d41b8966ab7c6b3de461843445fbba91c9..fb2cde90f12502a15e650bb11bbb44b300fc04f8 100644 (file)
--- a/libc.in
+++ b/libc.in
@@ -120,7 +120,11 @@ case "${resolv_conf_passthrough:-NO}" in
 
        # Now get any configured options
        opts="$resolv_conf_options${resolv_conf_options:+ }"
-       opts="$opts$($RESOLVCONF -l | key_get_value "options ")"
+       if [ "$(command -v list_resolv)" = list_resolv ]; then
+               opts="$opts$(list_resolv -l | key_get_value "options ")"
+       else
+               opts="$opts$($RESOLVCONF -l | key_get_value "options ")"
+       fi
        if [ -n "$opts" ]; then
                newconf="${newconf}options"
                for opt in $(uniqify $opts); do
index 8f6bb74f8a0a7336d7d483589afe0079c08f0592..bebf3309ec46d5fd5faa21f015cf5a9bc7074afc 100644 (file)
@@ -24,7 +24,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-ARGV0="$0"
+RESOLVCONF="$0"
 SYSCONFDIR=@SYSCONFDIR@
 LIBEXECDIR=@LIBEXECDIR@
 VARDIR=@VARDIR@
@@ -39,7 +39,7 @@ if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
 elif [ -d "$SYSCONFDIR/resolvconf" ]; then
        SYSCONFDIR="$SYSCONFDIR/resolvconf"
        if [ -f "$SYSCONFDIR"/interface-order ]; then
-               interface_order="$(cat "${SYSCONFDIR}"/interface-order)"
+               interface_order="$(cat "$SYSCONFDIR"/interface-order)"
        fi
 fi
 : ${dynamic_order:=tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]*}
@@ -54,7 +54,7 @@ error_exit()
 usage()
 {
        cat <<-EOF
-       Usage: ${ARGV0##*/} [options]
+       Usage: ${RESOLVCONF##*/} [options]
 
        Inform the system about any DNS updates.
 
@@ -76,7 +76,7 @@ usage()
          -s \$SVC \$CMD     Do \$CMD for the system service \$SVC
          -h               Show this help cruft
        EOF
-       [ -z "$@" ] && exit 0
+       [ -z "$1" ] && exit 0
        echo
        error_exit "$*"
 }
@@ -173,77 +173,17 @@ uniqify()
        echo "${result# *}"
 }
 
-force=false
-while getopts a:d:fhilm:ps:uv OPT; do
-       case "$OPT" in
-       f) force=true;;
-       h) usage;;
-       m) IF_METRIC="$OPTARG";;
-       p) IF_PRIVATE=1;;
-       s) cmd=s; service="$OPTARG";;
-       '?') ;;
-       *) cmd="$OPT"; iface="$OPTARG";;
-       esac
-done
-shift $(($OPTIND - 1))
-args="$iface${iface:+ }$@"
-
-# We do our service restarting here so that our subscribers don't have to know
-# about the OS's init system.
-if [ "$cmd" = "s" ]; then
-       if [ -n "$1" ]; then
-               action="$1"
-               shift
-       fi
-       [ -z "$action" ] && usage "Action not specified"
-
-       # If restarting check if service is running or not if we can
-       if [ "$action" = restart ]; then
-               if [ -s /var/run/"$service".pid ]; then
-                       kill -0 $(cat /var/run/"$service".pid) 2>/dev/null
-               elif [ -s /var/run/"$service"/"$service".pid ]; then
-                       kill -0 $(cat /var/run/"$service"/"$service".pid) \
-                               2>/dev/null
-               elif [ -s /var/run/"$service"/pid ]; then
-                       kill -0 $(cat /var/run/"$service"/pid) 2>/dev/null
-               else
-                       false
-               fi
-               # Service not running, so don't restart
-               [ $? != 0 ] && exit 0
-       fi      
-       if [ -x /sbin/service ]; then
-               service "$service" "$action" "$@" 
-       elif [ -x /etc/init.d/"$service" -a -x /sbin/runscript ]; then
-               if [ "$action" = "restart" ]; then
-                       /etc/init.d/"$service" --quiet --nodeps \
-                               conditionalrestart "$@"
-               else
-                       /etc/init.d/"$service" --quiet --nodeps \
-                               "$action" "$@"
-               fi
-       elif [ -x /etc/init.d/"$service" ]; then
-               /etc/init.d/"$service" "$action" "$@"
-       elif [ -x /etc/rc.d/"${service}" ]; then
-               /etc/rc.d/"$service" "$action" "$@" 
-       elif [ -x /etc/rc.d/rc."$service" ]; then
-               /etc/rc.d/rc."$service" "$action" "$@"
-       else
-               error_exit "Don't know how to interact with services on" \
-                       "this platform"
-       fi
-       exit $?
-fi
+list_resolv()
+{
+       [ -d "$IFACEDIR" ] || return 0
 
-# -l lists our resolv files, optionally for a specific interface
-if [ "$cmd" = l -o "$cmd" = i ]; then
-       [ -d "$IFACEDIR" ] || exit 0
+       local report=false list= retval=0 cmd="$1"
+       shift
 
-       report=false
        # If we have an interface ordering list, then use that.
        # It works by just using pathname expansion in the interface directory.
-       if [ -n "$args" ]; then
-               list="$args"
+       if [ -n "$1" ]; then
+               list="$@"
                $force || report=true
        else
                cd "$IFACEDIR"
@@ -264,7 +204,6 @@ if [ "$cmd" = l -o "$cmd" = i ]; then
                list="$list *"
        fi
 
-       retval=0
        cd "$IFACEDIR"
        for i in $(uniqify $list); do
                # Only list interfaces which we really have
@@ -276,18 +215,19 @@ if [ "$cmd" = l -o "$cmd" = i ]; then
                        continue
                fi
                
-               if [ "$cmd" = i ]; then
+               if [ "$cmd" = i -o "$cmd" = "-i" ]; then
                        printf "$i "
                else
                        echo_resolv "$i"
                fi
        done
-       [ "$cmd" = i ] && echo
-       exit $retval 
-fi
+       [ "$cmd" = i -o "$cmd" = "-i" ] && echo
+       return $retval
+}
 
-if [ "$cmd" = v ]; then
-       eval "$("$ARGV0" -l "$iface" | parse_resolv)"
+make_vars()
+{
+       eval "$(list_resolv -l "$@" | parse_resolv)"
 
        # Ensure that we only list each domain once
        newdomains=
@@ -316,7 +256,79 @@ if [ "$cmd" = v ]; then
        echo "DOMAINS='$newdomains'"
        echo "SEARCH='$(uniqify $SEARCH)'"
        echo "NAMESERVERS='$(uniqify $NAMESERVERS)'"
-       exit 0
+}
+
+force=false
+while getopts a:d:fhilm:ps:uv OPT; do
+       case "$OPT" in
+       f) force=true;;
+       h) usage;;
+       m) IF_METRIC="$OPTARG";;
+       p) IF_PRIVATE=1;;
+       s) cmd=s; service="$OPTARG";;
+       '?') ;;
+       *) cmd="$OPT"; iface="$OPTARG";;
+       esac
+done
+shift $(($OPTIND - 1))
+args="$iface${iface:+ }$@"
+
+# We do our service restarting here so that our subscribers don't have to know
+# about the OS's init system.
+if [ "$cmd" = "s" ]; then
+       if [ -n "$1" ]; then
+               action="$1"
+               shift
+       fi
+       [ -z "$action" ] && usage "Action not specified"
+
+       # If restarting check if service is running or not if we can
+       if [ "$action" = restart ]; then
+               if [ -s /var/run/"$service".pid ]; then
+                       kill -0 $(cat /var/run/"$service".pid) 2>/dev/null
+               elif [ -s /var/run/"$service"/"$service".pid ]; then
+                       kill -0 $(cat /var/run/"$service"/"$service".pid) \
+                               2>/dev/null
+               elif [ -s /var/run/"$service"/pid ]; then
+                       kill -0 $(cat /var/run/"$service"/pid) 2>/dev/null
+               else
+                       false
+               fi
+               # Service not running, so don't restart
+               [ $? != 0 ] && exit 0
+       fi      
+       if [ -x /sbin/service ]; then
+               service "$service" "$action" "$@" 
+       elif [ -x /etc/init.d/"$service" -a -x /sbin/runscript ]; then
+               if [ "$action" = "restart" ]; then
+                       /etc/init.d/"$service" --quiet --nodeps \
+                               conditionalrestart "$@"
+               else
+                       /etc/init.d/"$service" --quiet --nodeps \
+                               "$action" "$@"
+               fi
+       elif [ -x /etc/init.d/"$service" ]; then
+               /etc/init.d/"$service" "$action" "$@"
+       elif [ -x /etc/rc.d/"${service}" ]; then
+               /etc/rc.d/"$service" "$action" "$@" 
+       elif [ -x /etc/rc.d/rc."$service" ]; then
+               /etc/rc.d/rc."$service" "$action" "$@"
+       else
+               error_exit "Don't know how to interact with services on" \
+                       "this platform"
+       fi
+       exit $?
+fi
+
+# -l lists our resolv files, optionally for a specific interface
+if [ "$cmd" = l -o "$cmd" = i ]; then
+       list_resolv "$cmd" "$args"
+       exit $?
+fi
+
+if [ "$cmd" = v ]; then
+       make_vars "$iface"
+       exit $?
 fi
 
 # Test that we have valid options
@@ -416,12 +428,11 @@ if [ "$cmd" = a ]; then
        esac
 fi
 
-eval "$($ARGV0 -v)"
-export DOMAINS SEARCH NAMESERVERS
+eval "$(make_vars)"
 retval=0
 for script in "$LIBEXECDIR"/*; do
-       [ -f "$script" -a -x "$script" ] || continue
-       RESOLVCONF="$ARGV0" "$script" "$cmd" "$iface"
+       [ -f "$script" ] || continue
+       ( . "$script" "$cmd" "$iface" )
        retval=$(($retval + $?))
 done
 exit $retval