summaryrefslogtreecommitdiffstats
path: root/contrib/openresolv/resolvconf.in
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/openresolv/resolvconf.in')
-rw-r--r--contrib/openresolv/resolvconf.in186
1 files changed, 151 insertions, 35 deletions
diff --git a/contrib/openresolv/resolvconf.in b/contrib/openresolv/resolvconf.in
index 3bfed5e..3ac72f3 100644
--- a/contrib/openresolv/resolvconf.in
+++ b/contrib/openresolv/resolvconf.in
@@ -25,9 +25,12 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
RESOLVCONF="$0"
+OPENRESOLV_VERSION="3.8.1"
SYSCONFDIR=@SYSCONFDIR@
LIBEXECDIR=@LIBEXECDIR@
VARDIR=@VARDIR@
+RCDIR=@RCDIR@
+RESTARTCMD=@RESTARTCMD@
# Disregard dhcpcd setting
unset interface_order state_dir
@@ -71,55 +74,45 @@ error_exit()
usage()
{
cat <<-EOF
- Usage: ${RESOLVCONF##*/} [options]
+ Usage: ${RESOLVCONF##*/} [options] command [argument]
Inform the system about any DNS updates.
- Options:
+ Commands:
-a \$INTERFACE Add DNS information to the specified interface
(DNS supplied via stdin in resolv.conf format)
- -m metric Give the added DNS information a metric
- -p Mark the interface as private
- -x Mark the interface as exclusive
-d \$INTERFACE Delete DNS information from the specified interface
- -f Ignore non existant interfaces
- -I Init the state dir
- -u Run updates from our current DNS information
- -l [\$PATTERN] Show DNS information, optionally from interfaces
- that match the specified pattern
+ -h Show this help cruft
-i [\$PATTERN] Show interfaces that have supplied DNS information
optionally from interfaces that match the specified
pattern
+ -l [\$PATTERN] Show DNS information, optionally from interfaces
+ that match the specified pattern
+
+ -u Run updates from our current DNS information
+
+ Options:
+ -f Ignore non existent interfaces
+ -m metric Give the added DNS information a metric
+ -p Mark the interface as private
+ -x Mark the interface as exclusive
+
+ Subscriber and System Init Commands:
+ -I Init the state dir
+ -r \$SERVICE Restart the system service
+ (restarting a non-existent or non-running service
+ should have no output and return 0)
+ -R Show the system service restart command
-v [\$PATTERN] echo NEWDOMAIN, NEWSEARCH and NEWNS variables to
the console
- -h Show this help cruft
+ -V [\$PATTERN] Same as -v, but only uses configuration in
+ $SYSCONFDIR/resolvconf.conf
EOF
[ -z "$1" ] && exit 0
echo
error_exit "$*"
}
-echo_resolv()
-{
- local line= OIFS="$IFS"
-
- [ -n "$1" -a -f "$IFACEDIR/$1" ] || return 1
- echo "# resolv.conf from $1"
- # Our variable maker works of the fact each resolv.conf per interface
- # is separated by blank lines.
- # So we remove them when echoing them.
- while read -r line; do
- IFS="$OIFS"
- if [ -n "$line" ]; then
- # We need to set IFS here to preserve any whitespace
- IFS=''
- printf "%s\n" "$line"
- fi
- done < "$IFACEDIR/$1"
- echo
- IFS="$OIFS"
-}
-
# Strip any trailing dot from each name as a FQDN does not belong
# in resolv.conf(5)
# If you think otherwise, capture a DNS trace and you'll see libc
@@ -159,7 +152,10 @@ parse_resolv()
private=false
for p in $private_interfaces; do
case "$iface" in
- "$p"|"$p":*) private=true; break;;
+ "$p"|"$p":*)
+ private=true
+ break
+ ;;
esac
done
fi
@@ -261,6 +257,108 @@ config_mkdirs()
return $e
}
+# With the advent of alternative init systems, it's possible to have
+# more than one installed. So we need to try and guess what one we're
+# using unless overriden by configure.
+# Note that restarting a service is a last resort - the subscribers
+# should make a reasonable attempt to reconfigre the service via some
+# method, normally SIGHUP.
+detect_init()
+{
+ [ -n "$RESTARTCMD" ] && return 0
+
+ # Detect the running init system.
+ # As systemd and OpenRC can be installed on top of legacy init
+ # systems we try to detect them first.
+ local status="@STATUSARG@"
+ : ${status:=status}
+ if [ -x /bin/systemctl -a -S /run/systemd/private ]; then
+ RESTARTCMD="if /bin/systemctl --quiet is-active \$1.service; then
+ /bin/systemctl restart \$1.service;
+fi"
+ elif [ -x /usr/bin/systemctl -a -S /run/systemd/private ]; then
+ RESTARTCMD="if /usr/bin/systemctl --quiet is-active \$1.service; then
+ /usr/bin/systemctl restart \$1.service;
+fi"
+ elif [ -x /sbin/rc-service -a \
+ -s /libexec/rc/init.d/softlevel -o -s /run/openrc/softlevel ]
+ then
+ RESTARTCMD="/sbin/rc-service -i \$1 -- -Ds restart"
+ elif [ -x /usr/sbin/invoke-rc.d ]; then
+ RCDIR=/etc/init.d
+ RESTARTCMD="if /usr/sbin/invoke-rc.d --quiet \$1 status 1>/dev/null 2>&1; then
+ /usr/sbin/invoke-rc.d \$1 restart;
+fi"
+ elif [ -x /sbin/service ]; then
+ # Old RedHat
+ RCDIR=/etc/init.d
+ RESTARTCMD="if /sbin/service \$1; then
+ /sbin/service \$1 restart;
+fi"
+ elif [ -x /usr/sbin/service ]; then
+ # Could be FreeBSD
+ RESTARTCMD="if /usr/sbin/service \$1 $status 1>/dev/null 2>&1; then
+ /usr/sbin/service \$1 restart;
+fi"
+ elif [ -x /bin/sv ]; then
+ RESTARTCMD="/bin/sv try-restart \$1"
+ elif [ -x /usr/bin/sv ]; then
+ RESTARTCMD="/usr/bin/sv try-restart \$1"
+ elif [ -e /etc/arch-release -a -d /etc/rc.d ]; then
+ RCDIR=/etc/rc.d
+ RESTARTCMD="if [ -e /var/run/daemons/\$1 ]; then
+ /etc/rc.d/\$1 restart;
+fi"
+ elif [ -e /etc/slackware-version -a -d /etc/rc.d ]; then
+ RESTARTCMD="if /etc/rc.d/rc.\$1 status 1>/dev/null 2>&1; then
+ /etc/rc.d/rc.\$1 restart;
+fi"
+ elif [ -e /etc/rc.d/rc.subr -a -d /etc/rc.d ]; then
+ # OpenBSD
+ RESTARTCMD="if /etc/rc.d/\$1 check 1>/dev/null 2>&1; then
+ /etc/rc.d/\$1 restart;
+fi"
+ else
+ for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do
+ [ -d $x ] || continue
+ RESTARTCMD="if $x/\$1 $status 1>/dev/null 2>&1; then
+ $x/\$1 restart;
+fi"
+ break
+ done
+ fi
+
+ if [ -z "$RESTARTCMD" ]; then
+ if [ "$NOINIT_WARNED" != true ]; then
+ warn "could not detect a useable init system"
+ _NOINIT_WARNED=true
+ fi
+ return 1
+ fi
+ _NOINIT_WARNED=
+ return 0
+}
+
+echo_resolv()
+{
+ local line= OIFS="$IFS"
+
+ [ -n "$1" -a -f "$IFACEDIR/$1" ] || return 1
+ echo "# resolv.conf from $1"
+ # Our variable maker works of the fact each resolv.conf per interface
+ # is separated by blank lines.
+ # So we remove them when echoing them.
+ while read -r line; do
+ IFS="$OIFS"
+ if [ -n "$line" ]; then
+ # We need to set IFS here to preserve any whitespace
+ IFS=''
+ printf "%s\n" "$line"
+ fi
+ done < "$IFACEDIR/$1"
+ IFS="$OIFS"
+}
+
list_resolv()
{
[ -d "$IFACEDIR" ] || return 0
@@ -320,6 +418,7 @@ list_resolv()
cd "$IFACEDIR"
retval=1
+ excl=true
for i in $(uniqify $list); do
# Only list interfaces which we really have
if ! [ -f "$i" ]; then
@@ -334,6 +433,7 @@ list_resolv()
printf %s "$i "
else
echo_resolv "$i"
+ echo
fi
[ $? = 0 -a "$retval" = 1 ] && retval=0
done
@@ -499,7 +599,7 @@ make_vars()
force=false
VFLAG=
-while getopts a:Dd:fhIilm:puvVx OPT; do
+while getopts a:Dd:fhIilm:pRruvVx OPT; do
case "$OPT" in
f) force=true;;
h) usage;;
@@ -541,6 +641,18 @@ if [ "$cmd" = l -o "$cmd" = i ]; then
exit $?
fi
+# Restart a service or echo the command to restart a service
+if [ "$cmd" = r -o "$cmd" = R ]; then
+ detect_init || exit 1
+ if [ "$cmd" = r ]; then
+ set -- $args
+ eval $RESTARTCMD
+ else
+ echo "$RESTARTCMD"
+ fi
+ exit $?
+fi
+
# Not normally needed, but subscribers should be able to run independently
if [ "$cmd" = v -o -n "$VFLAG" ]; then
make_vars "$iface"
@@ -765,13 +877,17 @@ case "${resolvconf:-YES}" in
*) exit 0;;
esac
+# Try and detect a suitable init system for our scripts
+detect_init
+export RESTARTCMD RCDIR _NOINIT_WARNED
+
eval "$(make_vars)"
export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS
: ${list_resolv:=list_resolv -l}
retval=0
# Run scripts in the same directory resolvconf is run from
-# in case any scripts accidently dump files in the wrong place.
+# in case any scripts accidentally dump files in the wrong place.
cd "$_PWD"
for script in "$LIBEXECDIR"/*; do
if [ -f "$script" ]; then
OpenPOWER on IntegriCloud