dnsmasq: distinct Ubus names for multiple instances
authorEtan Kissling <etan.kissling@gmail.com>
Sun, 27 Jun 2021 22:02:46 +0000 (22:02 +0000)
committerKevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
Tue, 29 Jun 2021 10:27:26 +0000 (11:27 +0100)
Currently, when using multiple dnsmasq instances they are all assigned
to the same Ubus instance name. This does not work, as only a single
instance can register with Ubus at a time. In the log, this leads to
`Cannot add object to UBus: Invalid argument` error messages.
Furthermore, upstream 3c93e8eb41952a9c91699386132d6fe83050e9be changes
behaviour so that instead of the log, dnsmasq exits at start instead.

With this patch, all dnsmasq instances are assigned unique names so that
they can register with Ubus concurrently. One of the enabled instances
is always assigned the previous default name "dnsmasq" to avoid breaking
backwards compatibility with other software relying on that default.
Previously, a random instance got assigned that name (while the others
produced error logs). Now, the first unnamed dnsmasq config section is
assigned the default name. If there are no unnamed dnsmasq sections the
first encountered named dnsmasq config section is assigned instead.

A similar issue exists for Dbus and was similarly addressed.

Signed-off-by: Etan Kissling <etan.kissling@gmail.com>
[tweaked commit message] dnsmasq was not crashing it is exiting
Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
package/network/services/dnsmasq/files/dnsmasq.init

index 44e7d2d4f90c0fb40cb4181cfd8550c1a4ad4441..c4c262ad698d9b443ff6ebd40a272c20968de5e6 100644 (file)
@@ -161,7 +161,7 @@ append_server() {
 }
 
 append_rev_server() {
-        xappend "--rev-server=$1"
+       xappend "--rev-server=$1"
 }
 
 append_address() {
@@ -878,8 +878,16 @@ dnsmasq_start()
        append_bool "$cfg" noresolv "--no-resolv"
        append_bool "$cfg" localise_queries "--localise-queries"
        append_bool "$cfg" readethers "--read-ethers"
-       append_bool "$cfg" dbus "--enable-dbus"
-       append_bool "$cfg" ubus "--enable-ubus" 1
+
+       local instance_name="dnsmasq.$cfg"
+       if [ "$cfg" = "$DEFAULT_INSTANCE" ]; then
+               instance_name="dnsmasq"
+       fi
+       config_get_bool dbus "$cfg" "dbus" 0
+       [ $dbus -gt 0 ] && xappend "--enable-dbus=uk.org.thekelleys.$instance_name"
+       config_get_bool ubus "$cfg" "ubus" 1
+       [ $ubus -gt 0 ] && xappend "--enable-ubus=$instance_name"
+
        append_bool "$cfg" expandhosts "--expand-hosts"
        config_get tftp_root "$cfg" "tftp_root"
        [ -n "$tftp_root" ] && mkdir -p "$tftp_root" && append_bool "$cfg" enable_tftp "--enable-tftp"
@@ -1160,6 +1168,7 @@ boot()
 start_service() {
        local instance="$1"
        local instance_found=0
+       local first_instance=""
 
        . /lib/functions/network.sh
 
@@ -1170,10 +1179,27 @@ start_service() {
                        if [ -n "$instance" ] && [ "$instance" = "$name" ]; then
                                instance_found=1
                        fi
+                       if [ -z "$DEFAULT_INSTANCE" ]; then
+                               local disabled
+                               config_get_bool disabled "$name" disabled 0
+                               if [ "$disabled" -eq 0 ]; then
+                                       # First enabled section will be assigned default instance name.
+                                       # Unnamed sections get precedence over named sections.
+                                       if expr "$cfg" : 'cfg[0-9a-f]*$' >/dev/null = "9"; then # See uci_fixup_section.
+                                               DEFAULT_INSTANCE="$name" # Unnamed config section.
+                                       elif [ -z "$first_instance" ]; then
+                                               first_instance="$name"
+                                       fi
+                               fi
+                       fi
                fi
        }
 
+       DEFAULT_INSTANCE=""
        config_load dhcp
+       if [ -z "$DEFAULT_INSTANCE" ]; then
+               DEFAULT_INSTANCE="$first_instance" # No unnamed config section was found.
+       fi
 
        if [ -n "$instance" ]; then
                [ "$instance_found" -gt 0 ] || return