Use cat instead of grep for listing our resolvconfs
[openresolv] / libc
diff --git a/libc b/libc
index 022d38a355da3689b0b58d62c603bce52be8f594..ba0cbdd46c6e77d22e53b7b8b63157e9df9e8494 100755 (executable)
--- a/libc
+++ b/libc
@@ -1,21 +1,40 @@
 #!/bin/sh
 # Copyright 2006 Gentoo Foundation
-# Distributed under the terms of the GNU General Public License v2
-
-# libc resolv.conf updater
-
-# Written by Roy Marples (uberlord@gentoo.org)
-# Heavily based on Debian resolvconf by Thomas Hood
-
-[ "$(readlink /etc/resolv.conf 2>/dev/null)" \
-       != "resolvconf/run/resolv.conf" ] && exit 0
-
-RESOLVCONF="$(resolvconf -l)"
-BASE="/etc/resolvconf/resolv.conf.d/base"
+# Copyright 2007 Roy Marples
+# All rights reserved
+
+# libc subscriber for resolvconf 
+
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+#       copyright notice, this list of conditions and the following
+#       disclaimer in the documentation and/or other materials provided
+#       with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+PREFIX=
+RESOLVCONF="${PREFIX}"/etc/resolvconf
+RESOLVCONFS="$(resolvconf -l)"
+BASE="${RESOLVCONF}/resolv.conf.d/base"
 
 uniqify() {
     local result=
-    while [ -n "$1" ] ; do
+    while [ -n "$1" ]; do
                case " ${result} " in
                        *" $1 "*) ;;
                        *) result="${result} $1" ;;
@@ -26,12 +45,12 @@ uniqify() {
 }
 
 OUR_NS=
-if [ -e "${BASE}" ] ; then
+if [ -e "${BASE}" ]; then
        OUR_NS="$(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]*//p' "${BASE}")"
 fi
 OUR_NS="$(uniqify \
        ${OUR_NS} \
-       $(echo "${RESOLVCONF}" \
+       $(echo "${RESOLVCONFS}" \
        | sed -n -e 's/^[[:space:]]*nameserver[[:space:]]*//p') \
 )"
 
@@ -40,7 +59,7 @@ OUR_NS="$(uniqify \
 i=0
 NS=
 LOCALH=false
-for N in ${OUR_NS} ; do
+for N in ${OUR_NS}; do
        i=$((${i} + 1))
        NS="${NS} ${N}"
        [ "${i}" = "3" ] && break
@@ -56,22 +75,22 @@ done
 # the order given to us.
 OUR_SEARCH=
 if ${LOCALH} ; then
-       if [ -e "${BASE}" ] ; then
+       if [ -e "${BASE}" ]; then
                OUR_SEARCH="$(sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' "${BASE}")"
        fi
-       OUR_SEARCH="${OUR_SEARCH} $(echo "${RESOLVCONF}" \
+       OUR_SEARCH="${OUR_SEARCH} $(echo "${RESOLVCONFS}" \
                | sed -n 's/^[[:space:]]*search[[:space:]]*//p')"
-       if [ -e "${BASE}" ] ; then
+       if [ -e "${BASE}" ]; then
                OUR_SEARCH="${OUR_SEARCH} $(sed -n -e 's/^[[:space:]]*domain[[:space:]]*//p' "${BASE}")"
        fi
-       OUR_SEARCH="${OUR_SEARCH} $( echo "${RESOLVCONF}" \
+       OUR_SEARCH="${OUR_SEARCH} $( echo "${RESOLVCONFS}" \
                | sed -n -e 's/^[[:space:]]*domain[[:space:]]*//p')"
 else
-       if [ -e "${BASE}" ] ; then
+       if [ -e "${BASE}" ]; then
                OUR_SEARCH="$(sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' \
                        -e 's/^[[:space:]]*domain[[:space:]]*//p' "${BASE}")"
        fi
