3 # Script to update the resolver list for dnsmasq
5 # N.B. Resolvconf may run us even if dnsmasq is not running.
6 # If dnsmasq is installed then we go ahead and update
7 # the resolver list in case dnsmasq is started later.
9 # Assumption: On entry, PWD contains the resolv.conf-type files
11 # Requires bash because it uses a non-POSIX printf extension.
13 # Licensed under the GNU GPL. See /usr/share/common-licenses/GPL.
18 RUN_DIR
="/var/run/dnsmasq"
19 RSLVRLIST_FILE
="${RUN_DIR}/resolv.conf"
20 TMP_FILE
="${RSLVRLIST_FILE}_new.$$"
22 [ -x /usr
/sbin
/dnsmasq
] ||
exit 0
23 [ -x /lib
/resolvconf
/list-records
] ||
exit 1
27 report_err
() { echo "$0: Error: $*" >&2 ; }
29 # Stores arguments (minus duplicates) in RSLT, separated by spaces
30 # Doesn't work properly if an argument itself contain whitespace
36 [ "$1" = "$E" ] && { shift ; continue 2 ; }
38 RSLT
="${RSLT:+$RSLT }$1"
43 if [ ! -d "$RUN_DIR" ] && ! mkdir
--parents --mode=0755 "$RUN_DIR" ; then
44 report_err
"Failed trying to create directory $RUN_DIR"
48 RSLVCNFFILES
="$(/lib/resolvconf/list-records | sed -e '/^lo.dnsmasq$/d')"
51 if [ "$RSLVCNFFILES" ] ; then
52 uniquify $
(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
56 # Dnsmasq uses the mtime of $RSLVRLIST_FILE, with a resolution of one second,
57 # to detect changes in the file. This means that if a resolvconf update occurs
58 # within one second of the previous one then dnsmasq may fail to notice the
59 # more recent change. To work around this problem we sleep here to ensure
60 # that the new mtime is different.
61 if [ -f "$RSLVRLIST_FILE" ] && [ "$(ls -go --time-style='+%s' "$RSLVRLIST_FILE" | { read p h s t n ; echo "$t" ; })" = "$(date +%s)" ] ; then
65 clean_up
() { rm -f "$TMP_FILE" ; }
68 for N
in $NMSRVRS ; do echo "nameserver $N" >> "$TMP_FILE" ; done
69 mv -f "$TMP_FILE" "$RSLVRLIST_FILE"