Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 1 | #!/bin/sh |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 2 | # |
| 3 | # Script to update the resolver list for dnsmasq |
| 4 | # |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 5 | # N.B. Resolvconf may run us even if dnsmasq is not (yet) running. |
| 6 | # If dnsmasq is installed then we go ahead and update the resolver list |
| 7 | # in case dnsmasq is started later. |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 8 | # |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 9 | # Assumption: On entry, PWD contains the resolv.conf-type files. |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 10 | # |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 11 | # This file is part of the dnsmasq package. |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 12 | # |
| 13 | |
| 14 | set -e |
| 15 | |
Simon Kelley | 4790115 | 2015-09-29 22:54:41 +0100 | [diff] [blame] | 16 | RUN_DIR="/run/dnsmasq" |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 17 | RSLVRLIST_FILE="${RUN_DIR}/resolv.conf" |
| 18 | TMP_FILE="${RSLVRLIST_FILE}_new.$$" |
Simon Kelley | 831b5ba | 2013-09-05 15:36:25 +0100 | [diff] [blame] | 19 | MY_NAME_FOR_RESOLVCONF="dnsmasq" |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 20 | |
| 21 | [ -x /usr/sbin/dnsmasq ] || exit 0 |
| 22 | [ -x /lib/resolvconf/list-records ] || exit 1 |
| 23 | |
| 24 | PATH=/bin:/sbin |
| 25 | |
| 26 | report_err() { echo "$0: Error: $*" >&2 ; } |
| 27 | |
| 28 | # Stores arguments (minus duplicates) in RSLT, separated by spaces |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 29 | # Doesn't work properly if an argument itself contains whitespace |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 30 | uniquify() |
| 31 | { |
| 32 | RSLT="" |
| 33 | while [ "$1" ] ; do |
| 34 | for E in $RSLT ; do |
| 35 | [ "$1" = "$E" ] && { shift ; continue 2 ; } |
| 36 | done |
| 37 | RSLT="${RSLT:+$RSLT }$1" |
| 38 | shift |
| 39 | done |
| 40 | } |
| 41 | |
| 42 | if [ ! -d "$RUN_DIR" ] && ! mkdir --parents --mode=0755 "$RUN_DIR" ; then |
| 43 | report_err "Failed trying to create directory $RUN_DIR" |
| 44 | exit 1 |
| 45 | fi |
| 46 | |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 47 | RSLVCNFFILES="" |
Simon Kelley | 831b5ba | 2013-09-05 15:36:25 +0100 | [diff] [blame] | 48 | for F in $(/lib/resolvconf/list-records --after "lo.$MY_NAME_FOR_RESOLVCONF") ; do |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 49 | case "$F" in |
Simon Kelley | 831b5ba | 2013-09-05 15:36:25 +0100 | [diff] [blame] | 50 | "lo.$MY_NAME_FOR_RESOLVCONF") |
| 51 | # Omit own record |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 52 | ;; |
Simon Kelley | 831b5ba | 2013-09-05 15:36:25 +0100 | [diff] [blame] | 53 | lo.*) |
| 54 | # Include no more records after one for a local nameserver |
| 55 | RSLVCNFFILES="${RSLVCNFFILES:+$RSLVCNFFILES }$F" |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 56 | break |
| 57 | ;; |
| 58 | *) |
| 59 | RSLVCNFFILES="${RSLVCNFFILES:+$RSLVCNFFILES }$F" |
| 60 | ;; |
| 61 | esac |
| 62 | done |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 63 | |
| 64 | NMSRVRS="" |
| 65 | if [ "$RSLVCNFFILES" ] ; then |
| 66 | uniquify $(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES) |
| 67 | NMSRVRS="$RSLT" |
| 68 | fi |
| 69 | |
| 70 | # Dnsmasq uses the mtime of $RSLVRLIST_FILE, with a resolution of one second, |
| 71 | # to detect changes in the file. This means that if a resolvconf update occurs |
| 72 | # within one second of the previous one then dnsmasq may fail to notice the |
Simon Kelley | 4849332 | 2013-05-28 14:49:54 +0100 | [diff] [blame] | 73 | # more recent change. To work around this problem we sleep one second here |
| 74 | # if necessary in order to ensure that the new mtime is different. |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 75 | if [ -f "$RSLVRLIST_FILE" ] && [ "$(ls -go --time-style='+%s' "$RSLVRLIST_FILE" | { read p h s t n ; echo "$t" ; })" = "$(date +%s)" ] ; then |
| 76 | sleep 1 |
| 77 | fi |
| 78 | |
| 79 | clean_up() { rm -f "$TMP_FILE" ; } |
| 80 | trap clean_up EXIT |
| 81 | : >| "$TMP_FILE" |
| 82 | for N in $NMSRVRS ; do echo "nameserver $N" >> "$TMP_FILE" ; done |
| 83 | mv -f "$TMP_FILE" "$RSLVRLIST_FILE" |
| 84 | |