Add support for resolvconf integration as done in Ubuntu. This implies
updates of start-scripts, resolvconf plugin (on nameserver update ...),
populate-volatiles control file for saved nameserver list.

Signed-off-by: Jens Rehsack <[email protected]>
---
 .../recipes-support/dnsmasq/dnsmasq.inc            | 10 +++
 .../recipes-support/dnsmasq/files/99_dnsmasq       |  1 +
 .../dnsmasq/files/dnsmasq.resolvconf               | 84 ++++++++++++++++++++++
 .../recipes-support/dnsmasq/files/dnsmasq.service  |  7 +-
 meta-networking/recipes-support/dnsmasq/files/init | 69 +++++++++++++++++-
 5 files changed, 167 insertions(+), 4 deletions(-)
 create mode 100644 meta-networking/recipes-support/dnsmasq/files/99_dnsmasq
 create mode 100755 
meta-networking/recipes-support/dnsmasq/files/dnsmasq.resolvconf

diff --git a/meta-networking/recipes-support/dnsmasq/dnsmasq.inc 
b/meta-networking/recipes-support/dnsmasq/dnsmasq.inc
index 7fec606..622e630 100644
--- a/meta-networking/recipes-support/dnsmasq/dnsmasq.inc
+++ b/meta-networking/recipes-support/dnsmasq/dnsmasq.inc
@@ -23,6 +23,7 @@ PACKAGECONFIG[dbus] = ",,dbus"
 PACKAGECONFIG[idn] = ",,libidn"
 PACKAGECONFIG[conntrack] = ",,libnetfilter-conntrack"
 PACKAGECONFIG[lua] = ",,lua"
+PACKAGECONFIG[resolvconf] = ",,,resolvconf"
 EXTRA_OEMAKE = "\
     'COPTS=${@base_contains('PACKAGECONFIG', 'dbus', '-DHAVE_DBUS', '', d)} \
            ${@base_contains('PACKAGECONFIG', 'idn', '-DHAVE_IDN', '', d)} \
@@ -32,6 +33,8 @@ EXTRA_OEMAKE = "\
     'LDFLAGS=${LDFLAGS}' \
 "

+SRC_URI += "${@bb.utils.contains('PACKAGECONFIG', 'resolvconf', 
'file://dnsmasq.resolvconf file://99_dnsmasq', '', d)}"
+
 do_compile_append() {
     # build dhcp_release
     cd ${S}/contrib/wrt
@@ -56,6 +59,13 @@ do_install () {
         install -d ${D}${sysconfdir}/dbus-1/system.d
         install -m 644 dbus/dnsmasq.conf ${D}${sysconfdir}/dbus-1/system.d/
     fi
+    if [ "${@base_contains('PACKAGECONFIG', 'resolvconf', 'resolvconf', '', 
d)}" != "" ]; then
+       install -d ${D}${sysconfdir}/resolvconf/update.d/
+       install -m 0755 ${WORKDIR}/dnsmasq.resolvconf 
${D}${sysconfdir}/resolvconf/update.d/dnsmasq
+
+       install -d ${D}${sysconfdir}/default/volatiles
+       install -m 0644 ${WORKDIR}/99_dnsmasq 
${D}${sysconfdir}/default/volatiles
+    fi
 }

 CONFFILES_${PN} = "${sysconfdir}/dnsmasq.conf"
