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="/run/dnsmasq"
17 RSLVRLIST_FILE="${RUN_DIR}/resolv.conf"
18 TMP_FILE="${RSLVRLIST_FILE}_new.$$"
19 MY_NAME_FOR_RESOLVCONF="dnsmasq"
21 [ -x /usr/bin/dnsmasq ] || exit 0
22 [ -x /lib/resolvconf/list-records ] || exit 1
26 report_err() { echo "$0: Error: $*" >&2 ; }
28 # Stores arguments (minus duplicates) in RSLT, separated by spaces
29 # Doesn't work properly if an argument itself contains whitespace
35 [ "$1" = "$E" ] && { shift ; continue 2 ; }
37 RSLT="${RSLT:+$RSLT }$1"
42 if [ ! -d "$RUN_DIR" ] && ! mkdir --parents --mode=0755 "$RUN_DIR" ; then
43 report_err "Failed trying to create directory $RUN_DIR"
48 for F in $(/lib/resolvconf/list-records --after "lo.$MY_NAME_FOR_RESOLVCONF") ; do
50 "lo.$MY_NAME_FOR_RESOLVCONF")
54 # Include no more records after one for a local nameserver
55 RSLVCNFFILES="${RSLVCNFFILES:+$RSLVCNFFILES }$F"
59 RSLVCNFFILES="${RSLVCNFFILES:+$RSLVCNFFILES }$F"
65 if [ "$RSLVCNFFILES" ] ; then
66 uniquify $(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
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
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.
75 if [ -f "$RSLVRLIST_FILE" ] && [ "$(stat -c %X "$RSLVRLIST_FILE")" = "$(date +%s)" ] ; then
79 clean_up() { rm -f "$TMP_FILE" ; }
82 for N in $NMSRVRS ; do echo "nameserver $N" >> "$TMP_FILE" ; done
83 mv -f "$TMP_FILE" "$RSLVRLIST_FILE"