-       OUR_SEARCH="${OUR_SEARCH} $(echo "${RESOLVCONF}" \
+       OUR_SEARCH="${OUR_SEARCH} $(echo "${RESOLVCONFS}" \
                | sed -n -e 's/^[[:space:]]*search[[:space:]]*//p' \
                        -e 's/^[[:space:]]*domain[[:space:]]*//p')"
 fi
@@ -79,7 +98,7 @@ fi
 # libc only allows for 6 search domains 
 i=0
 SEARCH=
-for S in $(uniqify ${OUR_SEARCH}) ; do
+for S in $(uniqify ${OUR_SEARCH}); do
        i=$((${i} + 1))
        SEARCH="${SEARCH} ${S}"
        [ "${i}" = "6" ] && break
@@ -88,15 +107,15 @@ done
 
 # Hold our new resolv.conf in a variable to save on temporary files
 NEWCONF="# Generated by resolvconf\n"
-[ -e /etc/resolvconf/resolv.conf.d/head ] \
-       && NEWCONF="${NEWCONF}$(cat /etc/resolvconf/resolv.conf.d/head)\n"
+[ -e "${RESOLVCONF}"/resolv.conf.d/head ] \
+       && NEWCONF="${NEWCONF}$(cat "${RESOLVCONF}"/resolv.conf.d/head)\n"
 [ -n "${SEARCH}" ] && NEWCONF="${NEWCONF}${SEARCH}\n"
-for N in ${NS} ; do
+for N in ${NS}; do
        NEWCONF="${NEWCONF}nameserver ${N}\n"
 done
 
 # Now dump everything else from our resolvs
-if [ -e "${BASE}" ] ; then
+if [ -e "${BASE}" ]; then
        NEWCONF="${NEWCONF}$(sed -e '/^[[:space:]]*$/d' \
                -e '/^[[:space:]]*nameserver[[:space:]]*.*/d' \
                -e '/^[[:space:]]*search[[:space:]]*.*/d' \
@@ -105,30 +124,27 @@ if [ -e "${BASE}" ] ; then
 fi
 
 # We don't know we're using GNU sed, so we do it like this
-NEWCONF="${NEWCONF}$(echo "${RESOLVCONF}" | sed -e '/^[[:space:]]*$/d' \
+NEWCONF="${NEWCONF}$(echo "${RESOLVCONFS}" | sed -e '/^[[:space:]]*$/d' \
        -e '/^[[:space:]]*#/d' \
        -e '/^[[:space:]]*nameserver[[:space:]]*.*/d' \
        -e '/^[[:space:]]*search[[:space:]]*.*/d' \
        -e '/^[[:space:]]*domain[[:space:]]*.*/d' \
        )"
-[ -e /etc/resolvconf/resolv.conf.d/tail ] \
-       && NEWCONF="${NEWCONF}$(cat /etc/resolvconf/resolv.conf.d/tail)"
+[ -e "${RESOLVCONF}"/resolv.conf.d/tail ] \
+       && NEWCONF="${NEWCONF}$(cat "${RESOLVCONF}"/resolv.conf.d/tail)"
 
 # Check if the file has actually changed or not
-if [ -e /etc/resolv.conf ] ; then
-       [ "$(cat /etc/resolv.conf)" = "$(printf "${NEWCONF}")" ] && exit 0
+if [ -e "${RESOLVCONF}"/run/resolv.conf ]; then
+       [ "$(cat "${RESOLVCONF}"/run/resolv.conf)" = "$(printf "${NEWCONF}")" ] && exit 0
 fi
 
 # Create our resolv.conf now
-printf "${NEWCONF}" > /etc/resolvconf/run/resolv.conf
+printf "${NEWCONF}" > "${RESOLVCONF}"/run/resolv.conf
 
-# Restart nscd if it's running
-if [ -x /etc/init.d/nscd ] ; then
-       /etc/init.d/nscd --nodeps --quiet conditionalrestart
-fi
+resolvconf -s nscd restart
 
 # Notify users of the resolver
-for x in /etc/resolvconf/update-libc.d/* ; do
+for x in "${REVOLVCONF}"/update-libc.d/*; do
        [ -e "${x}" ] && "${x}" "$@"
 done