diff --git a/meta-networking/recipes-support/dnsmasq/files/99_dnsmasq 
b/meta-networking/recipes-support/dnsmasq/files/99_dnsmasq
new file mode 100644
index 0000000..f52ce4e
--- /dev/null
+++ b/meta-networking/recipes-support/dnsmasq/files/99_dnsmasq
@@ -0,0 +1 @@
+d root root 0755 /run/dnsmasq none
diff --git a/meta-networking/recipes-support/dnsmasq/files/dnsmasq.resolvconf 
b/meta-networking/recipes-support/dnsmasq/files/dnsmasq.resolvconf
new file mode 100755
index 0000000..7e19686
--- /dev/null
+++ b/meta-networking/recipes-support/dnsmasq/files/dnsmasq.resolvconf
@@ -0,0 +1,84 @@
+#!/bin/sh
+#
+# Script to update the resolver list for dnsmasq
+#
+# N.B. Resolvconf may run us even if dnsmasq is not (yet) running.
+# If dnsmasq is installed then we go ahead and update the resolver list
+# in case dnsmasq is started later.
+#
+# Assumption: On entry, PWD contains the resolv.conf-type files.
+#
+# This file is part of the dnsmasq package.
+#
+
+set -e
+
+RUN_DIR="/run/dnsmasq"
+RSLVRLIST_FILE="${RUN_DIR}/resolv.conf"
+TMP_FILE="${RSLVRLIST_FILE}_new.$$"
+MY_NAME_FOR_RESOLVCONF="dnsmasq"
+
+[ -x /usr/bin/dnsmasq ] || exit 0
+[ -x /lib/resolvconf/list-records ] || exit 1
+
+PATH=/bin:/sbin
+
+report_err() { echo "$0: Error: $*" >&2 ; }
+
+# Stores arguments (minus duplicates) in RSLT, separated by spaces
+# Doesn't work properly if an argument itself contains whitespace
+uniquify()
+{
+       RSLT=""
+       while [ "$1" ] ; do
+               for E in $RSLT ; do
+                       [ "$1" = "$E" ] && { shift ; continue 2 ; }
+               done
+               RSLT="${RSLT:+$RSLT }$1"
+               shift
+       done
+}
+
+if [ ! -d "$RUN_DIR" ] && ! mkdir --parents --mode=0755 "$RUN_DIR" ; then
+       report_err "Failed trying to create directory $RUN_DIR"
+       exit 1
+fi
+
+RSLVCNFFILES=""
+for F in $(/lib/resolvconf/list-records --after "lo.$MY_NAME_FOR_RESOLVCONF") 
; do
+       case "$F" in
+           "lo.$MY_NAME_FOR_RESOLVCONF")
+               # Omit own record
+               ;;
+           lo.*)
+               # Include no more records after one for a local nameserver
+               RSLVCNFFILES="${RSLVCNFFILES:+$RSLVCNFFILES }$F"
+               break
+               ;;
+         *)
+               RSLVCNFFILES="${RSLVCNFFILES:+$RSLVCNFFILES }$F"
+               ;;
+       esac
+done
+
+NMSRVRS=""
+if [ "$RSLVCNFFILES" ] ; then
+       uniquify $(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' 
$RSLVCNFFILES)
+       NMSRVRS="$RSLT"
+fi
+
+# Dnsmasq uses the mtime of $RSLVRLIST_FILE, with a resolution of one second,
+# to detect changes in the file. This means that if a resolvconf update occurs
+# within one second of the previous one then dnsmasq may fail to notice the
+# more recent change. To work around this problem we sleep one second here
+# if necessary in order to ensure that the new mtime is different.
+if [ -f "$RSLVRLIST_FILE" ] && [ "$(stat -c %X "$RSLVRLIST_FILE")" = "$(date 
+%s)" ] ; then
+       sleep 1
+fi
+
+clean_up() { rm -f "$TMP_FILE" ; }
+trap clean_up EXIT
+: >| "$TMP_FILE"
+for N in $NMSRVRS ; do echo "nameserver $N" >> "$TMP_FILE" ; done
+mv -f "$TMP_FILE" "$RSLVRLIST_FILE"
+
diff --git a/meta-networking/recipes-support/dnsmasq/files/dnsmasq.service 
b/meta-networking/recipes-support/dnsmasq/files/dnsmasq.service
index 549e15e..c3637e1 100644
--- a/meta-networking/recipes-support/dnsmasq/files/dnsmasq.service
+++ b/meta-networking/recipes-support/dnsmasq/files/dnsmasq.service
@@ -5,8 +5,11 @@ After=network.target
 [Service]
 Type=forking
 PIDFile=/run/dnsmasq.pid
-ExecStart=/usr/bin/dnsmasq -x /run/dnsmasq.pid
-ExecReload=/bin/kill -HUP $(/bin/cat /run/dnsmasq.pid)
+ExecStartPre=/usr/sbin/dnsmasq --test
+ExecStart=/etc/init.d/dnsmasq systemd-exec
+ExecStartPost=/etc/init.d/dnsmasq systemd-start-resolvconf
+ExecStopPre=/etc/init.d/dnsmasq systemd-stop-resolvconf
+ExecReload=/bin/kill -HUP $MAINPID

 [Install]
 WantedBy=multi-user.target
