]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/unbound/unbound-dhcp-leases-bridge
unbound-dhcp-leases-bridge: Replace leases file atomically
[ipfire-2.x.git] / config / unbound / unbound-dhcp-leases-bridge
index 4804dba2a1b0332ef50459f114832e5b92eeb40c..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
 
@@ -222,7 +224,7 @@ class DHCPLeases(object):
                                # exists in the list of known leases. If so replace
                                # if with the most recent lease
                                for i, l in enumerate(leases):
-                                       if l.hwaddr == lease.hwaddr:
+                                       if l.ipaddr == lease.ipaddr:
                                                leases[i] = max(lease, l)
                                                break
 
@@ -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)