]> 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 a41354349d3deb7bbefb3acef2d69593cb00b4d1..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
 
@@ -486,42 +488,62 @@ class UnboundConfigWriter(object):
                if not new_leases and not removed_leases:
                        return
 
-               # Update cache
-               self._cached_leases = leases
-
                # Write out all leases
                self.write_dhcp_leases(leases)
 
                # Update unbound about changes
                for l in removed_leases:
-                       for name, ttl, type, content in l.rrset:
-                               log.debug("Removing records for %s" % name)
-                               self._control("local_data_remove", name)
+                       try:
+                               for name, ttl, type, content in l.rrset:
+                                       log.debug("Removing records for %s" % name)
+                                       self._control("local_data_remove", name)
+
+                       # If the lease cannot be removed we will try the next one
+                       except:
+                               continue
+
+                       # If the removal was successful, we will remove it from the cache
+                       else:
+                               self._cached_leases.remove(l)
 
                for l in new_leases:
-                       for rr in l.rrset:
-                               log.debug("Adding new record %s" % " ".join(rr))
-                               self._control("local_data", *rr)
+                       try:
+                               for rr in l.rrset:
+                                       log.debug("Adding new record %s" % " ".join(rr))
+                                       self._control("local_data", *rr)
 
+                       # If the lease cannot be added we will try the next one
+                       except:
+                               continue
+
+                       # Add lease to cache when successfully added
+                       else:
+                               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)
 
                try:
-                       return subprocess.check_output(command)
+                       subprocess.check_output(command)
 
                # Log any errors
                except subprocess.CalledProcessError as e:
                        log.critical("Could not run %s, error code: %s: %s" % (
                                " ".join(command), e.returncode, e.output))
 
+                       raise
+
 
 if __name__ == "__main__":
        parser = argparse.ArgumentParser(description="Bridge for DHCP Leases and Unbound DNS")