diff --git a/meta-networking/recipes-support/dnsmasq/files/init 
b/meta-networking/recipes-support/dnsmasq/files/init
index d1aa9e5..e4ca3d8 100644
--- a/meta-networking/recipes-support/dnsmasq/files/init
+++ b/meta-networking/recipes-support/dnsmasq/files/init
@@ -8,15 +8,68 @@ test -f $DAEMON || exit 0

 set -e

+if [ -r /etc/default/$NAME ]
+then
+       . /etc/default/$NAME
+fi
+
+DNSMASQ_CONF="/etc/dnsmasq.conf"
+test "/etc/dnsmasq.d/*" != '/etc/dnsmasq.d/*' && DNSMASQ_CONF="${DNSMASQ_CONF} 
/etc/dnsmasq.d/*"
+
+test -z "${PIDFILE}" && PIFILE="/run/dnsmasq.pid"
+
+if [ -z "$IGNORE_RESOLVCONF" ]
+then
+       egrep -h -q '^no-resolv' ${DNSMASQ_CONF} && IGNORE_RESOLVCONF="yes"
+fi
+
+# RESOLV_CONF:
+# If the resolvconf package is installed then use the resolv conf file
+# that it provides as the default.  Otherwise use /etc/resolv.conf as
+# the default.
+#
+# If IGNORE_RESOLVCONF is set in /etc/default/dnsmasq or an explicit
+# filename is set there then this inhibits the use of the resolvconf-provided
+# information.
+#
+# Note that if the resolvconf package is installed it is not possible to
+# override it just by configuration in /etc/dnsmasq.conf, it is necessary
+# to set IGNORE_RESOLVCONF=yes in /etc/default/dnsmasq.
+
+test -z "$RESOLV_CONF" -a "$IGNORE_RESOLVCONF" != "yes" -a -x /sbin/resolvconf 
&& \
+       RESOLV_CONF=/run/dnsmasq/resolv.conf
+
+start_resolvconf()
+{
+        if [ "$IGNORE_RESOLVCONF" != "yes" -a -x /sbin/resolvconf ]
+       then
+               echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.$NAME
+       fi
+       :
+}
+
+stop_resolvconf()
+{
+       if [ "$IGNORE_RESOLVCONF" != "yes" -a -x /sbin/resolvconf ]
+       then
+               /sbin/resolvconf -d lo.$NAME
+       fi
+       :
+}
+
 case "$1" in
     start)
         echo -n "starting $DESC: $NAME... "
        test -d /var/lib/misc/ || mkdir /var/lib/misc/
-       start-stop-daemon -S -x $DAEMON -- $ARGS
+       start-stop-daemon -S -x $DAEMON -- $ARGS \
+               ${RESOLV_CONF:+ -r $RESOLV_CONF} \
+               ${PIDFILE:+ -x $PIDFILE}
+       test $? -eq 0 && start_resolvconf
        echo "done."
        ;;
     stop)
         echo -n "stopping $DESC: $NAME... "
+       stop_resolvconf
        start-stop-daemon -K -x $DAEMON
        echo "done."
        ;;
@@ -25,7 +78,7 @@ case "$1" in
        start-stop-daemon -q -K -t -x $DAEMON
        RET=$?
        if [ "$RET" = "0" ]; then
-               PID=`cat /var/run/dnsmasq.pid`
+               PID=`cat ${PIDFILE}`
                echo "($PID) is running"
        else
                echo "is not running"
@@ -43,6 +96,18 @@ case "$1" in
        killall -HUP $(basename ${DAEMON})
        echo "done."
        ;;
+    systemd-start-resolvconf)
+       start_resolvconf
+       ;;
+    systemd-stop-resolvconf)
+       stop_resolvconf
+       ;;
+    systemd-exec)
+       test -d /var/lib/misc/ || mkdir /var/lib/misc/
+       exec $DAEMON --keep-in-foreground $ARGS \
+               ${RESOLV_CONF:+ -r $RESOLV_CONF} \
+               ${PIDFILE:+ -x $PIDFILE}
+       ;;
     *)
        echo "Usage: $0 {start|stop|status|restart|reload}"
        exit 1
--
2.6.3


--
Jens Rehsack - [email protected]

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

-- 
_______________________________________________
Openembedded-devel mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-devel

Reply via email to