Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnsmasq / debian / resolvconf
blobfd785cf8ffa991a0e14c1588297e4b30bd5a9294
1 #!/bin/sh
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.
14 set -e
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
25 PATH=/bin:/sbin
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
31 uniquify()
33 RSLT=""
34 while [ "$1" ] ; do
35 for E in $RSLT ; do
36 [ "$1" = "$E" ] && { shift ; continue 2 ; }
37 done
38 RSLT="${RSLT:+$RSLT }$1"
39 shift
40 done
43 if [ ! -d "$RUN_DIR" ] && ! mkdir --parents --mode=0755 "$RUN_DIR" ; then
44 report_err "Failed trying to create directory $RUN_DIR"
45 exit 1
48 RSLVCNFFILES=""
49 for F in $(/lib/resolvconf/list-records) ; do
50 case "$F" in
51 "$MY_RECORD_NAME")
52 # Omit
54 "$DNSCRYPT_RECORD_NAME")
55 # Dnscrypt, I only have eyes for you
56 RSLVCNFFILES="$DNSCRYPT_RECORD_NAME"
57 break
60 RSLVCNFFILES="${RSLVCNFFILES:+$RSLVCNFFILES }$F"
62 esac
63 done
65 NMSRVRS=""
66 if [ "$RSLVCNFFILES" ] ; then
67 uniquify $(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
68 NMSRVRS="$RSLT"
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
77 sleep 1
80 clean_up() { rm -f "$TMP_FILE" ; }
81 trap clean_up EXIT
82 : >| "$TMP_FILE"
83 for N in $NMSRVRS ; do echo "nameserver $N" >> "$TMP_FILE" ; done
84 mv -f "$TMP_FILE" "$RSLVRLIST_FILE"