]> git.ipfire.org Git - people/fbuehrle/ipfire-2.x.git/commitdiff
unbound-dhcp-leases-bridge: Replace leases file atomically
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 28 Mar 2019 12:51:06 +0000 (12:51 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 28 Mar 2019 12:51:06 +0000 (12:51 +0000)
When there is a large number of leases, writing the file may
take a long time. When unbound is re-reading its configuration
in that time, the file might syntactically incorrect.

This change writes the file first and then moves it
to the right place in one transaction.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
config/rootfiles/core/130/filelists/files
config/unbound/unbound-dhcp-leases-bridge

index 28a32a7c655193416ec283e312cb2bb5fc140079..ab1e82fcbc0de07f1a02a98c5d45fd23de37125e 100644 (file)
@@ -18,6 +18,7 @@ usr/local/bin/ipsec-interfaces
 usr/local/bin/suricatactrl
 usr/local/bin/update-ids-ruleset
 usr/sbin/convert-snort
+usr/sbin/unbound-dhcp-leases-bridge
 var/ipfire/backup/bin/backup.pl
 var/ipfire/backup/include
 var/ipfire/general-functions.pl
index 54cd8135ba9fea2192b03a8266b0b61402f3bf1f..a8cd837bbfbf17b08cf03cd77eb5c11f7b56233d 100644 (file)
@@ -25,9 +25,11 @@ import daemon
 import ipaddress
 import logging
 import logging.handlers
+import os
 import re
 import signal
 import subprocess
+import tempfile
 
 import inotify.adapters
 
@@ -519,11 +521,15 @@ class UnboundConfigWriter(object):
                                self._cached_leases.append(l)
 
        def write_dhcp_leases(self, leases):
-               with open(self.path, "w") as f:
+               with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:
+                       filename = f.name
+
                        for l in leases:
                                for rr in l.rrset:
                                        f.write("local-data: \"%s\"\n" % " ".join(rr))
 
+               os.rename(filename, self.path)
+
        def _control(self, *args):
                command = ["unbound-control"]
                command.extend(args)