[source] dnsmasq: write atomic config file

LEDE Commits lede-commits at lists.infradead.org
Fri Jul 29 07:41:26 PDT 2016


nbd pushed a commit to source.git, branch master:
https://git.lede-project.org/?p=source.git;a=commitdiff;h=712b6fdc5c1d56287e1f4eae5724590b241b5674

commit 712b6fdc5c1d56287e1f4eae5724590b241b5674
Author: Ulrich Weber <ulrich.weber at riverbed.com>
AuthorDate: Mon Jun 13 15:21:47 2016 +0200

    dnsmasq: write atomic config file
    
    multiple invocation of dnsmasq script (e.g. by procd and hotplugd)
    might cause procd to restart dnsmasq with an incomplete config file.
    Config file generation might take quite a long time on larger configs
    due ubus calls for each listening interface...
    
    Signed-off-by: Ulrich Weber <ulrich.weber at riverbed.com>
---
 package/network/services/dnsmasq/files/dnsmasq.init | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/package/network/services/dnsmasq/files/dnsmasq.init b/package/network/services/dnsmasq/files/dnsmasq.init
index 5f7afdb..3809e6c 100644
--- a/package/network/services/dnsmasq/files/dnsmasq.init
+++ b/package/network/services/dnsmasq/files/dnsmasq.init
@@ -13,6 +13,7 @@ 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"
@@ -21,7 +22,7 @@ TIMEVALIDFILE="/var/state/dnsmasqsec"
 xappend() {
 	local value="$1"
 
-	echo "${value#--}" >> $CONFIGFILE
+	echo "${value#--}" >> $CONFIGFILE_TMP
 }
 
 hex_to_hostid() {
@@ -255,7 +256,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 ] && {
@@ -635,7 +636,7 @@ start_service() {
 
 	[ -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
@@ -648,7 +649,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
@@ -682,19 +683,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" ] && {



More information about the lede-commits mailing list