3 # Script to update the resolver list for dnsmasq
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.
9 # Assumption: On entry, PWD contains the resolv.conf-type files.
11 # This file is part of the dnsmasq package.
16 RUN_DIR
="/var/run/dnsmasq"
17 RSLVRLIST_FILE
="${RUN_DIR}/resolv.conf"
18 TMP_FILE
="${RSLVRLIST_FILE}_new.$$"
19 MY_RECORD_NAME
="lo.dnsmasq"
20 DNSCRYPT_RECORD_NAME
="lo.dnscrypt"
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 contains 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"
49 for F
in $
(/lib
/resolvconf
/list-records
) ; do
54 "$DNSCRYPT_RECORD_NAME")
55 # Dnscrypt, I only have eyes for you
56 RSLVCNFFILES
="$DNSCRYPT_RECORD_NAME"
60 RSLVCNFFILES
="${RSLVCNFFILES:+$RSLVCNFFILES }$F"
66 if [ "$RSLVCNFFILES" ] ; then
67 uniquify $
(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
71 # Dnsmasq uses the mtime of $RSLVRLIST_FILE, with a resolution of one second,
72 # to detect changes in the file. This means that if a resolvconf update occurs
73 # within one second of the previous one then dnsmasq may fail to notice the
74 # more recent change. To work around this problem we sleep one second here
75 # if necessary in order to ensure that the new mtime is different.
76 if [ -f "$RSLVRLIST_FILE" ] && [ "$(ls -go --time-style='+%s' "$RSLVRLIST_FILE" | { read p h s t n ; echo "$t" ; })" = "$(date +%s)" ] ; then
80 clean_up
() { rm -f "$TMP_FILE" ; }
83 for N
in $NMSRVRS ; do echo "nameserver $N" >> "$TMP_FILE" ; done
84 mv -f "$TMP_FILE" "$RSLVRLIST_FILE"