dnsmasq: remove use of uci state for getting network ifname
[openwrt/openwrt.git] / package / network / services / dnsmasq / files / dnsmasq.init
index 1a9903e50dc8edb2eb11f6d8c23ba12ba81f846b..54cb0ebfc8c8c80445561adec2d4a81770d12841 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/sh /etc/rc.common
 # Copyright (C) 2007-2012 OpenWrt.org
 
-START=60
+START=19
 
 USE_PROCD=1
 PROG=/usr/sbin/dnsmasq
@@ -13,14 +13,16 @@ ADD_LOCAL_DOMAIN=1
 ADD_LOCAL_HOSTNAME=1
 
 CONFIGFILE="/var/etc/dnsmasq.conf"
+CONFIGFILE_TMP="/var/etc/dnsmasq.conf.$$"
 HOSTFILE="/tmp/hosts/dhcp"
 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
 TIMESTAMPFILE="/etc/dnsmasq.time"
+TIMEVALIDFILE="/var/state/dnsmasqsec"
 
 xappend() {
        local value="$1"
 
-       echo "${value#--}" >> $CONFIGFILE
+       echo "${value#--}" >> $CONFIGFILE_TMP
 }
 
 hex_to_hostid() {
@@ -62,6 +64,12 @@ dhcp_check() {
 
        [ -s "$stamp" ] && return $(cat "$stamp")
 
+       # If there's no carrier yet, skip this interface.
+       # The init script will be called again once the link is up
+       case "$(devstatus "$ifname" | jsonfilter -e @.carrier)" in
+               false) return 1;;
+       esac
+
        udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
 
        [ $rv -eq 1 ] && \
@@ -111,12 +119,12 @@ append_ipset() {
 }
 
 append_interface() {
-       local ifname=$(uci_get_state network "$1" ifname "$1")
+       network_get_device ifname "$1"
        xappend "--interface=$ifname"
 }
 
 append_notinterface() {
-       local ifname=$(uci_get_state network "$1" ifname "$1")
+       network_get_device ifname "$1"
        xappend "--except-interface=$ifname"
 }
 
@@ -235,7 +243,12 @@ dnsmasq() {
        [ "$dnssec" -gt 0 ] && {
                xappend "--conf-file=$TRUSTANCHORSFILE"
                xappend "--dnssec"
-               xappend "--dnssec-timestamp=$TIMESTAMPFILE"
+               [ -x /etc/init.d/sysntpd ] && {
+                       /etc/init.d/sysntpd enabled
+                       [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
+                               [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
+                       }
+               }
                append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
        }
 
@@ -249,7 +262,7 @@ dnsmasq() {
        xappend "--user=dnsmasq"
        xappend "--group=dnsmasq"
 
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
 
        config_get_bool enable_tftp "$cfg" enable_tftp 0
        [ "$enable_tftp" -gt 0 ] && {
@@ -609,14 +622,18 @@ dhcp_relay_add() {
 service_triggers()
 {
        procd_add_reload_trigger "dhcp"
+       procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
 }
 
-boot() {
-       # Will be launched through hotplug
-       return 0
+boot()
+{
+       BOOT=1
+       start "$@"
 }
 
 start_service() {
+       [ -n "$BOOT" ] && return
+
        include /lib/functions
 
        config_load dhcp
@@ -627,12 +644,9 @@ start_service() {
        mkdir -p /var/lib/misc
        touch /tmp/dhcp.leases
 
-       if [ ! -f "$TIMESTAMPFILE" ]; then
-               touch "$TIMESTAMPFILE"
-               chown dnsmasq.dnsmasq "$TIMESTAMPFILE"
-       fi
+       [ -f "$TIMESTAMPFILE" ] && rm -f "$TIMESTAMPFILE"
 
-       echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
+       echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
        echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
 
        # if we did this last, we could override auto-generated config
@@ -645,7 +659,7 @@ start_service() {
        args=""
        config_foreach dnsmasq dnsmasq
        config_foreach dhcp_host_add host
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
        config_foreach dhcp_boot_add boot
        config_foreach dhcp_mac_add mac
        config_foreach dhcp_tag_add tag
@@ -679,19 +693,20 @@ start_service() {
                }
        }
 
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
        config_foreach dhcp_srv_add srvhost
        config_foreach dhcp_mx_add mxhost
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
 
        config_get odhcpd_is_active odhcpd maindhcp
        if [ "$odhcpd_is_active" != "1" ]; then
                config_foreach dhcp_add dhcp
        fi
 
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
        config_foreach dhcp_cname_add cname
-       echo >> $CONFIGFILE
+       echo >> $CONFIGFILE_TMP
+       mv -f $CONFIGFILE_TMP $CONFIGFILE
 
        rm -f /tmp/resolv.conf
        [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
@@ -709,7 +724,7 @@ start_service() {
 
        procd_add_jail dnsmasq ubus log
        procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom /etc/dnsmasq.conf /tmp/dnsmasq.d /tmp/resolv.conf.auto /etc/hosts /etc/ethers $EXTRA_MOUNT
-       procd_add_jail_mount_rw /var/run/dnsmasq/ /tmp/dhcp.leases $TIMESTAMPFILE
+       procd_add_jail_mount_rw /var/run/dnsmasq/ /tmp/dhcp.leases
 
        procd_close_instance
 }