Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.04 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Script to update the resolver list for dnsmasq
  4. #
  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.
  8. #
  9. # Assumption: On entry, PWD contains the resolv.conf-type files
  10. #
  11. # Requires bash because it uses a non-POSIX printf extension.
  12. #
  13. # Licensed under the GNU GPL.  See /usr/share/common-licenses/GPL.
  14. #
  15.  
  16. set -e
  17.  
  18. RUN_DIR="/var/run/dnsmasq"
  19. RSLVRLIST_FILE="${RUN_DIR}/resolv.conf"
  20. TMP_FILE="${RSLVRLIST_FILE}_new.$$"
  21.  
  22. [ -x /usr/sbin/dnsmasq ] || exit 0
  23. [ -x /lib/resolvconf/list-records ] || exit 1
  24.  
  25. PATH=/bin:/sbin
  26.  
  27. report_err() { echo "$0: Error: $*" >&2 ; }
  28.  
  29. # Stores arguments (minus duplicates) in RSLT, separated by spaces
  30. # Doesn't work properly if an argument itself contain whitespace
  31. uniquify()
  32. {
  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
  41. }
  42.  
  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
  46. fi
  47.  
  48. RSLVCNFFILES="$(/lib/resolvconf/list-records | sed -e '/^lo.dnsmasq$/d')"
  49.  
  50. NMSRVRS=""
  51. if [ "$RSLVCNFFILES" ] ; then
  52.     uniquify $(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
  53.     NMSRVRS="$RSLT"
  54. fi
  55.  
  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
  62.     sleep 1
  63. fi
  64.  
  65. clean_up() { rm -f "$TMP_FILE" ; }
  66. trap clean_up EXIT
  67. : >| "$TMP_FILE"
  68. for N in $NMSRVRS ; do echo "nameserver $N" >> "$TMP_FILE" ; done
  69. mv -f "$TMP_FILE" "$RSLVRLIST_FILE"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement